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

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