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.

VButton.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.dom.client.Document;
  6. import com.google.gwt.dom.client.Element;
  7. import com.google.gwt.dom.client.NativeEvent;
  8. import com.google.gwt.event.dom.client.BlurEvent;
  9. import com.google.gwt.event.dom.client.BlurHandler;
  10. import com.google.gwt.event.dom.client.ClickEvent;
  11. import com.google.gwt.event.dom.client.ClickHandler;
  12. import com.google.gwt.event.dom.client.FocusEvent;
  13. import com.google.gwt.event.dom.client.FocusHandler;
  14. import com.google.gwt.event.dom.client.KeyCodes;
  15. import com.google.gwt.event.shared.HandlerRegistration;
  16. import com.google.gwt.user.client.DOM;
  17. import com.google.gwt.user.client.Event;
  18. import com.google.gwt.user.client.ui.Accessibility;
  19. import com.google.gwt.user.client.ui.FocusWidget;
  20. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  21. import com.vaadin.terminal.gwt.client.BrowserInfo;
  22. import com.vaadin.terminal.gwt.client.EventId;
  23. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  24. import com.vaadin.terminal.gwt.client.Util;
  25. import com.vaadin.terminal.gwt.client.VTooltip;
  26. import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonClientToServerRpc;
  27. public class VButton extends FocusWidget implements ClickHandler, FocusHandler,
  28. BlurHandler {
  29. public static final String CLASSNAME = "v-button";
  30. private static final String CLASSNAME_PRESSED = "v-pressed";
  31. // mouse movement is checked before synthesizing click event on mouseout
  32. protected static int MOVE_THRESHOLD = 3;
  33. protected int mousedownX = 0;
  34. protected int mousedownY = 0;
  35. protected String paintableId;
  36. protected ApplicationConnection client;
  37. protected final Element wrapper = DOM.createSpan();
  38. protected Element errorIndicatorElement;
  39. protected final Element captionElement = DOM.createSpan();
  40. protected Icon icon;
  41. /**
  42. * Helper flag to handle special-case where the button is moved from under
  43. * mouse while clicking it. In this case mouse leaves the button without
  44. * moving.
  45. */
  46. protected boolean clickPending;
  47. private boolean enabled = true;
  48. private int tabIndex = 0;
  49. protected boolean disableOnClick = false;
  50. /*
  51. * BELOW PRIVATE MEMBERS COPY-PASTED FROM GWT CustomButton
  52. */
  53. /**
  54. * If <code>true</code>, this widget is capturing with the mouse held down.
  55. */
  56. private boolean isCapturing;
  57. /**
  58. * If <code>true</code>, this widget has focus with the space bar down.
  59. */
  60. private boolean isFocusing;
  61. /**
  62. * Used to decide whether to allow clicks to propagate up to the superclass
  63. * or container elements.
  64. */
  65. private boolean disallowNextClick = false;
  66. private boolean isHovering;
  67. protected HandlerRegistration focusHandlerRegistration;
  68. protected HandlerRegistration blurHandlerRegistration;
  69. protected int clickShortcut = 0;
  70. // TODO Move this to VButtonPaintable
  71. ButtonClientToServerRpc buttonRpcProxy;
  72. public VButton() {
  73. super(DOM.createDiv());
  74. setTabIndex(0);
  75. sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.FOCUSEVENTS
  76. | Event.KEYEVENTS);
  77. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  78. setStyleName(CLASSNAME);
  79. // Add a11y role "button"
  80. Accessibility.setRole(getElement(), Accessibility.ROLE_BUTTON);
  81. wrapper.setClassName(getStylePrimaryName() + "-wrap");
  82. getElement().appendChild(wrapper);
  83. captionElement.setClassName(getStylePrimaryName() + "-caption");
  84. wrapper.appendChild(captionElement);
  85. addClickHandler(this);
  86. }
  87. public void setText(String text) {
  88. captionElement.setInnerText(text);
  89. }
  90. @SuppressWarnings("deprecation")
  91. @Override
  92. /*
  93. * Copy-pasted from GWT CustomButton, some minor modifications done:
  94. *
  95. * -for IE/Opera added CLASSNAME_PRESSED
  96. *
  97. * -event.preventDefault() commented from ONMOUSEDOWN (Firefox won't apply
  98. * :active styles if it is present)
  99. *
  100. * -Tooltip event handling added
  101. *
  102. * -onload event handler added (for icon handling)
  103. */
  104. public void onBrowserEvent(Event event) {
  105. if (client != null) {
  106. client.handleTooltipEvent(event, this);
  107. }
  108. if (DOM.eventGetType(event) == Event.ONLOAD) {
  109. Util.notifyParentOfSizeChange(this, true);
  110. }
  111. // Should not act on button if disabled.
  112. if (!isEnabled()) {
  113. // This can happen when events are bubbled up from non-disabled
  114. // children
  115. return;
  116. }
  117. int type = DOM.eventGetType(event);
  118. switch (type) {
  119. case Event.ONCLICK:
  120. // If clicks are currently disallowed, keep it from bubbling or
  121. // being passed to the superclass.
  122. if (disallowNextClick) {
  123. event.stopPropagation();
  124. disallowNextClick = false;
  125. return;
  126. }
  127. break;
  128. case Event.ONMOUSEDOWN:
  129. if (DOM.isOrHasChild(getElement(), DOM.eventGetTarget(event))) {
  130. // This was moved from mouseover, which iOS sometimes skips.
  131. // We're certainly hovering at this point, and we don't actually
  132. // need that information before this point.
  133. setHovering(true);
  134. }
  135. if (event.getButton() == Event.BUTTON_LEFT) {
  136. // save mouse position to detect movement before synthesizing
  137. // event later
  138. mousedownX = event.getClientX();
  139. mousedownY = event.getClientY();
  140. disallowNextClick = true;
  141. clickPending = true;
  142. setFocus(true);
  143. DOM.setCapture(getElement());
  144. isCapturing = true;
  145. // Prevent dragging (on some browsers);
  146. // DOM.eventPreventDefault(event);
  147. if (BrowserInfo.get().isIE() || BrowserInfo.get().isOpera()) {
  148. addStyleName(CLASSNAME_PRESSED);
  149. }
  150. }
  151. break;
  152. case Event.ONMOUSEUP:
  153. if (isCapturing) {
  154. isCapturing = false;
  155. DOM.releaseCapture(getElement());
  156. if (isHovering() && event.getButton() == Event.BUTTON_LEFT) {
  157. // Click ok
  158. disallowNextClick = false;
  159. }
  160. if (BrowserInfo.get().isIE() || BrowserInfo.get().isOpera()) {
  161. removeStyleName(CLASSNAME_PRESSED);
  162. }
  163. }
  164. break;
  165. case Event.ONMOUSEMOVE:
  166. clickPending = false;
  167. if (isCapturing) {
  168. // Prevent dragging (on other browsers);
  169. DOM.eventPreventDefault(event);
  170. }
  171. break;
  172. case Event.ONMOUSEOUT:
  173. Element to = event.getRelatedTarget();
  174. if (getElement().isOrHasChild(DOM.eventGetTarget(event))
  175. && (to == null || !getElement().isOrHasChild(to))) {
  176. if (clickPending
  177. && Math.abs(mousedownX - event.getClientX()) < MOVE_THRESHOLD
  178. && Math.abs(mousedownY - event.getClientY()) < MOVE_THRESHOLD) {
  179. onClick();
  180. break;
  181. }
  182. clickPending = false;
  183. if (isCapturing) {
  184. }
  185. setHovering(false);
  186. if (BrowserInfo.get().isIE() || BrowserInfo.get().isOpera()) {
  187. removeStyleName(CLASSNAME_PRESSED);
  188. }
  189. }
  190. break;
  191. case Event.ONBLUR:
  192. if (isFocusing) {
  193. isFocusing = false;
  194. }
  195. break;
  196. case Event.ONLOSECAPTURE:
  197. if (isCapturing) {
  198. isCapturing = false;
  199. }
  200. break;
  201. }
  202. super.onBrowserEvent(event);
  203. // Synthesize clicks based on keyboard events AFTER the normal key
  204. // handling.
  205. if ((event.getTypeInt() & Event.KEYEVENTS) != 0) {
  206. switch (type) {
  207. case Event.ONKEYDOWN:
  208. if (event.getKeyCode() == 32 /* space */) {
  209. isFocusing = true;
  210. event.preventDefault();
  211. }
  212. break;
  213. case Event.ONKEYUP:
  214. if (isFocusing && event.getKeyCode() == 32 /* space */) {
  215. isFocusing = false;
  216. /*
  217. * If click shortcut is space then the shortcut handler will
  218. * take care of the click.
  219. */
  220. if (clickShortcut != 32 /* space */) {
  221. onClick();
  222. }
  223. event.preventDefault();
  224. }
  225. break;
  226. case Event.ONKEYPRESS:
  227. if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
  228. /*
  229. * If click shortcut is enter then the shortcut handler will
  230. * take care of the click.
  231. */
  232. if (clickShortcut != KeyCodes.KEY_ENTER) {
  233. onClick();
  234. }
  235. event.preventDefault();
  236. }
  237. break;
  238. }
  239. }
  240. }
  241. final void setHovering(boolean hovering) {
  242. if (hovering != isHovering()) {
  243. isHovering = hovering;
  244. }
  245. }
  246. final boolean isHovering() {
  247. return isHovering;
  248. }
  249. /*
  250. * (non-Javadoc)
  251. *
  252. * @see
  253. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  254. * .dom.client.ClickEvent)
  255. */
  256. public void onClick(ClickEvent event) {
  257. if (paintableId == null || client == null) {
  258. return;
  259. }
  260. if (BrowserInfo.get().isSafari()) {
  261. VButton.this.setFocus(true);
  262. }
  263. if (disableOnClick) {
  264. setEnabled(false);
  265. buttonRpcProxy.disableOnClick();
  266. }
  267. // Add mouse details
  268. MouseEventDetails details = new MouseEventDetails(
  269. event.getNativeEvent(), getElement());
  270. buttonRpcProxy.click(details.serialize());
  271. clickPending = false;
  272. }
  273. /*
  274. * ALL BELOW COPY-PASTED FROM GWT CustomButton
  275. */
  276. /**
  277. * Called internally when the user finishes clicking on this button. The
  278. * default behavior is to fire the click event to listeners. Subclasses that
  279. * override {@link #onClickStart()} should override this method to restore
  280. * the normal widget display.
  281. * <p>
  282. * To add custom code for a click event, override
  283. * {@link #onClick(ClickEvent)} instead of this.
  284. */
  285. protected void onClick() {
  286. // Allow the click we're about to synthesize to pass through to the
  287. // superclass and containing elements. Element.dispatchEvent() is
  288. // synchronous, so we simply set and clear the flag within this method.
  289. disallowNextClick = false;
  290. // Mouse coordinates are not always available (e.g., when the click is
  291. // caused by a keyboard event).
  292. NativeEvent evt = Document.get().createClickEvent(1, 0, 0, 0, 0, false,
  293. false, false, false);
  294. getElement().dispatchEvent(evt);
  295. }
  296. /**
  297. * Sets whether this button is enabled.
  298. *
  299. * @param enabled
  300. * <code>true</code> to enable the button, <code>false</code> to
  301. * disable it
  302. */
  303. @Override
  304. public final void setEnabled(boolean enabled) {
  305. if (isEnabled() != enabled) {
  306. this.enabled = enabled;
  307. if (!enabled) {
  308. cleanupCaptureState();
  309. Accessibility.removeState(getElement(),
  310. Accessibility.STATE_PRESSED);
  311. super.setTabIndex(-1);
  312. addStyleName(ApplicationConnection.DISABLED_CLASSNAME);
  313. } else {
  314. Accessibility.setState(getElement(),
  315. Accessibility.STATE_PRESSED, "false");
  316. super.setTabIndex(tabIndex);
  317. removeStyleName(ApplicationConnection.DISABLED_CLASSNAME);
  318. }
  319. }
  320. }
  321. @Override
  322. public final boolean isEnabled() {
  323. return enabled;
  324. }
  325. @Override
  326. public final void setTabIndex(int index) {
  327. super.setTabIndex(index);
  328. tabIndex = index;
  329. }
  330. /**
  331. * Resets internal state if this button can no longer service events. This
  332. * can occur when the widget becomes detached or disabled.
  333. */
  334. private void cleanupCaptureState() {
  335. if (isCapturing || isFocusing) {
  336. DOM.releaseCapture(getElement());
  337. isCapturing = false;
  338. isFocusing = false;
  339. }
  340. }
  341. private static native int getHorizontalBorderAndPaddingWidth(Element elem)
  342. /*-{
  343. // THIS METHOD IS ONLY USED FOR INTERNET EXPLORER, IT DOESN'T WORK WITH OTHERS
  344. var convertToPixel = function(elem, value) {
  345. // From the awesome hack by Dean Edwards
  346. // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
  347. // Remember the original values
  348. var left = elem.style.left, rsLeft = elem.runtimeStyle.left;
  349. // Put in the new values to get a computed value out
  350. elem.runtimeStyle.left = elem.currentStyle.left;
  351. elem.style.left = value || 0;
  352. var ret = elem.style.pixelLeft;
  353. // Revert the changed values
  354. elem.style.left = left;
  355. elem.runtimeStyle.left = rsLeft;
  356. return ret;
  357. }
  358. var ret = 0;
  359. var sides = ["Right","Left"];
  360. for(var i=0; i<2; i++) {
  361. var side = sides[i];
  362. var value;
  363. // Border -------------------------------------------------------
  364. if(elem.currentStyle["border"+side+"Style"] != "none") {
  365. value = elem.currentStyle["border"+side+"Width"];
  366. if ( !/^\d+(px)?$/i.test( value ) && /^\d/.test( value ) ) {
  367. ret += convertToPixel(elem, value);
  368. } else if(value.length > 2) {
  369. ret += parseInt(value.substr(0, value.length-2));
  370. }
  371. }
  372. // Padding -------------------------------------------------------
  373. value = elem.currentStyle["padding"+side];
  374. if ( !/^\d+(px)?$/i.test( value ) && /^\d/.test( value ) ) {
  375. ret += convertToPixel(elem, value);
  376. } else if(value.length > 2) {
  377. ret += parseInt(value.substr(0, value.length-2));
  378. }
  379. }
  380. return ret;
  381. }-*/;
  382. public void onFocus(FocusEvent arg0) {
  383. client.updateVariable(paintableId, EventId.FOCUS, "", true);
  384. }
  385. public void onBlur(BlurEvent arg0) {
  386. client.updateVariable(paintableId, EventId.BLUR, "", true);
  387. }
  388. }