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.

PopupView.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.Serializable;
  6. import java.lang.reflect.Method;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import com.vaadin.terminal.PaintException;
  10. import com.vaadin.terminal.PaintTarget;
  11. import com.vaadin.terminal.gwt.client.ui.VPopupView;
  12. /**
  13. *
  14. * A component for displaying a two different views to data. The minimized view
  15. * is normally used to render the component, and when it is clicked the full
  16. * view is displayed on a popup. The inner class {@link PopupView.Content} is
  17. * used to deliver contents to this component.
  18. *
  19. * @author IT Mill Ltd.
  20. */
  21. @SuppressWarnings("serial")
  22. @ClientWidget(VPopupView.class)
  23. public class PopupView extends AbstractComponentContainer {
  24. private Content content;
  25. private boolean hideOnMouseOut;
  26. private Component visibleComponent;
  27. private static final Method POPUP_VISIBILITY_METHOD;
  28. static {
  29. try {
  30. POPUP_VISIBILITY_METHOD = PopupVisibilityListener.class
  31. .getDeclaredMethod("popupVisibilityChange",
  32. new Class[] { PopupVisibilityEvent.class });
  33. } catch (final java.lang.NoSuchMethodException e) {
  34. // This should never happen
  35. throw new java.lang.RuntimeException(
  36. "Internal error finding methods in PopupView");
  37. }
  38. }
  39. /**
  40. * Iterator for the visible components (zero or one components), used by
  41. * {@link PopupView#getComponentIterator()}.
  42. */
  43. private static class SingleComponentIterator implements
  44. Iterator<Component>, Serializable {
  45. private final Component component;
  46. private boolean first;
  47. public SingleComponentIterator(Component component) {
  48. this.component = component;
  49. first = (component == null);
  50. }
  51. public boolean hasNext() {
  52. return !first;
  53. }
  54. public Component next() {
  55. if (!first) {
  56. first = true;
  57. return component;
  58. } else {
  59. return null;
  60. }
  61. }
  62. public void remove() {
  63. throw new UnsupportedOperationException();
  64. }
  65. }
  66. /* Constructors */
  67. /**
  68. * A simple way to create a PopupPanel. Note that the minimal representation
  69. * may not be dynamically updated, in order to achieve this create your own
  70. * Content object and use {@link PopupView#PopupView(Content)}.
  71. *
  72. * @param small
  73. * the minimal textual representation as HTML
  74. * @param large
  75. * the full, Component-type representation
  76. */
  77. public PopupView(final java.lang.String small, final Component large) {
  78. this(new PopupView.Content() {
  79. public java.lang.String getMinimizedValueAsHTML() {
  80. return small;
  81. }
  82. public Component getPopupComponent() {
  83. return large;
  84. }
  85. });
  86. }
  87. /**
  88. * Creates a PopupView through the PopupView.Content interface. This allows
  89. * the creator to dynamically change the contents of the PopupView.
  90. *
  91. * @param content
  92. * the PopupView.Content that contains the information for this
  93. */
  94. public PopupView(PopupView.Content content) {
  95. super();
  96. hideOnMouseOut = true;
  97. setContent(content);
  98. }
  99. /**
  100. * This method will replace the current content of the panel with a new one.
  101. *
  102. * @param newContent
  103. * PopupView.Content object containing new information for the
  104. * PopupView
  105. * @throws IllegalArgumentException
  106. * if the method is passed a null value, or if one of the
  107. * content methods returns null
  108. */
  109. public void setContent(PopupView.Content newContent)
  110. throws IllegalArgumentException {
  111. if (newContent == null) {
  112. throw new IllegalArgumentException("Content must not be null");
  113. }
  114. content = newContent;
  115. requestRepaint();
  116. }
  117. /**
  118. * Returns the content-package for this PopupView.
  119. *
  120. * @return the PopupView.Content for this object or null
  121. */
  122. public PopupView.Content getContent() {
  123. return content;
  124. }
  125. /**
  126. * @deprecated Use {@link #setPopupVisible()} instead.
  127. */
  128. @Deprecated
  129. public void setPopupVisibility(boolean visible) {
  130. setPopupVisible(visible);
  131. }
  132. /**
  133. * @deprecated Use {@link #isPopupVisible()} instead.
  134. */
  135. @Deprecated
  136. public boolean getPopupVisibility() {
  137. return isPopupVisible();
  138. }
  139. /**
  140. * Set the visibility of the popup. Does not hide the minimal
  141. * representation.
  142. *
  143. * @param visible
  144. */
  145. public void setPopupVisible(boolean visible) {
  146. if (isPopupVisible() != visible) {
  147. if (visible) {
  148. visibleComponent = content.getPopupComponent();
  149. if (visibleComponent == null) {
  150. throw new java.lang.IllegalStateException(
  151. "PopupView.Content did not return Component to set visible");
  152. }
  153. super.addComponent(visibleComponent);
  154. } else {
  155. super.removeComponent(visibleComponent);
  156. visibleComponent = null;
  157. }
  158. fireEvent(new PopupVisibilityEvent(this));
  159. requestRepaint();
  160. }
  161. }
  162. /**
  163. * Return whether the popup is visible.
  164. *
  165. * @return true if the popup is showing
  166. */
  167. public boolean isPopupVisible() {
  168. return visibleComponent != null;
  169. }
  170. /**
  171. * Check if this popup will be hidden when the user takes the mouse cursor
  172. * out of the popup area.
  173. *
  174. * @return true if the popup is hidden on mouse out, false otherwise
  175. */
  176. public boolean isHideOnMouseOut() {
  177. return hideOnMouseOut;
  178. }
  179. /**
  180. * Should the popup automatically hide when the user takes the mouse cursor
  181. * out of the popup area? If this is false, the user must click outside the
  182. * popup to close it. The default is true.
  183. *
  184. * @param hideOnMouseOut
  185. *
  186. */
  187. public void setHideOnMouseOut(boolean hideOnMouseOut) {
  188. this.hideOnMouseOut = hideOnMouseOut;
  189. }
  190. /*
  191. * Methods inherited from AbstractComponentContainer. These are unnecessary
  192. * (but mandatory). Most of them are not supported in this implementation.
  193. */
  194. /**
  195. * This class only contains other components when the popup is showing.
  196. *
  197. * @see com.vaadin.ui.ComponentContainer#getComponentIterator()
  198. */
  199. public Iterator<Component> getComponentIterator() {
  200. return new SingleComponentIterator(visibleComponent);
  201. }
  202. /**
  203. * Not supported in this implementation.
  204. *
  205. * @see com.vaadin.ui.AbstractComponentContainer#removeAllComponents()
  206. * @throws UnsupportedOperationException
  207. */
  208. @Override
  209. public void removeAllComponents() {
  210. throw new UnsupportedOperationException();
  211. }
  212. /**
  213. * Not supported in this implementation.
  214. *
  215. * @see com.vaadin.ui.AbstractComponentContainer#moveComponentsFrom(com.vaadin.ui.ComponentContainer)
  216. * @throws UnsupportedOperationException
  217. */
  218. @Override
  219. public void moveComponentsFrom(ComponentContainer source)
  220. throws UnsupportedOperationException {
  221. throw new UnsupportedOperationException();
  222. }
  223. /**
  224. * Not supported in this implementation.
  225. *
  226. * @see com.vaadin.ui.AbstractComponentContainer#addComponent(com.vaadin.ui.Component)
  227. * @throws UnsupportedOperationException
  228. */
  229. @Override
  230. public void addComponent(Component c) throws UnsupportedOperationException {
  231. throw new UnsupportedOperationException();
  232. }
  233. /**
  234. * Not supported in this implementation.
  235. *
  236. * @see com.vaadin.ui.ComponentContainer#replaceComponent(com.vaadin.ui.Component,
  237. * com.vaadin.ui.Component)
  238. * @throws UnsupportedOperationException
  239. */
  240. public void replaceComponent(Component oldComponent, Component newComponent)
  241. throws UnsupportedOperationException {
  242. throw new UnsupportedOperationException();
  243. }
  244. /**
  245. * Not supported in this implementation
  246. *
  247. * @see com.vaadin.ui.AbstractComponentContainer#removeComponent(com.vaadin.ui.Component)
  248. */
  249. @Override
  250. public void removeComponent(Component c)
  251. throws UnsupportedOperationException {
  252. throw new UnsupportedOperationException();
  253. }
  254. /*
  255. * Methods for server-client communications.
  256. */
  257. /**
  258. * Paint (serialize) the component for the client.
  259. *
  260. * @see com.vaadin.ui.AbstractComponent#paintContent(com.vaadin.terminal.PaintTarget)
  261. */
  262. @Override
  263. public void paintContent(PaintTarget target) throws PaintException {
  264. // Superclass writes any common attributes in the paint target.
  265. super.paintContent(target);
  266. String html = content.getMinimizedValueAsHTML();
  267. if (html == null) {
  268. html = "";
  269. }
  270. target.addAttribute("html", html);
  271. target.addAttribute("hideOnMouseOut", hideOnMouseOut);
  272. // Only paint component to client if we know that the popup is showing
  273. if (isPopupVisible()) {
  274. target.startTag("popupComponent");
  275. visibleComponent.paint(target);
  276. target.endTag("popupComponent");
  277. }
  278. target.addVariable(this, "popupVisibility", isPopupVisible());
  279. }
  280. /**
  281. * Deserialize changes received from client.
  282. *
  283. * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
  284. * java.util.Map)
  285. */
  286. @Override
  287. public void changeVariables(Object source, Map variables) {
  288. if (variables.containsKey("popupVisibility")) {
  289. setPopupVisible(((Boolean) variables.get("popupVisibility"))
  290. .booleanValue());
  291. }
  292. }
  293. /**
  294. * Used to deliver customized content-packages to the PopupView. These are
  295. * dynamically loaded when they are redrawn. The user must take care that
  296. * neither of these methods ever return null.
  297. */
  298. public interface Content extends Serializable {
  299. /**
  300. * This should return a small view of the full data.
  301. *
  302. * @return value in HTML format
  303. */
  304. public String getMinimizedValueAsHTML();
  305. /**
  306. * This should return the full Component representing the data
  307. *
  308. * @return a Component for the value
  309. */
  310. public Component getPopupComponent();
  311. }
  312. /**
  313. * Add a listener that is called whenever the visibility of the popup is
  314. * changed.
  315. *
  316. * @param listener
  317. * the listener to add
  318. * @see PopupVisibilityListener
  319. * @see PopupVisibilityEvent
  320. * @see #removeListener(PopupVisibilityListener)
  321. *
  322. */
  323. public void addListener(PopupVisibilityListener listener) {
  324. addListener(PopupVisibilityEvent.class, listener,
  325. POPUP_VISIBILITY_METHOD);
  326. }
  327. /**
  328. * Removes a previously added listener, so that it no longer receives events
  329. * when the visibility of the popup changes.
  330. *
  331. * @param listener
  332. * the listener to remove
  333. * @see PopupVisibilityListener
  334. * @see #addListener(PopupVisibilityListener)
  335. */
  336. public void removeListener(PopupVisibilityListener listener) {
  337. removeListener(PopupVisibilityEvent.class, listener,
  338. POPUP_VISIBILITY_METHOD);
  339. }
  340. /**
  341. * This event is received by the PopupVisibilityListeners when the
  342. * visibility of the popup changes. You can get the new visibility directly
  343. * with {@link #isPopupVisible()}, or get the PopupView that produced the
  344. * event with {@link #getPopupView()}.
  345. *
  346. */
  347. public class PopupVisibilityEvent extends Event {
  348. public PopupVisibilityEvent(PopupView source) {
  349. super(source);
  350. }
  351. /**
  352. * Get the PopupView instance that is the source of this event.
  353. *
  354. * @return the source PopupView
  355. */
  356. public PopupView getPopupView() {
  357. return (PopupView) getSource();
  358. }
  359. /**
  360. * Returns the current visibility of the popup.
  361. *
  362. * @return true if the popup is visible
  363. */
  364. public boolean isPopupVisible() {
  365. return getPopupView().isPopupVisible();
  366. }
  367. }
  368. /**
  369. * Defines a listener that can receive a PopupVisibilityEvent when the
  370. * visibility of the popup changes.
  371. *
  372. */
  373. public interface PopupVisibilityListener extends Serializable {
  374. /**
  375. * Pass to {@link PopupView#PopupVisibilityEvent} to start listening for
  376. * popup visibility changes.
  377. *
  378. * @param event
  379. * the event
  380. *
  381. * @see {@link PopupVisibilityEvent}
  382. * @see {@link PopupView#addListener(PopupVisibilityListener)}
  383. */
  384. public void popupVisibilityChange(PopupVisibilityEvent event);
  385. }
  386. }