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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 com.google.gwt.dom.client.Style;
  10. import com.google.gwt.user.client.DOM;
  11. import com.google.gwt.user.client.Element;
  12. import com.google.gwt.user.client.ui.FlowPanel;
  13. import com.google.gwt.user.client.ui.SimplePanel;
  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.StyleConstants;
  18. import com.vaadin.terminal.gwt.client.UIDL;
  19. import com.vaadin.terminal.gwt.client.Util;
  20. import com.vaadin.terminal.gwt.client.VCaption;
  21. import com.vaadin.terminal.gwt.client.VConsole;
  22. import com.vaadin.terminal.gwt.client.ConnectorMap;
  23. import com.vaadin.terminal.gwt.client.ComponentConnector;
  24. import com.vaadin.terminal.gwt.client.ValueMap;
  25. public class VCssLayout extends SimplePanel {
  26. public static final String TAGNAME = "csslayout";
  27. public static final String CLASSNAME = "v-" + TAGNAME;
  28. FlowPane panel = new FlowPane();
  29. Element margin = DOM.createDiv();
  30. public VCssLayout() {
  31. super();
  32. getElement().appendChild(margin);
  33. setStyleName(CLASSNAME);
  34. margin.setClassName(CLASSNAME + "-margin");
  35. setWidget(panel);
  36. }
  37. @Override
  38. protected Element getContainerElement() {
  39. return margin;
  40. }
  41. public class FlowPane extends FlowPanel {
  42. private final HashMap<Widget, VCaption> widgetToCaption = new HashMap<Widget, VCaption>();
  43. private ApplicationConnection client;
  44. private int lastIndex;
  45. public FlowPane() {
  46. super();
  47. setStyleName(CLASSNAME + "-container");
  48. }
  49. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  50. // for later requests
  51. this.client = client;
  52. final Collection<Widget> oldWidgets = new HashSet<Widget>();
  53. for (final Iterator<Widget> iterator = iterator(); iterator
  54. .hasNext();) {
  55. oldWidgets.add(iterator.next());
  56. }
  57. ValueMap mapAttribute = null;
  58. if (uidl.hasAttribute("css")) {
  59. mapAttribute = uidl.getMapAttribute("css");
  60. }
  61. lastIndex = 0;
  62. for (final Iterator<Object> i = uidl.getChildIterator(); i
  63. .hasNext();) {
  64. final UIDL r = (UIDL) i.next();
  65. final ComponentConnector child = client.getPaintable(r);
  66. final Widget widget = child.getWidget();
  67. if (widget.getParent() == this) {
  68. oldWidgets.remove(widget);
  69. VCaption vCaption = widgetToCaption.get(widget);
  70. if (vCaption != null) {
  71. addOrMove(vCaption, lastIndex++);
  72. oldWidgets.remove(vCaption);
  73. }
  74. }
  75. addOrMove(widget, lastIndex++);
  76. if (mapAttribute != null && mapAttribute.containsKey(r.getId())) {
  77. String css = null;
  78. try {
  79. Style style = widget.getElement().getStyle();
  80. css = mapAttribute.getString(r.getId());
  81. String[] cssRules = css.split(";");
  82. for (int j = 0; j < cssRules.length; j++) {
  83. String[] rule = cssRules[j].split(":");
  84. if (rule.length == 0) {
  85. continue;
  86. } else {
  87. style.setProperty(
  88. makeCamelCase(rule[0].trim()),
  89. rule[1].trim());
  90. }
  91. }
  92. } catch (Exception e) {
  93. VConsole.log("CssLayout encounterd invalid css string: "
  94. + css);
  95. }
  96. }
  97. if (!r.getBooleanAttribute("cached")) {
  98. child.updateFromUIDL(r, client);
  99. }
  100. }
  101. // loop oldWidgetWrappers that where not re-attached and unregister
  102. // them
  103. for (Widget w : oldWidgets) {
  104. remove(w);
  105. ConnectorMap paintableMap = ConnectorMap.get(client);
  106. if (paintableMap.isConnector(w)) {
  107. final ComponentConnector p = ConnectorMap.get(client)
  108. .getConnector(w);
  109. client.unregisterPaintable(p);
  110. }
  111. VCaption vCaption = widgetToCaption.remove(w);
  112. if (vCaption != null) {
  113. remove(vCaption);
  114. }
  115. }
  116. }
  117. private void addOrMove(Widget child, int index) {
  118. if (child.getParent() == this) {
  119. int currentIndex = getWidgetIndex(child);
  120. if (index == currentIndex) {
  121. return;
  122. }
  123. }
  124. insert(child, index);
  125. }
  126. public void updateCaption(ComponentConnector paintable, UIDL uidl) {
  127. Widget widget = paintable.getWidget();
  128. VCaption caption = widgetToCaption.get(widget);
  129. if (VCaption.isNeeded(uidl, paintable.getState())) {
  130. if (caption == null) {
  131. caption = new VCaption(paintable, client);
  132. widgetToCaption.put(widget, caption);
  133. insert(caption, getWidgetIndex(widget));
  134. lastIndex++;
  135. } else if (!caption.isAttached()) {
  136. insert(caption, getWidgetIndex(widget));
  137. lastIndex++;
  138. }
  139. caption.updateCaption(uidl);
  140. } else if (caption != null) {
  141. remove(caption);
  142. widgetToCaption.remove(widget);
  143. }
  144. }
  145. ComponentConnector getComponent(Element element) {
  146. return Util
  147. .getPaintableForElement(client, VCssLayout.this, element);
  148. }
  149. }
  150. private static final String makeCamelCase(String cssProperty) {
  151. // TODO this might be cleaner to implement with regexp
  152. while (cssProperty.contains("-")) {
  153. int indexOf = cssProperty.indexOf("-");
  154. cssProperty = cssProperty.substring(0, indexOf)
  155. + String.valueOf(cssProperty.charAt(indexOf + 1))
  156. .toUpperCase() + cssProperty.substring(indexOf + 2);
  157. }
  158. if ("float".equals(cssProperty)) {
  159. if (BrowserInfo.get().isIE()) {
  160. return "styleFloat";
  161. } else {
  162. return "cssFloat";
  163. }
  164. }
  165. return cssProperty;
  166. }
  167. /**
  168. * Sets CSS classes for margin and spacing based on the given parameters.
  169. *
  170. * @param margins
  171. * A {@link VMarginInfo} object that provides info on
  172. * top/left/bottom/right margins
  173. * @param spacing
  174. * true to enable spacing, false otherwise
  175. */
  176. protected void setMarginAndSpacingStyles(VMarginInfo margins,
  177. boolean spacing) {
  178. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  179. + StyleConstants.MARGIN_TOP, margins.hasTop());
  180. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  181. + StyleConstants.MARGIN_RIGHT, margins.hasRight());
  182. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  183. + StyleConstants.MARGIN_BOTTOM, margins.hasBottom());
  184. setStyleName(margin, VCssLayout.CLASSNAME + "-"
  185. + StyleConstants.MARGIN_LEFT, margins.hasLeft());
  186. setStyleName(margin, VCssLayout.CLASSNAME + "-" + "spacing", spacing);
  187. }
  188. }