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.

PanelConnector.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.core.client.GWT;
  6. import com.google.gwt.dom.client.Style;
  7. import com.google.gwt.dom.client.Style.Unit;
  8. import com.google.gwt.event.dom.client.DomEvent.Type;
  9. import com.google.gwt.event.shared.EventHandler;
  10. import com.google.gwt.event.shared.HandlerRegistration;
  11. import com.google.gwt.user.client.ui.Widget;
  12. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  13. import com.vaadin.terminal.gwt.client.ComponentConnector;
  14. import com.vaadin.terminal.gwt.client.ComponentState;
  15. import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangedEvent;
  16. import com.vaadin.terminal.gwt.client.LayoutManager;
  17. import com.vaadin.terminal.gwt.client.UIDL;
  18. public class PanelConnector extends AbstractComponentContainerConnector
  19. implements SimpleManagedLayout, PostLayoutListener {
  20. public static class PanelState extends ComponentState {
  21. private int tabIndex;
  22. private int scrollLeft, scrollTop;
  23. public int getTabIndex() {
  24. return tabIndex;
  25. }
  26. public void setTabIndex(int tabIndex) {
  27. this.tabIndex = tabIndex;
  28. }
  29. public int getScrollLeft() {
  30. return scrollLeft;
  31. }
  32. public void setScrollLeft(int scrollLeft) {
  33. this.scrollLeft = scrollLeft;
  34. }
  35. public int getScrollTop() {
  36. return scrollTop;
  37. }
  38. public void setScrollTop(int scrollTop) {
  39. this.scrollTop = scrollTop;
  40. }
  41. }
  42. public static final String CLICK_EVENT_IDENTIFIER = "click";
  43. private Integer uidlScrollTop;
  44. private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
  45. CLICK_EVENT_IDENTIFIER) {
  46. @Override
  47. protected <H extends EventHandler> HandlerRegistration registerHandler(
  48. H handler, Type<H> type) {
  49. return getWidget().addDomHandler(handler, type);
  50. }
  51. };
  52. private Integer uidlScrollLeft;
  53. @Override
  54. public void init() {
  55. VPanel panel = getWidget();
  56. LayoutManager layoutManager = getLayoutManager();
  57. layoutManager.registerDependency(this, panel.captionNode);
  58. layoutManager.registerDependency(this, panel.bottomDecoration);
  59. layoutManager.registerDependency(this, panel.contentNode);
  60. }
  61. @Override
  62. protected boolean delegateCaptionHandling() {
  63. return false;
  64. }
  65. @Override
  66. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  67. if (isRealUpdate(uidl)) {
  68. // Handle caption displaying and style names, prior generics.
  69. // Affects size calculations
  70. // Restore default stylenames
  71. getWidget().contentNode.setClassName(VPanel.CLASSNAME + "-content");
  72. getWidget().bottomDecoration.setClassName(VPanel.CLASSNAME
  73. + "-deco");
  74. getWidget().captionNode.setClassName(VPanel.CLASSNAME + "-caption");
  75. boolean hasCaption = false;
  76. if (getState().getCaption() != null
  77. && !"".equals(getState().getCaption())) {
  78. getWidget().setCaption(getState().getCaption());
  79. hasCaption = true;
  80. } else {
  81. getWidget().setCaption("");
  82. getWidget().captionNode.setClassName(VPanel.CLASSNAME
  83. + "-nocaption");
  84. }
  85. // Add proper stylenames for all elements. This way we can prevent
  86. // unwanted CSS selector inheritance.
  87. final String captionBaseClass = VPanel.CLASSNAME
  88. + (hasCaption ? "-caption" : "-nocaption");
  89. final String contentBaseClass = VPanel.CLASSNAME + "-content";
  90. final String decoBaseClass = VPanel.CLASSNAME + "-deco";
  91. String captionClass = captionBaseClass;
  92. String contentClass = contentBaseClass;
  93. String decoClass = decoBaseClass;
  94. if (getState().hasStyles()) {
  95. for (String style : getState().getStyles()) {
  96. captionClass += " " + captionBaseClass + "-" + style;
  97. contentClass += " " + contentBaseClass + "-" + style;
  98. decoClass += " " + decoBaseClass + "-" + style;
  99. }
  100. }
  101. getWidget().captionNode.setClassName(captionClass);
  102. getWidget().contentNode.setClassName(contentClass);
  103. getWidget().bottomDecoration.setClassName(decoClass);
  104. }
  105. // Ensure correct implementation
  106. super.updateFromUIDL(uidl, client);
  107. if (!isRealUpdate(uidl)) {
  108. return;
  109. }
  110. clickEventHandler.handleEventHandlerRegistration(client);
  111. getWidget().client = client;
  112. getWidget().id = uidl.getId();
  113. if (getState().getIcon() != null) {
  114. getWidget().setIconUri(getState().getIcon().getURL(), client);
  115. } else {
  116. getWidget().setIconUri(null, client);
  117. }
  118. getWidget().handleError(uidl);
  119. // Render content
  120. // final UIDL layoutUidl = uidl.getChildUIDL(0);
  121. // final ComponentConnector newLayout = client.getPaintable(layoutUidl);
  122. // if (newLayout != getWidget().layout) {
  123. // if (getWidget().layout != null) {
  124. // client.unregisterPaintable(getWidget().layout);
  125. // }
  126. // getWidget().setWidget(newLayout.getWidget());
  127. // getWidget().layout = newLayout;
  128. // }
  129. // getWidget().layout.updateFromUIDL(layoutUidl, client);
  130. // We may have actions attached to this panel
  131. if (uidl.getChildCount() > 0) {
  132. final int cnt = uidl.getChildCount();
  133. for (int i = 0; i < cnt; i++) {
  134. UIDL childUidl = uidl.getChildUIDL(i);
  135. if (childUidl.getTag().equals("actions")) {
  136. if (getWidget().shortcutHandler == null) {
  137. getWidget().shortcutHandler = new ShortcutActionHandler(
  138. getConnectorId(), client);
  139. }
  140. getWidget().shortcutHandler.updateActionMap(childUidl);
  141. }
  142. }
  143. }
  144. if (getState().getScrollTop() != getWidget().scrollTop) {
  145. // Sizes are not yet up to date, so changing the scroll position
  146. // is deferred to after the layout phase
  147. uidlScrollTop = getState().getScrollTop();
  148. }
  149. if (getState().getScrollLeft() != getWidget().scrollLeft) {
  150. // Sizes are not yet up to date, so changing the scroll position
  151. // is deferred to after the layout phase
  152. uidlScrollLeft = getState().getScrollLeft();
  153. }
  154. // And apply tab index
  155. getWidget().contentNode.setTabIndex(getState().getTabIndex());
  156. }
  157. public void updateCaption(ComponentConnector component, UIDL uidl) {
  158. // NOP: layouts caption, errors etc not rendered in Panel
  159. }
  160. @Override
  161. public VPanel getWidget() {
  162. return (VPanel) super.getWidget();
  163. }
  164. @Override
  165. protected Widget createWidget() {
  166. return GWT.create(VPanel.class);
  167. }
  168. public void layout() {
  169. updateSizes();
  170. }
  171. void updateSizes() {
  172. VPanel panel = getWidget();
  173. Style contentStyle = panel.contentNode.getStyle();
  174. if (isUndefinedHeight()) {
  175. contentStyle.clearHeight();
  176. } else {
  177. contentStyle.setHeight(100, Unit.PCT);
  178. }
  179. if (isUndefinedWidth()) {
  180. contentStyle.clearWidth();
  181. } else {
  182. contentStyle.setWidth(100, Unit.PCT);
  183. }
  184. LayoutManager layoutManager = getLayoutManager();
  185. int top = layoutManager.getInnerHeight(panel.captionNode);
  186. int bottom = layoutManager.getInnerHeight(panel.bottomDecoration);
  187. Style style = panel.getElement().getStyle();
  188. panel.captionNode.getStyle().setMarginTop(-top, Unit.PX);
  189. panel.bottomDecoration.getStyle().setMarginBottom(-bottom, Unit.PX);
  190. style.setPaddingTop(top, Unit.PX);
  191. style.setPaddingBottom(bottom, Unit.PX);
  192. // Update scroll positions
  193. panel.contentNode.setScrollTop(panel.scrollTop);
  194. panel.contentNode.setScrollLeft(panel.scrollLeft);
  195. // Read actual value back to ensure update logic is correct
  196. panel.scrollTop = panel.contentNode.getScrollTop();
  197. panel.scrollLeft = panel.contentNode.getScrollLeft();
  198. }
  199. public void postLayout() {
  200. VPanel panel = getWidget();
  201. if (uidlScrollTop != null) {
  202. panel.contentNode.setScrollTop(uidlScrollTop.intValue());
  203. // Read actual value back to ensure update logic is correct
  204. // TODO Does this trigger reflows?
  205. panel.scrollTop = panel.contentNode.getScrollTop();
  206. uidlScrollTop = null;
  207. }
  208. if (uidlScrollLeft != null) {
  209. panel.contentNode.setScrollLeft(uidlScrollLeft.intValue());
  210. // Read actual value back to ensure update logic is correct
  211. // TODO Does this trigger reflows?
  212. panel.scrollLeft = panel.contentNode.getScrollLeft();
  213. uidlScrollLeft = null;
  214. }
  215. }
  216. @Override
  217. public PanelState getState() {
  218. return (PanelState) super.getState();
  219. }
  220. @Override
  221. protected PanelState createState() {
  222. return GWT.create(PanelState.class);
  223. }
  224. @Override
  225. public void connectorHierarchyChanged(ConnectorHierarchyChangedEvent event) {
  226. super.connectorHierarchyChanged(event);
  227. possiblyUnregisterOldChild(event);
  228. // We always have 0 or 1 child
  229. ComponentConnector newChild = null;
  230. if (getChildren().size() != 0) {
  231. // We now have one child
  232. newChild = getChildren().get(0);
  233. }
  234. getWidget().setWidget(newChild.getWidget());
  235. }
  236. private void possiblyUnregisterOldChild(ConnectorHierarchyChangedEvent event) {
  237. // Did we have a child that needs to be unregistered?
  238. if (event.getOldChildren().size() != 0) {
  239. ComponentConnector oldChild = event.getOldChildren().get(0);
  240. // We had a child, unregister it
  241. // TODO Should be handled by the framework
  242. if (oldChild.getParent() == null) {
  243. getConnection().unregisterPaintable(oldChild);
  244. }
  245. }
  246. }
  247. }