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.

VUI.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright 2000-2016 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.client.ui;
  17. import java.util.List;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.event.dom.client.HasScrollHandlers;
  20. import com.google.gwt.event.dom.client.ScrollEvent;
  21. import com.google.gwt.event.dom.client.ScrollHandler;
  22. import com.google.gwt.event.logical.shared.HasResizeHandlers;
  23. import com.google.gwt.event.logical.shared.ResizeEvent;
  24. import com.google.gwt.event.logical.shared.ResizeHandler;
  25. import com.google.gwt.event.shared.HandlerRegistration;
  26. import com.google.gwt.user.client.Timer;
  27. import com.google.gwt.user.client.Window;
  28. import com.google.gwt.user.client.ui.SimplePanel;
  29. import com.vaadin.client.ApplicationConnection;
  30. import com.vaadin.client.ComponentConnector;
  31. import com.vaadin.client.ConnectorMap;
  32. import com.vaadin.client.Focusable;
  33. import com.vaadin.client.LayoutManager;
  34. import com.vaadin.client.Profiler;
  35. import com.vaadin.client.VConsole;
  36. import com.vaadin.client.WidgetUtil;
  37. import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  38. import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler;
  39. import com.vaadin.client.ui.ui.UIConnector;
  40. import com.vaadin.shared.ApplicationConstants;
  41. /**
  42. *
  43. */
  44. public class VUI extends SimplePanel implements ResizeHandler,
  45. Window.ClosingHandler, ShortcutActionHandlerOwner, Focusable,
  46. com.google.gwt.user.client.ui.Focusable, HasResizeHandlers,
  47. HasScrollHandlers {
  48. private static final int MONITOR_PARENT_TIMER_INTERVAL = 1000;
  49. /** For internal use only. May be removed or replaced in the future. */
  50. public String id;
  51. /** For internal use only. May be removed or replaced in the future. */
  52. public ShortcutActionHandler actionHandler;
  53. /*
  54. * Last known window size used to detect whether VView should be layouted
  55. * again. Detection must check window size, because the VView size might be
  56. * fixed and thus not automatically adapt to changed window sizes.
  57. */
  58. private int windowWidth;
  59. private int windowHeight;
  60. /*
  61. * Last know view size used to detect whether new dimensions should be sent
  62. * to the server.
  63. */
  64. private int viewWidth;
  65. private int viewHeight;
  66. /** For internal use only. May be removed or replaced in the future. */
  67. public ApplicationConnection connection;
  68. /**
  69. * Keep track of possible parent size changes when an embedded application.
  70. *
  71. * Uses {@link #parentWidth} and {@link #parentHeight} as an optimization to
  72. * keep track of when there is a real change.
  73. */
  74. private Timer resizeTimer;
  75. /** stored width of parent for embedded application auto-resize */
  76. private int parentWidth;
  77. /** stored height of parent for embedded application auto-resize */
  78. private int parentHeight;
  79. /** For internal use only. May be removed or replaced in the future. */
  80. public boolean resizeLazy = false;
  81. private TouchScrollHandler touchScrollHandler;
  82. private VLazyExecutor delayedResizeExecutor = new VLazyExecutor(200,
  83. () -> performSizeCheck());
  84. private Element storedFocus;
  85. public VUI() {
  86. super();
  87. // Allow focusing the view by using the focus() method, the view
  88. // should not be in the document focus flow
  89. getElement().setTabIndex(-1);
  90. makeScrollable();
  91. }
  92. /**
  93. * Start to periodically monitor for parent element resizes if embedded
  94. * application (e.g. portlet).
  95. */
  96. @Override
  97. protected void onLoad() {
  98. super.onLoad();
  99. if (isMonitoringParentSize()) {
  100. resizeTimer = new Timer() {
  101. @Override
  102. public void run() {
  103. // trigger check to see if parent size has changed,
  104. // recalculate layouts
  105. performSizeCheck();
  106. resizeTimer.schedule(MONITOR_PARENT_TIMER_INTERVAL);
  107. }
  108. };
  109. resizeTimer.schedule(MONITOR_PARENT_TIMER_INTERVAL);
  110. }
  111. }
  112. /**
  113. * Stop monitoring for parent element resizes.
  114. */
  115. @Override
  116. protected void onUnload() {
  117. if (resizeTimer != null) {
  118. resizeTimer.cancel();
  119. resizeTimer = null;
  120. }
  121. super.onUnload();
  122. }
  123. /**
  124. * Called when the window or parent div might have been resized.
  125. *
  126. * This immediately checks the sizes of the window and the parent div (if
  127. * monitoring it) and triggers layout recalculation if they have changed.
  128. */
  129. protected void performSizeCheck() {
  130. windowSizeMaybeChanged(Window.getClientWidth(),
  131. Window.getClientHeight());
  132. }
  133. /**
  134. * Called when the window or parent div might have been resized.
  135. *
  136. * This immediately checks the sizes of the window and the parent div (if
  137. * monitoring it) and triggers layout recalculation if they have changed.
  138. *
  139. * @param newWindowWidth
  140. * The new width of the window
  141. * @param newWindowHeight
  142. * The new height of the window
  143. *
  144. * @deprecated use {@link #performSizeCheck()}
  145. */
  146. @Deprecated
  147. protected void windowSizeMaybeChanged(int newWindowWidth,
  148. int newWindowHeight) {
  149. if (connection == null) {
  150. // Connection is null if the timer fires before the first UIDL
  151. // update
  152. return;
  153. }
  154. boolean changed = false;
  155. ComponentConnector connector = ConnectorMap.get(connection)
  156. .getConnector(this);
  157. if (windowWidth != newWindowWidth) {
  158. windowWidth = newWindowWidth;
  159. changed = true;
  160. connector.getLayoutManager().reportOuterWidth(connector,
  161. newWindowWidth);
  162. VConsole.log("New window width: " + windowWidth);
  163. }
  164. if (windowHeight != newWindowHeight) {
  165. windowHeight = newWindowHeight;
  166. changed = true;
  167. connector.getLayoutManager().reportOuterHeight(connector,
  168. newWindowHeight);
  169. VConsole.log("New window height: " + windowHeight);
  170. }
  171. Element parentElement = getElement().getParentElement();
  172. if (isMonitoringParentSize() && parentElement != null) {
  173. // check also for parent size changes
  174. int newParentWidth = parentElement.getClientWidth();
  175. int newParentHeight = parentElement.getClientHeight();
  176. if (parentWidth != newParentWidth) {
  177. parentWidth = newParentWidth;
  178. changed = true;
  179. VConsole.log("New parent width: " + parentWidth);
  180. }
  181. if (parentHeight != newParentHeight) {
  182. parentHeight = newParentHeight;
  183. changed = true;
  184. VConsole.log("New parent height: " + parentHeight);
  185. }
  186. }
  187. if (changed) {
  188. /*
  189. * If the window size has changed, layout the VView again and send
  190. * new size to the server if the size changed. (Just checking VView
  191. * size would cause us to ignore cases when a relatively sized VView
  192. * should shrink as the content's size is fixed and would thus not
  193. * automatically shrink.)
  194. */
  195. VConsole.log(
  196. "Running layout functions due to window or parent resize");
  197. // update size to avoid (most) redundant re-layout passes
  198. // there can still be an extra layout recalculation if webkit
  199. // overflow fix updates the size in a deferred block
  200. if (isMonitoringParentSize() && parentElement != null) {
  201. parentWidth = parentElement.getClientWidth();
  202. parentHeight = parentElement.getClientHeight();
  203. }
  204. sendClientResized();
  205. LayoutManager layoutManager = connector.getLayoutManager();
  206. if (layoutManager.isLayoutRunning()) {
  207. layoutManager.layoutLater();
  208. } else {
  209. layoutManager.layoutNow();
  210. }
  211. }
  212. }
  213. /**
  214. * @return the name of the theme in use by this UI.
  215. * @deprecated as of 7.3. Use {@link UIConnector#getActiveTheme()} instead.
  216. */
  217. @Deprecated
  218. public String getTheme() {
  219. return ((UIConnector) ConnectorMap.get(connection).getConnector(this))
  220. .getActiveTheme();
  221. }
  222. /**
  223. * Returns true if the body is NOT generated, i.e if someone else has made
  224. * the page that we're running in. Otherwise we're in charge of the whole
  225. * page.
  226. *
  227. * @return true if we're running embedded
  228. */
  229. public boolean isEmbedded() {
  230. return !getElement().getOwnerDocument().getBody().getClassName()
  231. .contains(ApplicationConstants.GENERATED_BODY_CLASSNAME);
  232. }
  233. /**
  234. * Returns true if the size of the parent should be checked periodically and
  235. * the application should react to its changes.
  236. *
  237. * @return true if size of parent should be tracked
  238. */
  239. protected boolean isMonitoringParentSize() {
  240. // could also perform a more specific check (Liferay portlet)
  241. return isEmbedded();
  242. }
  243. /*
  244. * (non-Javadoc)
  245. *
  246. * @see
  247. * com.google.gwt.event.logical.shared.ResizeHandler#onResize(com.google
  248. * .gwt.event.logical.shared.ResizeEvent)
  249. */
  250. @Override
  251. public void onResize(ResizeEvent event) {
  252. triggerSizeChangeCheck();
  253. }
  254. /**
  255. * Called when a resize event is received.
  256. *
  257. * This may trigger a lazy refresh or perform the size check immediately
  258. * depending on the browser used and whether the server side requests
  259. * resizes to be lazy.
  260. */
  261. private void triggerSizeChangeCheck() {
  262. /*
  263. * We may postpone these events to avoid slowness when resizing the
  264. * browser window. Constantly recalculating the layout causes the resize
  265. * operation to be really slow with complex layouts.
  266. */
  267. boolean lazy = resizeLazy;
  268. if (lazy) {
  269. delayedResizeExecutor.trigger();
  270. } else {
  271. performSizeCheck();
  272. }
  273. }
  274. /**
  275. * Send new dimensions to the server.
  276. * <p>
  277. * For internal use only. May be removed or replaced in the future.
  278. */
  279. public void sendClientResized() {
  280. Profiler.enter("VUI.sendClientResized");
  281. Element parentElement = getElement().getParentElement();
  282. int viewHeight = parentElement.getClientHeight();
  283. int viewWidth = parentElement.getClientWidth();
  284. ResizeEvent.fire(this, viewWidth, viewHeight);
  285. Profiler.leave("VUI.sendClientResized");
  286. }
  287. public static native void goTo(String url)
  288. /*-{
  289. $wnd.location = url;
  290. }-*/;
  291. @Override
  292. public void onWindowClosing(Window.ClosingEvent event) {
  293. // Ensure that any change in the currently focused component is noted
  294. // before refreshing. Ensures that e.g. text in the focused text field
  295. // does not disappear on refresh (when preserve on refresh is enabled)
  296. connection.flushActiveConnector();
  297. }
  298. private static native void loadAppIdListFromDOM(List<String> list)
  299. /*-{
  300. for (var j in $wnd.vaadin.vaadinConfigurations) {
  301. // $entry not needed as function is not exported
  302. list.@java.util.Collection::add(Ljava/lang/Object;)(j);
  303. }
  304. }-*/;
  305. @Override
  306. public ShortcutActionHandler getShortcutActionHandler() {
  307. return actionHandler;
  308. }
  309. @Override
  310. public void focus() {
  311. setFocus(true);
  312. }
  313. /**
  314. * Ensures the widget is scrollable e.g. after style name changes.
  315. * <p>
  316. * For internal use only. May be removed or replaced in the future.
  317. */
  318. public void makeScrollable() {
  319. if (touchScrollHandler == null) {
  320. touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
  321. }
  322. touchScrollHandler.addElement(getElement());
  323. }
  324. @Override
  325. public HandlerRegistration addResizeHandler(ResizeHandler resizeHandler) {
  326. return addHandler(resizeHandler, ResizeEvent.getType());
  327. }
  328. @Override
  329. public HandlerRegistration addScrollHandler(ScrollHandler scrollHandler) {
  330. return addHandler(scrollHandler, ScrollEvent.getType());
  331. }
  332. @Override
  333. public int getTabIndex() {
  334. return FocusUtil.getTabIndex(this);
  335. }
  336. @Override
  337. public void setAccessKey(char key) {
  338. FocusUtil.setAccessKey(this, key);
  339. }
  340. @Override
  341. public void setFocus(boolean focused) {
  342. FocusUtil.setFocus(this, focused);
  343. }
  344. @Override
  345. public void setTabIndex(int index) {
  346. FocusUtil.setTabIndex(this, index);
  347. }
  348. /**
  349. * Allows to store the currently focused Element.
  350. *
  351. * Current use case is to store the focus when a Window is opened. Does
  352. * currently handle only a single value. Needs to be extended for #12158
  353. *
  354. * @param focusedElement
  355. */
  356. public void storeFocus() {
  357. storedFocus = WidgetUtil.getFocusedElement();
  358. }
  359. /**
  360. * Restores the previously stored focus Element.
  361. *
  362. * Current use case is to restore the focus when a Window is closed. Does
  363. * currently handle only a single value. Needs to be extended for #12158
  364. */
  365. public void focusStoredElement() {
  366. if (storedFocus != null) {
  367. storedFocus.focus();
  368. }
  369. }
  370. }