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.

VCssLayout.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.Set;
  10. import com.google.gwt.dom.client.Style;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Element;
  13. import com.google.gwt.user.client.ui.FlowPanel;
  14. import com.google.gwt.user.client.ui.SimplePanel;
  15. import com.google.gwt.user.client.ui.Widget;
  16. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  17. import com.vaadin.terminal.gwt.client.BrowserInfo;
  18. import com.vaadin.terminal.gwt.client.Container;
  19. import com.vaadin.terminal.gwt.client.RenderSpace;
  20. import com.vaadin.terminal.gwt.client.StyleConstants;
  21. import com.vaadin.terminal.gwt.client.UIDL;
  22. import com.vaadin.terminal.gwt.client.Util;
  23. import com.vaadin.terminal.gwt.client.VCaption;
  24. import com.vaadin.terminal.gwt.client.VConsole;
  25. import com.vaadin.terminal.gwt.client.VPaintableMap;
  26. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  27. import com.vaadin.terminal.gwt.client.ValueMap;
  28. public class VCssLayout extends SimplePanel implements Container {
  29. public static final String TAGNAME = "csslayout";
  30. public static final String CLASSNAME = "v-" + TAGNAME;
  31. FlowPane panel = new FlowPane();
  32. Element margin = DOM.createDiv();
  33. private boolean hasHeight;
  34. private boolean hasWidth;
  35. boolean rendering;
  36. public VCssLayout() {
  37. super();
  38. getElement().appendChild(margin);
  39. setStyleName(CLASSNAME);
  40. margin.setClassName(CLASSNAME + "-margin");
  41. setWidget(panel);
  42. }
  43. @Override
  44. protected Element getContainerElement() {
  45. return margin;
  46. }
  47. @Override
  48. public void setWidth(String width) {
  49. super.setWidth(width);
  50. // panel.setWidth(width);
  51. hasWidth = width != null && !width.equals("");
  52. if (!rendering) {
  53. panel.updateRelativeSizes();
  54. }
  55. }
  56. @Override
  57. public void setHeight(String height) {
  58. super.setHeight(height);
  59. // panel.setHeight(height);
  60. hasHeight = height != null && !height.equals("");
  61. if (!rendering) {
  62. panel.updateRelativeSizes();
  63. }
  64. }
  65. public boolean hasChildComponent(Widget component) {
  66. return panel.hasChildComponent(component);
  67. }
  68. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  69. panel.replaceChildComponent(oldComponent, newComponent);
  70. }
  71. public class FlowPane extends FlowPanel {
  72. private final HashMap<Widget, VCaption> widgetToCaption = new HashMap<Widget, VCaption>();
  73. private ApplicationConnection client;
  74. private int lastIndex;
  75. public FlowPane() {
  76. super();
  77. setStyleName(CLASSNAME + "-container");
  78. }
  79. public void updateRelativeSizes() {
  80. for (Widget w : getChildren()) {
  81. if (w instanceof VPaintableWidget) {
  82. client.handleComponentRelativeSize(w);
  83. }
  84. }
  85. }
  86. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  87. // for later requests
  88. this.client = client;
  89. final Collection<Widget> oldWidgets = new HashSet<Widget>();
  90. for (final Iterator<Widget> iterator = iterator(); iterator
  91. .hasNext();) {
  92. oldWidgets.add(iterator.next());
  93. }
  94. ValueMap mapAttribute = null;
  95. if (uidl.hasAttribute("css")) {
  96. mapAttribute = uidl.getMapAttribute("css");
  97. }
  98. lastIndex = 0;
  99. for (final Iterator<Object> i = uidl.getChildIterator(); i
  100. .hasNext();) {
  101. final UIDL r = (UIDL) i.next();
  102. final VPaintableWidget child = client.getPaintable(r);
  103. final Widget widget = child.getWidgetForPaintable();
  104. if (widget.getParent() == this) {
  105. oldWidgets.remove(widget);
  106. VCaption vCaption = widgetToCaption.get(widget);
  107. if (vCaption != null) {
  108. addOrMove(vCaption, lastIndex++);
  109. oldWidgets.remove(vCaption);
  110. }
  111. }
  112. addOrMove(widget, lastIndex++);
  113. if (mapAttribute != null && mapAttribute.containsKey(r.getId())) {
  114. String css = null;
  115. try {
  116. Style style = widget.getElement().getStyle();
  117. css = mapAttribute.getString(r.getId());
  118. String[] cssRules = css.split(";");
  119. for (int j = 0; j < cssRules.length; j++) {
  120. String[] rule = cssRules[j].split(":");
  121. if (rule.length == 0) {
  122. continue;
  123. } else {
  124. style.setProperty(
  125. makeCamelCase(rule[0].trim()),
  126. rule[1].trim());
  127. }
  128. }
  129. } catch (Exception e) {
  130. VConsole.log("CssLayout encounterd invalid css string: "
  131. + css);
  132. }
  133. }
  134. if (!r.getBooleanAttribute("cached")) {
  135. child.updateFromUIDL(r, client);
  136. }
  137. }
  138. // loop oldWidgetWrappers that where not re-attached and unregister
  139. // them
  140. for (Widget w : oldWidgets) {
  141. remove(w);
  142. VPaintableMap paintableMap = VPaintableMap.get(client);
  143. if (paintableMap.isPaintable(w)) {
  144. final VPaintableWidget p = VPaintableMap.get(client)
  145. .getPaintable(w);
  146. client.unregisterPaintable(p);
  147. }
  148. VCaption vCaption = widgetToCaption.remove(w);
  149. if (vCaption != null) {
  150. remove(vCaption);
  151. }
  152. }
  153. }
  154. private void addOrMove(Widget child, int index) {
  155. if (child.getParent() == this) {
  156. int currentIndex = getWidgetIndex(child);
  157. if (index == currentIndex) {
  158. return;
  159. }
  160. }
  161. insert(child, index);
  162. }
  163. public boolean hasChildComponent(Widget component) {
  164. return component.getParent() == this;
  165. }
  166. public void replaceChildComponent(Widget oldComponent,
  167. Widget newComponent) {
  168. VCaption caption = widgetToCaption.get(oldComponent);
  169. if (caption != null) {
  170. remove(caption);
  171. widgetToCaption.remove(oldComponent);
  172. }
  173. int index = getWidgetIndex(oldComponent);
  174. if (index >= 0) {
  175. remove(oldComponent);
  176. insert(newComponent, index);
  177. }
  178. }
  179. public void updateCaption(VPaintableWidget paintable, UIDL uidl) {
  180. Widget widget = paintable.getWidgetForPaintable();
  181. VCaption caption = widgetToCaption.get(widget);
  182. if (VCaption.isNeeded(uidl, paintable.getState())) {
  183. if (caption == null) {
  184. caption = new VCaption(paintable, client);
  185. widgetToCaption.put(widget, caption);
  186. insert(caption, getWidgetIndex(widget));
  187. lastIndex++;
  188. } else if (!caption.isAttached()) {
  189. insert(caption, getWidgetIndex(widget));
  190. lastIndex++;
  191. }
  192. caption.updateCaption(uidl);
  193. } else if (caption != null) {
  194. remove(caption);
  195. widgetToCaption.remove(widget);
  196. }
  197. }
  198. VPaintableWidget getComponent(Element element) {
  199. return Util
  200. .getPaintableForElement(client, VCssLayout.this, element);
  201. }
  202. }
  203. private RenderSpace space;
  204. public RenderSpace getAllocatedSpace(Widget child) {
  205. if (space == null) {
  206. space = new RenderSpace(-1, -1) {
  207. @Override
  208. public int getWidth() {
  209. if (BrowserInfo.get().isIE()) {
  210. int width = getOffsetWidth();
  211. int margins = margin.getOffsetWidth()
  212. - panel.getOffsetWidth();
  213. return width - margins;
  214. } else {
  215. return panel.getOffsetWidth();
  216. }
  217. }
  218. @Override
  219. public int getHeight() {
  220. int height = getOffsetHeight();
  221. int margins = margin.getOffsetHeight()
  222. - panel.getOffsetHeight();
  223. return height - margins;
  224. }
  225. };
  226. }
  227. return space;
  228. }
  229. public boolean requestLayout(Set<Widget> children) {
  230. if (hasSize()) {
  231. return true;
  232. } else {
  233. // Size may have changed
  234. // TODO optimize this: cache size if not fixed, handle both width
  235. // and height separately
  236. return false;
  237. }
  238. }
  239. private boolean hasSize() {
  240. return hasWidth && hasHeight;
  241. }
  242. private static final String makeCamelCase(String cssProperty) {
  243. // TODO this might be cleaner to implement with regexp
  244. while (cssProperty.contains("-")) {
  245. int indexOf = cssProperty.indexOf("-");
  246. cssProperty = cssProperty.substring(0, indexOf)
  247. + String.valueOf(cssProperty.charAt(indexOf + 1))
  248. .toUpperCase() + cssProperty.substring(indexOf + 2);
  249. }
  250. if ("float".equals(cssProperty)) {
  251. if (BrowserInfo.get().isIE()) {
  252. return "styleFloat";
  253. } else {
  254. return "cssFloat";
  255. }
  256. }
  257. return cssProperty;
  258. }
  259. /**
  260. * Sets CSS classes for margin and spacing based on the given parameters.
  261. *
  262. * @param margins
  263. * A {@link VMarginInfo} object that provides info on
  264. * top/left/bottom/right margins
  265. * @param spacing
  266. * true to enable spacing, false otherwise
  267. */
  268. protected void setMarginAndSpacingStyles(VMarginInfo margins,
  269. boolean spacing) {
  270. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  271. + StyleConstants.MARGIN_TOP, margins.hasTop());
  272. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  273. + StyleConstants.MARGIN_RIGHT, margins.hasRight());
  274. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  275. + StyleConstants.MARGIN_BOTTOM, margins.hasBottom());
  276. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  277. + StyleConstants.MARGIN_LEFT, margins.hasLeft());
  278. setStyleName(margin, VCssLayout.CLASSNAME + "-" + "spacing", spacing);
  279. }
  280. }