Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IFormLayout.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import com.google.gwt.user.client.DOM;
  8. import com.google.gwt.user.client.Element;
  9. import com.google.gwt.user.client.Event;
  10. import com.google.gwt.user.client.ui.FlexTable;
  11. import com.google.gwt.user.client.ui.HTML;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  14. import com.itmill.toolkit.terminal.gwt.client.Container;
  15. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  16. import com.itmill.toolkit.terminal.gwt.client.StyleConstants;
  17. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  18. import com.itmill.toolkit.terminal.gwt.client.Util;
  19. /**
  20. * Two col Layout that places caption on left col and field on right col
  21. */
  22. public class IFormLayout extends FlexTable implements Container {
  23. private final static String CLASSNAME = "i-formlayout";
  24. HashMap componentToCaption = new HashMap();
  25. private ApplicationConnection client;
  26. private HashMap componentToError = new HashMap();
  27. public IFormLayout() {
  28. super();
  29. setStylePrimaryName(CLASSNAME);
  30. DOM.setElementProperty(getElement(), "cellPadding", "0");
  31. DOM.setElementProperty(getElement(), "cellSpacing", "0");
  32. }
  33. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  34. this.client = client;
  35. if (client.updateComponent(this, uidl, true)) {
  36. return;
  37. }
  38. final MarginInfo margins = new MarginInfo(uidl
  39. .getIntAttribute("margins"));
  40. Element margin = getElement();
  41. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  42. margins.hasTop());
  43. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  44. margins.hasRight());
  45. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  46. margins.hasBottom());
  47. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  48. margins.hasLeft());
  49. setStyleName(margin, CLASSNAME + "-" + "spacing", uidl
  50. .hasAttribute("spacing"));
  51. int i = 0;
  52. for (final Iterator it = uidl.getChildIterator(); it.hasNext(); i++) {
  53. prepareCell(i, 1);
  54. final UIDL childUidl = (UIDL) it.next();
  55. final Paintable p = client.getPaintable(childUidl);
  56. Caption caption = (Caption) componentToCaption.get(p);
  57. if (caption == null) {
  58. caption = new Caption(p, client);
  59. componentToCaption.put(p, caption);
  60. }
  61. ErrorFlag error = (ErrorFlag) componentToError.get(p);
  62. if (error == null) {
  63. error = new ErrorFlag();
  64. componentToError.put(p, error);
  65. }
  66. prepareCell(i, 2);
  67. final Paintable oldComponent = (Paintable) getWidget(i, 2);
  68. if (oldComponent == null) {
  69. setWidget(i, 2, (Widget) p);
  70. } else if (oldComponent != p) {
  71. client.unregisterPaintable(oldComponent);
  72. setWidget(i, 2, (Widget) p);
  73. }
  74. getCellFormatter().setStyleName(i, 2, CLASSNAME + "-contentcell");
  75. getCellFormatter().setStyleName(i, 0, CLASSNAME + "-captioncell");
  76. setWidget(i, 0, caption);
  77. getCellFormatter().setStyleName(i, 1, CLASSNAME + "-errorcell");
  78. setWidget(i, 1, error);
  79. p.updateFromUIDL(childUidl, client);
  80. String rowstyles = CLASSNAME + "-row";
  81. if (i == 0) {
  82. rowstyles += " " + CLASSNAME + "-firstrow";
  83. }
  84. if (!it.hasNext()) {
  85. rowstyles += " " + CLASSNAME + "-lastrow";
  86. }
  87. getRowFormatter().setStyleName(i, rowstyles);
  88. }
  89. while (getRowCount() > i) {
  90. final Paintable p = (Paintable) getWidget(i, 2);
  91. client.unregisterPaintable(p);
  92. componentToCaption.remove(p);
  93. removeRow(i);
  94. }
  95. }
  96. public boolean hasChildComponent(Widget component) {
  97. return componentToCaption.containsKey(component);
  98. }
  99. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  100. int i;
  101. for (i = 0; i < getRowCount(); i++) {
  102. if (oldComponent == getWidget(i, 1)) {
  103. final Caption newCap = new Caption((Paintable) newComponent,
  104. client);
  105. setWidget(i, 0, newCap);
  106. setWidget(i, 1, newComponent);
  107. client.unregisterPaintable((Paintable) oldComponent);
  108. break;
  109. }
  110. }
  111. }
  112. public void updateCaption(Paintable component, UIDL uidl) {
  113. final Caption c = (Caption) componentToCaption.get(component);
  114. if (c != null) {
  115. c.updateCaption(uidl);
  116. }
  117. final ErrorFlag e = (ErrorFlag) componentToError.get(component);
  118. if (e != null) {
  119. e.updateFromUIDL(uidl, component);
  120. }
  121. }
  122. public class Caption extends HTML {
  123. public static final String CLASSNAME = "i-caption";
  124. private final Paintable owner;
  125. private Element requiredFieldIndicator;
  126. private Icon icon;
  127. private Element captionText;
  128. private final ApplicationConnection client;
  129. /**
  130. *
  131. * @param component
  132. * optional owner of caption. If not set, getOwner will
  133. * return null
  134. * @param client
  135. */
  136. public Caption(Paintable component, ApplicationConnection client) {
  137. super();
  138. this.client = client;
  139. owner = component;
  140. setStyleName(CLASSNAME);
  141. }
  142. public void updateCaption(UIDL uidl) {
  143. setVisible(!uidl.getBooleanAttribute("invisible"));
  144. setStyleName(getElement(), "i-disabled", uidl
  145. .hasAttribute("disabled"));
  146. boolean isEmpty = true;
  147. if (uidl.hasAttribute("icon")) {
  148. if (icon == null) {
  149. icon = new Icon(client);
  150. DOM.insertChild(getElement(), icon.getElement(), 0);
  151. }
  152. icon.setUri(uidl.getStringAttribute("icon"));
  153. isEmpty = false;
  154. } else {
  155. if (icon != null) {
  156. DOM.removeChild(getElement(), icon.getElement());
  157. icon = null;
  158. }
  159. }
  160. if (uidl.hasAttribute("caption")) {
  161. if (captionText == null) {
  162. captionText = DOM.createSpan();
  163. DOM.insertChild(getElement(), captionText, icon == null ? 0
  164. : 1);
  165. }
  166. String c = uidl.getStringAttribute("caption");
  167. if (c == null) {
  168. c = "";
  169. } else {
  170. isEmpty = false;
  171. }
  172. DOM.setInnerText(captionText, c);
  173. } else {
  174. // TODO should span also be removed
  175. }
  176. if (uidl.hasAttribute("description")) {
  177. if (captionText != null) {
  178. addStyleDependentName("hasdescription");
  179. } else {
  180. removeStyleDependentName("hasdescription");
  181. }
  182. }
  183. if (uidl.getBooleanAttribute("required")) {
  184. if (requiredFieldIndicator == null) {
  185. requiredFieldIndicator = DOM.createSpan();
  186. DOM.setInnerText(requiredFieldIndicator, "*");
  187. DOM.setElementProperty(requiredFieldIndicator, "className",
  188. "i-required-field-indicator");
  189. DOM.appendChild(getElement(), requiredFieldIndicator);
  190. }
  191. } else {
  192. if (requiredFieldIndicator != null) {
  193. DOM.removeChild(getElement(), requiredFieldIndicator);
  194. requiredFieldIndicator = null;
  195. }
  196. }
  197. // Workaround for IE weirdness, sometimes returns bad height in some
  198. // circumstances when Caption is empty. See #1444
  199. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  200. if (Util.isIE()) {
  201. if (isEmpty) {
  202. setHeight("0px");
  203. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  204. } else {
  205. setHeight("");
  206. DOM.setStyleAttribute(getElement(), "overflow", "");
  207. }
  208. }
  209. }
  210. /**
  211. * Returns Paintable for which this Caption belongs to.
  212. *
  213. * @return owner Widget
  214. */
  215. public Paintable getOwner() {
  216. return owner;
  217. }
  218. public void onBrowserEvent(Event event) {
  219. super.onBrowserEvent(event);
  220. if (client != null) {
  221. client.handleTooltipEvent(event, owner);
  222. }
  223. }
  224. }
  225. private class ErrorFlag extends HTML {
  226. private static final String CLASSNAME = ".i-form-layout-error-indicator";
  227. Element errorIndicatorElement;
  228. private Paintable owner;
  229. public ErrorFlag() {
  230. setStyleName(CLASSNAME);
  231. }
  232. public void updateFromUIDL(UIDL uidl, Paintable component) {
  233. owner = component;
  234. if (uidl.hasAttribute("error")) {
  235. if (errorIndicatorElement == null) {
  236. errorIndicatorElement = DOM.createDiv();
  237. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  238. DOM.setElementProperty(errorIndicatorElement, "className",
  239. "i-errorindicator");
  240. DOM.appendChild(getElement(), errorIndicatorElement);
  241. }
  242. } else if (errorIndicatorElement != null) {
  243. DOM.removeChild(getElement(), errorIndicatorElement);
  244. errorIndicatorElement = null;
  245. }
  246. }
  247. public void onBrowserEvent(Event event) {
  248. super.onBrowserEvent(event);
  249. if (owner != null) {
  250. client.handleTooltipEvent(event, owner);
  251. }
  252. }
  253. }
  254. public boolean childComponentSizesUpdated() {
  255. // TODO Auto-generated method stub
  256. return false;
  257. }
  258. }