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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import com.vaadin.shared.ui.popupview.PopupViewServerRpc;
  22. import com.vaadin.shared.ui.popupview.PopupViewState;
  23. /**
  24. *
  25. * A component for displaying a two different views to data. The minimized view
  26. * is normally used to render the component, and when it is clicked the full
  27. * view is displayed on a popup. The inner class {@link PopupView.Content} is
  28. * used to deliver contents to this component.
  29. *
  30. * @author Vaadin Ltd.
  31. */
  32. @SuppressWarnings("serial")
  33. public class PopupView extends AbstractComponent implements HasComponents {
  34. private Content content;
  35. private Component visibleComponent;
  36. private static final Method POPUP_VISIBILITY_METHOD;
  37. static {
  38. try {
  39. POPUP_VISIBILITY_METHOD = PopupVisibilityListener.class
  40. .getDeclaredMethod("popupVisibilityChange",
  41. new Class[] { PopupVisibilityEvent.class });
  42. } catch (final java.lang.NoSuchMethodException e) {
  43. // This should never happen
  44. throw new java.lang.RuntimeException(
  45. "Internal error finding methods in PopupView");
  46. }
  47. }
  48. private final PopupViewServerRpc rpc = new PopupViewServerRpc() {
  49. @Override
  50. public void setPopupVisibility(boolean visible) {
  51. setPopupVisible(visible);
  52. }
  53. };
  54. /* Constructors */
  55. /**
  56. * A simple way to create a PopupPanel. Note that the minimal representation
  57. * may not be dynamically updated, in order to achieve this create your own
  58. * Content object and use {@link PopupView#PopupView(Content)}.
  59. *
  60. * @param small
  61. * the minimal textual representation as HTML
  62. * @param large
  63. * the full, Component-type representation
  64. */
  65. public PopupView(final java.lang.String small, final Component large) {
  66. this(new PopupView.Content() {
  67. @Override
  68. public java.lang.String getMinimizedValueAsHTML() {
  69. return small;
  70. }
  71. @Override
  72. public Component getPopupComponent() {
  73. return large;
  74. }
  75. });
  76. }
  77. /**
  78. * Creates a PopupView through the PopupView.Content interface. This allows
  79. * the creator to dynamically change the contents of the PopupView.
  80. *
  81. * @param content
  82. * the PopupView.Content that contains the information for this
  83. */
  84. public PopupView(PopupView.Content content) {
  85. super();
  86. registerRpc(rpc);
  87. setHideOnMouseOut(true);
  88. setContent(content);
  89. }
  90. /**
  91. * This method will replace the current content of the panel with a new one.
  92. *
  93. * @param newContent
  94. * PopupView.Content object containing new information for the
  95. * PopupView
  96. * @throws IllegalArgumentException
  97. * if the method is passed a null value, or if one of the
  98. * content methods returns null
  99. */
  100. public void setContent(PopupView.Content newContent)
  101. throws IllegalArgumentException {
  102. if (newContent == null) {
  103. throw new IllegalArgumentException("Content must not be null");
  104. }
  105. content = newContent;
  106. markAsDirty();
  107. }
  108. /**
  109. * Returns the content-package for this PopupView.
  110. *
  111. * @return the PopupView.Content for this object or null
  112. */
  113. public PopupView.Content getContent() {
  114. return content;
  115. }
  116. /**
  117. * Set the visibility of the popup. Does not hide the minimal
  118. * representation.
  119. *
  120. * @param visible
  121. */
  122. public void setPopupVisible(boolean visible) {
  123. if (isPopupVisible() != visible) {
  124. if (visible) {
  125. visibleComponent = content.getPopupComponent();
  126. if (visibleComponent == null) {
  127. throw new java.lang.IllegalStateException(
  128. "PopupView.Content did not return Component to set visible");
  129. }
  130. if (visibleComponent.getParent() != null) {
  131. // If the component already has a parent, try to remove it
  132. AbstractSingleComponentContainer
  133. .removeFromParent(visibleComponent);
  134. }
  135. visibleComponent.setParent(this);
  136. } else {
  137. if (visibleComponent.getParent() == this) {
  138. visibleComponent.setParent(null);
  139. }
  140. visibleComponent = null;
  141. }
  142. fireEvent(new PopupVisibilityEvent(this));
  143. markAsDirty();
  144. }
  145. }
  146. @Override
  147. public void beforeClientResponse(boolean initial) {
  148. super.beforeClientResponse(initial);
  149. String html = content.getMinimizedValueAsHTML();
  150. if (html == null) {
  151. html = "";
  152. }
  153. getState().html = html;
  154. }
  155. /**
  156. * Return whether the popup is visible.
  157. *
  158. * @return true if the popup is showing
  159. */
  160. public boolean isPopupVisible() {
  161. return visibleComponent != null;
  162. }
  163. /**
  164. * Check if this popup will be hidden when the user takes the mouse cursor
  165. * out of the popup area.
  166. *
  167. * @return true if the popup is hidden on mouse out, false otherwise
  168. */
  169. public boolean isHideOnMouseOut() {
  170. return getState(false).hideOnMouseOut;
  171. }
  172. /**
  173. * Should the popup automatically hide when the user takes the mouse cursor
  174. * out of the popup area? If this is false, the user must click outside the
  175. * popup to close it. The default is true.
  176. *
  177. * @param hideOnMouseOut
  178. *
  179. */
  180. public void setHideOnMouseOut(boolean hideOnMouseOut) {
  181. getState().hideOnMouseOut = hideOnMouseOut;
  182. }
  183. /*
  184. * Methods inherited from AbstractComponentContainer. These are unnecessary
  185. * (but mandatory). Most of them are not supported in this implementation.
  186. */
  187. /**
  188. * This class only contains other components when the popup is showing.
  189. *
  190. * @see com.vaadin.ui.ComponentContainer#getComponentIterator()
  191. */
  192. @Override
  193. public Iterator<Component> iterator() {
  194. if (visibleComponent != null) {
  195. return Collections.singletonList(visibleComponent).iterator();
  196. } else {
  197. return Collections.<Component> emptyList().iterator();
  198. }
  199. }
  200. /**
  201. * Gets the number of contained components. Consistent with the iterator
  202. * returned by {@link #getComponentIterator()}.
  203. *
  204. * @return the number of contained components (zero or one)
  205. */
  206. public int getComponentCount() {
  207. return (visibleComponent != null ? 1 : 0);
  208. }
  209. @Override
  210. protected PopupViewState getState() {
  211. return (PopupViewState) super.getState();
  212. }
  213. @Override
  214. protected PopupViewState getState(boolean markAsDirty) {
  215. return (PopupViewState) super.getState(markAsDirty);
  216. }
  217. /**
  218. * Used to deliver customized content-packages to the PopupView. These are
  219. * dynamically loaded when they are redrawn. The user must take care that
  220. * neither of these methods ever return null.
  221. */
  222. public interface Content extends Serializable {
  223. /**
  224. * This should return a small view of the full data.
  225. *
  226. * @return value in HTML format
  227. */
  228. public String getMinimizedValueAsHTML();
  229. /**
  230. * This should return the full Component representing the data
  231. *
  232. * @return a Component for the value
  233. */
  234. public Component getPopupComponent();
  235. }
  236. /**
  237. * Add a listener that is called whenever the visibility of the popup is
  238. * changed.
  239. *
  240. * @param listener
  241. * the listener to add
  242. * @see PopupVisibilityListener
  243. * @see PopupVisibilityEvent
  244. * @see #removeListener(PopupVisibilityListener)
  245. *
  246. */
  247. public void addPopupVisibilityListener(PopupVisibilityListener listener) {
  248. addListener(PopupVisibilityEvent.class, listener,
  249. POPUP_VISIBILITY_METHOD);
  250. }
  251. /**
  252. * @deprecated As of 7.0, replaced by
  253. * {@link #addPopupVisibilityListener(PopupVisibilityListener)}
  254. **/
  255. @Deprecated
  256. public void addListener(PopupVisibilityListener listener) {
  257. addPopupVisibilityListener(listener);
  258. }
  259. /**
  260. * Removes a previously added listener, so that it no longer receives events
  261. * when the visibility of the popup changes.
  262. *
  263. * @param listener
  264. * the listener to remove
  265. * @see PopupVisibilityListener
  266. * @see #addListener(PopupVisibilityListener)
  267. */
  268. public void removePopupVisibilityListener(PopupVisibilityListener listener) {
  269. removeListener(PopupVisibilityEvent.class, listener,
  270. POPUP_VISIBILITY_METHOD);
  271. }
  272. /**
  273. * @deprecated As of 7.0, replaced by
  274. * {@link #removePopupVisibilityListener(PopupVisibilityListener)}
  275. **/
  276. @Deprecated
  277. public void removeListener(PopupVisibilityListener listener) {
  278. removePopupVisibilityListener(listener);
  279. }
  280. /**
  281. * This event is received by the PopupVisibilityListeners when the
  282. * visibility of the popup changes. You can get the new visibility directly
  283. * with {@link #isPopupVisible()}, or get the PopupView that produced the
  284. * event with {@link #getPopupView()}.
  285. *
  286. */
  287. public static class PopupVisibilityEvent extends Event {
  288. public PopupVisibilityEvent(PopupView source) {
  289. super(source);
  290. }
  291. /**
  292. * Get the PopupView instance that is the source of this event.
  293. *
  294. * @return the source PopupView
  295. */
  296. public PopupView getPopupView() {
  297. return (PopupView) getSource();
  298. }
  299. /**
  300. * Returns the current visibility of the popup.
  301. *
  302. * @return true if the popup is visible
  303. */
  304. public boolean isPopupVisible() {
  305. return getPopupView().isPopupVisible();
  306. }
  307. }
  308. /**
  309. * Defines a listener that can receive a PopupVisibilityEvent when the
  310. * visibility of the popup changes.
  311. *
  312. */
  313. public interface PopupVisibilityListener extends Serializable {
  314. /**
  315. * Pass to {@link PopupView#PopupVisibilityEvent} to start listening for
  316. * popup visibility changes.
  317. *
  318. * @param event
  319. * the event
  320. *
  321. * @see {@link PopupVisibilityEvent}
  322. * @see {@link PopupView#addListener(PopupVisibilityListener)}
  323. */
  324. public void popupVisibilityChange(PopupVisibilityEvent event);
  325. }
  326. }