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.

CellBasedLayout.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui.layout;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import com.google.gwt.dom.client.DivElement;
  9. import com.google.gwt.dom.client.Document;
  10. import com.google.gwt.dom.client.Node;
  11. import com.google.gwt.dom.client.NodeList;
  12. import com.google.gwt.dom.client.Style;
  13. import com.google.gwt.user.client.ui.ComplexPanel;
  14. import com.google.gwt.user.client.ui.Widget;
  15. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  16. import com.vaadin.terminal.gwt.client.BrowserInfo;
  17. import com.vaadin.terminal.gwt.client.Container;
  18. import com.vaadin.terminal.gwt.client.Paintable;
  19. import com.vaadin.terminal.gwt.client.UIDL;
  20. import com.vaadin.terminal.gwt.client.ui.VMarginInfo;
  21. public abstract class CellBasedLayout extends ComplexPanel implements Container {
  22. protected Map<Widget, ChildComponentContainer> widgetToComponentContainer = new HashMap<Widget, ChildComponentContainer>();
  23. protected ApplicationConnection client = null;
  24. protected DivElement root;
  25. public static final int ORIENTATION_VERTICAL = 0;
  26. public static final int ORIENTATION_HORIZONTAL = 1;
  27. protected Margins activeMargins = new Margins(0, 0, 0, 0);
  28. protected VMarginInfo activeMarginsInfo = new VMarginInfo(-1);
  29. protected boolean spacingEnabled = false;
  30. protected final Spacing spacingFromCSS = new Spacing(12, 12);
  31. protected final Spacing activeSpacing = new Spacing(0, 0);
  32. private boolean dynamicWidth;
  33. private boolean dynamicHeight;
  34. private final DivElement clearElement = Document.get().createDivElement();
  35. private String lastStyleName = "";
  36. private boolean marginsNeedsRecalculation = false;
  37. protected String STYLENAME_SPACING = "";
  38. protected String STYLENAME_MARGIN_TOP = "";
  39. protected String STYLENAME_MARGIN_RIGHT = "";
  40. protected String STYLENAME_MARGIN_BOTTOM = "";
  41. protected String STYLENAME_MARGIN_LEFT = "";
  42. public static class Spacing {
  43. public int hSpacing = 0;
  44. public int vSpacing = 0;
  45. public Spacing(int hSpacing, int vSpacing) {
  46. this.hSpacing = hSpacing;
  47. this.vSpacing = vSpacing;
  48. }
  49. @Override
  50. public String toString() {
  51. return "Spacing [hSpacing=" + hSpacing + ",vSpacing=" + vSpacing
  52. + "]";
  53. }
  54. }
  55. public CellBasedLayout() {
  56. super();
  57. setElement(Document.get().createDivElement());
  58. getElement().getStyle().setProperty("overflow", "hidden");
  59. if (BrowserInfo.get().isIE()) {
  60. getElement().getStyle().setProperty("position", "relative");
  61. getElement().getStyle().setProperty("zoom", "1");
  62. }
  63. root = Document.get().createDivElement();
  64. root.getStyle().setProperty("overflow", "hidden");
  65. if (BrowserInfo.get().isIE()) {
  66. root.getStyle().setProperty("position", "relative");
  67. }
  68. getElement().appendChild(root);
  69. Style style = clearElement.getStyle();
  70. style.setProperty("width", "0px");
  71. style.setProperty("height", "0px");
  72. style.setProperty("clear", "both");
  73. style.setProperty("overflow", "hidden");
  74. root.appendChild(clearElement);
  75. }
  76. public boolean hasChildComponent(Widget component) {
  77. return widgetToComponentContainer.containsKey(component);
  78. }
  79. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  80. this.client = client;
  81. // Only non-cached UIDL:s can introduce changes
  82. if (uidl.getBooleanAttribute("cached")) {
  83. return;
  84. }
  85. /**
  86. * Margin and spacind detection depends on classNames and must be set
  87. * before setting size. Here just update the details from UIDL and from
  88. * overridden setStyleName run actual margin detections.
  89. */
  90. updateMarginAndSpacingInfo(uidl);
  91. /*
  92. * This call should be made first. Ensure correct implementation, handle
  93. * size etc.
  94. */
  95. if (client.updateComponent(this, uidl, true)) {
  96. return;
  97. }
  98. handleDynamicDimensions(uidl);
  99. }
  100. @Override
  101. public void setStyleName(String styleName) {
  102. super.setStyleName(styleName);
  103. if (isAttached() && marginsNeedsRecalculation
  104. || !lastStyleName.equals(styleName)) {
  105. measureMarginsAndSpacing();
  106. lastStyleName = styleName;
  107. marginsNeedsRecalculation = false;
  108. }
  109. }
  110. private void handleDynamicDimensions(UIDL uidl) {
  111. String w = uidl.hasAttribute("width") ? uidl
  112. .getStringAttribute("width") : "";
  113. String h = uidl.hasAttribute("height") ? uidl
  114. .getStringAttribute("height") : "";
  115. if (w.equals("")) {
  116. dynamicWidth = true;
  117. } else {
  118. dynamicWidth = false;
  119. }
  120. if (h.equals("")) {
  121. dynamicHeight = true;
  122. } else {
  123. dynamicHeight = false;
  124. }
  125. }
  126. protected void addOrMoveChild(ChildComponentContainer childComponent,
  127. int position) {
  128. if (childComponent.getParent() == this) {
  129. if (getWidgetIndex(childComponent) != position) {
  130. // Detach from old position child.
  131. childComponent.removeFromParent();
  132. // Logical attach.
  133. getChildren().insert(childComponent, position);
  134. root.insertBefore(childComponent.getElement(), root
  135. .getChildNodes().getItem(position));
  136. adopt(childComponent);
  137. }
  138. } else {
  139. widgetToComponentContainer.put(childComponent.getWidget(),
  140. childComponent);
  141. // Logical attach.
  142. getChildren().insert(childComponent, position);
  143. // avoid inserts (they are slower than appends)
  144. boolean insert = true;
  145. if (widgetToComponentContainer.size() == position) {
  146. insert = false;
  147. }
  148. if (insert) {
  149. root.insertBefore(childComponent.getElement(), root
  150. .getChildNodes().getItem(position));
  151. } else {
  152. root.insertBefore(childComponent.getElement(), clearElement);
  153. }
  154. // Adopt.
  155. adopt(childComponent);
  156. }
  157. }
  158. protected ChildComponentContainer getComponentContainer(Widget child) {
  159. return widgetToComponentContainer.get(child);
  160. }
  161. protected boolean isDynamicWidth() {
  162. return dynamicWidth;
  163. }
  164. protected boolean isDynamicHeight() {
  165. return dynamicHeight;
  166. }
  167. private void updateMarginAndSpacingInfo(UIDL uidl) {
  168. if (!uidl.hasAttribute("invisible")) {
  169. int bitMask = uidl.getIntAttribute("margins");
  170. if (activeMarginsInfo.getBitMask() != bitMask) {
  171. activeMarginsInfo = new VMarginInfo(bitMask);
  172. marginsNeedsRecalculation = true;
  173. }
  174. boolean spacing = uidl.getBooleanAttribute("spacing");
  175. if (spacing != spacingEnabled) {
  176. marginsNeedsRecalculation = true;
  177. spacingEnabled = spacing;
  178. }
  179. }
  180. }
  181. private static DivElement measurement;
  182. private static DivElement measurement2;
  183. private static DivElement measurement3;
  184. private static DivElement helper;
  185. static {
  186. helper = Document.get().createDivElement();
  187. helper.setInnerHTML("<div style=\"position:absolute;top:0;left:0;height:0;visibility:hidden;overflow:hidden;\">"
  188. + "<div style=\"width:0;height:0;visibility:hidden;overflow:hidden;\">"
  189. + "</div></div>"
  190. + "<div style=\"position:absolute;height:0;overflow:hidden;\"></div>");
  191. NodeList<Node> childNodes = helper.getChildNodes();
  192. measurement = (DivElement) childNodes.getItem(0);
  193. measurement2 = (DivElement) measurement.getFirstChildElement();
  194. measurement3 = (DivElement) childNodes.getItem(1);
  195. }
  196. protected boolean measureMarginsAndSpacing() {
  197. if (!isAttached()) {
  198. return false;
  199. }
  200. // Measure spacing (actually CSS padding)
  201. measurement3.setClassName(STYLENAME_SPACING
  202. + (spacingEnabled ? "-on" : "-off"));
  203. String sn = getStylePrimaryName() + "-margin";
  204. if (activeMarginsInfo.hasTop()) {
  205. sn += " " + STYLENAME_MARGIN_TOP;
  206. }
  207. if (activeMarginsInfo.hasBottom()) {
  208. sn += " " + STYLENAME_MARGIN_BOTTOM;
  209. }
  210. if (activeMarginsInfo.hasLeft()) {
  211. sn += " " + STYLENAME_MARGIN_LEFT;
  212. }
  213. if (activeMarginsInfo.hasRight()) {
  214. sn += " " + STYLENAME_MARGIN_RIGHT;
  215. }
  216. // Measure top and left margins (actually CSS padding)
  217. measurement.setClassName(sn);
  218. root.appendChild(helper);
  219. activeSpacing.vSpacing = measurement3.getOffsetHeight();
  220. activeSpacing.hSpacing = measurement3.getOffsetWidth();
  221. activeMargins.setMarginTop(measurement2.getOffsetTop());
  222. activeMargins.setMarginLeft(measurement2.getOffsetLeft());
  223. activeMargins.setMarginRight(measurement.getOffsetWidth()
  224. - activeMargins.getMarginLeft());
  225. activeMargins.setMarginBottom(measurement.getOffsetHeight()
  226. - activeMargins.getMarginTop());
  227. // ApplicationConnection.getConsole().log("Margins: " + activeMargins);
  228. // ApplicationConnection.getConsole().log("Spacing: " + activeSpacing);
  229. // Util.alert("Margins: " + activeMargins);
  230. root.removeChild(helper);
  231. // apply margin
  232. Style style = root.getStyle();
  233. style.setPropertyPx("marginLeft", activeMargins.getMarginLeft());
  234. style.setPropertyPx("marginRight", activeMargins.getMarginRight());
  235. style.setPropertyPx("marginTop", activeMargins.getMarginTop());
  236. style.setPropertyPx("marginBottom", activeMargins.getMarginBottom());
  237. return true;
  238. }
  239. protected ChildComponentContainer getFirstChildComponentContainer() {
  240. int size = getChildren().size();
  241. if (size < 1) {
  242. return null;
  243. }
  244. return (ChildComponentContainer) getChildren().get(0);
  245. }
  246. protected void removeChildrenAfter(int pos) {
  247. // Remove all children after position "pos" but leave the clear element
  248. // in place
  249. int toRemove = getChildren().size() - pos;
  250. while (toRemove-- > 0) {
  251. /* flag to not if widget has been moved and rendered elsewhere */
  252. boolean relocated = false;
  253. ChildComponentContainer child = (ChildComponentContainer) getChildren()
  254. .get(pos);
  255. Widget widget = child.getWidget();
  256. if (widget == null) {
  257. // a rare case where child component has been relocated and
  258. // rendered elsewhere
  259. // clean widgetToComponentContainer map by iterating the correct
  260. // mapping
  261. Iterator<Widget> iterator = widgetToComponentContainer.keySet()
  262. .iterator();
  263. while (iterator.hasNext()) {
  264. Widget key = iterator.next();
  265. if (widgetToComponentContainer.get(key) == child) {
  266. widget = key;
  267. relocated = true;
  268. break;
  269. }
  270. }
  271. if (widget == null) {
  272. throw new NullPointerException();
  273. }
  274. }
  275. ChildComponentContainer remove = widgetToComponentContainer
  276. .remove(widget);
  277. remove(child);
  278. if (!relocated) {
  279. Paintable p = (Paintable) widget;
  280. client.unregisterPaintable(p);
  281. }
  282. }
  283. }
  284. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  285. ChildComponentContainer componentContainer = widgetToComponentContainer
  286. .remove(oldComponent);
  287. if (componentContainer == null) {
  288. return;
  289. }
  290. componentContainer.setWidget(newComponent);
  291. client.unregisterPaintable((Paintable) oldComponent);
  292. widgetToComponentContainer.put(newComponent, componentContainer);
  293. }
  294. }