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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.NativeEvent;
  7. import com.google.gwt.dom.client.Style;
  8. import com.google.gwt.dom.client.Style.Unit;
  9. import com.google.gwt.user.client.ui.Widget;
  10. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  11. import com.vaadin.terminal.gwt.client.ComponentConnector;
  12. import com.vaadin.terminal.gwt.client.ComponentState;
  13. import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent;
  14. import com.vaadin.terminal.gwt.client.LayoutManager;
  15. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  16. import com.vaadin.terminal.gwt.client.Paintable;
  17. import com.vaadin.terminal.gwt.client.UIDL;
  18. import com.vaadin.terminal.gwt.client.Util;
  19. import com.vaadin.terminal.gwt.client.communication.RpcProxy;
  20. import com.vaadin.terminal.gwt.client.communication.ServerRpc;
  21. import com.vaadin.ui.Panel;
  22. @Component(Panel.class)
  23. public class PanelConnector extends AbstractComponentContainerConnector
  24. implements Paintable, SimpleManagedLayout, PostLayoutListener {
  25. public interface PanelServerRPC extends ClickRPC, ServerRpc {
  26. }
  27. public static class PanelState extends ComponentState {
  28. private int tabIndex;
  29. private int scrollLeft, scrollTop;
  30. public int getTabIndex() {
  31. return tabIndex;
  32. }
  33. public void setTabIndex(int tabIndex) {
  34. this.tabIndex = tabIndex;
  35. }
  36. public int getScrollLeft() {
  37. return scrollLeft;
  38. }
  39. public void setScrollLeft(int scrollLeft) {
  40. this.scrollLeft = scrollLeft;
  41. }
  42. public int getScrollTop() {
  43. return scrollTop;
  44. }
  45. public void setScrollTop(int scrollTop) {
  46. this.scrollTop = scrollTop;
  47. }
  48. }
  49. private Integer uidlScrollTop;
  50. private ClickEventHandler clickEventHandler = new ClickEventHandler(this) {
  51. @Override
  52. protected void fireClick(NativeEvent event,
  53. MouseEventDetails mouseDetails) {
  54. rpc.click(mouseDetails);
  55. }
  56. };
  57. private Integer uidlScrollLeft;
  58. private PanelServerRPC rpc;
  59. @Override
  60. public void init() {
  61. rpc = RpcProxy.create(PanelServerRPC.class, this);
  62. VPanel panel = getWidget();
  63. LayoutManager layoutManager = getLayoutManager();
  64. layoutManager.registerDependency(this, panel.captionNode);
  65. layoutManager.registerDependency(this, panel.bottomDecoration);
  66. layoutManager.registerDependency(this, panel.contentNode);
  67. }
  68. @Override
  69. protected boolean delegateCaptionHandling() {
  70. return false;
  71. }
  72. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  73. if (isRealUpdate(uidl)) {
  74. // Handle caption displaying and style names, prior generics.
  75. // Affects size calculations
  76. // Restore default stylenames
  77. getWidget().contentNode.setClassName(VPanel.CLASSNAME + "-content");
  78. getWidget().bottomDecoration.setClassName(VPanel.CLASSNAME
  79. + "-deco");
  80. getWidget().captionNode.setClassName(VPanel.CLASSNAME + "-caption");
  81. boolean hasCaption = false;
  82. if (getState().getCaption() != null
  83. && !"".equals(getState().getCaption())) {
  84. getWidget().setCaption(getState().getCaption());
  85. hasCaption = true;
  86. } else {
  87. getWidget().setCaption("");
  88. getWidget().captionNode.setClassName(VPanel.CLASSNAME
  89. + "-nocaption");
  90. }
  91. // Add proper stylenames for all elements. This way we can prevent
  92. // unwanted CSS selector inheritance.
  93. final String captionBaseClass = VPanel.CLASSNAME
  94. + (hasCaption ? "-caption" : "-nocaption");
  95. final String contentBaseClass = VPanel.CLASSNAME + "-content";
  96. final String decoBaseClass = VPanel.CLASSNAME + "-deco";
  97. String captionClass = captionBaseClass;
  98. String contentClass = contentBaseClass;
  99. String decoClass = decoBaseClass;
  100. if (getState().hasStyles()) {
  101. for (String style : getState().getStyles()) {
  102. captionClass += " " + captionBaseClass + "-" + style;
  103. contentClass += " " + contentBaseClass + "-" + style;
  104. decoClass += " " + decoBaseClass + "-" + style;
  105. }
  106. }
  107. getWidget().captionNode.setClassName(captionClass);
  108. getWidget().contentNode.setClassName(contentClass);
  109. getWidget().bottomDecoration.setClassName(decoClass);
  110. }
  111. if (!isRealUpdate(uidl)) {
  112. return;
  113. }
  114. clickEventHandler.handleEventHandlerRegistration();
  115. getWidget().client = client;
  116. getWidget().id = uidl.getId();
  117. if (getState().getIcon() != null) {
  118. getWidget().setIconUri(getState().getIcon().getURL(), client);
  119. } else {
  120. getWidget().setIconUri(null, client);
  121. }
  122. getWidget().setErrorIndicatorVisible(
  123. null != getState().getErrorMessage());
  124. // We may have actions attached to this panel
  125. if (uidl.getChildCount() > 0) {
  126. final int cnt = uidl.getChildCount();
  127. for (int i = 0; i < cnt; i++) {
  128. UIDL childUidl = uidl.getChildUIDL(i);
  129. if (childUidl.getTag().equals("actions")) {
  130. if (getWidget().shortcutHandler == null) {
  131. getWidget().shortcutHandler = new ShortcutActionHandler(
  132. getConnectorId(), client);
  133. }
  134. getWidget().shortcutHandler.updateActionMap(childUidl);
  135. }
  136. }
  137. }
  138. if (getState().getScrollTop() != getWidget().scrollTop) {
  139. // Sizes are not yet up to date, so changing the scroll position
  140. // is deferred to after the layout phase
  141. uidlScrollTop = getState().getScrollTop();
  142. }
  143. if (getState().getScrollLeft() != getWidget().scrollLeft) {
  144. // Sizes are not yet up to date, so changing the scroll position
  145. // is deferred to after the layout phase
  146. uidlScrollLeft = getState().getScrollLeft();
  147. }
  148. // And apply tab index
  149. getWidget().contentNode.setTabIndex(getState().getTabIndex());
  150. }
  151. public void updateCaption(ComponentConnector component) {
  152. // NOP: layouts caption, errors etc not rendered in Panel
  153. }
  154. @Override
  155. public VPanel getWidget() {
  156. return (VPanel) super.getWidget();
  157. }
  158. @Override
  159. protected Widget createWidget() {
  160. return GWT.create(VPanel.class);
  161. }
  162. public void layout() {
  163. updateSizes();
  164. }
  165. void updateSizes() {
  166. VPanel panel = getWidget();
  167. LayoutManager layoutManager = getLayoutManager();
  168. int top = layoutManager.getInnerHeight(panel.captionNode);
  169. int bottom = layoutManager.getInnerHeight(panel.bottomDecoration);
  170. Style style = panel.getElement().getStyle();
  171. panel.captionNode.getStyle().setMarginTop(-top, Unit.PX);
  172. panel.bottomDecoration.getStyle().setMarginBottom(-bottom, Unit.PX);
  173. style.setPaddingTop(top, Unit.PX);
  174. style.setPaddingBottom(bottom, Unit.PX);
  175. // Update scroll positions
  176. panel.contentNode.setScrollTop(panel.scrollTop);
  177. panel.contentNode.setScrollLeft(panel.scrollLeft);
  178. // Read actual value back to ensure update logic is correct
  179. panel.scrollTop = panel.contentNode.getScrollTop();
  180. panel.scrollLeft = panel.contentNode.getScrollLeft();
  181. Util.runWebkitOverflowAutoFix(panel.contentNode);
  182. }
  183. public void postLayout() {
  184. VPanel panel = getWidget();
  185. if (uidlScrollTop != null) {
  186. panel.contentNode.setScrollTop(uidlScrollTop.intValue());
  187. // Read actual value back to ensure update logic is correct
  188. // TODO Does this trigger reflows?
  189. panel.scrollTop = panel.contentNode.getScrollTop();
  190. uidlScrollTop = null;
  191. }
  192. if (uidlScrollLeft != null) {
  193. panel.contentNode.setScrollLeft(uidlScrollLeft.intValue());
  194. // Read actual value back to ensure update logic is correct
  195. // TODO Does this trigger reflows?
  196. panel.scrollLeft = panel.contentNode.getScrollLeft();
  197. uidlScrollLeft = null;
  198. }
  199. }
  200. @Override
  201. public PanelState getState() {
  202. return (PanelState) super.getState();
  203. }
  204. @Override
  205. public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
  206. super.onConnectorHierarchyChange(event);
  207. // We always have 1 child, unless the child is hidden
  208. Widget newChildWidget = null;
  209. if (getChildren().size() == 1) {
  210. ComponentConnector newChild = getChildren().get(0);
  211. newChildWidget = newChild.getWidget();
  212. }
  213. getWidget().setWidget(newChildWidget);
  214. }
  215. }