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.

VPopupView.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashSet;
  6. import java.util.Iterator;
  7. import java.util.NoSuchElementException;
  8. import java.util.Set;
  9. import com.google.gwt.event.dom.client.ClickEvent;
  10. import com.google.gwt.event.dom.client.ClickHandler;
  11. import com.google.gwt.event.dom.client.KeyCodes;
  12. import com.google.gwt.event.logical.shared.CloseEvent;
  13. import com.google.gwt.event.logical.shared.CloseHandler;
  14. import com.google.gwt.user.client.DOM;
  15. import com.google.gwt.user.client.Element;
  16. import com.google.gwt.user.client.Event;
  17. import com.google.gwt.user.client.ui.Focusable;
  18. import com.google.gwt.user.client.ui.HTML;
  19. import com.google.gwt.user.client.ui.HasWidgets;
  20. import com.google.gwt.user.client.ui.Label;
  21. import com.google.gwt.user.client.ui.PopupPanel;
  22. import com.google.gwt.user.client.ui.RootPanel;
  23. import com.google.gwt.user.client.ui.Widget;
  24. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  25. import com.vaadin.terminal.gwt.client.Container;
  26. import com.vaadin.terminal.gwt.client.Paintable;
  27. import com.vaadin.terminal.gwt.client.RenderInformation.Size;
  28. import com.vaadin.terminal.gwt.client.RenderSpace;
  29. import com.vaadin.terminal.gwt.client.UIDL;
  30. import com.vaadin.terminal.gwt.client.Util;
  31. import com.vaadin.terminal.gwt.client.VCaption;
  32. import com.vaadin.terminal.gwt.client.VCaptionWrapper;
  33. import com.vaadin.terminal.gwt.client.VTooltip;
  34. import com.vaadin.terminal.gwt.client.ui.richtextarea.VRichTextArea;
  35. public class VPopupView extends HTML implements Container, Iterable<Widget> {
  36. public static final String CLASSNAME = "v-popupview";
  37. /** For server-client communication */
  38. private String uidlId;
  39. private ApplicationConnection client;
  40. /** This variable helps to communicate popup visibility to the server */
  41. private boolean hostPopupVisible;
  42. private final CustomPopup popup;
  43. private final Label loading = new Label();
  44. /**
  45. * loading constructor
  46. */
  47. public VPopupView() {
  48. super();
  49. popup = new CustomPopup();
  50. setStyleName(CLASSNAME);
  51. popup.setStyleName(CLASSNAME + "-popup");
  52. loading.setStyleName(CLASSNAME + "-loading");
  53. setHTML("");
  54. popup.setWidget(loading);
  55. // When we click to open the popup...
  56. addClickHandler(new ClickHandler() {
  57. public void onClick(ClickEvent event) {
  58. updateState(true);
  59. }
  60. });
  61. // ..and when we close it
  62. popup.addCloseHandler(new CloseHandler<PopupPanel>() {
  63. public void onClose(CloseEvent<PopupPanel> event) {
  64. updateState(false);
  65. }
  66. });
  67. popup.setAnimationEnabled(true);
  68. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  69. }
  70. /**
  71. *
  72. *
  73. * @see com.vaadin.terminal.gwt.client.Paintable#updateFromUIDL(com.vaadin.terminal.gwt.client.UIDL,
  74. * com.vaadin.terminal.gwt.client.ApplicationConnection)
  75. */
  76. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  77. // This call should be made first. Ensure correct implementation,
  78. // and don't let the containing layout manage caption.
  79. if (client.updateComponent(this, uidl, false)) {
  80. return;
  81. }
  82. // These are for future server connections
  83. this.client = client;
  84. uidlId = uidl.getId();
  85. hostPopupVisible = uidl.getBooleanVariable("popupVisibility");
  86. setHTML(uidl.getStringAttribute("html"));
  87. if (uidl.hasAttribute("hideOnMouseOut")) {
  88. popup.setHideOnMouseOut(uidl.getBooleanAttribute("hideOnMouseOut"));
  89. }
  90. // Render the popup if visible and show it.
  91. if (hostPopupVisible) {
  92. UIDL popupUIDL = uidl.getChildUIDL(0);
  93. // showPopupOnTop(popup, hostReference);
  94. preparePopup(popup);
  95. popup.updateFromUIDL(popupUIDL, client);
  96. if (uidl.hasAttribute("style")) {
  97. final String[] styles = uidl.getStringAttribute("style").split(
  98. " ");
  99. final StringBuffer styleBuf = new StringBuffer();
  100. final String primaryName = popup.getStylePrimaryName();
  101. styleBuf.append(primaryName);
  102. for (int i = 0; i < styles.length; i++) {
  103. styleBuf.append(" ");
  104. styleBuf.append(primaryName);
  105. styleBuf.append("-");
  106. styleBuf.append(styles[i]);
  107. }
  108. popup.setStyleName(styleBuf.toString());
  109. } else {
  110. popup.setStyleName(popup.getStylePrimaryName());
  111. }
  112. showPopup(popup);
  113. // The popup shouldn't be visible, try to hide it.
  114. } else {
  115. popup.hide();
  116. }
  117. }// updateFromUIDL
  118. /**
  119. * Update popup visibility to server
  120. *
  121. * @param visibility
  122. */
  123. private void updateState(boolean visible) {
  124. // If we know the server connection
  125. // then update the current situation
  126. if (uidlId != null && client != null && isAttached()) {
  127. client.updateVariable(uidlId, "popupVisibility", visible, true);
  128. }
  129. }
  130. private void preparePopup(final CustomPopup popup) {
  131. popup.setVisible(false);
  132. popup.show();
  133. }
  134. /**
  135. * Determines the correct position for a popup and displays the popup at
  136. * that position.
  137. *
  138. * By default, the popup is shown centered relative to its host component,
  139. * ensuring it is visible on the screen if possible.
  140. *
  141. * Can be overridden to customize the popup position.
  142. *
  143. * @param popup
  144. */
  145. protected void showPopup(final CustomPopup popup) {
  146. int windowTop = RootPanel.get().getAbsoluteTop();
  147. int windowLeft = RootPanel.get().getAbsoluteLeft();
  148. int windowRight = windowLeft + RootPanel.get().getOffsetWidth();
  149. int windowBottom = windowTop + RootPanel.get().getOffsetHeight();
  150. int offsetWidth = popup.getOffsetWidth();
  151. int offsetHeight = popup.getOffsetHeight();
  152. int hostHorizontalCenter = VPopupView.this.getAbsoluteLeft()
  153. + VPopupView.this.getOffsetWidth() / 2;
  154. int hostVerticalCenter = VPopupView.this.getAbsoluteTop()
  155. + VPopupView.this.getOffsetHeight() / 2;
  156. int left = hostHorizontalCenter - offsetWidth / 2;
  157. int top = hostVerticalCenter - offsetHeight / 2;
  158. // Don't show the popup outside the screen.
  159. if ((left + offsetWidth) > windowRight) {
  160. left -= (left + offsetWidth) - windowRight;
  161. }
  162. if ((top + offsetHeight) > windowBottom) {
  163. top -= (top + offsetHeight) - windowBottom;
  164. }
  165. if (left < 0) {
  166. left = 0;
  167. }
  168. if (top < 0) {
  169. top = 0;
  170. }
  171. popup.setPopupPosition(left, top);
  172. popup.setVisible(true);
  173. }
  174. /**
  175. * Make sure that we remove the popup when the main widget is removed.
  176. *
  177. * @see com.google.gwt.user.client.ui.Widget#onUnload()
  178. */
  179. @Override
  180. protected void onDetach() {
  181. popup.hide();
  182. super.onDetach();
  183. }
  184. private static native void nativeBlur(Element e)
  185. /*-{
  186. if(e && e.blur) {
  187. e.blur();
  188. }
  189. }-*/;
  190. /**
  191. * This class is only protected to enable overriding showPopup, and is
  192. * currently not intended to be extended or otherwise used directly. Its API
  193. * (other than it being a VOverlay) is to be considered private and
  194. * potentially subject to change.
  195. */
  196. protected class CustomPopup extends VOverlay {
  197. private Paintable popupComponentPaintable = null;
  198. private Widget popupComponentWidget = null;
  199. private VCaptionWrapper captionWrapper = null;
  200. private boolean hasHadMouseOver = false;
  201. private boolean hideOnMouseOut = true;
  202. private final Set<Element> activeChildren = new HashSet<Element>();
  203. private boolean hiding = false;
  204. public CustomPopup() {
  205. super(true, false, true); // autoHide, not modal, dropshadow
  206. }
  207. // For some reason ONMOUSEOUT events are not always received, so we have
  208. // to use ONMOUSEMOVE that doesn't target the popup
  209. @Override
  210. public boolean onEventPreview(Event event) {
  211. Element target = DOM.eventGetTarget(event);
  212. boolean eventTargetsPopup = DOM.isOrHasChild(getElement(), target);
  213. int type = DOM.eventGetType(event);
  214. // Catch children that use keyboard, so we can unfocus them when
  215. // hiding
  216. if (eventTargetsPopup && type == Event.ONKEYPRESS) {
  217. activeChildren.add(target);
  218. }
  219. if (eventTargetsPopup && type == Event.ONMOUSEMOVE) {
  220. hasHadMouseOver = true;
  221. }
  222. if (!eventTargetsPopup && type == Event.ONMOUSEMOVE) {
  223. if (hasHadMouseOver && hideOnMouseOut) {
  224. hide();
  225. return true;
  226. }
  227. }
  228. // Was the TAB key released outside of our popup?
  229. if (!eventTargetsPopup && type == Event.ONKEYUP
  230. && event.getKeyCode() == KeyCodes.KEY_TAB) {
  231. // Should we hide on focus out (mouse out)?
  232. if (hideOnMouseOut) {
  233. hide();
  234. return true;
  235. }
  236. }
  237. return super.onEventPreview(event);
  238. }
  239. @Override
  240. public void hide(boolean autoClosed) {
  241. hiding = true;
  242. syncChildren();
  243. unregisterPaintables();
  244. if (popupComponentWidget != null && popupComponentWidget != loading) {
  245. remove(popupComponentWidget);
  246. }
  247. hasHadMouseOver = false;
  248. super.hide(autoClosed);
  249. }
  250. @Override
  251. public void show() {
  252. hiding = false;
  253. super.show();
  254. }
  255. /**
  256. * Try to sync all known active child widgets to server
  257. */
  258. public void syncChildren() {
  259. // Notify children with focus
  260. if ((popupComponentWidget instanceof Focusable)) {
  261. ((Focusable) popupComponentWidget).setFocus(false);
  262. } else {
  263. checkForRTE(popupComponentWidget);
  264. }
  265. // Notify children that have used the keyboard
  266. for (Element e : activeChildren) {
  267. try {
  268. nativeBlur(e);
  269. } catch (Exception ignored) {
  270. }
  271. }
  272. activeChildren.clear();
  273. }
  274. private void checkForRTE(Widget popupComponentWidget2) {
  275. if (popupComponentWidget2 instanceof VRichTextArea) {
  276. ((VRichTextArea) popupComponentWidget2)
  277. .synchronizeContentToServer();
  278. } else if (popupComponentWidget2 instanceof HasWidgets) {
  279. HasWidgets hw = (HasWidgets) popupComponentWidget2;
  280. Iterator<Widget> iterator = hw.iterator();
  281. while (iterator.hasNext()) {
  282. checkForRTE(iterator.next());
  283. }
  284. }
  285. }
  286. @Override
  287. public boolean remove(Widget w) {
  288. popupComponentPaintable = null;
  289. popupComponentWidget = null;
  290. captionWrapper = null;
  291. return super.remove(w);
  292. }
  293. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  294. Paintable newPopupComponent = client.getPaintable(uidl
  295. .getChildUIDL(0));
  296. if (newPopupComponent != popupComponentPaintable) {
  297. setWidget((Widget) newPopupComponent);
  298. popupComponentWidget = (Widget) newPopupComponent;
  299. popupComponentPaintable = newPopupComponent;
  300. }
  301. popupComponentPaintable
  302. .updateFromUIDL(uidl.getChildUIDL(0), client);
  303. }
  304. public void unregisterPaintables() {
  305. if (popupComponentPaintable != null) {
  306. client.unregisterPaintable(popupComponentPaintable);
  307. }
  308. }
  309. public void setHideOnMouseOut(boolean hideOnMouseOut) {
  310. this.hideOnMouseOut = hideOnMouseOut;
  311. }
  312. /*
  313. *
  314. * We need a hack make popup act as a child of VPopupView in Vaadin's
  315. * component tree, but work in default GWT manner when closing or
  316. * opening.
  317. *
  318. * (non-Javadoc)
  319. *
  320. * @see com.google.gwt.user.client.ui.Widget#getParent()
  321. */
  322. @Override
  323. public Widget getParent() {
  324. if (!isAttached() || hiding) {
  325. return super.getParent();
  326. } else {
  327. return VPopupView.this;
  328. }
  329. }
  330. @Override
  331. protected void onDetach() {
  332. super.onDetach();
  333. hiding = false;
  334. }
  335. @Override
  336. public Element getContainerElement() {
  337. return super.getContainerElement();
  338. }
  339. }// class CustomPopup
  340. // Container methods
  341. public RenderSpace getAllocatedSpace(Widget child) {
  342. Size popupExtra = calculatePopupExtra();
  343. return new RenderSpace(RootPanel.get().getOffsetWidth()
  344. - popupExtra.getWidth(), RootPanel.get().getOffsetHeight()
  345. - popupExtra.getHeight());
  346. }
  347. /**
  348. * Calculate extra space taken by the popup decorations
  349. *
  350. * @return
  351. */
  352. protected Size calculatePopupExtra() {
  353. Element pe = popup.getElement();
  354. Element ipe = popup.getContainerElement();
  355. // border + padding
  356. int width = Util.getRequiredWidth(pe) - Util.getRequiredWidth(ipe);
  357. int height = Util.getRequiredHeight(pe) - Util.getRequiredHeight(ipe);
  358. return new Size(width, height);
  359. }
  360. public boolean hasChildComponent(Widget component) {
  361. if (popup.popupComponentWidget != null) {
  362. return popup.popupComponentWidget == component;
  363. } else {
  364. return false;
  365. }
  366. }
  367. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  368. popup.setWidget(newComponent);
  369. popup.popupComponentWidget = newComponent;
  370. }
  371. public boolean requestLayout(Set<Paintable> child) {
  372. popup.updateShadowSizeAndPosition();
  373. return true;
  374. }
  375. public void updateCaption(Paintable component, UIDL uidl) {
  376. if (VCaption.isNeeded(uidl)) {
  377. if (popup.captionWrapper != null) {
  378. popup.captionWrapper.updateCaption(uidl);
  379. } else {
  380. popup.captionWrapper = new VCaptionWrapper(component, client);
  381. popup.setWidget(popup.captionWrapper);
  382. popup.captionWrapper.updateCaption(uidl);
  383. }
  384. } else {
  385. if (popup.captionWrapper != null) {
  386. popup.setWidget(popup.popupComponentWidget);
  387. }
  388. }
  389. popup.popupComponentWidget = (Widget) component;
  390. popup.popupComponentPaintable = component;
  391. }
  392. @Override
  393. public void onBrowserEvent(Event event) {
  394. super.onBrowserEvent(event);
  395. if (client != null) {
  396. client.handleTooltipEvent(event, this);
  397. }
  398. }
  399. public Iterator<Widget> iterator() {
  400. return new Iterator<Widget>() {
  401. int pos = 0;
  402. public boolean hasNext() {
  403. // There is a child widget only if next() has not been called.
  404. return (pos == 0);
  405. }
  406. public Widget next() {
  407. // Next can be called only once to return the popup.
  408. if (pos != 0) {
  409. throw new NoSuchElementException();
  410. }
  411. pos++;
  412. return popup;
  413. }
  414. public void remove() {
  415. throw new UnsupportedOperationException();
  416. }
  417. };
  418. }
  419. }// class VPopupView