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.

VFormLayout.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  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.List;
  9. import com.google.gwt.event.dom.client.ClickEvent;
  10. import com.google.gwt.event.dom.client.ClickHandler;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Element;
  13. import com.google.gwt.user.client.Event;
  14. import com.google.gwt.user.client.ui.FlexTable;
  15. import com.google.gwt.user.client.ui.HTML;
  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.ComponentState;
  21. import com.vaadin.terminal.gwt.client.Focusable;
  22. import com.vaadin.terminal.gwt.client.StyleConstants;
  23. import com.vaadin.terminal.gwt.client.UIDL;
  24. import com.vaadin.terminal.gwt.client.ConnectorMap;
  25. import com.vaadin.terminal.gwt.client.ComponentConnector;
  26. import com.vaadin.terminal.gwt.client.VTooltip;
  27. /**
  28. * Two col Layout that places caption on left col and field on right col
  29. */
  30. public class VFormLayout extends SimplePanel {
  31. private final static String CLASSNAME = "v-formlayout";
  32. ApplicationConnection client;
  33. VFormLayoutTable table;
  34. public VFormLayout() {
  35. super();
  36. setStyleName(CLASSNAME);
  37. table = new VFormLayoutTable();
  38. setWidget(table);
  39. }
  40. /**
  41. * Parses the stylenames from shared state
  42. *
  43. * @param state
  44. * shared state of the component
  45. * @return An array of stylenames
  46. */
  47. private String[] getStylesFromState(ComponentState state) {
  48. List<String> styles = new ArrayList<String>();
  49. if (state.hasStyles()) {
  50. String[] stylesnames = state.getStyle().split(" ");
  51. for (String name : stylesnames) {
  52. styles.add(name);
  53. }
  54. }
  55. if (state.isDisabled()) {
  56. styles.add(ApplicationConnection.DISABLED_CLASSNAME);
  57. }
  58. return styles.toArray(new String[styles.size()]);
  59. }
  60. public class VFormLayoutTable extends FlexTable implements ClickHandler {
  61. private static final int COLUMN_CAPTION = 0;
  62. private static final int COLUMN_ERRORFLAG = 1;
  63. private static final int COLUMN_WIDGET = 2;
  64. private HashMap<Widget, Caption> widgetToCaption = new HashMap<Widget, Caption>();
  65. private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<Widget, ErrorFlag>();
  66. public VFormLayoutTable() {
  67. DOM.setElementProperty(getElement(), "cellPadding", "0");
  68. DOM.setElementProperty(getElement(), "cellSpacing", "0");
  69. }
  70. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  71. final VMarginInfo margins = new VMarginInfo(
  72. uidl.getIntAttribute("margins"));
  73. Element margin = getElement();
  74. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  75. margins.hasTop());
  76. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  77. margins.hasRight());
  78. setStyleName(margin,
  79. CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  80. margins.hasBottom());
  81. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  82. margins.hasLeft());
  83. setStyleName(margin, CLASSNAME + "-" + "spacing",
  84. uidl.hasAttribute("spacing"));
  85. int i = 0;
  86. for (final Iterator<?> it = uidl.getChildIterator(); it.hasNext(); i++) {
  87. prepareCell(i, 1);
  88. final UIDL childUidl = (UIDL) it.next();
  89. final ComponentConnector childPaintable = client
  90. .getPaintable(childUidl);
  91. Widget childWidget = childPaintable.getWidget();
  92. Caption caption = widgetToCaption.get(childWidget);
  93. if (caption == null) {
  94. caption = new Caption(childPaintable, client);
  95. caption.addClickHandler(this);
  96. widgetToCaption.put(childWidget, caption);
  97. }
  98. ErrorFlag error = widgetToError.get(childWidget);
  99. if (error == null) {
  100. error = new ErrorFlag();
  101. widgetToError.put(childWidget, error);
  102. }
  103. prepareCell(i, COLUMN_WIDGET);
  104. Widget oldWidget = getWidget(i, COLUMN_WIDGET);
  105. if (oldWidget == null) {
  106. setWidget(i, COLUMN_WIDGET, childWidget);
  107. } else if (oldWidget != childWidget) {
  108. final ComponentConnector oldPaintable = ConnectorMap.get(
  109. client).getConnector(oldWidget);
  110. client.unregisterPaintable(oldPaintable);
  111. setWidget(i, COLUMN_WIDGET, childWidget);
  112. }
  113. getCellFormatter().setStyleName(i, COLUMN_WIDGET,
  114. CLASSNAME + "-contentcell");
  115. getCellFormatter().setStyleName(i, COLUMN_CAPTION,
  116. CLASSNAME + "-captioncell");
  117. setWidget(i, COLUMN_CAPTION, caption);
  118. getCellFormatter().setStyleName(i, COLUMN_ERRORFLAG,
  119. CLASSNAME + "-errorcell");
  120. setWidget(i, COLUMN_ERRORFLAG, error);
  121. childPaintable.updateFromUIDL(childUidl, client);
  122. // Update cell width when isRelativeWidth has been udpated
  123. if (childPaintable.isRelativeWidth()) {
  124. getCellFormatter().setWidth(i, COLUMN_WIDGET, "100%");
  125. } else {
  126. getCellFormatter().setWidth(i, COLUMN_WIDGET, null);
  127. }
  128. String rowstyles = CLASSNAME + "-row";
  129. if (i == 0) {
  130. rowstyles += " " + CLASSNAME + "-firstrow";
  131. }
  132. if (!it.hasNext()) {
  133. rowstyles += " " + CLASSNAME + "-lastrow";
  134. }
  135. getRowFormatter().setStyleName(i, rowstyles);
  136. }
  137. while (getRowCount() > i) {
  138. Widget w = getWidget(i, COLUMN_WIDGET);
  139. final ComponentConnector p = ConnectorMap.get(client)
  140. .getConnector(w);
  141. client.unregisterPaintable(p);
  142. widgetToCaption.remove(w);
  143. removeRow(i);
  144. }
  145. /*
  146. * Must update relative sized fields last when it is clear how much
  147. * space they are allowed to use
  148. */
  149. for (Widget p : widgetToCaption.keySet()) {
  150. client.handleComponentRelativeSize(p);
  151. }
  152. }
  153. public void updateCaption(ComponentConnector paintable, UIDL uidl) {
  154. final Caption c = widgetToCaption.get(paintable
  155. .getWidget());
  156. if (c != null) {
  157. c.updateCaption(uidl, paintable.getState());
  158. }
  159. final ErrorFlag e = widgetToError.get(paintable
  160. .getWidget());
  161. if (e != null) {
  162. e.updateFromUIDL(uidl, paintable);
  163. }
  164. }
  165. /*
  166. * (non-Javadoc)
  167. *
  168. * @see
  169. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  170. * .event.dom.client.ClickEvent)
  171. */
  172. public void onClick(ClickEvent event) {
  173. Caption caption = (Caption) event.getSource();
  174. if (caption.getOwner() != null) {
  175. if (caption.getOwner() instanceof Focusable) {
  176. ((Focusable) caption.getOwner()).focus();
  177. } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  178. ((com.google.gwt.user.client.ui.Focusable) caption
  179. .getOwner()).setFocus(true);
  180. }
  181. }
  182. }
  183. }
  184. // TODO why duplicated here?
  185. public class Caption extends HTML {
  186. public static final String CLASSNAME = "v-caption";
  187. private final ComponentConnector owner;
  188. private Element requiredFieldIndicator;
  189. private Icon icon;
  190. private Element captionText;
  191. private final ApplicationConnection client;
  192. /**
  193. *
  194. * @param component
  195. * optional owner of caption. If not set, getOwner will
  196. * return null
  197. * @param client
  198. */
  199. public Caption(ComponentConnector component, ApplicationConnection client) {
  200. super();
  201. this.client = client;
  202. owner = component;
  203. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  204. }
  205. private void setStyles(String[] styles) {
  206. String styleName = CLASSNAME;
  207. if (styles != null) {
  208. for (String style : styles) {
  209. if (ApplicationConnection.DISABLED_CLASSNAME.equals(style)) {
  210. // Add v-disabled also without classname prefix so
  211. // generic v-disabled CSS rules work
  212. styleName += " " + style;
  213. }
  214. styleName += " " + CLASSNAME + "-" + style;
  215. }
  216. }
  217. setStyleName(styleName);
  218. }
  219. public void updateCaption(UIDL uidl, ComponentState state) {
  220. setVisible(!uidl.getBooleanAttribute("invisible"));
  221. // Update styles as they might have changed when the caption changed
  222. setStyles(getStylesFromState(state));
  223. boolean isEmpty = true;
  224. if (uidl.hasAttribute(AbstractComponentConnector.ATTRIBUTE_ICON)) {
  225. if (icon == null) {
  226. icon = new Icon(client);
  227. DOM.insertChild(getElement(), icon.getElement(), 0);
  228. }
  229. icon.setUri(uidl
  230. .getStringAttribute(AbstractComponentConnector.ATTRIBUTE_ICON));
  231. isEmpty = false;
  232. } else {
  233. if (icon != null) {
  234. DOM.removeChild(getElement(), icon.getElement());
  235. icon = null;
  236. }
  237. }
  238. if (state.getCaption() != null) {
  239. if (captionText == null) {
  240. captionText = DOM.createSpan();
  241. DOM.insertChild(getElement(), captionText, icon == null ? 0
  242. : 1);
  243. }
  244. String c = state.getCaption();
  245. if (c == null) {
  246. c = "";
  247. } else {
  248. isEmpty = false;
  249. }
  250. DOM.setInnerText(captionText, c);
  251. } else {
  252. // TODO should span also be removed
  253. }
  254. if (state.hasDescription() && captionText != null) {
  255. addStyleDependentName("hasdescription");
  256. } else {
  257. removeStyleDependentName("hasdescription");
  258. }
  259. if (uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED)) {
  260. if (requiredFieldIndicator == null) {
  261. requiredFieldIndicator = DOM.createSpan();
  262. DOM.setInnerText(requiredFieldIndicator, "*");
  263. DOM.setElementProperty(requiredFieldIndicator, "className",
  264. "v-required-field-indicator");
  265. DOM.appendChild(getElement(), requiredFieldIndicator);
  266. }
  267. } else {
  268. if (requiredFieldIndicator != null) {
  269. DOM.removeChild(getElement(), requiredFieldIndicator);
  270. requiredFieldIndicator = null;
  271. }
  272. }
  273. // Workaround for IE weirdness, sometimes returns bad height in some
  274. // circumstances when Caption is empty. See #1444
  275. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  276. // FIXME: This could be unnecessary for IE8+
  277. if (BrowserInfo.get().isIE()) {
  278. if (isEmpty) {
  279. setHeight("0px");
  280. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  281. } else {
  282. setHeight("");
  283. DOM.setStyleAttribute(getElement(), "overflow", "");
  284. }
  285. }
  286. }
  287. /**
  288. * Returns Paintable for which this Caption belongs to.
  289. *
  290. * @return owner Widget
  291. */
  292. public ComponentConnector getOwner() {
  293. return owner;
  294. }
  295. @Override
  296. public void onBrowserEvent(Event event) {
  297. super.onBrowserEvent(event);
  298. if (client != null) {
  299. client.handleTooltipEvent(event, owner);
  300. }
  301. }
  302. }
  303. private class ErrorFlag extends HTML {
  304. private static final String CLASSNAME = VFormLayout.CLASSNAME
  305. + "-error-indicator";
  306. Element errorIndicatorElement;
  307. private ComponentConnector owner;
  308. public ErrorFlag() {
  309. setStyleName(CLASSNAME);
  310. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  311. }
  312. public void updateFromUIDL(UIDL uidl, ComponentConnector component) {
  313. owner = component;
  314. if (uidl.hasAttribute("error")
  315. && !uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_HIDEERRORS)) {
  316. if (errorIndicatorElement == null) {
  317. errorIndicatorElement = DOM.createDiv();
  318. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  319. DOM.setElementProperty(errorIndicatorElement, "className",
  320. "v-errorindicator");
  321. DOM.appendChild(getElement(), errorIndicatorElement);
  322. }
  323. } else if (errorIndicatorElement != null) {
  324. DOM.removeChild(getElement(), errorIndicatorElement);
  325. errorIndicatorElement = null;
  326. }
  327. }
  328. @Override
  329. public void onBrowserEvent(Event event) {
  330. super.onBrowserEvent(event);
  331. if (owner != null) {
  332. client.handleTooltipEvent(event, owner);
  333. }
  334. }
  335. }
  336. }