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

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