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.

Panel.java 9.3KB

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