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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.ComponentConnector;
  21. import com.vaadin.terminal.gwt.client.ComponentState;
  22. import com.vaadin.terminal.gwt.client.ConnectorMap;
  23. import com.vaadin.terminal.gwt.client.Focusable;
  24. import com.vaadin.terminal.gwt.client.StyleConstants;
  25. import com.vaadin.terminal.gwt.client.UIDL;
  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.isEnabled()) {
  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.getWidget());
  155. if (c != null) {
  156. c.updateCaption(uidl, paintable.getState());
  157. }
  158. final ErrorFlag e = widgetToError.get(paintable.getWidget());
  159. if (e != null) {
  160. e.updateFromUIDL(uidl, paintable);
  161. }
  162. }
  163. /*
  164. * (non-Javadoc)
  165. *
  166. * @see
  167. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  168. * .event.dom.client.ClickEvent)
  169. */
  170. public void onClick(ClickEvent event) {
  171. Caption caption = (Caption) event.getSource();
  172. if (caption.getOwner() != null) {
  173. if (caption.getOwner() instanceof Focusable) {
  174. ((Focusable) caption.getOwner()).focus();
  175. } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  176. ((com.google.gwt.user.client.ui.Focusable) caption
  177. .getOwner()).setFocus(true);
  178. }
  179. }
  180. }
  181. }
  182. // TODO why duplicated here?
  183. public class Caption extends HTML {
  184. public static final String CLASSNAME = "v-caption";
  185. private final ComponentConnector owner;
  186. private Element requiredFieldIndicator;
  187. private Icon icon;
  188. private Element captionText;
  189. private final ApplicationConnection client;
  190. /**
  191. *
  192. * @param component
  193. * optional owner of caption. If not set, getOwner will
  194. * return null
  195. * @param client
  196. */
  197. public Caption(ComponentConnector component,
  198. ApplicationConnection client) {
  199. super();
  200. this.client = client;
  201. owner = component;
  202. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  203. }
  204. private void setStyles(String[] styles) {
  205. String styleName = CLASSNAME;
  206. if (styles != null) {
  207. for (String style : styles) {
  208. if (ApplicationConnection.DISABLED_CLASSNAME.equals(style)) {
  209. // Add v-disabled also without classname prefix so
  210. // generic v-disabled CSS rules work
  211. styleName += " " + style;
  212. }
  213. styleName += " " + CLASSNAME + "-" + style;
  214. }
  215. }
  216. setStyleName(styleName);
  217. }
  218. public void updateCaption(UIDL uidl, ComponentState state) {
  219. setVisible(!uidl.getBooleanAttribute("invisible"));
  220. // Update styles as they might have changed when the caption changed
  221. setStyles(getStylesFromState(state));
  222. boolean isEmpty = true;
  223. if (state.getIcon() != null) {
  224. if (icon == null) {
  225. icon = new Icon(client);
  226. DOM.insertChild(getElement(), icon.getElement(), 0);
  227. }
  228. icon.setUri(state.getIcon().getURL());
  229. isEmpty = false;
  230. } else {
  231. if (icon != null) {
  232. DOM.removeChild(getElement(), icon.getElement());
  233. icon = null;
  234. }
  235. }
  236. if (state.getCaption() != null) {
  237. if (captionText == null) {
  238. captionText = DOM.createSpan();
  239. DOM.insertChild(getElement(), captionText, icon == null ? 0
  240. : 1);
  241. }
  242. String c = state.getCaption();
  243. if (c == null) {
  244. c = "";
  245. } else {
  246. isEmpty = false;
  247. }
  248. DOM.setInnerText(captionText, c);
  249. } else {
  250. // TODO should span also be removed
  251. }
  252. if (state.hasDescription() && captionText != null) {
  253. addStyleDependentName("hasdescription");
  254. } else {
  255. removeStyleDependentName("hasdescription");
  256. }
  257. if (uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED)) {
  258. if (requiredFieldIndicator == null) {
  259. requiredFieldIndicator = DOM.createSpan();
  260. DOM.setInnerText(requiredFieldIndicator, "*");
  261. DOM.setElementProperty(requiredFieldIndicator, "className",
  262. "v-required-field-indicator");
  263. DOM.appendChild(getElement(), requiredFieldIndicator);
  264. }
  265. } else {
  266. if (requiredFieldIndicator != null) {
  267. DOM.removeChild(getElement(), requiredFieldIndicator);
  268. requiredFieldIndicator = null;
  269. }
  270. }
  271. // Workaround for IE weirdness, sometimes returns bad height in some
  272. // circumstances when Caption is empty. See #1444
  273. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  274. // FIXME: This could be unnecessary for IE8+
  275. if (BrowserInfo.get().isIE()) {
  276. if (isEmpty) {
  277. setHeight("0px");
  278. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  279. } else {
  280. setHeight("");
  281. DOM.setStyleAttribute(getElement(), "overflow", "");
  282. }
  283. }
  284. }
  285. /**
  286. * Returns Paintable for which this Caption belongs to.
  287. *
  288. * @return owner Widget
  289. */
  290. public ComponentConnector getOwner() {
  291. return owner;
  292. }
  293. @Override
  294. public void onBrowserEvent(Event event) {
  295. super.onBrowserEvent(event);
  296. if (client != null) {
  297. client.handleTooltipEvent(event, owner);
  298. }
  299. }
  300. }
  301. private class ErrorFlag extends HTML {
  302. private static final String CLASSNAME = VFormLayout.CLASSNAME
  303. + "-error-indicator";
  304. Element errorIndicatorElement;
  305. private ComponentConnector owner;
  306. public ErrorFlag() {
  307. setStyleName(CLASSNAME);
  308. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  309. }
  310. public void updateFromUIDL(UIDL uidl, ComponentConnector component) {
  311. owner = component;
  312. if (uidl.hasAttribute("error")
  313. && !uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_HIDEERRORS)) {
  314. if (errorIndicatorElement == null) {
  315. errorIndicatorElement = DOM.createDiv();
  316. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  317. DOM.setElementProperty(errorIndicatorElement, "className",
  318. "v-errorindicator");
  319. DOM.appendChild(getElement(), errorIndicatorElement);
  320. }
  321. } else if (errorIndicatorElement != null) {
  322. DOM.removeChild(getElement(), errorIndicatorElement);
  323. errorIndicatorElement = null;
  324. }
  325. }
  326. @Override
  327. public void onBrowserEvent(Event event) {
  328. super.onBrowserEvent(event);
  329. if (owner != null) {
  330. client.handleTooltipEvent(event, owner);
  331. }
  332. }
  333. }
  334. }