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.

IView.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import java.util.HashSet;
  6. import java.util.Iterator;
  7. import com.google.gwt.user.client.DOM;
  8. import com.google.gwt.user.client.Element;
  9. import com.google.gwt.user.client.Event;
  10. import com.google.gwt.user.client.Timer;
  11. import com.google.gwt.user.client.Window;
  12. import com.google.gwt.user.client.WindowResizeListener;
  13. import com.google.gwt.user.client.ui.RootPanel;
  14. import com.google.gwt.user.client.ui.SimplePanel;
  15. import com.google.gwt.user.client.ui.Widget;
  16. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  17. import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
  18. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  19. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  20. import com.itmill.toolkit.terminal.gwt.client.Util;
  21. /**
  22. *
  23. */
  24. public class IView extends SimplePanel implements Paintable,
  25. WindowResizeListener {
  26. private static final String CLASSNAME = "i-view";
  27. private String theme;
  28. private Paintable layout;
  29. private final HashSet subWindows = new HashSet();
  30. private String id;
  31. private ShortcutActionHandler actionHandler;
  32. /** stored width for IE resize optimization */
  33. private int width;
  34. /** stored height for IE resize optimization */
  35. private int height;
  36. /**
  37. * We are postponing resize process with IE. IE bugs with scrollbars in some
  38. * situations, that causes false onWindowResized calls. With Timer we will
  39. * give IE some time to decide if it really wants to keep current size
  40. * (scrollbars).
  41. */
  42. private Timer resizeTimer;
  43. public IView(String elementId) {
  44. super();
  45. setStyleName(CLASSNAME);
  46. DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
  47. DOM.setElementProperty(getElement(), "tabIndex", "0");
  48. RootPanel.get(elementId).add(this);
  49. Window.addWindowResizeListener(this);
  50. // set focus to iview element by default to listen possible keyboard
  51. // shortcuts
  52. if (BrowserInfo.get().isOpera() || BrowserInfo.get().isSafari()
  53. && BrowserInfo.get().getWebkitVersion() < 526) {
  54. // old webkits don't support focusing div elements
  55. Element fElem = DOM.createInputCheck();
  56. DOM.setStyleAttribute(fElem, "margin", "0");
  57. DOM.setStyleAttribute(fElem, "padding", "0");
  58. DOM.setStyleAttribute(fElem, "border", "0");
  59. DOM.setStyleAttribute(fElem, "outline", "0");
  60. DOM.setStyleAttribute(fElem, "width", "1px");
  61. DOM.setStyleAttribute(fElem, "height", "1px");
  62. DOM.setStyleAttribute(fElem, "position", "absolute");
  63. DOM.setStyleAttribute(fElem, "opacity", "0.1");
  64. DOM.appendChild(getElement(), fElem);
  65. focus(fElem);
  66. } else {
  67. focus(getElement());
  68. }
  69. }
  70. private static native void focus(Element el)
  71. /*-{
  72. try {
  73. el.focus();
  74. } catch (e) {
  75. }
  76. }-*/;
  77. public String getTheme() {
  78. return theme;
  79. }
  80. /**
  81. * Used to reload host page on theme changes.
  82. */
  83. private static native void reloadHostPage()
  84. /*-{
  85. $wnd.location.reload();
  86. }-*/;
  87. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  88. id = uidl.getId();
  89. String newTheme = uidl.getStringAttribute("theme");
  90. if (theme != null && !newTheme.equals(theme)) {
  91. // Complete page refresh is needed due css can affect layout
  92. // calculations etc
  93. reloadHostPage();
  94. } else {
  95. theme = newTheme;
  96. }
  97. if (uidl.hasAttribute("style")) {
  98. addStyleName(uidl.getStringAttribute("style"));
  99. }
  100. com.google.gwt.user.client.Window.setTitle(uidl
  101. .getStringAttribute("caption"));
  102. // Process children
  103. int childIndex = 0;
  104. // Open URL:s
  105. while (childIndex < uidl.getChildCount()
  106. && "open".equals(uidl.getChildUIDL(childIndex).getTag())) {
  107. final UIDL open = uidl.getChildUIDL(childIndex);
  108. final String url = open.getStringAttribute("src");
  109. final String target = open.getStringAttribute("name");
  110. if (target == null) {
  111. goTo(url);
  112. } else {
  113. // TODO width & height
  114. Window.open(url, target != null ? target : null, "");
  115. }
  116. childIndex++;
  117. }
  118. // Draw this application level window
  119. UIDL childUidl = uidl.getChildUIDL(childIndex);
  120. final Paintable lo = client.getPaintable(childUidl);
  121. if (layout != null) {
  122. if (layout != lo) {
  123. // remove old
  124. client.unregisterPaintable(layout);
  125. // add new
  126. setWidget((Widget) lo);
  127. layout = lo;
  128. }
  129. } else {
  130. setWidget((Widget) lo);
  131. layout = lo;
  132. }
  133. layout.updateFromUIDL(childUidl, client);
  134. // Update subwindows
  135. final HashSet removedSubWindows = new HashSet(subWindows);
  136. // Open new windows
  137. while ((childUidl = uidl.getChildUIDL(childIndex++)) != null) {
  138. if ("window".equals(childUidl.getTag())) {
  139. final Paintable w = client.getPaintable(childUidl);
  140. if (subWindows.contains(w)) {
  141. removedSubWindows.remove(w);
  142. } else {
  143. subWindows.add(w);
  144. }
  145. w.updateFromUIDL(childUidl, client);
  146. } else if ("actions".equals(childUidl.getTag())) {
  147. if (actionHandler == null) {
  148. actionHandler = new ShortcutActionHandler(id, client);
  149. }
  150. actionHandler.updateActionMap(childUidl);
  151. } else if (childUidl.getTag().equals("notifications")) {
  152. for (final Iterator it = childUidl.getChildIterator(); it
  153. .hasNext();) {
  154. final UIDL notification = (UIDL) it.next();
  155. String html = "";
  156. if (notification.hasAttribute("icon")) {
  157. final String parsedUri = client
  158. .translateToolkitUri(notification
  159. .getStringAttribute("icon"));
  160. html += "<IMG src=\"" + parsedUri + "\" />";
  161. }
  162. if (notification.hasAttribute("caption")) {
  163. html += "<H1>"
  164. + notification.getStringAttribute("caption")
  165. + "</H1>";
  166. }
  167. if (notification.hasAttribute("message")) {
  168. html += "<p>"
  169. + notification.getStringAttribute("message")
  170. + "</p>";
  171. }
  172. final String style = notification.hasAttribute("style") ? notification
  173. .getStringAttribute("style")
  174. : null;
  175. final int position = notification
  176. .getIntAttribute("position");
  177. final int delay = notification.getIntAttribute("delay");
  178. new INotification(delay).show(html, position, style);
  179. }
  180. }
  181. }
  182. // Close old windows
  183. for (final Iterator rem = removedSubWindows.iterator(); rem.hasNext();) {
  184. final IWindow w = (IWindow) rem.next();
  185. client.unregisterPaintable(w);
  186. subWindows.remove(w);
  187. w.hide();
  188. }
  189. onWindowResized(Window.getClientWidth(), Window.getClientHeight());
  190. // IE somehow fails some layout on first run, force layout
  191. // functions
  192. // Util.runDescendentsLayout(this);
  193. }
  194. public void onBrowserEvent(Event event) {
  195. super.onBrowserEvent(event);
  196. if (DOM.eventGetType(event) == Event.ONKEYDOWN && actionHandler != null) {
  197. actionHandler.handleKeyboardEvent(event);
  198. return;
  199. }
  200. }
  201. public void onWindowResized(int width, int height) {
  202. if (Util.isIE()) {
  203. /*
  204. * IE will give us some false resized events due bugs with
  205. * scrollbars. Postponing layout phase to see if size was really
  206. * changed.
  207. */
  208. if (resizeTimer == null) {
  209. resizeTimer = new Timer() {
  210. public void run() {
  211. boolean changed = false;
  212. if (IView.this.width != getOffsetWidth()) {
  213. IView.this.width = getOffsetWidth();
  214. changed = true;
  215. ApplicationConnection.getConsole().log(
  216. "window w" + IView.this.width);
  217. }
  218. if (IView.this.height != getOffsetHeight()) {
  219. IView.this.height = getOffsetHeight();
  220. changed = true;
  221. ApplicationConnection.getConsole().log(
  222. "window h" + IView.this.height);
  223. }
  224. if (changed) {
  225. ApplicationConnection
  226. .getConsole()
  227. .log(
  228. "Running layout functions due window resize");
  229. Util.runDescendentsLayout(IView.this);
  230. }
  231. }
  232. };
  233. } else {
  234. resizeTimer.cancel();
  235. }
  236. resizeTimer.schedule(200);
  237. } else {
  238. // temporary set overflow hidden, not to let scrollbars disturb
  239. // layout functions
  240. final String overflow = DOM.getStyleAttribute(getElement(),
  241. "overflow");
  242. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  243. ApplicationConnection.getConsole().log(
  244. "Running layout functions due window resize");
  245. Util.runDescendentsLayout(this);
  246. DOM.setStyleAttribute(getElement(), "overflow", overflow);
  247. }
  248. }
  249. public native static void goTo(String url)
  250. /*-{
  251. $wnd.location = url;
  252. }-*/;
  253. }