Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VViewPaintable.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashSet;
  6. import java.util.Iterator;
  7. import com.google.gwt.core.client.GWT;
  8. import com.google.gwt.core.client.Scheduler;
  9. import com.google.gwt.event.dom.client.DomEvent.Type;
  10. import com.google.gwt.event.shared.EventHandler;
  11. import com.google.gwt.event.shared.HandlerRegistration;
  12. import com.google.gwt.user.client.Command;
  13. import com.google.gwt.user.client.DOM;
  14. import com.google.gwt.user.client.Event;
  15. import com.google.gwt.user.client.History;
  16. import com.google.gwt.user.client.Window;
  17. import com.google.gwt.user.client.ui.RootPanel;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  20. import com.vaadin.terminal.gwt.client.BrowserInfo;
  21. import com.vaadin.terminal.gwt.client.Focusable;
  22. import com.vaadin.terminal.gwt.client.UIDL;
  23. import com.vaadin.terminal.gwt.client.Util;
  24. import com.vaadin.terminal.gwt.client.VConsole;
  25. import com.vaadin.terminal.gwt.client.VPaintableMap;
  26. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  27. public class VViewPaintable extends VAbstractPaintableWidgetContainer {
  28. private static final String CLICK_EVENT_IDENTIFIER = VPanelPaintable.CLICK_EVENT_IDENTIFIER;
  29. @Override
  30. public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
  31. getWidgetForPaintable().rendering = true;
  32. // As VView is not created in the same way as all other paintables we
  33. // have to set the id here
  34. setId(uidl.getId());
  35. getWidgetForPaintable().id = uidl.getId();
  36. boolean firstPaint = getWidgetForPaintable().connection == null;
  37. getWidgetForPaintable().connection = client;
  38. getWidgetForPaintable().immediate = getState().isImmediate();
  39. getWidgetForPaintable().resizeLazy = uidl
  40. .hasAttribute(VView.RESIZE_LAZY);
  41. String newTheme = uidl.getStringAttribute("theme");
  42. if (getWidgetForPaintable().theme != null
  43. && !newTheme.equals(getWidgetForPaintable().theme)) {
  44. // Complete page refresh is needed due css can affect layout
  45. // calculations etc
  46. getWidgetForPaintable().reloadHostPage();
  47. } else {
  48. getWidgetForPaintable().theme = newTheme;
  49. }
  50. // this also implicitly removes old styles
  51. getWidgetForPaintable().setStyleName(
  52. getWidgetForPaintable().getStylePrimaryName() + " "
  53. + getState().getStyle());
  54. clickEventHandler.handleEventHandlerRegistration(client);
  55. if (!getWidgetForPaintable().isEmbedded()
  56. && getState().getCaption() != null) {
  57. // only change window title if we're in charge of the whole page
  58. com.google.gwt.user.client.Window.setTitle(getState().getCaption());
  59. }
  60. // Process children
  61. int childIndex = 0;
  62. // Open URL:s
  63. boolean isClosed = false; // was this window closed?
  64. while (childIndex < uidl.getChildCount()
  65. && "open".equals(uidl.getChildUIDL(childIndex).getTag())) {
  66. final UIDL open = uidl.getChildUIDL(childIndex);
  67. final String url = client.translateVaadinUri(open
  68. .getStringAttribute("src"));
  69. final String target = open.getStringAttribute("name");
  70. if (target == null) {
  71. // source will be opened to this browser window, but we may have
  72. // to finish rendering this window in case this is a download
  73. // (and window stays open).
  74. Scheduler.get().scheduleDeferred(new Command() {
  75. public void execute() {
  76. VView.goTo(url);
  77. }
  78. });
  79. } else if ("_self".equals(target)) {
  80. // This window is closing (for sure). Only other opens are
  81. // relevant in this change. See #3558, #2144
  82. isClosed = true;
  83. VView.goTo(url);
  84. } else {
  85. String options;
  86. if (open.hasAttribute("border")) {
  87. if (open.getStringAttribute("border").equals("minimal")) {
  88. options = "menubar=yes,location=no,status=no";
  89. } else {
  90. options = "menubar=no,location=no,status=no";
  91. }
  92. } else {
  93. options = "resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes";
  94. }
  95. if (open.hasAttribute("width")) {
  96. int w = open.getIntAttribute("width");
  97. options += ",width=" + w;
  98. }
  99. if (open.hasAttribute("height")) {
  100. int h = open.getIntAttribute("height");
  101. options += ",height=" + h;
  102. }
  103. Window.open(url, target, options);
  104. }
  105. childIndex++;
  106. }
  107. if (isClosed) {
  108. // don't render the content, something else will be opened to this
  109. // browser view
  110. getWidgetForPaintable().rendering = false;
  111. return;
  112. }
  113. // Draw this application level window
  114. UIDL childUidl = uidl.getChildUIDL(childIndex);
  115. final VPaintableWidget lo = client.getPaintable(childUidl);
  116. if (getWidgetForPaintable().layout != null) {
  117. if (getWidgetForPaintable().layout != lo) {
  118. // remove old
  119. client.unregisterPaintable(getWidgetForPaintable().layout);
  120. // add new
  121. getWidgetForPaintable().setWidget(lo.getWidgetForPaintable());
  122. getWidgetForPaintable().layout = lo;
  123. }
  124. } else {
  125. getWidgetForPaintable().setWidget(lo.getWidgetForPaintable());
  126. getWidgetForPaintable().layout = lo;
  127. }
  128. getWidgetForPaintable().layout.updateFromUIDL(childUidl, client);
  129. if (isRealUpdate(childUidl)) {
  130. getWidgetForPaintable().updateParentFrameSize();
  131. }
  132. // Save currently open subwindows to track which will need to be closed
  133. final HashSet<VWindow> removedSubWindows = new HashSet<VWindow>(
  134. getWidgetForPaintable().subWindows);
  135. // Handle other UIDL children
  136. while ((childUidl = uidl.getChildUIDL(++childIndex)) != null) {
  137. String tag = childUidl.getTag().intern();
  138. if (tag == "actions") {
  139. if (getWidgetForPaintable().actionHandler == null) {
  140. getWidgetForPaintable().actionHandler = new ShortcutActionHandler(
  141. getWidgetForPaintable().id, client);
  142. }
  143. getWidgetForPaintable().actionHandler
  144. .updateActionMap(childUidl);
  145. } else if (tag == "execJS") {
  146. String script = childUidl.getStringAttribute("script");
  147. VView.eval(script);
  148. } else if (tag == "notifications") {
  149. for (final Iterator<?> it = childUidl.getChildIterator(); it
  150. .hasNext();) {
  151. final UIDL notification = (UIDL) it.next();
  152. VNotification.showNotification(client, notification);
  153. }
  154. } else {
  155. // subwindows
  156. final VWindowPaintable w = (VWindowPaintable) client
  157. .getPaintable(childUidl);
  158. VWindow windowWidget = w.getWidgetForPaintable();
  159. if (getWidgetForPaintable().subWindows.contains(windowWidget)) {
  160. removedSubWindows.remove(windowWidget);
  161. } else {
  162. getWidgetForPaintable().subWindows.add(windowWidget);
  163. }
  164. w.updateFromUIDL(childUidl, client);
  165. }
  166. }
  167. // Close old windows which where not in UIDL anymore
  168. for (final Iterator<VWindow> rem = removedSubWindows.iterator(); rem
  169. .hasNext();) {
  170. final VWindow w = rem.next();
  171. client.unregisterPaintable(VPaintableMap.get(getConnection())
  172. .getPaintable(w));
  173. getWidgetForPaintable().subWindows.remove(w);
  174. w.hide();
  175. }
  176. if (uidl.hasAttribute("focused")) {
  177. // set focused component when render phase is finished
  178. Scheduler.get().scheduleDeferred(new Command() {
  179. public void execute() {
  180. VPaintableWidget paintable = (VPaintableWidget) uidl
  181. .getPaintableAttribute("focused", getConnection());
  182. final Widget toBeFocused = paintable
  183. .getWidgetForPaintable();
  184. /*
  185. * Two types of Widgets can be focused, either implementing
  186. * GWT HasFocus of a thinner Vaadin specific Focusable
  187. * interface.
  188. */
  189. if (toBeFocused instanceof com.google.gwt.user.client.ui.Focusable) {
  190. final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget = (com.google.gwt.user.client.ui.Focusable) toBeFocused;
  191. toBeFocusedWidget.setFocus(true);
  192. } else if (toBeFocused instanceof Focusable) {
  193. ((Focusable) toBeFocused).focus();
  194. } else {
  195. VConsole.log("Could not focus component");
  196. }
  197. }
  198. });
  199. }
  200. // Add window listeners on first paint, to prevent premature
  201. // variablechanges
  202. if (firstPaint) {
  203. Window.addWindowClosingHandler(getWidgetForPaintable());
  204. Window.addResizeHandler(getWidgetForPaintable());
  205. }
  206. getWidgetForPaintable().onResize();
  207. // finally set scroll position from UIDL
  208. if (uidl.hasVariable("scrollTop")) {
  209. getWidgetForPaintable().scrollable = true;
  210. getWidgetForPaintable().scrollTop = uidl
  211. .getIntVariable("scrollTop");
  212. DOM.setElementPropertyInt(getWidgetForPaintable().getElement(),
  213. "scrollTop", getWidgetForPaintable().scrollTop);
  214. getWidgetForPaintable().scrollLeft = uidl
  215. .getIntVariable("scrollLeft");
  216. DOM.setElementPropertyInt(getWidgetForPaintable().getElement(),
  217. "scrollLeft", getWidgetForPaintable().scrollLeft);
  218. } else {
  219. getWidgetForPaintable().scrollable = false;
  220. }
  221. // Safari workaround must be run after scrollTop is updated as it sets
  222. // scrollTop using a deferred command.
  223. if (BrowserInfo.get().isSafari()) {
  224. Util.runWebkitOverflowAutoFix(getWidgetForPaintable().getElement());
  225. }
  226. getWidgetForPaintable().scrollIntoView(uidl);
  227. if (uidl.hasAttribute(VView.FRAGMENT_VARIABLE)) {
  228. getWidgetForPaintable().currentFragment = uidl
  229. .getStringAttribute(VView.FRAGMENT_VARIABLE);
  230. if (!getWidgetForPaintable().currentFragment.equals(History
  231. .getToken())) {
  232. History.newItem(getWidgetForPaintable().currentFragment, true);
  233. }
  234. } else {
  235. // Initial request for which the server doesn't yet have a fragment
  236. // (and haven't shown any interest in getting one)
  237. getWidgetForPaintable().currentFragment = History.getToken();
  238. // Include current fragment in the next request
  239. client.updateVariable(getWidgetForPaintable().id,
  240. VView.FRAGMENT_VARIABLE,
  241. getWidgetForPaintable().currentFragment, false);
  242. }
  243. getWidgetForPaintable().rendering = false;
  244. }
  245. public void init(String rootPanelId,
  246. ApplicationConnection applicationConnection) {
  247. DOM.sinkEvents(getWidgetForPaintable().getElement(), Event.ONKEYDOWN
  248. | Event.ONSCROLL);
  249. // iview is focused when created so element needs tabIndex
  250. // 1 due 0 is at the end of natural tabbing order
  251. DOM.setElementProperty(getWidgetForPaintable().getElement(),
  252. "tabIndex", "1");
  253. RootPanel root = RootPanel.get(rootPanelId);
  254. // Remove the v-app-loading or any splash screen added inside the div by
  255. // the user
  256. root.getElement().setInnerHTML("");
  257. // For backwards compatibility with static index pages only.
  258. // No longer added by AbstractApplicationServlet/Portlet
  259. root.removeStyleName("v-app-loading");
  260. String themeUri = applicationConnection.getConfiguration()
  261. .getThemeUri();
  262. String themeName = themeUri.substring(themeUri.lastIndexOf('/'));
  263. themeName = themeName.replaceAll("[^a-zA-Z0-9]", "");
  264. root.addStyleName("v-theme-" + themeName);
  265. root.add(getWidgetForPaintable());
  266. if (applicationConnection.getConfiguration().isStandalone()) {
  267. // set focus to iview element by default to listen possible keyboard
  268. // shortcuts. For embedded applications this is unacceptable as we
  269. // don't want to steal focus from the main page nor we don't want
  270. // side-effects from focusing (scrollIntoView).
  271. getWidgetForPaintable().getElement().focus();
  272. }
  273. getWidgetForPaintable().parentFrame = VView.getParentFrame();
  274. }
  275. private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
  276. CLICK_EVENT_IDENTIFIER) {
  277. @Override
  278. protected <H extends EventHandler> HandlerRegistration registerHandler(
  279. H handler, Type<H> type) {
  280. return getWidgetForPaintable().addDomHandler(handler, type);
  281. }
  282. };
  283. public void updateCaption(VPaintableWidget component, UIDL uidl) {
  284. // NOP The main view never draws caption for its layout
  285. }
  286. @Override
  287. public VView getWidgetForPaintable() {
  288. return (VView) super.getWidgetForPaintable();
  289. }
  290. @Override
  291. protected Widget createWidget() {
  292. return GWT.create(VView.class);
  293. }
  294. }