Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Button.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.Collection;
  20. import org.jsoup.nodes.Attributes;
  21. import org.jsoup.nodes.Element;
  22. import com.vaadin.event.Action;
  23. import com.vaadin.event.ShortcutAction;
  24. import com.vaadin.event.ShortcutAction.KeyCode;
  25. import com.vaadin.event.ShortcutAction.ModifierKey;
  26. import com.vaadin.event.ShortcutListener;
  27. import com.vaadin.server.Resource;
  28. import com.vaadin.shared.MouseEventDetails;
  29. import com.vaadin.shared.Registration;
  30. import com.vaadin.shared.ui.button.ButtonServerRpc;
  31. import com.vaadin.shared.ui.button.ButtonState;
  32. import com.vaadin.ui.declarative.DesignAttributeHandler;
  33. import com.vaadin.ui.declarative.DesignContext;
  34. import com.vaadin.ui.declarative.DesignFormatter;
  35. import com.vaadin.util.ReflectTools;
  36. import elemental.json.Json;
  37. /**
  38. * A generic button component.
  39. *
  40. * @author Vaadin Ltd.
  41. * @since 3.0
  42. */
  43. @SuppressWarnings("serial")
  44. public class Button extends AbstractFocusable
  45. implements Action.ShortcutNotifier {
  46. private ButtonServerRpc rpc = new ButtonServerRpc() {
  47. @Override
  48. public void click(MouseEventDetails mouseEventDetails) {
  49. fireClick(mouseEventDetails);
  50. }
  51. @Override
  52. public void disableOnClick() throws RuntimeException {
  53. setEnabled(false);
  54. // Makes sure the enabled=false state is noticed at once - otherwise
  55. // a following setEnabled(true) call might have no effect. see
  56. // ticket #10030
  57. updateDiffstate("enabled", Json.create(false));
  58. }
  59. };
  60. /**
  61. * Creates a new push button.
  62. */
  63. public Button() {
  64. registerRpc(rpc);
  65. }
  66. /**
  67. * Creates a new push button with the given caption.
  68. *
  69. * @param caption
  70. * the Button caption.
  71. */
  72. public Button(String caption) {
  73. this();
  74. setCaption(caption);
  75. }
  76. /**
  77. * Creates a new push button with the given icon.
  78. *
  79. * @param icon
  80. * the icon
  81. */
  82. public Button(Resource icon) {
  83. this();
  84. setIcon(icon);
  85. }
  86. /**
  87. * Creates a new push button with the given caption and icon.
  88. *
  89. * @param caption
  90. * the caption
  91. * @param icon
  92. * the icon
  93. */
  94. public Button(String caption, Resource icon) {
  95. this();
  96. setCaption(caption);
  97. setIcon(icon);
  98. }
  99. /**
  100. * Creates a new push button with a click listener.
  101. *
  102. * @param caption
  103. * the Button caption.
  104. * @param listener
  105. * the Button click listener.
  106. */
  107. public Button(String caption, ClickListener listener) {
  108. this(caption);
  109. addClickListener(listener);
  110. }
  111. /**
  112. * Click event. This event is thrown, when the button is clicked.
  113. *
  114. * @author Vaadin Ltd.
  115. * @since 3.0
  116. */
  117. public static class ClickEvent extends Component.Event {
  118. private final MouseEventDetails details;
  119. /**
  120. * New instance of text change event.
  121. *
  122. * @param source
  123. * the Source of the event.
  124. */
  125. public ClickEvent(Component source) {
  126. super(source);
  127. details = null;
  128. }
  129. /**
  130. * Constructor with mouse details
  131. *
  132. * @param source
  133. * The source where the click took place
  134. * @param details
  135. * Details about the mouse click
  136. */
  137. public ClickEvent(Component source, MouseEventDetails details) {
  138. super(source);
  139. this.details = details;
  140. }
  141. /**
  142. * Gets the Button where the event occurred.
  143. *
  144. * @return the Source of the event.
  145. */
  146. public Button getButton() {
  147. return (Button) getSource();
  148. }
  149. /**
  150. * Returns the mouse position (x coordinate) when the click took place.
  151. * The position is relative to the browser client area.
  152. *
  153. * @return The mouse cursor x position or -1 if unknown
  154. */
  155. public int getClientX() {
  156. if (null != details) {
  157. return details.getClientX();
  158. } else {
  159. return -1;
  160. }
  161. }
  162. /**
  163. * Returns the mouse position (y coordinate) when the click took place.
  164. * The position is relative to the browser client area.
  165. *
  166. * @return The mouse cursor y position or -1 if unknown
  167. */
  168. public int getClientY() {
  169. if (null != details) {
  170. return details.getClientY();
  171. } else {
  172. return -1;
  173. }
  174. }
  175. /**
  176. * Returns the relative mouse position (x coordinate) when the click
  177. * took place. The position is relative to the clicked component.
  178. *
  179. * @return The mouse cursor x position relative to the clicked layout
  180. * component or -1 if no x coordinate available
  181. */
  182. public int getRelativeX() {
  183. if (null != details) {
  184. return details.getRelativeX();
  185. } else {
  186. return -1;
  187. }
  188. }
  189. /**
  190. * Returns the relative mouse position (y coordinate) when the click
  191. * took place. The position is relative to the clicked component.
  192. *
  193. * @return The mouse cursor y position relative to the clicked layout
  194. * component or -1 if no y coordinate available
  195. */
  196. public int getRelativeY() {
  197. if (null != details) {
  198. return details.getRelativeY();
  199. } else {
  200. return -1;
  201. }
  202. }
  203. /**
  204. * Checks if the Alt key was down when the mouse event took place.
  205. *
  206. * @return true if Alt was down when the event occured, false otherwise
  207. * or if unknown
  208. */
  209. public boolean isAltKey() {
  210. if (null != details) {
  211. return details.isAltKey();
  212. } else {
  213. return false;
  214. }
  215. }
  216. /**
  217. * Checks if the Ctrl key was down when the mouse event took place.
  218. *
  219. * @return true if Ctrl was pressed when the event occured, false
  220. * otherwise or if unknown
  221. */
  222. public boolean isCtrlKey() {
  223. if (null != details) {
  224. return details.isCtrlKey();
  225. } else {
  226. return false;
  227. }
  228. }
  229. /**
  230. * Checks if the Meta key was down when the mouse event took place.
  231. *
  232. * @return true if Meta was pressed when the event occured, false
  233. * otherwise or if unknown
  234. */
  235. public boolean isMetaKey() {
  236. if (null != details) {
  237. return details.isMetaKey();
  238. } else {
  239. return false;
  240. }
  241. }
  242. /**
  243. * Checks if the Shift key was down when the mouse event took place.
  244. *
  245. * @return true if Shift was pressed when the event occured, false
  246. * otherwise or if unknown
  247. */
  248. public boolean isShiftKey() {
  249. if (null != details) {
  250. return details.isShiftKey();
  251. } else {
  252. return false;
  253. }
  254. }
  255. }
  256. /**
  257. * Interface for listening for a {@link ClickEvent} fired by a
  258. * {@link Component}.
  259. *
  260. * @author Vaadin Ltd.
  261. * @since 3.0
  262. */
  263. public interface ClickListener extends Serializable {
  264. public static final Method BUTTON_CLICK_METHOD = ReflectTools
  265. .findMethod(ClickListener.class, "buttonClick",
  266. ClickEvent.class);
  267. /**
  268. * Called when a {@link Button} has been clicked. A reference to the
  269. * button is given by {@link ClickEvent#getButton()}.
  270. *
  271. * @param event
  272. * An event containing information about the click.
  273. */
  274. public void buttonClick(ClickEvent event);
  275. }
  276. /**
  277. * Adds the button click listener.
  278. *
  279. * @see Registration
  280. *
  281. * @param listener
  282. * the Listener to be added.
  283. * @return a registration object for removing the listener
  284. */
  285. public Registration addClickListener(ClickListener listener) {
  286. return addListener(ClickEvent.class, listener,
  287. ClickListener.BUTTON_CLICK_METHOD);
  288. }
  289. /**
  290. * Removes the button click listener.
  291. *
  292. * @param listener
  293. * the Listener to be removed.
  294. *
  295. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in the
  296. * registration object returned from
  297. * {@link #addClickListener(ClickListener)}.
  298. */
  299. @Deprecated
  300. public void removeClickListener(ClickListener listener) {
  301. removeListener(ClickEvent.class, listener,
  302. ClickListener.BUTTON_CLICK_METHOD);
  303. }
  304. /**
  305. * Simulates a button click, notifying all server-side listeners.
  306. *
  307. * No action is taken is the button is disabled.
  308. */
  309. public void click() {
  310. if (isEnabled()) {
  311. fireClick();
  312. }
  313. }
  314. /**
  315. * Fires a click event to all listeners without any event details.
  316. *
  317. * In subclasses, override {@link #fireClick(MouseEventDetails)} instead of
  318. * this method.
  319. */
  320. protected void fireClick() {
  321. fireEvent(new Button.ClickEvent(this));
  322. }
  323. /**
  324. * Fires a click event to all listeners.
  325. *
  326. * @param details
  327. * MouseEventDetails from which keyboard modifiers and other
  328. * information about the mouse click can be obtained. If the
  329. * button was clicked by a keyboard event, some of the fields may
  330. * be empty/undefined.
  331. */
  332. protected void fireClick(MouseEventDetails details) {
  333. fireEvent(new Button.ClickEvent(this, details));
  334. }
  335. /*
  336. * Actions
  337. */
  338. protected ClickShortcut clickShortcut;
  339. /**
  340. * Makes it possible to invoke a click on this button by pressing the given
  341. * {@link KeyCode} and (optional) {@link ModifierKey}s.<br/>
  342. * The shortcut is global (bound to the containing Window).
  343. *
  344. * @param keyCode
  345. * the keycode for invoking the shortcut
  346. * @param modifiers
  347. * the (optional) modifiers for invoking the shortcut, null for
  348. * none
  349. */
  350. public void setClickShortcut(int keyCode, int... modifiers) {
  351. if (clickShortcut != null) {
  352. removeShortcutListener(clickShortcut);
  353. }
  354. clickShortcut = new ClickShortcut(this, keyCode, modifiers);
  355. addShortcutListener(clickShortcut);
  356. getState().clickShortcutKeyCode = clickShortcut.getKeyCode();
  357. }
  358. /**
  359. * Removes the keyboard shortcut previously set with
  360. * {@link #setClickShortcut(int, int...)}.
  361. */
  362. public void removeClickShortcut() {
  363. if (clickShortcut != null) {
  364. removeShortcutListener(clickShortcut);
  365. clickShortcut = null;
  366. getState().clickShortcutKeyCode = 0;
  367. }
  368. }
  369. /**
  370. * A {@link ShortcutListener} specifically made to define a keyboard
  371. * shortcut that invokes a click on the given button.
  372. *
  373. */
  374. public static class ClickShortcut extends ShortcutListener {
  375. protected Button button;
  376. /**
  377. * Creates a keyboard shortcut for clicking the given button using the
  378. * shorthand notation defined in {@link ShortcutAction}.
  379. *
  380. * @param button
  381. * to be clicked when the shortcut is invoked
  382. * @param shorthandCaption
  383. * the caption with shortcut keycode and modifiers indicated
  384. */
  385. public ClickShortcut(Button button, String shorthandCaption) {
  386. super(shorthandCaption);
  387. this.button = button;
  388. }
  389. /**
  390. * Creates a keyboard shortcut for clicking the given button using the
  391. * given {@link KeyCode} and {@link ModifierKey}s.
  392. *
  393. * @param button
  394. * to be clicked when the shortcut is invoked
  395. * @param keyCode
  396. * KeyCode to react to
  397. * @param modifiers
  398. * optional modifiers for shortcut
  399. */
  400. public ClickShortcut(Button button, int keyCode, int... modifiers) {
  401. super(null, keyCode, modifiers);
  402. this.button = button;
  403. }
  404. /**
  405. * Creates a keyboard shortcut for clicking the given button using the
  406. * given {@link KeyCode}.
  407. *
  408. * @param button
  409. * to be clicked when the shortcut is invoked
  410. * @param keyCode
  411. * KeyCode to react to
  412. */
  413. public ClickShortcut(Button button, int keyCode) {
  414. this(button, keyCode, null);
  415. }
  416. @Override
  417. public void handleAction(Object sender, Object target) {
  418. button.click();
  419. }
  420. }
  421. /**
  422. * Determines if a button is automatically disabled when clicked. See
  423. * {@link #setDisableOnClick(boolean)} for details.
  424. *
  425. * @return true if the button is disabled when clicked, false otherwise
  426. */
  427. public boolean isDisableOnClick() {
  428. return getState(false).disableOnClick;
  429. }
  430. /**
  431. * Determines if a button is automatically disabled when clicked. If this is
  432. * set to true the button will be automatically disabled when clicked,
  433. * typically to prevent (accidental) extra clicks on a button.
  434. * <p>
  435. * Note that this is only used when the click comes from the user, not when
  436. * calling {@link #click()} method programmatically. Also, if developer
  437. * wants to re-enable the button, it needs to be done programmatically.
  438. * </p>
  439. *
  440. * @param disableOnClick
  441. * true to disable button when it is clicked, false otherwise
  442. */
  443. public void setDisableOnClick(boolean disableOnClick) {
  444. getState().disableOnClick = disableOnClick;
  445. }
  446. @Override
  447. protected ButtonState getState() {
  448. return (ButtonState) super.getState();
  449. }
  450. @Override
  451. protected ButtonState getState(boolean markAsDirty) {
  452. return (ButtonState) super.getState(markAsDirty);
  453. }
  454. /**
  455. * Sets the component's icon and alt text.
  456. *
  457. * An alt text is shown when an image could not be loaded, and read by
  458. * assisitve devices.
  459. *
  460. * @param icon
  461. * the icon to be shown with the component's caption.
  462. * @param iconAltText
  463. * String to use as alt text
  464. */
  465. public void setIcon(Resource icon, String iconAltText) {
  466. super.setIcon(icon);
  467. getState().iconAltText = iconAltText == null ? "" : iconAltText;
  468. }
  469. /**
  470. * Returns the icon's alt text.
  471. *
  472. * @return String with the alt text
  473. */
  474. public String getIconAlternateText() {
  475. return getState(false).iconAltText;
  476. }
  477. public void setIconAlternateText(String iconAltText) {
  478. getState().iconAltText = iconAltText;
  479. }
  480. /**
  481. * Set whether the caption text is rendered as HTML or not. You might need
  482. * to re-theme button to allow higher content than the original text style.
  483. *
  484. * If set to true, the captions are passed to the browser as html and the
  485. * developer is responsible for ensuring no harmful html is used. If set to
  486. * false, the content is passed to the browser as plain text.
  487. *
  488. * @param htmlContentAllowed
  489. * <code>true</code> if caption is rendered as HTML,
  490. * <code>false</code> otherwise
  491. *
  492. * @deprecated as of 8.0.0, use {@link #setCaptionAsHtml(boolean)} instead.
  493. */
  494. @Deprecated
  495. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  496. getState().captionAsHtml = htmlContentAllowed;
  497. }
  498. /**
  499. * Return HTML rendering setting
  500. *
  501. * @return <code>true</code> if the caption text is to be rendered as HTML,
  502. * <code>false</code> otherwise
  503. *
  504. * @deprecated as of 8.0.0, use {@link #isCaptionAsHtml()} instead.
  505. */
  506. @Deprecated
  507. public boolean isHtmlContentAllowed() {
  508. return getState(false).captionAsHtml;
  509. }
  510. /*
  511. * (non-Javadoc)
  512. *
  513. * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element,
  514. * com.vaadin.ui.declarative.DesignContext)
  515. */
  516. @Override
  517. public void readDesign(Element design, DesignContext designContext) {
  518. super.readDesign(design, designContext);
  519. Attributes attr = design.attributes();
  520. String content;
  521. // plain-text (default is html)
  522. Boolean plain = DesignAttributeHandler
  523. .readAttribute(DESIGN_ATTR_PLAIN_TEXT, attr, Boolean.class);
  524. if (plain == null || !plain) {
  525. setCaptionAsHtml(true);
  526. content = design.html();
  527. } else {
  528. // content is not intended to be interpreted as HTML,
  529. // so html entities need to be decoded
  530. content = DesignFormatter.decodeFromTextNode(design.html());
  531. }
  532. setCaption(content);
  533. if (attr.hasKey("icon-alt")) {
  534. setIconAlternateText(DesignAttributeHandler
  535. .readAttribute("icon-alt", attr, String.class));
  536. }
  537. // click-shortcut
  538. removeClickShortcut();
  539. ShortcutAction action = DesignAttributeHandler
  540. .readAttribute("click-shortcut", attr, ShortcutAction.class);
  541. if (action != null) {
  542. setClickShortcut(action.getKeyCode(), action.getModifiers());
  543. }
  544. }
  545. /*
  546. * (non-Javadoc)
  547. *
  548. * @see com.vaadin.ui.AbstractComponent#getCustomAttributes()
  549. */
  550. @Override
  551. protected Collection<String> getCustomAttributes() {
  552. Collection<String> result = super.getCustomAttributes();
  553. result.add(DESIGN_ATTR_PLAIN_TEXT);
  554. result.add("caption");
  555. result.add("icon-alt");
  556. result.add("icon-alternate-text");
  557. result.add("click-shortcut");
  558. result.add("html-content-allowed");
  559. result.add("caption-as-html");
  560. return result;
  561. }
  562. /*
  563. * (non-Javadoc)
  564. *
  565. * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element
  566. * , com.vaadin.ui.declarative.DesignContext)
  567. */
  568. @Override
  569. public void writeDesign(Element design, DesignContext designContext) {
  570. super.writeDesign(design, designContext);
  571. Attributes attr = design.attributes();
  572. Button def = designContext.getDefaultInstance(this);
  573. String content = getCaption();
  574. if (content != null) {
  575. design.html(content);
  576. }
  577. // plain-text (default is html)
  578. if (!isHtmlContentAllowed()) {
  579. design.attr(DESIGN_ATTR_PLAIN_TEXT, true);
  580. // encode HTML entities
  581. if (content != null) {
  582. design.html(DesignFormatter.encodeForTextNode(content));
  583. }
  584. }
  585. // icon-alt
  586. DesignAttributeHandler.writeAttribute("icon-alt", attr,
  587. getIconAlternateText(), def.getIconAlternateText(),
  588. String.class, designContext);
  589. // click-shortcut
  590. if (clickShortcut != null) {
  591. DesignAttributeHandler.writeAttribute("click-shortcut", attr,
  592. clickShortcut, null, ShortcutAction.class, designContext);
  593. }
  594. }
  595. }