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

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