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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.LayoutManager;
  15. import com.vaadin.terminal.gwt.client.UIDL;
  16. public class PanelConnector extends AbstractComponentContainerConnector
  17. implements SimpleManagedLayout, PostLayoutListener {
  18. public static final String CLICK_EVENT_IDENTIFIER = "click";
  19. private Integer uidlScrollTop;
  20. private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
  21. CLICK_EVENT_IDENTIFIER) {
  22. @Override
  23. protected <H extends EventHandler> HandlerRegistration registerHandler(
  24. H handler, Type<H> type) {
  25. return getWidget().addDomHandler(handler, type);
  26. }
  27. };
  28. private Integer uidlScrollLeft;
  29. @Override
  30. public void init() {
  31. VPanel panel = getWidget();
  32. LayoutManager layoutManager = getLayoutManager();
  33. layoutManager.registerDependency(this, panel.captionNode);
  34. layoutManager.registerDependency(this, panel.bottomDecoration);
  35. layoutManager.registerDependency(this, panel.contentNode);
  36. }
  37. @Override
  38. protected boolean delegateCaptionHandling() {
  39. return false;
  40. }
  41. @Override
  42. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  43. if (isRealUpdate(uidl)) {
  44. // Handle caption displaying and style names, prior generics.
  45. // Affects size calculations
  46. // Restore default stylenames
  47. getWidget().contentNode.setClassName(VPanel.CLASSNAME + "-content");
  48. getWidget().bottomDecoration.setClassName(VPanel.CLASSNAME
  49. + "-deco");
  50. getWidget().captionNode.setClassName(VPanel.CLASSNAME + "-caption");
  51. boolean hasCaption = false;
  52. if (getState().getCaption() != null
  53. && !"".equals(getState().getCaption())) {
  54. getWidget().setCaption(getState().getCaption());
  55. hasCaption = true;
  56. } else {
  57. getWidget().setCaption("");
  58. getWidget().captionNode.setClassName(VPanel.CLASSNAME
  59. + "-nocaption");
  60. }
  61. // Add proper stylenames for all elements. This way we can prevent
  62. // unwanted CSS selector inheritance.
  63. final String captionBaseClass = VPanel.CLASSNAME
  64. + (hasCaption ? "-caption" : "-nocaption");
  65. final String contentBaseClass = VPanel.CLASSNAME + "-content";
  66. final String decoBaseClass = VPanel.CLASSNAME + "-deco";
  67. String captionClass = captionBaseClass;
  68. String contentClass = contentBaseClass;
  69. String decoClass = decoBaseClass;
  70. if (getState().hasStyles()) {
  71. final String[] styles = getState().getStyle().split(" ");
  72. for (int i = 0; i < styles.length; i++) {
  73. captionClass += " " + captionBaseClass + "-" + styles[i];
  74. contentClass += " " + contentBaseClass + "-" + styles[i];
  75. decoClass += " " + decoBaseClass + "-" + styles[i];
  76. }
  77. }
  78. getWidget().captionNode.setClassName(captionClass);
  79. getWidget().contentNode.setClassName(contentClass);
  80. getWidget().bottomDecoration.setClassName(decoClass);
  81. }
  82. // Ensure correct implementation
  83. super.updateFromUIDL(uidl, client);
  84. if (!isRealUpdate(uidl)) {
  85. return;
  86. }
  87. clickEventHandler.handleEventHandlerRegistration(client);
  88. getWidget().client = client;
  89. getWidget().id = uidl.getId();
  90. getWidget().setIconUri(uidl, client);
  91. getWidget().handleError(uidl);
  92. // Render content
  93. final UIDL layoutUidl = uidl.getChildUIDL(0);
  94. final ComponentConnector newLayout = client.getPaintable(layoutUidl);
  95. if (newLayout != getWidget().layout) {
  96. if (getWidget().layout != null) {
  97. client.unregisterPaintable(getWidget().layout);
  98. }
  99. getWidget().setWidget(newLayout.getWidget());
  100. getWidget().layout = newLayout;
  101. }
  102. getWidget().layout.updateFromUIDL(layoutUidl, client);
  103. // We may have actions attached to this panel
  104. if (uidl.getChildCount() > 1) {
  105. final int cnt = uidl.getChildCount();
  106. for (int i = 1; i < cnt; i++) {
  107. UIDL childUidl = uidl.getChildUIDL(i);
  108. if (childUidl.getTag().equals("actions")) {
  109. if (getWidget().shortcutHandler == null) {
  110. getWidget().shortcutHandler = new ShortcutActionHandler(
  111. getId(), client);
  112. }
  113. getWidget().shortcutHandler.updateActionMap(childUidl);
  114. }
  115. }
  116. }
  117. if (uidl.hasVariable("scrollTop")
  118. && uidl.getIntVariable("scrollTop") != getWidget().scrollTop) {
  119. // Sizes are not yet up to date, so changing the scroll position
  120. // is deferred to after the layout phase
  121. uidlScrollTop = new Integer(uidl.getIntVariable("scrollTop"));
  122. }
  123. if (uidl.hasVariable("scrollLeft")
  124. && uidl.getIntVariable("scrollLeft") != getWidget().scrollLeft) {
  125. // Sizes are not yet up to date, so changing the scroll position
  126. // is deferred to after the layout phase
  127. uidlScrollLeft = new Integer(uidl.getIntVariable("scrollLeft"));
  128. }
  129. // And apply tab index
  130. if (uidl.hasVariable("tabindex")) {
  131. getWidget().contentNode
  132. .setTabIndex(uidl.getIntVariable("tabindex"));
  133. }
  134. }
  135. public void updateCaption(ComponentConnector component, UIDL uidl) {
  136. // NOP: layouts caption, errors etc not rendered in Panel
  137. }
  138. @Override
  139. public VPanel getWidget() {
  140. return (VPanel) super.getWidget();
  141. }
  142. @Override
  143. protected Widget createWidget() {
  144. return GWT.create(VPanel.class);
  145. }
  146. public void layout() {
  147. updateSizes();
  148. }
  149. void updateSizes() {
  150. VPanel panel = getWidget();
  151. Style contentStyle = panel.contentNode.getStyle();
  152. if (isUndefinedHeight()) {
  153. contentStyle.clearHeight();
  154. } else {
  155. contentStyle.setHeight(100, Unit.PCT);
  156. }
  157. if (isUndefinedWidth()) {
  158. contentStyle.clearWidth();
  159. } else {
  160. contentStyle.setWidth(100, Unit.PCT);
  161. }
  162. LayoutManager layoutManager = getLayoutManager();
  163. int top = layoutManager.getOuterHeight(panel.captionNode);
  164. int bottom = layoutManager.getOuterHeight(panel.bottomDecoration);
  165. Style style = panel.getElement().getStyle();
  166. panel.captionNode.getStyle().setMarginTop(-top, Unit.PX);
  167. panel.bottomDecoration.getStyle().setMarginBottom(-bottom, Unit.PX);
  168. style.setPaddingTop(top, Unit.PX);
  169. style.setPaddingBottom(bottom, Unit.PX);
  170. // Update scroll positions
  171. panel.contentNode.setScrollTop(panel.scrollTop);
  172. panel.contentNode.setScrollLeft(panel.scrollLeft);
  173. // Read actual value back to ensure update logic is correct
  174. panel.scrollTop = panel.contentNode.getScrollTop();
  175. panel.scrollLeft = panel.contentNode.getScrollLeft();
  176. }
  177. public void postLayout() {
  178. VPanel panel = getWidget();
  179. if (uidlScrollTop != null) {
  180. panel.contentNode.setScrollTop(uidlScrollTop.intValue());
  181. // Read actual value back to ensure update logic is correct
  182. // TODO Does this trigger reflows?
  183. panel.scrollTop = panel.contentNode.getScrollTop();
  184. uidlScrollTop = null;
  185. }
  186. if (uidlScrollLeft != null) {
  187. panel.contentNode.setScrollLeft(uidlScrollLeft.intValue());
  188. // Read actual value back to ensure update logic is correct
  189. // TODO Does this trigger reflows?
  190. panel.scrollLeft = panel.contentNode.getScrollLeft();
  191. uidlScrollLeft = null;
  192. }
  193. }
  194. }