Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Panel.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright 2000-2021 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.util.Collection;
  18. import java.util.Map;
  19. import org.jsoup.nodes.Element;
  20. import com.vaadin.event.Action;
  21. import com.vaadin.event.Action.Handler;
  22. import com.vaadin.event.ActionManager;
  23. import com.vaadin.event.MouseEvents.ClickEvent;
  24. import com.vaadin.event.MouseEvents.ClickListener;
  25. import com.vaadin.server.PaintException;
  26. import com.vaadin.server.PaintTarget;
  27. import com.vaadin.server.Scrollable;
  28. import com.vaadin.shared.EventId;
  29. import com.vaadin.shared.MouseEventDetails;
  30. import com.vaadin.shared.Registration;
  31. import com.vaadin.shared.ui.panel.PanelServerRpc;
  32. import com.vaadin.shared.ui.panel.PanelState;
  33. import com.vaadin.ui.Component.Focusable;
  34. import com.vaadin.ui.declarative.DesignContext;
  35. /**
  36. * Panel - a simple single component container.
  37. *
  38. * @author Vaadin Ltd.
  39. * @since 3.0
  40. */
  41. @SuppressWarnings("serial")
  42. public class Panel extends AbstractSingleComponentContainer
  43. implements Scrollable, Action.Notifier, Focusable, LegacyComponent {
  44. /**
  45. * Keeps track of the Actions added to this component, and manages the
  46. * painting and handling as well.
  47. */
  48. protected ActionManager actionManager;
  49. private PanelServerRpc rpc = (MouseEventDetails mouseDetails) -> fireEvent(
  50. new ClickEvent(Panel.this, mouseDetails));
  51. /**
  52. * Creates a new empty panel.
  53. */
  54. public Panel() {
  55. this((ComponentContainer) null);
  56. }
  57. /**
  58. * Creates a new empty panel which contains the given content.
  59. *
  60. * @param content
  61. * the content for the panel.
  62. */
  63. public Panel(Component content) {
  64. registerRpc(rpc);
  65. setContent(content);
  66. setWidth(100, Unit.PERCENTAGE);
  67. getState().tabIndex = -1;
  68. }
  69. /**
  70. * Creates a new empty panel with caption.
  71. *
  72. * @param caption
  73. * the caption used in the panel (HTML).
  74. */
  75. public Panel(String caption) {
  76. this(caption, null);
  77. }
  78. /**
  79. * Creates a new empty panel with the given caption and content.
  80. *
  81. * @param caption
  82. * the caption of the panel (HTML).
  83. * @param content
  84. * the content used in the panel.
  85. */
  86. public Panel(String caption, Component content) {
  87. this(content);
  88. setCaption(caption);
  89. }
  90. @Override
  91. public void setCaption(String caption) {
  92. super.setCaption(caption);
  93. }
  94. /*
  95. * (non-Javadoc)
  96. *
  97. * @see com.vaadin.server.LegacyComponent#paintContent(com.vaadin.server
  98. * .PaintTarget)
  99. */
  100. @Override
  101. public void paintContent(PaintTarget target) throws PaintException {
  102. if (actionManager != null) {
  103. actionManager.paintActions(null, target);
  104. }
  105. }
  106. /**
  107. * Called when one or more variables handled by the implementing class are
  108. * changed.
  109. *
  110. * @see com.vaadin.server.VariableOwner#changeVariables(Object, Map)
  111. */
  112. @Override
  113. public void changeVariables(Object source, Map<String, Object> variables) {
  114. // Get new size
  115. final Integer newWidth = (Integer) variables.get("width");
  116. final Integer newHeight = (Integer) variables.get("height");
  117. if (newWidth != null && newWidth.intValue() != getWidth()) {
  118. setWidth(newWidth.intValue(), UNITS_PIXELS);
  119. }
  120. if (newHeight != null && newHeight.intValue() != getHeight()) {
  121. setHeight(newHeight.intValue(), UNITS_PIXELS);
  122. }
  123. // Scrolling
  124. final Integer newScrollX = (Integer) variables.get("scrollLeft");
  125. final Integer newScrollY = (Integer) variables.get("scrollTop");
  126. if (newScrollX != null && newScrollX.intValue() != getScrollLeft()) {
  127. // set internally, not to fire request repaint
  128. getState().scrollLeft = newScrollX.intValue();
  129. }
  130. if (newScrollY != null && newScrollY.intValue() != getScrollTop()) {
  131. // set internally, not to fire request repaint
  132. getState().scrollTop = newScrollY.intValue();
  133. }
  134. // Actions
  135. if (actionManager != null) {
  136. actionManager.handleActions(variables, this);
  137. }
  138. }
  139. /* Scrolling functionality */
  140. /*
  141. * (non-Javadoc)
  142. *
  143. * @see com.vaadin.server.Scrollable#setScrollable(boolean)
  144. */
  145. @Override
  146. public int getScrollLeft() {
  147. return getState(false).scrollLeft;
  148. }
  149. /*
  150. * (non-Javadoc)
  151. *
  152. * @see com.vaadin.server.Scrollable#setScrollable(boolean)
  153. */
  154. @Override
  155. public int getScrollTop() {
  156. return getState(false).scrollTop;
  157. }
  158. /*
  159. * (non-Javadoc)
  160. *
  161. * @see com.vaadin.server.Scrollable#setScrollLeft(int)
  162. */
  163. @Override
  164. public void setScrollLeft(int scrollLeft) {
  165. if (scrollLeft < 0) {
  166. throw new IllegalArgumentException(
  167. "Scroll offset must be at least 0");
  168. }
  169. getState().scrollLeft = scrollLeft;
  170. }
  171. /*
  172. * (non-Javadoc)
  173. *
  174. * @see com.vaadin.server.Scrollable#setScrollTop(int)
  175. */
  176. @Override
  177. public void setScrollTop(int scrollTop) {
  178. if (scrollTop < 0) {
  179. throw new IllegalArgumentException(
  180. "Scroll offset must be at least 0");
  181. }
  182. getState().scrollTop = scrollTop;
  183. }
  184. /*
  185. * ACTIONS
  186. */
  187. @Override
  188. protected ActionManager getActionManager() {
  189. if (actionManager == null) {
  190. actionManager = new ActionManager(this);
  191. }
  192. return actionManager;
  193. }
  194. @Override
  195. public <T extends Action & com.vaadin.event.Action.Listener> void addAction(
  196. T action) {
  197. getActionManager().addAction(action);
  198. }
  199. @Override
  200. public <T extends Action & com.vaadin.event.Action.Listener> void removeAction(
  201. T action) {
  202. if (actionManager != null) {
  203. actionManager.removeAction(action);
  204. }
  205. }
  206. @Override
  207. public void addActionHandler(Handler actionHandler) {
  208. getActionManager().addActionHandler(actionHandler);
  209. }
  210. @Override
  211. public void removeActionHandler(Handler actionHandler) {
  212. if (actionManager != null) {
  213. actionManager.removeActionHandler(actionHandler);
  214. }
  215. }
  216. /**
  217. * Removes all action handlers.
  218. */
  219. public void removeAllActionHandlers() {
  220. if (actionManager != null) {
  221. actionManager.removeAllActionHandlers();
  222. }
  223. }
  224. /**
  225. * Add a click listener to the Panel. The listener is called whenever the
  226. * user clicks inside the Panel. Also when the click targets a component
  227. * inside the Panel, provided the targeted component does not prevent the
  228. * click event from propagating.
  229. *
  230. * @see Registration
  231. *
  232. * @param listener
  233. * The listener to add, not null
  234. * @return a registration object for removing the listener
  235. * @since 8.0
  236. */
  237. public Registration addClickListener(ClickListener listener) {
  238. return addListener(EventId.CLICK_EVENT_IDENTIFIER, ClickEvent.class,
  239. listener, ClickListener.clickMethod);
  240. }
  241. /**
  242. * Remove a click listener from the Panel. The listener should earlier have
  243. * been added using {@link #addClickListener(ClickListener)}.
  244. *
  245. * @param listener
  246. * The listener to remove
  247. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in the
  248. * registration object returned from
  249. * {@link #addClickListener(ClickListener)}.
  250. */
  251. @Deprecated
  252. public void removeClickListener(ClickListener listener) {
  253. removeListener(EventId.CLICK_EVENT_IDENTIFIER, ClickEvent.class,
  254. listener);
  255. }
  256. /**
  257. * {@inheritDoc}
  258. */
  259. @Override
  260. public int getTabIndex() {
  261. return getState(false).tabIndex;
  262. }
  263. /**
  264. * {@inheritDoc}
  265. */
  266. @Override
  267. public void setTabIndex(int tabIndex) {
  268. getState().tabIndex = tabIndex;
  269. }
  270. /**
  271. * Moves keyboard focus to the component. {@see Focusable#focus()}
  272. *
  273. */
  274. @Override
  275. public void focus() {
  276. super.focus();
  277. }
  278. @Override
  279. protected PanelState getState() {
  280. return (PanelState) super.getState();
  281. }
  282. @Override
  283. protected PanelState getState(boolean markAsDirty) {
  284. return (PanelState) super.getState(markAsDirty);
  285. }
  286. @Override
  287. public void readDesign(Element design, DesignContext designContext) {
  288. super.readDesign(design, designContext);
  289. }
  290. @Override
  291. protected Collection<String> getCustomAttributes() {
  292. Collection<String> attributes = super.getCustomAttributes();
  293. return attributes;
  294. }
  295. @Override
  296. public void writeDesign(Element design, DesignContext designContext) {
  297. super.writeDesign(design, designContext);
  298. // handle tabindex
  299. designContext.getDefaultInstance(this);
  300. }
  301. }