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.

VView.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.LinkedHashSet;
  7. import com.google.gwt.core.client.Scheduler;
  8. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  9. import com.google.gwt.event.logical.shared.ResizeEvent;
  10. import com.google.gwt.event.logical.shared.ResizeHandler;
  11. import com.google.gwt.event.logical.shared.ValueChangeEvent;
  12. import com.google.gwt.event.logical.shared.ValueChangeHandler;
  13. import com.google.gwt.event.shared.HandlerRegistration;
  14. import com.google.gwt.user.client.Command;
  15. import com.google.gwt.user.client.DOM;
  16. import com.google.gwt.user.client.Event;
  17. import com.google.gwt.user.client.History;
  18. import com.google.gwt.user.client.Timer;
  19. import com.google.gwt.user.client.Window;
  20. import com.google.gwt.user.client.ui.SimplePanel;
  21. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  22. import com.vaadin.terminal.gwt.client.BrowserInfo;
  23. import com.vaadin.terminal.gwt.client.ComponentConnector;
  24. import com.vaadin.terminal.gwt.client.Focusable;
  25. import com.vaadin.terminal.gwt.client.UIDL;
  26. import com.vaadin.terminal.gwt.client.Util;
  27. import com.vaadin.terminal.gwt.client.VConsole;
  28. import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  29. /**
  30. *
  31. */
  32. public class VView extends SimplePanel implements ResizeHandler,
  33. Window.ClosingHandler, ShortcutActionHandlerOwner, Focusable {
  34. public static final String FRAGMENT_VARIABLE = "fragment";
  35. private static final String CLASSNAME = "v-view";
  36. public static final String NOTIFICATION_HTML_CONTENT_NOT_ALLOWED = "useplain";
  37. String theme;
  38. ComponentConnector layout;
  39. final LinkedHashSet<VWindow> subWindows = new LinkedHashSet<VWindow>();
  40. String id;
  41. ShortcutActionHandler actionHandler;
  42. /** stored width for IE resize optimization */
  43. private int width;
  44. /** stored height for IE resize optimization */
  45. private int height;
  46. ApplicationConnection connection;
  47. /** Identifies the click event */
  48. public static final String CLICK_EVENT_ID = "click";
  49. /**
  50. * We are postponing resize process with IE. IE bugs with scrollbars in some
  51. * situations, that causes false onWindowResized calls. With Timer we will
  52. * give IE some time to decide if it really wants to keep current size
  53. * (scrollbars).
  54. */
  55. private Timer resizeTimer;
  56. int scrollTop;
  57. int scrollLeft;
  58. boolean rendering;
  59. boolean scrollable;
  60. boolean immediate;
  61. boolean resizeLazy = false;
  62. /**
  63. * Attribute name for the lazy resize setting .
  64. */
  65. public static final String RESIZE_LAZY = "rL";
  66. private HandlerRegistration historyHandlerRegistration;
  67. /**
  68. * The current URI fragment, used to avoid sending updates if nothing has
  69. * changed.
  70. */
  71. String currentFragment;
  72. /**
  73. * Listener for URI fragment changes. Notifies the server of the new value
  74. * whenever the value changes.
  75. */
  76. private final ValueChangeHandler<String> historyChangeHandler = new ValueChangeHandler<String>() {
  77. public void onValueChange(ValueChangeEvent<String> event) {
  78. String newFragment = event.getValue();
  79. // Send the new fragment to the server if it has changed
  80. if (!newFragment.equals(currentFragment) && connection != null) {
  81. currentFragment = newFragment;
  82. connection.updateVariable(id, FRAGMENT_VARIABLE, newFragment,
  83. true);
  84. }
  85. }
  86. };
  87. private VLazyExecutor delayedResizeExecutor = new VLazyExecutor(200,
  88. new ScheduledCommand() {
  89. public void execute() {
  90. windowSizeMaybeChanged(Window.getClientWidth(),
  91. Window.getClientHeight());
  92. }
  93. });
  94. public VView() {
  95. super();
  96. setStyleName(CLASSNAME);
  97. // Allow focusing the view by using the focus() method, the view
  98. // should not be in the document focus flow
  99. getElement().setTabIndex(-1);
  100. }
  101. @Override
  102. protected void onAttach() {
  103. super.onAttach();
  104. historyHandlerRegistration = History
  105. .addValueChangeHandler(historyChangeHandler);
  106. currentFragment = History.getToken();
  107. }
  108. @Override
  109. protected void onDetach() {
  110. super.onDetach();
  111. historyHandlerRegistration.removeHandler();
  112. historyHandlerRegistration = null;
  113. }
  114. /**
  115. * Called when the window might have been resized.
  116. *
  117. * @param newWidth
  118. * The new width of the window
  119. * @param newHeight
  120. * The new height of the window
  121. */
  122. protected void windowSizeMaybeChanged(int newWidth, int newHeight) {
  123. boolean changed = false;
  124. if (width != newWidth) {
  125. width = newWidth;
  126. changed = true;
  127. VConsole.log("New window width: " + width);
  128. }
  129. if (height != newHeight) {
  130. height = newHeight;
  131. changed = true;
  132. VConsole.log("New window height: " + height);
  133. }
  134. if (changed) {
  135. VConsole.log("Running layout functions due to window resize");
  136. Util.runWebkitOverflowAutoFix(getElement());
  137. sendClientResized();
  138. connection.doLayout(false);
  139. }
  140. }
  141. public String getTheme() {
  142. return theme;
  143. }
  144. /**
  145. * Used to reload host page on theme changes.
  146. */
  147. static native void reloadHostPage()
  148. /*-{
  149. $wnd.location.reload();
  150. }-*/;
  151. /**
  152. * Evaluate the given script in the browser document.
  153. *
  154. * @param script
  155. * Script to be executed.
  156. */
  157. static native void eval(String script)
  158. /*-{
  159. try {
  160. if (script == null) return;
  161. $wnd.eval(script);
  162. } catch (e) {
  163. }
  164. }-*/;
  165. /**
  166. * Returns true if the body is NOT generated, i.e if someone else has made
  167. * the page that we're running in. Otherwise we're in charge of the whole
  168. * page.
  169. *
  170. * @return true if we're running embedded
  171. */
  172. public boolean isEmbedded() {
  173. return !getElement().getOwnerDocument().getBody().getClassName()
  174. .contains(ApplicationConnection.GENERATED_BODY_CLASSNAME);
  175. }
  176. /**
  177. * Tries to scroll paintable referenced from given UIDL snippet to be
  178. * visible.
  179. *
  180. * @param uidl
  181. */
  182. void scrollIntoView(final UIDL uidl) {
  183. if (uidl.hasAttribute("scrollTo")) {
  184. Scheduler.get().scheduleDeferred(new Command() {
  185. public void execute() {
  186. final ComponentConnector paintable = (ComponentConnector) uidl
  187. .getPaintableAttribute("scrollTo", connection);
  188. paintable.getWidget().getElement().scrollIntoView();
  189. }
  190. });
  191. }
  192. }
  193. @Override
  194. public void onBrowserEvent(Event event) {
  195. super.onBrowserEvent(event);
  196. int type = DOM.eventGetType(event);
  197. if (type == Event.ONKEYDOWN && actionHandler != null) {
  198. actionHandler.handleKeyboardEvent(event);
  199. return;
  200. } else if (scrollable && type == Event.ONSCROLL) {
  201. updateScrollPosition();
  202. }
  203. }
  204. /**
  205. * Updates scroll position from DOM and saves variables to server.
  206. */
  207. private void updateScrollPosition() {
  208. int oldTop = scrollTop;
  209. int oldLeft = scrollLeft;
  210. scrollTop = DOM.getElementPropertyInt(getElement(), "scrollTop");
  211. scrollLeft = DOM.getElementPropertyInt(getElement(), "scrollLeft");
  212. if (connection != null && !rendering) {
  213. if (oldTop != scrollTop) {
  214. connection.updateVariable(id, "scrollTop", scrollTop, false);
  215. }
  216. if (oldLeft != scrollLeft) {
  217. connection.updateVariable(id, "scrollLeft", scrollLeft, false);
  218. }
  219. }
  220. }
  221. /*
  222. * (non-Javadoc)
  223. *
  224. * @see
  225. * com.google.gwt.event.logical.shared.ResizeHandler#onResize(com.google
  226. * .gwt.event.logical.shared.ResizeEvent)
  227. */
  228. public void onResize(ResizeEvent event) {
  229. onResize();
  230. }
  231. /**
  232. * Called when a resize event is received.
  233. */
  234. void onResize() {
  235. /*
  236. * IE (pre IE9 at least) will give us some false resize events due to
  237. * problems with scrollbars. Firefox 3 might also produce some extra
  238. * events. We postpone both the re-layouting and the server side event
  239. * for a while to deal with these issues.
  240. *
  241. * We may also postpone these events to avoid slowness when resizing the
  242. * browser window. Constantly recalculating the layout causes the resize
  243. * operation to be really slow with complex layouts.
  244. */
  245. boolean lazy = resizeLazy || BrowserInfo.get().isIE8();
  246. if (lazy) {
  247. delayedResizeExecutor.trigger();
  248. } else {
  249. windowSizeMaybeChanged(Window.getClientWidth(),
  250. Window.getClientHeight());
  251. }
  252. }
  253. /**
  254. * Send new dimensions to the server.
  255. */
  256. private void sendClientResized() {
  257. connection.updateVariable(id, "height", height, false);
  258. connection.updateVariable(id, "width", width, immediate);
  259. }
  260. public native static void goTo(String url)
  261. /*-{
  262. $wnd.location = url;
  263. }-*/;
  264. public void onWindowClosing(Window.ClosingEvent event) {
  265. // Change focus on this window in order to ensure that all state is
  266. // collected from textfields
  267. // TODO this is a naive hack, that only works with text fields and may
  268. // cause some odd issues. Should be replaced with a decent solution, see
  269. // also related BeforeShortcutActionListener interface. Same interface
  270. // might be usable here.
  271. VTextField.flushChangesFromFocusedTextField();
  272. }
  273. private native static void loadAppIdListFromDOM(ArrayList<String> list)
  274. /*-{
  275. var j;
  276. for(j in $wnd.vaadin.vaadinConfigurations) {
  277. list.@java.util.Collection::add(Ljava/lang/Object;)(j);
  278. }
  279. }-*/;
  280. /**
  281. * Return an iterator for current subwindows. This method is meant for
  282. * testing purposes only.
  283. *
  284. * @return
  285. */
  286. public ArrayList<VWindow> getSubWindowList() {
  287. ArrayList<VWindow> windows = new ArrayList<VWindow>(subWindows.size());
  288. for (VWindow widget : subWindows) {
  289. windows.add(widget);
  290. }
  291. return windows;
  292. }
  293. public ShortcutActionHandler getShortcutActionHandler() {
  294. return actionHandler;
  295. }
  296. public void focus() {
  297. getElement().focus();
  298. }
  299. }