選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ISizeableGridLayout.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package com.itmill.toolkit.terminal.gwt.client.ui.absolutegrid;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import com.google.gwt.user.client.Command;
  6. import com.google.gwt.user.client.DOM;
  7. import com.google.gwt.user.client.DeferredCommand;
  8. import com.google.gwt.user.client.Element;
  9. import com.google.gwt.user.client.ui.Widget;
  10. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  11. import com.itmill.toolkit.terminal.gwt.client.ICaption;
  12. import com.itmill.toolkit.terminal.gwt.client.Container;
  13. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  14. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  15. import com.itmill.toolkit.terminal.gwt.client.ui.MarginInfo;
  16. /**
  17. * Proto level implementation of GridLayout.
  18. *
  19. * All cell's will be equally sized.
  20. *
  21. */
  22. public class ISizeableGridLayout extends IAbsoluteGrid implements Paintable,
  23. Container {
  24. public static final String CLASSNAME = "i-gridlayout";
  25. private int spacing;
  26. private HashMap paintableToCellMap = new HashMap();
  27. private ApplicationConnection client;
  28. private MarginPixels mp;
  29. private String oldStyleString = "";
  30. public ISizeableGridLayout() {
  31. super();
  32. setStyleName(CLASSNAME);
  33. }
  34. protected int getSpacingSize() {
  35. return spacing;
  36. }
  37. public void updateFromUIDL(UIDL uidl, final ApplicationConnection client) {
  38. this.client = client;
  39. if (client.updateComponent(this, uidl, true)) {
  40. return;
  41. }
  42. // act properly in rare case where style name changes
  43. String newStyleString = "";
  44. if (uidl.hasAttribute("style")) {
  45. newStyleString = uidl.getStringAttribute("style");
  46. }
  47. if (!newStyleString.equals(oldStyleString)) {
  48. // reset detected margin values as they may change due style change
  49. mp = null;
  50. // also force extra layout phase after render (fails initially if
  51. // changed)
  52. DeferredCommand.addCommand(new Command() {
  53. public void execute() {
  54. client.requestLayoutPhase();
  55. }
  56. });
  57. }
  58. oldStyleString = newStyleString;
  59. if (uidl.hasAttribute("caption")) {
  60. setTitle(uidl.getStringAttribute("caption"));
  61. }
  62. int row = 0, column = 0;
  63. final ArrayList oldCells = new ArrayList();
  64. for (final Iterator iterator = getCellIterator(); iterator.hasNext();) {
  65. oldCells.add(iterator.next());
  66. }
  67. clear();
  68. setCols(uidl.getIntAttribute("w"));
  69. setRows(uidl.getIntAttribute("h"));
  70. handleMargins(uidl);
  71. spacing = uidl.getBooleanAttribute("spacing") ? detectSpacingSize() : 0;
  72. final int[] alignments = uidl.getIntArrayAttribute("alignments");
  73. int alignmentIndex = 0;
  74. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  75. final UIDL r = (UIDL) i.next();
  76. if ("gr".equals(r.getTag())) {
  77. column = 0;
  78. for (final Iterator j = r.getChildIterator(); j.hasNext();) {
  79. final UIDL c = (UIDL) j.next();
  80. if ("gc".equals(c.getTag())) {
  81. // Set cell width
  82. int colSpan;
  83. if (c.hasAttribute("w")) {
  84. colSpan = c.getIntAttribute("w");
  85. } else {
  86. colSpan = 1;
  87. }
  88. // Set cell height
  89. int rowSpan;
  90. if (c.hasAttribute("h")) {
  91. rowSpan = c.getIntAttribute("h");
  92. } else {
  93. rowSpan = 1;
  94. }
  95. final UIDL u = c.getChildUIDL(0);
  96. if (u != null) {
  97. final Paintable child = client.getPaintable(u);
  98. IAbsoluteGridCell cell = getCell(column, row);
  99. paintableToCellMap.put(child, cell);
  100. cell.rowSpan = rowSpan;
  101. cell.colSpan = colSpan;
  102. oldCells.remove(cell);
  103. cell.setAlignment(alignments[alignmentIndex++]);
  104. cell.render();
  105. cell.setWidget((Widget) child);
  106. if (!u.getBooleanAttribute("cached")) {
  107. child.updateFromUIDL(u, client);
  108. }
  109. cell.vAling();
  110. }
  111. column += colSpan;
  112. }
  113. }
  114. row++;
  115. }
  116. }
  117. // loop oldWidgetWrappers that where not re-attached and unregister them
  118. for (final Iterator it = oldCells.iterator(); it.hasNext();) {
  119. final IAbsoluteGridCell w = (IAbsoluteGridCell) it.next();
  120. client.unregisterPaintable((Paintable) w.getWidget());
  121. w.removeFromParent();
  122. paintableToCellMap.remove(w.getWidget());
  123. }
  124. }
  125. protected void handleMargins(UIDL uidl) {
  126. final MarginInfo margins = new MarginInfo(uidl
  127. .getIntAttribute("margins"));
  128. if (mp == null) {
  129. mp = detectMargins(getElement(), CLASSNAME);
  130. }
  131. marginTop = margins.hasTop() ? mp.top : 0;
  132. marginRight = margins.hasRight() ? mp.right : 0;
  133. marginBottom = margins.hasBottom() ? mp.bottom : 0;
  134. marginLeft = margins.hasLeft() ? mp.left : 0;
  135. }
  136. private int detectSpacingSize() {
  137. if (mp == null) {
  138. mp = detectMargins(getElement(), CLASSNAME);
  139. }
  140. return mp.spacing;
  141. }
  142. public boolean hasChildComponent(Widget component) {
  143. if (paintableToCellMap.containsKey(component)) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  150. // TODO Auto-generated method stub
  151. }
  152. public void updateCaption(Paintable component, UIDL uidl) {
  153. IAbsoluteGridCell cell = (IAbsoluteGridCell) paintableToCellMap
  154. .get(component);
  155. ICaption c = cell.getCaption();
  156. if (c == null) {
  157. c = new ICaption(component, client);
  158. cell.setCaption(c);
  159. }
  160. c.updateCaption(uidl);
  161. }
  162. /**
  163. * Helper method to detect proper sizes (set via css) for margins and
  164. * spacings.
  165. *
  166. * @param baseElement
  167. * measurements will be done withing this element
  168. * @param baseStyleName
  169. * base style name
  170. * @return
  171. */
  172. public static MarginPixels detectMargins(Element baseElement,
  173. String baseStyleName) {
  174. Element wrap = DOM.createDiv();
  175. DOM.setStyleAttribute(wrap, "position", "absolute");
  176. DOM.setStyleAttribute(wrap, "visibility", "hidden");
  177. Element left = DOM.createDiv();
  178. DOM.setElementProperty(left, "className", baseStyleName
  179. + "-margin-left");
  180. DOM.setStyleAttribute(left, "width", "0");
  181. DOM.appendChild(wrap, left);
  182. Element right = DOM.createDiv();
  183. DOM.setElementProperty(right, "className", baseStyleName
  184. + "-margin-right");
  185. DOM.setStyleAttribute(right, "width", "0");
  186. DOM.appendChild(wrap, right);
  187. Element top = DOM.createDiv();
  188. DOM.setElementProperty(top, "className", baseStyleName + "-margin-top");
  189. DOM.setStyleAttribute(top, "width", "0");
  190. DOM.appendChild(wrap, top);
  191. Element bottom = DOM.createDiv();
  192. DOM.setElementProperty(bottom, "className", baseStyleName
  193. + "-margin-bottom");
  194. DOM.setStyleAttribute(bottom, "width", "0");
  195. DOM.appendChild(wrap, bottom);
  196. Element spacing = DOM.createDiv();
  197. DOM.setElementProperty(spacing, "className", baseStyleName
  198. + "-spacing-element");
  199. DOM.setStyleAttribute(spacing, "width", "0");
  200. DOM.appendChild(wrap, spacing);
  201. DOM.insertChild(baseElement, wrap, 0);
  202. MarginPixels marginPixels = new MarginPixels();
  203. marginPixels.top = DOM.getElementPropertyInt(top, "offsetHeight");
  204. marginPixels.right = DOM.getElementPropertyInt(right, "offsetWidth");
  205. marginPixels.bottom = DOM.getElementPropertyInt(bottom, "offsetHeight");
  206. marginPixels.left = DOM.getElementPropertyInt(left, "offsetWidth");
  207. marginPixels.spacing = DOM
  208. .getElementPropertyInt(spacing, "offsetWidth");
  209. DOM.removeChild(baseElement, wrap);
  210. return marginPixels;
  211. }
  212. }
  213. class MarginPixels {
  214. public int spacing;
  215. public int top;
  216. public int bottom;
  217. public int left;
  218. public int right;
  219. }