You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VRichTextArea.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui.richtextarea;
  5. import com.google.gwt.user.client.Command;
  6. import com.google.gwt.user.client.DOM;
  7. import com.google.gwt.user.client.DeferredCommand;
  8. import com.google.gwt.user.client.Element;
  9. import com.google.gwt.user.client.ui.ChangeListener;
  10. import com.google.gwt.user.client.ui.Composite;
  11. import com.google.gwt.user.client.ui.FlowPanel;
  12. import com.google.gwt.user.client.ui.FocusListener;
  13. import com.google.gwt.user.client.ui.HTML;
  14. import com.google.gwt.user.client.ui.KeyboardListener;
  15. import com.google.gwt.user.client.ui.RichTextArea;
  16. import com.google.gwt.user.client.ui.Widget;
  17. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  18. import com.vaadin.terminal.gwt.client.BrowserInfo;
  19. import com.vaadin.terminal.gwt.client.Paintable;
  20. import com.vaadin.terminal.gwt.client.UIDL;
  21. import com.vaadin.terminal.gwt.client.Util;
  22. import com.vaadin.terminal.gwt.client.ui.Field;
  23. /**
  24. * This class implements a basic client side rich text editor component.
  25. *
  26. * @author IT Mill Ltd.
  27. *
  28. */
  29. public class VRichTextArea extends Composite implements Paintable, Field,
  30. ChangeListener, FocusListener, KeyboardListener {
  31. /**
  32. * The input node CSS classname.
  33. */
  34. public static final String CLASSNAME = "i-richtextarea";
  35. protected String id;
  36. protected ApplicationConnection client;
  37. private boolean immediate = false;
  38. private RichTextArea rta = new RichTextArea();
  39. private VRichTextToolbar formatter = new VRichTextToolbar(rta);
  40. private HTML html = new HTML();
  41. private final FlowPanel fp = new FlowPanel();
  42. private boolean enabled = true;
  43. private int extraHorizontalPixels = -1;
  44. private int extraVerticalPixels = -1;
  45. private int maxLength = -1;
  46. private int toolbarNaturalWidth = 500;
  47. public VRichTextArea() {
  48. fp.add(formatter);
  49. rta.setWidth("100%");
  50. rta.addFocusListener(this);
  51. fp.add(rta);
  52. initWidget(fp);
  53. setStyleName(CLASSNAME);
  54. }
  55. public void setEnabled(boolean enabled) {
  56. if (this.enabled != enabled) {
  57. rta.setEnabled(enabled);
  58. if (enabled) {
  59. fp.remove(html);
  60. fp.add(rta);
  61. } else {
  62. html.setHTML(rta.getHTML());
  63. fp.remove(rta);
  64. fp.add(html);
  65. }
  66. this.enabled = enabled;
  67. }
  68. }
  69. public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
  70. this.client = client;
  71. id = uidl.getId();
  72. if (uidl.hasVariable("text")) {
  73. if (BrowserInfo.get().isIE()) {
  74. // rta is rather buggy in IE (as pretty much everything is)
  75. // it needs some "shaking" not to fall into uneditable state
  76. // see #2374
  77. rta.getBasicFormatter().toggleBold();
  78. rta.getBasicFormatter().toggleBold();
  79. }
  80. rta.setHTML(uidl.getStringVariable("text"));
  81. }
  82. setEnabled(!uidl.getBooleanAttribute("disabled"));
  83. if (client.updateComponent(this, uidl, true)) {
  84. return;
  85. }
  86. immediate = uidl.getBooleanAttribute("immediate");
  87. int newMaxLength = uidl.hasAttribute("maxLength") ? uidl
  88. .getIntAttribute("maxLength") : -1;
  89. if (newMaxLength >= 0) {
  90. if (maxLength == -1) {
  91. rta.addKeyboardListener(this);
  92. }
  93. maxLength = newMaxLength;
  94. } else if (maxLength != -1) {
  95. getElement().setAttribute("maxlength", "");
  96. maxLength = -1;
  97. rta.removeKeyboardListener(this);
  98. }
  99. }
  100. public void onChange(Widget sender) {
  101. if (client != null && id != null) {
  102. client.updateVariable(id, "text", rta.getText(), immediate);
  103. }
  104. }
  105. public void onFocus(Widget sender) {
  106. }
  107. public void onLostFocus(Widget sender) {
  108. final String html = rta.getHTML();
  109. client.updateVariable(id, "text", html, immediate);
  110. }
  111. /**
  112. * @return space used by components paddings and borders
  113. */
  114. private int getExtraHorizontalPixels() {
  115. if (extraHorizontalPixels < 0) {
  116. detectExtraSizes();
  117. }
  118. return extraHorizontalPixels;
  119. }
  120. /**
  121. * @return space used by components paddings and borders
  122. */
  123. private int getExtraVerticalPixels() {
  124. if (extraVerticalPixels < 0) {
  125. detectExtraSizes();
  126. }
  127. return extraVerticalPixels;
  128. }
  129. /**
  130. * Detects space used by components paddings and borders.
  131. */
  132. private void detectExtraSizes() {
  133. Element clone = Util.cloneNode(getElement(), false);
  134. DOM.setElementAttribute(clone, "id", "");
  135. DOM.setStyleAttribute(clone, "visibility", "hidden");
  136. DOM.setStyleAttribute(clone, "position", "absolute");
  137. // due FF3 bug set size to 10px and later subtract it from extra pixels
  138. DOM.setStyleAttribute(clone, "width", "10px");
  139. DOM.setStyleAttribute(clone, "height", "10px");
  140. DOM.appendChild(DOM.getParent(getElement()), clone);
  141. extraHorizontalPixels = DOM.getElementPropertyInt(clone, "offsetWidth") - 10;
  142. extraVerticalPixels = DOM.getElementPropertyInt(clone, "offsetHeight") - 10;
  143. DOM.removeChild(DOM.getParent(getElement()), clone);
  144. }
  145. @Override
  146. public void setHeight(String height) {
  147. if (height.endsWith("px")) {
  148. int h = Integer.parseInt(height.substring(0, height.length() - 2));
  149. h -= getExtraVerticalPixels();
  150. if (h < 0) {
  151. h = 0;
  152. }
  153. super.setHeight(h + "px");
  154. } else {
  155. super.setHeight(height);
  156. }
  157. if (height == null || height.equals("")) {
  158. rta.setHeight("");
  159. } else {
  160. int editorHeight = getOffsetHeight() - getExtraVerticalPixels()
  161. - formatter.getOffsetHeight();
  162. rta.setHeight(editorHeight + "px");
  163. }
  164. }
  165. @Override
  166. public void setWidth(String width) {
  167. if (width.endsWith("px")) {
  168. int w = Integer.parseInt(width.substring(0, width.length() - 2));
  169. w -= getExtraHorizontalPixels();
  170. if (w < 0) {
  171. w = 0;
  172. }
  173. super.setWidth(w + "px");
  174. } else if (width.equals("")) {
  175. /*
  176. * IE cannot calculate the width of the 100% iframe correctly if
  177. * there is no width specified for the parent. In this case we would
  178. * use the toolbar but IE cannot calculate the width of that one
  179. * correctly either in all cases. So we end up using a default width
  180. * for a RichTextArea with no width definition in all browsers (for
  181. * compatibility).
  182. */
  183. super.setWidth(toolbarNaturalWidth + "px");
  184. } else {
  185. super.setWidth(width);
  186. }
  187. }
  188. public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  189. // NOP
  190. }
  191. public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  192. if (maxLength >= 0) {
  193. DeferredCommand.addCommand(new Command() {
  194. public void execute() {
  195. if (rta.getHTML().length() > maxLength) {
  196. rta.setHTML(rta.getHTML().substring(0, maxLength));
  197. }
  198. }
  199. });
  200. }
  201. }
  202. public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  203. // NOP
  204. }
  205. }