Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbsoluteLayoutConnector.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui.absolutelayout;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import com.google.gwt.dom.client.Style;
  8. import com.google.gwt.dom.client.Style.Unit;
  9. import com.google.gwt.user.client.Element;
  10. import com.google.gwt.user.client.ui.Widget;
  11. import com.vaadin.shared.ui.Connect;
  12. import com.vaadin.shared.ui.LayoutClickRpc;
  13. import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutServerRpc;
  14. import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutState;
  15. import com.vaadin.terminal.gwt.client.ComponentConnector;
  16. import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent;
  17. import com.vaadin.terminal.gwt.client.DirectionalManagedLayout;
  18. import com.vaadin.terminal.gwt.client.Util;
  19. import com.vaadin.terminal.gwt.client.VCaption;
  20. import com.vaadin.terminal.gwt.client.communication.RpcProxy;
  21. import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
  22. import com.vaadin.terminal.gwt.client.ui.AbstractComponentContainerConnector;
  23. import com.vaadin.terminal.gwt.client.ui.LayoutClickEventHandler;
  24. import com.vaadin.terminal.gwt.client.ui.absolutelayout.VAbsoluteLayout.AbsoluteWrapper;
  25. import com.vaadin.ui.AbsoluteLayout;
  26. @Connect(AbsoluteLayout.class)
  27. public class AbsoluteLayoutConnector extends
  28. AbstractComponentContainerConnector implements DirectionalManagedLayout {
  29. private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
  30. this) {
  31. @Override
  32. protected ComponentConnector getChildComponent(Element element) {
  33. return getConnectorForElement(element);
  34. }
  35. @Override
  36. protected LayoutClickRpc getLayoutClickRPC() {
  37. return rpc;
  38. };
  39. };
  40. private AbsoluteLayoutServerRpc rpc;
  41. private Map<String, AbsoluteWrapper> connectorIdToComponentWrapper = new HashMap<String, AbsoluteWrapper>();
  42. @Override
  43. protected void init() {
  44. super.init();
  45. rpc = RpcProxy.create(AbsoluteLayoutServerRpc.class, this);
  46. }
  47. /**
  48. * Returns the deepest nested child component which contains "element". The
  49. * child component is also returned if "element" is part of its caption.
  50. *
  51. * @param element
  52. * An element that is a nested sub element of the root element in
  53. * this layout
  54. * @return The Paintable which the element is a part of. Null if the element
  55. * belongs to the layout and not to a child.
  56. */
  57. protected ComponentConnector getConnectorForElement(Element element) {
  58. return Util.getConnectorForElement(getConnection(), getWidget(),
  59. element);
  60. }
  61. @Override
  62. public void updateCaption(ComponentConnector component) {
  63. VAbsoluteLayout absoluteLayoutWidget = getWidget();
  64. AbsoluteWrapper componentWrapper = getWrapper(component);
  65. boolean captionIsNeeded = VCaption.isNeeded(component.getState());
  66. VCaption caption = componentWrapper.getCaption();
  67. if (captionIsNeeded) {
  68. if (caption == null) {
  69. caption = new VCaption(component, getConnection());
  70. absoluteLayoutWidget.add(caption);
  71. componentWrapper.setCaption(caption);
  72. }
  73. caption.updateCaption();
  74. componentWrapper.updateCaptionPosition();
  75. } else {
  76. if (caption != null) {
  77. caption.removeFromParent();
  78. }
  79. }
  80. }
  81. @Override
  82. public VAbsoluteLayout getWidget() {
  83. return (VAbsoluteLayout) super.getWidget();
  84. }
  85. @Override
  86. public AbsoluteLayoutState getState() {
  87. return (AbsoluteLayoutState) super.getState();
  88. }
  89. @Override
  90. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  91. super.onStateChanged(stateChangeEvent);
  92. clickEventHandler.handleEventHandlerRegistration();
  93. // TODO Margin handling
  94. for (ComponentConnector child : getChildComponents()) {
  95. getWrapper(child).setPosition(
  96. getState().getConnectorPosition(child));
  97. }
  98. };
  99. private AbsoluteWrapper getWrapper(ComponentConnector child) {
  100. String childId = child.getConnectorId();
  101. AbsoluteWrapper wrapper = connectorIdToComponentWrapper.get(childId);
  102. if (wrapper != null) {
  103. return wrapper;
  104. }
  105. wrapper = new AbsoluteWrapper(child.getWidget());
  106. connectorIdToComponentWrapper.put(childId, wrapper);
  107. getWidget().add(wrapper);
  108. return wrapper;
  109. }
  110. @Override
  111. public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
  112. super.onConnectorHierarchyChange(event);
  113. for (ComponentConnector child : getChildComponents()) {
  114. getWrapper(child);
  115. }
  116. for (ComponentConnector oldChild : event.getOldChildren()) {
  117. if (oldChild.getParent() != this) {
  118. String connectorId = oldChild.getConnectorId();
  119. AbsoluteWrapper absoluteWrapper = connectorIdToComponentWrapper
  120. .remove(connectorId);
  121. absoluteWrapper.destroy();
  122. }
  123. }
  124. }
  125. @Override
  126. public void layoutVertically() {
  127. VAbsoluteLayout layout = getWidget();
  128. for (ComponentConnector paintable : getChildComponents()) {
  129. Widget widget = paintable.getWidget();
  130. AbsoluteWrapper wrapper = (AbsoluteWrapper) widget.getParent();
  131. Style wrapperStyle = wrapper.getElement().getStyle();
  132. if (paintable.isRelativeHeight()) {
  133. int h;
  134. if (wrapper.top != null && wrapper.bottom != null) {
  135. h = wrapper.getOffsetHeight();
  136. } else if (wrapper.bottom != null) {
  137. // top not defined, available space 0... bottom of
  138. // wrapper
  139. h = wrapper.getElement().getOffsetTop()
  140. + wrapper.getOffsetHeight();
  141. } else {
  142. // top defined or both undefined, available space ==
  143. // canvas - top
  144. h = layout.canvas.getOffsetHeight()
  145. - wrapper.getElement().getOffsetTop();
  146. }
  147. wrapperStyle.setHeight(h, Unit.PX);
  148. getLayoutManager().reportHeightAssignedToRelative(paintable, h);
  149. } else {
  150. wrapperStyle.clearHeight();
  151. }
  152. wrapper.updateCaptionPosition();
  153. }
  154. }
  155. @Override
  156. public void layoutHorizontally() {
  157. VAbsoluteLayout layout = getWidget();
  158. for (ComponentConnector paintable : getChildComponents()) {
  159. AbsoluteWrapper wrapper = getWrapper(paintable);
  160. Style wrapperStyle = wrapper.getElement().getStyle();
  161. if (paintable.isRelativeWidth()) {
  162. int w;
  163. if (wrapper.left != null && wrapper.right != null) {
  164. w = wrapper.getOffsetWidth();
  165. } else if (wrapper.right != null) {
  166. // left == null
  167. // available width == right edge == offsetleft + width
  168. w = wrapper.getOffsetWidth()
  169. + wrapper.getElement().getOffsetLeft();
  170. } else {
  171. // left != null && right == null || left == null &&
  172. // right == null
  173. // available width == canvas width - offset left
  174. w = layout.canvas.getOffsetWidth()
  175. - wrapper.getElement().getOffsetLeft();
  176. }
  177. wrapperStyle.setWidth(w, Unit.PX);
  178. getLayoutManager().reportWidthAssignedToRelative(paintable, w);
  179. } else {
  180. wrapperStyle.clearWidth();
  181. }
  182. wrapper.updateCaptionPosition();
  183. }
  184. }
  185. }