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.

IGridLayout.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import com.google.gwt.user.client.Command;
  9. import com.google.gwt.user.client.DOM;
  10. import com.google.gwt.user.client.DeferredCommand;
  11. import com.google.gwt.user.client.Element;
  12. import com.google.gwt.user.client.ui.FlexTable;
  13. import com.google.gwt.user.client.ui.HasHorizontalAlignment;
  14. import com.google.gwt.user.client.ui.HasVerticalAlignment;
  15. import com.google.gwt.user.client.ui.SimplePanel;
  16. import com.google.gwt.user.client.ui.Widget;
  17. import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
  18. import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;
  19. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  20. import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
  21. import com.itmill.toolkit.terminal.gwt.client.ICaptionWrapper;
  22. import com.itmill.toolkit.terminal.gwt.client.Container;
  23. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  24. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  25. import com.itmill.toolkit.terminal.gwt.client.StyleConstants;
  26. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  27. import com.itmill.toolkit.terminal.gwt.client.Util;
  28. public class IGridLayout extends SimplePanel implements Paintable, Container,
  29. ContainerResizedListener {
  30. public static final String CLASSNAME = "i-gridlayout";
  31. private Grid grid = new Grid();
  32. private boolean needsLayout = false;
  33. private boolean needsFF2Hack = BrowserInfo.get().isFF2();
  34. private Element margin = DOM.createDiv();
  35. private Element meterElement;
  36. private String width;
  37. public IGridLayout() {
  38. super();
  39. DOM.appendChild(getElement(), margin);
  40. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  41. setStyleName(CLASSNAME);
  42. setWidget(grid);
  43. }
  44. protected Element getContainerElement() {
  45. return margin;
  46. }
  47. public void setWidth(String width) {
  48. this.width = width;
  49. if (width != null && !width.equals("")) {
  50. needsLayout = true;
  51. } else {
  52. needsLayout = false;
  53. grid.setWidth("");
  54. }
  55. }
  56. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  57. if (client.updateComponent(this, uidl, true)) {
  58. return;
  59. }
  60. final MarginInfo margins = new MarginInfo(uidl
  61. .getIntAttribute("margins"));
  62. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  63. margins.hasTop());
  64. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  65. margins.hasRight());
  66. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  67. margins.hasBottom());
  68. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  69. margins.hasLeft());
  70. setStyleName(margin, CLASSNAME + "-" + "spacing", uidl
  71. .hasAttribute("spacing"));
  72. iLayout();
  73. grid.updateFromUIDL(uidl, client);
  74. }
  75. public boolean hasChildComponent(Widget component) {
  76. return grid.hasChildComponent(component);
  77. }
  78. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  79. grid.replaceChildComponent(oldComponent, newComponent);
  80. }
  81. public void updateCaption(Paintable component, UIDL uidl) {
  82. grid.updateCaption(component, uidl);
  83. }
  84. public class Grid extends FlexTable implements Paintable, Container {
  85. /** Widget to captionwrapper map */
  86. private final HashMap widgetToCaptionWrapper = new HashMap();
  87. public Grid() {
  88. super();
  89. setStyleName(CLASSNAME + "-grid");
  90. }
  91. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  92. int row = 0, column = 0;
  93. final ArrayList oldWidgetWrappers = new ArrayList();
  94. for (final Iterator iterator = iterator(); iterator.hasNext();) {
  95. oldWidgetWrappers.add(iterator.next());
  96. }
  97. /* Clear() removes all widgets but leaves the tr and td tags */
  98. clear();
  99. boolean structuralChange = uidl
  100. .getBooleanAttribute("structuralChange");
  101. /*
  102. * If a row has been inserted or removed at the middle of the table
  103. * we need to remove all old tr and td tags.
  104. */
  105. if (structuralChange) {
  106. while (getRowCount() > 0) {
  107. removeRow(0);
  108. }
  109. }
  110. final int[] alignments = uidl.getIntArrayAttribute("alignments");
  111. int alignmentIndex = 0;
  112. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  113. final UIDL r = (UIDL) i.next();
  114. if ("gr".equals(r.getTag())) {
  115. column = 0;
  116. for (final Iterator j = r.getChildIterator(); j.hasNext();) {
  117. final UIDL c = (UIDL) j.next();
  118. if ("gc".equals(c.getTag())) {
  119. prepareCell(row, column);
  120. // Set cell width
  121. int w;
  122. if (c.hasAttribute("w")) {
  123. w = c.getIntAttribute("w");
  124. } else {
  125. w = 1;
  126. }
  127. FlexCellFormatter formatter = (FlexCellFormatter) getCellFormatter();
  128. // set col span
  129. formatter.setColSpan(row, column, w);
  130. String styleNames = CLASSNAME + "-cell";
  131. if (column == 0) {
  132. styleNames += " " + CLASSNAME + "-firstcol";
  133. }
  134. if (row == 0) {
  135. styleNames += " " + CLASSNAME + "-firstrow";
  136. }
  137. formatter.setStyleName(row, column, styleNames);
  138. // Set cell height
  139. int h;
  140. if (c.hasAttribute("h")) {
  141. h = c.getIntAttribute("h");
  142. } else {
  143. h = 1;
  144. }
  145. ((FlexCellFormatter) getCellFormatter())
  146. .setRowSpan(row, column, h);
  147. final UIDL u = c.getChildUIDL(0);
  148. if (u != null) {
  149. AlignmentInfo alignmentInfo = new AlignmentInfo(
  150. alignments[alignmentIndex++]);
  151. VerticalAlignmentConstant va;
  152. if (alignmentInfo.isBottom()) {
  153. va = HasVerticalAlignment.ALIGN_BOTTOM;
  154. } else if (alignmentInfo.isTop()) {
  155. va = HasVerticalAlignment.ALIGN_TOP;
  156. } else {
  157. va = HasVerticalAlignment.ALIGN_MIDDLE;
  158. }
  159. HorizontalAlignmentConstant ha;
  160. if (alignmentInfo.isLeft()) {
  161. ha = HasHorizontalAlignment.ALIGN_LEFT;
  162. } else if (alignmentInfo.isHorizontalCenter()) {
  163. ha = HasHorizontalAlignment.ALIGN_CENTER;
  164. } else {
  165. ha = HasHorizontalAlignment.ALIGN_RIGHT;
  166. }
  167. formatter.setAlignment(row, column, ha, va);
  168. final Paintable child = client.getPaintable(u);
  169. ICaptionWrapper wr;
  170. if (widgetToCaptionWrapper.containsKey(child)) {
  171. wr = (ICaptionWrapper) widgetToCaptionWrapper
  172. .get(child);
  173. oldWidgetWrappers.remove(wr);
  174. } else {
  175. wr = new ICaptionWrapper(child, client);
  176. widgetToCaptionWrapper.put(child, wr);
  177. }
  178. setWidget(row, column, wr);
  179. DOM.setStyleAttribute(wr.getElement(),
  180. "textAlign", alignmentInfo
  181. .getHorizontalAlignment());
  182. if (!u.getBooleanAttribute("cached")) {
  183. child.updateFromUIDL(u, client);
  184. }
  185. }
  186. column++;
  187. }
  188. }
  189. row++;
  190. }
  191. }
  192. // loop oldWidgetWrappers that where not re-attached and unregister
  193. // them
  194. for (final Iterator it = oldWidgetWrappers.iterator(); it.hasNext();) {
  195. final ICaptionWrapper w = (ICaptionWrapper) it.next();
  196. client.unregisterPaintable(w.getPaintable());
  197. widgetToCaptionWrapper.remove(w.getPaintable());
  198. }
  199. // fix rendering bug on FF2 (#1838)
  200. if (needsFF2Hack) {
  201. DeferredCommand.addCommand(new Command() {
  202. public void execute() {
  203. Element firstcell = getCellFormatter().getElement(0, 0);
  204. if (firstcell != null) {
  205. String styleAttribute = DOM.getStyleAttribute(
  206. firstcell, "verticalAlign");
  207. DOM.setStyleAttribute(firstcell, "verticalAlign",
  208. "");
  209. int elementPropertyInt = DOM.getElementPropertyInt(
  210. firstcell, "offsetWidth");
  211. DOM.setStyleAttribute(firstcell, "verticalAlign",
  212. styleAttribute);
  213. if (elementPropertyInt > 0) {
  214. needsFF2Hack = false;
  215. }
  216. }
  217. }
  218. });
  219. }
  220. }
  221. public boolean hasChildComponent(Widget component) {
  222. if (widgetToCaptionWrapper.containsKey(component)) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. public void replaceChildComponent(Widget oldComponent,
  228. Widget newComponent) {
  229. // TODO Auto-generated method stub
  230. }
  231. public void updateCaption(Paintable component, UIDL uidl) {
  232. final ICaptionWrapper wrapper = (ICaptionWrapper) widgetToCaptionWrapper
  233. .get(component);
  234. wrapper.updateCaption(uidl);
  235. }
  236. }
  237. public void iLayout() {
  238. if (needsLayout) {
  239. super.setWidth(width);
  240. if (meterElement == null) {
  241. meterElement = DOM.createDiv();
  242. DOM.setStyleAttribute(meterElement, "overflow", "hidden");
  243. DOM.setStyleAttribute(meterElement, "height", "0");
  244. DOM.appendChild(getContainerElement(), meterElement);
  245. }
  246. int contentWidth = DOM.getElementPropertyInt(meterElement,
  247. "offsetWidth");
  248. int offsetWidth = getOffsetWidth();
  249. grid.setWidth((offsetWidth - (offsetWidth - contentWidth)) + "px");
  250. } else {
  251. grid.setWidth("");
  252. }
  253. Util.runDescendentsLayout(this);
  254. }
  255. }