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