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.

AbstractOrderedLayoutConnector.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.List;
  6. import com.google.gwt.dom.client.Style;
  7. import com.google.gwt.user.client.Element;
  8. import com.google.gwt.user.client.ui.Widget;
  9. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  10. import com.vaadin.terminal.gwt.client.ComponentConnector;
  11. import com.vaadin.terminal.gwt.client.DirectionalManagedLayout;
  12. import com.vaadin.terminal.gwt.client.LayoutManager;
  13. import com.vaadin.terminal.gwt.client.Paintable;
  14. import com.vaadin.terminal.gwt.client.UIDL;
  15. import com.vaadin.terminal.gwt.client.Util;
  16. import com.vaadin.terminal.gwt.client.VCaption;
  17. import com.vaadin.terminal.gwt.client.ValueMap;
  18. import com.vaadin.terminal.gwt.client.communication.RpcProxy;
  19. import com.vaadin.terminal.gwt.client.communication.ServerRpc;
  20. import com.vaadin.terminal.gwt.client.ui.layout.ComponentConnectorLayoutSlot;
  21. import com.vaadin.terminal.gwt.client.ui.layout.VLayoutSlot;
  22. public abstract class AbstractOrderedLayoutConnector extends
  23. AbstractComponentContainerConnector implements Paintable,
  24. DirectionalManagedLayout {
  25. public interface AbstractOrderedLayoutServerRPC extends LayoutClickRPC,
  26. ServerRpc {
  27. }
  28. AbstractOrderedLayoutServerRPC rpc;
  29. private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
  30. this) {
  31. @Override
  32. protected ComponentConnector getChildComponent(Element element) {
  33. return Util.getConnectorForElement(getConnection(), getWidget(),
  34. element);
  35. }
  36. @Override
  37. protected LayoutClickRPC getLayoutClickRPC() {
  38. return rpc;
  39. };
  40. };
  41. @Override
  42. public void init() {
  43. rpc = RpcProxy.create(AbstractOrderedLayoutServerRPC.class, this);
  44. getLayoutManager().registerDependency(this,
  45. getWidget().spacingMeasureElement);
  46. }
  47. public void updateCaption(ComponentConnector component) {
  48. VMeasuringOrderedLayout layout = getWidget();
  49. if (VCaption.isNeeded(component.getState())) {
  50. VLayoutSlot layoutSlot = layout.getSlotForChild(component
  51. .getWidget());
  52. VCaption caption = layoutSlot.getCaption();
  53. if (caption == null) {
  54. caption = new VCaption(component, getConnection());
  55. Widget widget = component.getWidget();
  56. layout.setCaption(widget, caption);
  57. }
  58. caption.updateCaption();
  59. } else {
  60. layout.setCaption(component.getWidget(), null);
  61. getLayoutManager().setNeedsUpdate(this);
  62. }
  63. }
  64. @Override
  65. public VMeasuringOrderedLayout getWidget() {
  66. return (VMeasuringOrderedLayout) super.getWidget();
  67. }
  68. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  69. if (!isRealUpdate(uidl)) {
  70. return;
  71. }
  72. clickEventHandler.handleEventHandlerRegistration();
  73. VMeasuringOrderedLayout layout = getWidget();
  74. ValueMap expandRatios = uidl.getMapAttribute("expandRatios");
  75. ValueMap alignments = uidl.getMapAttribute("alignments");
  76. for (ComponentConnector child : getChildren()) {
  77. VLayoutSlot slot = layout.getSlotForChild(child.getWidget());
  78. String pid = child.getConnectorId();
  79. AlignmentInfo alignment;
  80. if (alignments.containsKey(pid)) {
  81. alignment = new AlignmentInfo(alignments.getInt(pid));
  82. } else {
  83. alignment = AlignmentInfo.TOP_LEFT;
  84. }
  85. slot.setAlignment(alignment);
  86. double expandRatio;
  87. if (expandRatios.containsKey(pid)) {
  88. expandRatio = expandRatios.getRawNumber(pid);
  89. } else {
  90. expandRatio = 0;
  91. }
  92. slot.setExpandRatio(expandRatio);
  93. }
  94. int bitMask = uidl.getIntAttribute("margins");
  95. layout.updateMarginStyleNames(new VMarginInfo(bitMask));
  96. layout.updateSpacingStyleName(uidl.getBooleanAttribute("spacing"));
  97. getLayoutManager().setNeedsUpdate(this);
  98. }
  99. private int getSizeForInnerSize(int size, boolean isVertical) {
  100. LayoutManager layoutManager = getLayoutManager();
  101. Element element = getWidget().getElement();
  102. if (isVertical) {
  103. return size + layoutManager.getBorderHeight(element)
  104. + layoutManager.getPaddingHeight(element);
  105. } else {
  106. return size + layoutManager.getBorderWidth(element)
  107. + layoutManager.getPaddingWidth(element);
  108. }
  109. }
  110. private static String getSizeProperty(boolean isVertical) {
  111. return isVertical ? "height" : "width";
  112. }
  113. private boolean isUndefinedInDirection(boolean isVertical) {
  114. if (isVertical) {
  115. return isUndefinedHeight();
  116. } else {
  117. return isUndefinedWidth();
  118. }
  119. }
  120. private int getInnerSizeInDirection(boolean isVertical) {
  121. if (isVertical) {
  122. return getLayoutManager().getInnerHeight(getWidget().getElement());
  123. } else {
  124. return getLayoutManager().getInnerWidth(getWidget().getElement());
  125. }
  126. }
  127. private void layoutPrimaryDirection() {
  128. VMeasuringOrderedLayout layout = getWidget();
  129. boolean isVertical = layout.isVertical;
  130. boolean isUndefined = isUndefinedInDirection(isVertical);
  131. int startPadding = getStartPadding(isVertical);
  132. int spacingSize = getSpacingInDirection(isVertical);
  133. int allocatedSize;
  134. if (isUndefined) {
  135. allocatedSize = -1;
  136. } else {
  137. allocatedSize = getInnerSizeInDirection(isVertical);
  138. }
  139. allocatedSize = layout.layoutPrimaryDirection(spacingSize,
  140. allocatedSize, startPadding);
  141. Style ownStyle = getWidget().getElement().getStyle();
  142. if (isUndefined) {
  143. ownStyle.setPropertyPx(getSizeProperty(isVertical),
  144. getSizeForInnerSize(allocatedSize, isVertical));
  145. } else {
  146. ownStyle.setProperty(getSizeProperty(isVertical),
  147. getDefinedSize(isVertical));
  148. }
  149. }
  150. private int getSpacingInDirection(boolean isVertical) {
  151. if (isVertical) {
  152. return getLayoutManager().getOuterHeight(
  153. getWidget().spacingMeasureElement);
  154. } else {
  155. return getLayoutManager().getOuterWidth(
  156. getWidget().spacingMeasureElement);
  157. }
  158. }
  159. private void layoutSecondaryDirection() {
  160. VMeasuringOrderedLayout layout = getWidget();
  161. boolean isVertical = layout.isVertical;
  162. boolean isUndefined = isUndefinedInDirection(!isVertical);
  163. int startPadding = getStartPadding(!isVertical);
  164. int allocatedSize;
  165. if (isUndefined) {
  166. allocatedSize = -1;
  167. } else {
  168. allocatedSize = getInnerSizeInDirection(!isVertical);
  169. }
  170. allocatedSize = layout.layoutSecondaryDirection(allocatedSize,
  171. startPadding);
  172. Style ownStyle = getWidget().getElement().getStyle();
  173. if (isUndefined) {
  174. ownStyle.setPropertyPx(getSizeProperty(!getWidget().isVertical),
  175. getSizeForInnerSize(allocatedSize, !getWidget().isVertical));
  176. } else {
  177. ownStyle.setProperty(getSizeProperty(!getWidget().isVertical),
  178. getDefinedSize(!getWidget().isVertical));
  179. }
  180. }
  181. private String getDefinedSize(boolean isVertical) {
  182. if (isVertical) {
  183. return getState().getHeight();
  184. } else {
  185. return getState().getWidth();
  186. }
  187. }
  188. private int getStartPadding(boolean isVertical) {
  189. if (isVertical) {
  190. return getLayoutManager().getPaddingTop(getWidget().getElement());
  191. } else {
  192. return getLayoutManager().getPaddingLeft(getWidget().getElement());
  193. }
  194. }
  195. public void layoutHorizontally() {
  196. if (getWidget().isVertical) {
  197. layoutSecondaryDirection();
  198. } else {
  199. layoutPrimaryDirection();
  200. }
  201. }
  202. public void layoutVertically() {
  203. if (getWidget().isVertical) {
  204. layoutPrimaryDirection();
  205. } else {
  206. layoutSecondaryDirection();
  207. }
  208. }
  209. @Override
  210. public void onConnectorHierarchyChange(
  211. com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent event) {
  212. super.onConnectorHierarchyChange(event);
  213. List<ComponentConnector> previousChildren = event.getOldChildren();
  214. int currentIndex = 0;
  215. VMeasuringOrderedLayout layout = getWidget();
  216. for (ComponentConnector child : getChildren()) {
  217. Widget childWidget = child.getWidget();
  218. VLayoutSlot slot = layout.getSlotForChild(childWidget);
  219. if (childWidget.getParent() != layout) {
  220. // If the child widget was previously attached to another
  221. // AbstractOrderedLayout a slot might be found that belongs to
  222. // another AbstractOrderedLayout. In this case we discard it and
  223. // create a new slot.
  224. slot = new ComponentConnectorLayoutSlot(getWidget()
  225. .getStylePrimaryName(), child, this);
  226. }
  227. layout.addOrMove(slot, currentIndex++);
  228. }
  229. for (ComponentConnector child : previousChildren) {
  230. if (child.getParent() != this) {
  231. // Remove slot if the connector is no longer a child of this
  232. // layout
  233. layout.removeSlotForWidget(child.getWidget());
  234. }
  235. }
  236. };
  237. }