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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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
  188. .setInnerHTML("<div style=\"position:absolute;top:0;left:0;height:0;visibility:hidden;overflow:hidden;\">"
  189. + "<div style=\"width:0;height:0;visibility:hidden;overflow:hidden;\">"
  190. + "</div></div>"
  191. + "<div style=\"position:absolute;height:0;overflow:hidden;\"></div>");
  192. NodeList<Node> childNodes = helper.getChildNodes();
  193. measurement = (DivElement) childNodes.getItem(0);
  194. measurement2 = (DivElement) measurement.getFirstChildElement();
  195. measurement3 = (DivElement) childNodes.getItem(1);
  196. }
  197. protected boolean measureMarginsAndSpacing() {
  198. if (!isAttached()) {
  199. return false;
  200. }
  201. // Measure spacing (actually CSS padding)
  202. measurement3.setClassName(STYLENAME_SPACING
  203. + (spacingEnabled ? "-on" : "-off"));
  204. String sn = getStylePrimaryName() + "-margin";
  205. if (activeMarginsInfo.hasTop()) {
  206. sn += " " + STYLENAME_MARGIN_TOP;
  207. }
  208. if (activeMarginsInfo.hasBottom()) {
  209. sn += " " + STYLENAME_MARGIN_BOTTOM;
  210. }
  211. if (activeMarginsInfo.hasLeft()) {
  212. sn += " " + STYLENAME_MARGIN_LEFT;
  213. }
  214. if (activeMarginsInfo.hasRight()) {
  215. sn += " " + STYLENAME_MARGIN_RIGHT;
  216. }
  217. // Measure top and left margins (actually CSS padding)
  218. measurement.setClassName(sn);
  219. root.appendChild(helper);
  220. activeSpacing.vSpacing = measurement3.getOffsetHeight();
  221. activeSpacing.hSpacing = measurement3.getOffsetWidth();
  222. activeMargins.setMarginTop(measurement2.getOffsetTop());
  223. activeMargins.setMarginLeft(measurement2.getOffsetLeft());
  224. activeMargins.setMarginRight(measurement.getOffsetWidth()
  225. - activeMargins.getMarginLeft());
  226. activeMargins.setMarginBottom(measurement.getOffsetHeight()
  227. - activeMargins.getMarginTop());
  228. // ApplicationConnection.getConsole().log("Margins: " + activeMargins);
  229. // ApplicationConnection.getConsole().log("Spacing: " + activeSpacing);
  230. // Util.alert("Margins: " + activeMargins);
  231. root.removeChild(helper);
  232. // apply margin
  233. Style style = root.getStyle();
  234. style.setPropertyPx("marginLeft", activeMargins.getMarginLeft());
  235. style.setPropertyPx("marginRight", activeMargins.getMarginRight());
  236. style.setPropertyPx("marginTop", activeMargins.getMarginTop());
  237. style.setPropertyPx("marginBottom", activeMargins.getMarginBottom());
  238. return true;
  239. }
  240. protected ChildComponentContainer getFirstChildComponentContainer() {
  241. int size = getChildren().size();
  242. if (size < 1) {
  243. return null;
  244. }
  245. return (ChildComponentContainer) getChildren().get(0);
  246. }
  247. protected void removeChildrenAfter(int pos) {
  248. // Remove all children after position "pos" but leave the clear element
  249. // in place
  250. int toRemove = getChildren().size() - pos;
  251. while (toRemove-- > 0) {
  252. /* flag to not if widget has been moved and rendered elsewhere */
  253. boolean relocated = false;
  254. ChildComponentContainer child = (ChildComponentContainer) getChildren()
  255. .get(pos);
  256. Widget widget = child.getWidget();
  257. if (widget == null) {
  258. // a rare case where child component has been relocated and
  259. // rendered elsewhere
  260. // clean widgetToComponentContainer map by iterating the correct
  261. // mapping
  262. Iterator<Widget> iterator = widgetToComponentContainer.keySet()
  263. .iterator();
  264. while (iterator.hasNext()) {
  265. Widget key = iterator.next();
  266. if (widgetToComponentContainer.get(key) == child) {
  267. widget = key;
  268. relocated = true;
  269. break;
  270. }
  271. }
  272. if (widget == null) {
  273. throw new NullPointerException();
  274. }
  275. }
  276. ChildComponentContainer remove = widgetToComponentContainer
  277. .remove(widget);
  278. remove(child);
  279. if (!relocated) {
  280. Paintable p = (Paintable) widget;
  281. client.unregisterPaintable(p);
  282. }
  283. }
  284. }
  285. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  286. ChildComponentContainer componentContainer = widgetToComponentContainer
  287. .remove(oldComponent);
  288. if (componentContainer == null) {
  289. return;
  290. }
  291. componentContainer.setWidget(newComponent);
  292. client.unregisterPaintable((Paintable) oldComponent);
  293. widgetToComponentContainer.put(newComponent, componentContainer);
  294. }
  295. }