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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui.richtextarea;
  5. import com.google.gwt.core.client.Scheduler;
  6. import com.google.gwt.event.dom.client.BlurEvent;
  7. import com.google.gwt.event.dom.client.BlurHandler;
  8. import com.google.gwt.event.dom.client.ChangeEvent;
  9. import com.google.gwt.event.dom.client.ChangeHandler;
  10. import com.google.gwt.event.dom.client.KeyDownEvent;
  11. import com.google.gwt.event.dom.client.KeyDownHandler;
  12. import com.google.gwt.event.dom.client.KeyPressEvent;
  13. import com.google.gwt.event.dom.client.KeyPressHandler;
  14. import com.google.gwt.event.shared.HandlerRegistration;
  15. import com.google.gwt.user.client.Command;
  16. import com.google.gwt.user.client.DOM;
  17. import com.google.gwt.user.client.Element;
  18. import com.google.gwt.user.client.Event;
  19. import com.google.gwt.user.client.Timer;
  20. import com.google.gwt.user.client.ui.Composite;
  21. import com.google.gwt.user.client.ui.FlowPanel;
  22. import com.google.gwt.user.client.ui.Focusable;
  23. import com.google.gwt.user.client.ui.HTML;
  24. import com.google.gwt.user.client.ui.RichTextArea;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  27. import com.vaadin.terminal.gwt.client.BrowserInfo;
  28. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  29. import com.vaadin.terminal.gwt.client.UIDL;
  30. import com.vaadin.terminal.gwt.client.Util;
  31. import com.vaadin.terminal.gwt.client.ui.Field;
  32. import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler;
  33. import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.BeforeShortcutActionListener;
  34. import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  35. /**
  36. * This class implements a basic client side rich text editor component.
  37. *
  38. * @author Vaadin Ltd.
  39. *
  40. */
  41. public class VRichTextArea extends Composite implements VPaintableWidget, Field,
  42. ChangeHandler, BlurHandler, KeyPressHandler, KeyDownHandler,
  43. BeforeShortcutActionListener, Focusable {
  44. /**
  45. * The input node CSS classname.
  46. */
  47. public static final String CLASSNAME = "v-richtextarea";
  48. protected String id;
  49. protected ApplicationConnection client;
  50. private boolean immediate = false;
  51. private RichTextArea rta;
  52. private VRichTextToolbar formatter;
  53. private HTML html = new HTML();
  54. private final FlowPanel fp = new FlowPanel();
  55. private boolean enabled = true;
  56. private int extraHorizontalPixels = -1;
  57. private int extraVerticalPixels = -1;
  58. private int maxLength = -1;
  59. private int toolbarNaturalWidth = 500;
  60. private HandlerRegistration keyPressHandler;
  61. private ShortcutActionHandlerOwner hasShortcutActionHandler;
  62. private String currentValue = "";
  63. private boolean readOnly = false;
  64. public VRichTextArea() {
  65. createRTAComponents();
  66. fp.add(formatter);
  67. fp.add(rta);
  68. initWidget(fp);
  69. setStyleName(CLASSNAME);
  70. }
  71. private void createRTAComponents() {
  72. rta = new RichTextArea();
  73. rta.setWidth("100%");
  74. rta.addBlurHandler(this);
  75. rta.addKeyDownHandler(this);
  76. formatter = new VRichTextToolbar(rta);
  77. }
  78. public void setEnabled(boolean enabled) {
  79. if (this.enabled != enabled) {
  80. // rta.setEnabled(enabled);
  81. swapEditableArea();
  82. this.enabled = enabled;
  83. }
  84. }
  85. /**
  86. * Swaps html to rta and visa versa.
  87. */
  88. private void swapEditableArea() {
  89. if (html.isAttached()) {
  90. fp.remove(html);
  91. if (BrowserInfo.get().isWebkit()) {
  92. fp.remove(formatter);
  93. createRTAComponents(); // recreate new RTA to bypass #5379
  94. fp.add(formatter);
  95. }
  96. rta.setHTML(currentValue);
  97. fp.add(rta);
  98. } else {
  99. html.setHTML(currentValue);
  100. fp.remove(rta);
  101. fp.add(html);
  102. }
  103. }
  104. public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
  105. this.client = client;
  106. id = uidl.getId();
  107. if (uidl.hasVariable("text")) {
  108. currentValue = uidl.getStringVariable("text");
  109. if (rta.isAttached()) {
  110. rta.setHTML(currentValue);
  111. } else {
  112. html.setHTML(currentValue);
  113. }
  114. }
  115. if (!uidl.hasAttribute("cached")) {
  116. setEnabled(!uidl.getBooleanAttribute("disabled"));
  117. }
  118. if (client.updateComponent(this, uidl, true)) {
  119. return;
  120. }
  121. setReadOnly(uidl.getBooleanAttribute("readonly"));
  122. immediate = uidl.getBooleanAttribute("immediate");
  123. int newMaxLength = uidl.hasAttribute("maxLength") ? uidl
  124. .getIntAttribute("maxLength") : -1;
  125. if (newMaxLength >= 0) {
  126. if (maxLength == -1) {
  127. keyPressHandler = rta.addKeyPressHandler(this);
  128. }
  129. maxLength = newMaxLength;
  130. } else if (maxLength != -1) {
  131. getElement().setAttribute("maxlength", "");
  132. maxLength = -1;
  133. keyPressHandler.removeHandler();
  134. }
  135. if (uidl.hasAttribute("selectAll")) {
  136. selectAll();
  137. }
  138. }
  139. private void selectAll() {
  140. /*
  141. * There is a timing issue if trying to select all immediately on first
  142. * render. Simple deferred command is not enough. Using Timer with
  143. * moderated timeout. If this appears to fail on many (most likely slow)
  144. * environments, consider increasing the timeout.
  145. *
  146. * FF seems to require the most time to stabilize its RTA. On Vaadin
  147. * tiergarden test machines, 200ms was not enough always (about 50%
  148. * success rate) - 300 ms was 100% successful. This however was not
  149. * enough on a sluggish old non-virtualized XP test machine. A bullet
  150. * proof solution would be nice, GWT 2.1 might however solve these. At
  151. * least setFocus has a workaround for this kind of issue.
  152. */
  153. new Timer() {
  154. @Override
  155. public void run() {
  156. rta.getFormatter().selectAll();
  157. }
  158. }.schedule(320);
  159. }
  160. private void setReadOnly(boolean b) {
  161. if (isReadOnly() != b) {
  162. swapEditableArea();
  163. readOnly = b;
  164. }
  165. // reset visibility in case enabled state changed and the formatter was
  166. // recreated
  167. formatter.setVisible(!readOnly);
  168. }
  169. private boolean isReadOnly() {
  170. return readOnly;
  171. }
  172. // TODO is this really used, or does everything go via onBlur() only?
  173. public void onChange(ChangeEvent event) {
  174. synchronizeContentToServer();
  175. }
  176. /**
  177. * Method is public to let popupview force synchronization on close.
  178. */
  179. public void synchronizeContentToServer() {
  180. if (client != null && id != null) {
  181. final String html = rta.getHTML();
  182. if (!html.equals(currentValue)) {
  183. client.updateVariable(id, "text", html, immediate);
  184. currentValue = html;
  185. }
  186. }
  187. }
  188. public void onBlur(BlurEvent event) {
  189. synchronizeContentToServer();
  190. // TODO notify possible server side blur/focus listeners
  191. }
  192. /**
  193. * @return space used by components paddings and borders
  194. */
  195. private int getExtraHorizontalPixels() {
  196. if (extraHorizontalPixels < 0) {
  197. detectExtraSizes();
  198. }
  199. return extraHorizontalPixels;
  200. }
  201. /**
  202. * @return space used by components paddings and borders
  203. */
  204. private int getExtraVerticalPixels() {
  205. if (extraVerticalPixels < 0) {
  206. detectExtraSizes();
  207. }
  208. return extraVerticalPixels;
  209. }
  210. /**
  211. * Detects space used by components paddings and borders.
  212. */
  213. private void detectExtraSizes() {
  214. Element clone = Util.cloneNode(getElement(), false);
  215. DOM.setElementAttribute(clone, "id", "");
  216. DOM.setStyleAttribute(clone, "visibility", "hidden");
  217. DOM.setStyleAttribute(clone, "position", "absolute");
  218. // due FF3 bug set size to 10px and later subtract it from extra pixels
  219. DOM.setStyleAttribute(clone, "width", "10px");
  220. DOM.setStyleAttribute(clone, "height", "10px");
  221. DOM.appendChild(DOM.getParent(getElement()), clone);
  222. extraHorizontalPixels = DOM.getElementPropertyInt(clone, "offsetWidth") - 10;
  223. extraVerticalPixels = DOM.getElementPropertyInt(clone, "offsetHeight") - 10;
  224. DOM.removeChild(DOM.getParent(getElement()), clone);
  225. }
  226. @Override
  227. public void setHeight(String height) {
  228. if (height.endsWith("px")) {
  229. int h = Integer.parseInt(height.substring(0, height.length() - 2));
  230. h -= getExtraVerticalPixels();
  231. if (h < 0) {
  232. h = 0;
  233. }
  234. super.setHeight(h + "px");
  235. } else {
  236. super.setHeight(height);
  237. }
  238. if (height == null || height.equals("")) {
  239. rta.setHeight("");
  240. } else {
  241. /*
  242. * The formatter height will be initially calculated wrong so we
  243. * delay the height setting so the DOM has had time to stabilize.
  244. */
  245. Scheduler.get().scheduleDeferred(new Command() {
  246. public void execute() {
  247. int editorHeight = getOffsetHeight()
  248. - getExtraVerticalPixels()
  249. - formatter.getOffsetHeight();
  250. if (editorHeight < 0) {
  251. editorHeight = 0;
  252. }
  253. rta.setHeight(editorHeight + "px");
  254. }
  255. });
  256. }
  257. }
  258. @Override
  259. public void setWidth(String width) {
  260. if (width.endsWith("px")) {
  261. int w = Integer.parseInt(width.substring(0, width.length() - 2));
  262. w -= getExtraHorizontalPixels();
  263. if (w < 0) {
  264. w = 0;
  265. }
  266. super.setWidth(w + "px");
  267. } else if (width.equals("")) {
  268. /*
  269. * IE cannot calculate the width of the 100% iframe correctly if
  270. * there is no width specified for the parent. In this case we would
  271. * use the toolbar but IE cannot calculate the width of that one
  272. * correctly either in all cases. So we end up using a default width
  273. * for a RichTextArea with no width definition in all browsers (for
  274. * compatibility).
  275. */
  276. super.setWidth(toolbarNaturalWidth + "px");
  277. } else {
  278. super.setWidth(width);
  279. }
  280. }
  281. public void onKeyPress(KeyPressEvent event) {
  282. if (maxLength >= 0) {
  283. Scheduler.get().scheduleDeferred(new Command() {
  284. public void execute() {
  285. if (rta.getHTML().length() > maxLength) {
  286. rta.setHTML(rta.getHTML().substring(0, maxLength));
  287. }
  288. }
  289. });
  290. }
  291. }
  292. public void onKeyDown(KeyDownEvent event) {
  293. // delegate to closest shortcut action handler
  294. // throw event from the iframe forward to the shortcuthandler
  295. ShortcutActionHandler shortcutHandler = getShortcutHandlerOwner()
  296. .getShortcutActionHandler();
  297. if (shortcutHandler != null) {
  298. shortcutHandler
  299. .handleKeyboardEvent(com.google.gwt.user.client.Event
  300. .as(event.getNativeEvent()), this);
  301. }
  302. }
  303. private ShortcutActionHandlerOwner getShortcutHandlerOwner() {
  304. if (hasShortcutActionHandler == null) {
  305. Widget parent = getParent();
  306. while (parent != null) {
  307. if (parent instanceof ShortcutActionHandlerOwner) {
  308. break;
  309. }
  310. parent = parent.getParent();
  311. }
  312. hasShortcutActionHandler = (ShortcutActionHandlerOwner) parent;
  313. }
  314. return hasShortcutActionHandler;
  315. }
  316. public void onBeforeShortcutAction(Event e) {
  317. synchronizeContentToServer();
  318. }
  319. public int getTabIndex() {
  320. return rta.getTabIndex();
  321. }
  322. public void setAccessKey(char key) {
  323. rta.setAccessKey(key);
  324. }
  325. public void setFocus(boolean focused) {
  326. /*
  327. * Similar issue as with selectAll. Focusing must happen before possible
  328. * selectall, so keep the timeout here lower.
  329. */
  330. new Timer() {
  331. @Override
  332. public void run() {
  333. rta.setFocus(true);
  334. }
  335. }.schedule(300);
  336. }
  337. public void setTabIndex(int index) {
  338. rta.setTabIndex(index);
  339. }
  340. public Widget getWidgetForPaintable() {
  341. return this;
  342. }
  343. }