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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.Event;
  9. import com.google.gwt.user.client.Window;
  10. import com.google.gwt.user.client.WindowResizeListener;
  11. import com.google.gwt.user.client.ui.RootPanel;
  12. import com.google.gwt.user.client.ui.SimplePanel;
  13. import com.google.gwt.user.client.ui.Widget;
  14. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  15. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  16. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  17. import com.itmill.toolkit.terminal.gwt.client.Util;
  18. /**
  19. *
  20. */
  21. public class IView extends SimplePanel implements Paintable,
  22. WindowResizeListener {
  23. private static final String CLASSNAME = "i-view";
  24. private String theme;
  25. private Paintable layout;
  26. private final HashSet subWindows = new HashSet();
  27. private String id;
  28. private ShortcutActionHandler actionHandler;
  29. public IView(String elementId) {
  30. super();
  31. setStyleName(CLASSNAME);
  32. DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
  33. RootPanel.get(elementId).add(this);
  34. Window.addWindowResizeListener(this);
  35. }
  36. public String getTheme() {
  37. return theme;
  38. }
  39. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  40. id = uidl.getId();
  41. // Some attributes to note
  42. theme = uidl.getStringAttribute("theme");
  43. com.google.gwt.user.client.Window.setTitle(uidl
  44. .getStringAttribute("caption"));
  45. // Process children
  46. int childIndex = 0;
  47. // Open URL:s
  48. while (childIndex < uidl.getChildCount()
  49. && "open".equals(uidl.getChildUIDL(childIndex).getTag())) {
  50. final UIDL open = uidl.getChildUIDL(childIndex);
  51. final String url = open.getStringAttribute("src");
  52. final String target = open.getStringAttribute("name");
  53. if (target == null) {
  54. goTo(url);
  55. } else {
  56. // TODO width & height
  57. Window.open(url, target != null ? target : null, "");
  58. }
  59. childIndex++;
  60. }
  61. // Draw this application level window
  62. UIDL childUidl = uidl.getChildUIDL(childIndex);
  63. final Paintable lo = client.getPaintable(childUidl);
  64. if (layout != null) {
  65. if (layout != lo) {
  66. // remove old
  67. client.unregisterPaintable(layout);
  68. // add new
  69. setWidget((Widget) lo);
  70. layout = lo;
  71. }
  72. } else {
  73. setWidget((Widget) lo);
  74. layout = lo;
  75. }
  76. layout.updateFromUIDL(childUidl, client);
  77. // Update subwindows
  78. final HashSet removedSubWindows = new HashSet(subWindows);
  79. // Open new windows
  80. while ((childUidl = uidl.getChildUIDL(childIndex++)) != null) {
  81. if ("window".equals(childUidl.getTag())) {
  82. final Paintable w = client.getPaintable(childUidl);
  83. if (subWindows.contains(w)) {
  84. removedSubWindows.remove(w);
  85. } else {
  86. subWindows.add(w);
  87. }
  88. w.updateFromUIDL(childUidl, client);
  89. } else if ("actions".equals(childUidl.getTag())) {
  90. if (actionHandler == null) {
  91. actionHandler = new ShortcutActionHandler(id, client);
  92. }
  93. actionHandler.updateActionMap(childUidl);
  94. } else if (childUidl.getTag().equals("notifications")) {
  95. for (final Iterator it = childUidl.getChildIterator(); it
  96. .hasNext();) {
  97. final UIDL notification = (UIDL) it.next();
  98. String html = "";
  99. if (notification.hasAttribute("caption")) {
  100. html += "<H1>"
  101. + notification.getStringAttribute("caption")
  102. + "</H1>";
  103. }
  104. if (notification.hasAttribute("message")) {
  105. html += "<p>"
  106. + notification.getStringAttribute("message")
  107. + "</p>";
  108. }
  109. final String style = notification.hasAttribute("style") ? notification
  110. .getStringAttribute("style")
  111. : null;
  112. final int position = notification
  113. .getIntAttribute("position");
  114. final int delay = notification.getIntAttribute("delay");
  115. new Notification(delay).show(html, position, style);
  116. }
  117. }
  118. }
  119. // Close old windows
  120. for (final Iterator rem = removedSubWindows.iterator(); rem.hasNext();) {
  121. final IWindow w = (IWindow) rem.next();
  122. client.unregisterPaintable(w);
  123. subWindows.remove(w);
  124. RootPanel.get().remove(w);
  125. }
  126. if (true) {
  127. // IE somehow fails some layout on first run, force layout
  128. // functions
  129. Util.runDescendentsLayout(this);
  130. }
  131. }
  132. public void onBrowserEvent(Event event) {
  133. super.onBrowserEvent(event);
  134. if (DOM.eventGetType(event) == Event.ONKEYDOWN && actionHandler != null) {
  135. actionHandler.handleKeyboardEvent(event);
  136. return;
  137. }
  138. }
  139. public void onWindowResized(int width, int height) {
  140. Util.runDescendentsLayout(this);
  141. }
  142. public native static void goTo(String url)
  143. /*-{
  144. $wnd.location = url;
  145. }-*/;
  146. }