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

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