Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

VPanelPaintable.java 8.6KB

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