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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import com.google.gwt.aria.client.Roles;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.dom.client.Style.Overflow;
  23. import com.google.gwt.event.dom.client.ClickEvent;
  24. import com.google.gwt.event.dom.client.ClickHandler;
  25. import com.google.gwt.user.client.DOM;
  26. import com.google.gwt.user.client.ui.FlexTable;
  27. import com.google.gwt.user.client.ui.HTML;
  28. import com.google.gwt.user.client.ui.SimplePanel;
  29. import com.google.gwt.user.client.ui.Widget;
  30. import com.vaadin.client.ApplicationConnection;
  31. import com.vaadin.client.BrowserInfo;
  32. import com.vaadin.client.ComponentConnector;
  33. import com.vaadin.client.Focusable;
  34. import com.vaadin.client.StyleConstants;
  35. import com.vaadin.client.VTooltip;
  36. import com.vaadin.client.ui.aria.AriaHelper;
  37. import com.vaadin.shared.AbstractComponentState;
  38. import com.vaadin.shared.ComponentConstants;
  39. import com.vaadin.shared.ui.ComponentStateUtil;
  40. import com.vaadin.shared.ui.MarginInfo;
  41. /**
  42. * Two col Layout that places caption on left col and field on right col
  43. */
  44. public class VFormLayout extends SimplePanel {
  45. private final static String CLASSNAME = "v-formlayout";
  46. /** For internal use only. May be removed or replaced in the future. */
  47. public VFormLayoutTable table;
  48. public VFormLayout() {
  49. super();
  50. setStyleName(CLASSNAME);
  51. addStyleName(StyleConstants.UI_LAYOUT);
  52. table = new VFormLayoutTable();
  53. setWidget(table);
  54. }
  55. /**
  56. * Parses the stylenames from shared state
  57. *
  58. * @param state
  59. * shared state of the component
  60. * @param enabled
  61. * @return An array of stylenames
  62. */
  63. private String[] getStylesFromState(AbstractComponentState state,
  64. boolean enabled) {
  65. List<String> styles = new ArrayList<String>();
  66. if (ComponentStateUtil.hasStyles(state)) {
  67. for (String name : state.styles) {
  68. styles.add(name);
  69. }
  70. }
  71. if (!enabled) {
  72. styles.add(ApplicationConnection.DISABLED_CLASSNAME);
  73. }
  74. return styles.toArray(new String[styles.size()]);
  75. }
  76. public class VFormLayoutTable extends FlexTable implements ClickHandler {
  77. private static final int COLUMN_CAPTION = 0;
  78. private static final int COLUMN_ERRORFLAG = 1;
  79. private static final int COLUMN_WIDGET = 2;
  80. private HashMap<Widget, Caption> widgetToCaption = new HashMap<Widget, Caption>();
  81. private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<Widget, ErrorFlag>();
  82. public VFormLayoutTable() {
  83. DOM.setElementProperty(getElement(), "cellPadding", "0");
  84. DOM.setElementProperty(getElement(), "cellSpacing", "0");
  85. Roles.getPresentationRole().set(getElement());
  86. }
  87. /*
  88. * (non-Javadoc)
  89. *
  90. * @see
  91. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  92. * .event.dom.client.ClickEvent)
  93. */
  94. @Override
  95. public void onClick(ClickEvent event) {
  96. Caption caption = (Caption) event.getSource();
  97. if (caption.getOwner() != null) {
  98. if (caption.getOwner() instanceof Focusable) {
  99. ((Focusable) caption.getOwner()).focus();
  100. } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  101. ((com.google.gwt.user.client.ui.Focusable) caption
  102. .getOwner()).setFocus(true);
  103. }
  104. }
  105. }
  106. public void setMargins(MarginInfo margins) {
  107. Element margin = getElement();
  108. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  109. margins.hasTop());
  110. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  111. margins.hasRight());
  112. setStyleName(margin,
  113. CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  114. margins.hasBottom());
  115. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  116. margins.hasLeft());
  117. }
  118. public void setSpacing(boolean spacing) {
  119. setStyleName(getElement(), CLASSNAME + "-" + "spacing", spacing);
  120. }
  121. public void setRowCount(int rowNr) {
  122. for (int i = 0; i < rowNr; i++) {
  123. prepareCell(i, COLUMN_CAPTION);
  124. getCellFormatter().setStyleName(i, COLUMN_CAPTION,
  125. CLASSNAME + "-captioncell");
  126. prepareCell(i, 1);
  127. getCellFormatter().setStyleName(i, COLUMN_ERRORFLAG,
  128. CLASSNAME + "-errorcell");
  129. prepareCell(i, 2);
  130. getCellFormatter().setStyleName(i, COLUMN_WIDGET,
  131. CLASSNAME + "-contentcell");
  132. String rowstyles = CLASSNAME + "-row";
  133. if (i == 0) {
  134. rowstyles += " " + CLASSNAME + "-firstrow";
  135. }
  136. if (i == rowNr - 1) {
  137. rowstyles += " " + CLASSNAME + "-lastrow";
  138. }
  139. getRowFormatter().setStyleName(i, rowstyles);
  140. }
  141. while (getRowCount() != rowNr) {
  142. removeRow(rowNr);
  143. }
  144. }
  145. public void setChild(int rowNr, Widget childWidget, Caption caption,
  146. ErrorFlag error) {
  147. setWidget(rowNr, COLUMN_WIDGET, childWidget);
  148. setWidget(rowNr, COLUMN_CAPTION, caption);
  149. setWidget(rowNr, COLUMN_ERRORFLAG, error);
  150. widgetToCaption.put(childWidget, caption);
  151. widgetToError.put(childWidget, error);
  152. }
  153. public Caption getCaption(Widget childWidget) {
  154. return widgetToCaption.get(childWidget);
  155. }
  156. public ErrorFlag getError(Widget childWidget) {
  157. return widgetToError.get(childWidget);
  158. }
  159. public void cleanReferences(Widget oldChildWidget) {
  160. widgetToError.remove(oldChildWidget);
  161. widgetToCaption.remove(oldChildWidget);
  162. }
  163. public void updateCaption(Widget widget, AbstractComponentState state,
  164. boolean enabled) {
  165. final Caption c = widgetToCaption.get(widget);
  166. if (c != null) {
  167. c.updateCaption(state, enabled);
  168. }
  169. }
  170. public void updateError(Widget widget, String errorMessage,
  171. boolean hideErrors) {
  172. final ErrorFlag e = widgetToError.get(widget);
  173. if (e != null) {
  174. e.updateError(errorMessage, hideErrors);
  175. }
  176. }
  177. }
  178. // TODO why duplicated here?
  179. public class Caption extends HTML {
  180. public static final String CLASSNAME = "v-caption";
  181. private final ComponentConnector owner;
  182. private Element requiredFieldIndicator;
  183. private Icon icon;
  184. private Element captionText;
  185. /**
  186. *
  187. * @param component
  188. * optional owner of caption. If not set, getOwner will
  189. * return null
  190. */
  191. public Caption(ComponentConnector component) {
  192. super();
  193. owner = component;
  194. }
  195. private void setStyles(String[] styles) {
  196. String styleName = CLASSNAME;
  197. if (styles != null) {
  198. for (String style : styles) {
  199. if (ApplicationConnection.DISABLED_CLASSNAME.equals(style)) {
  200. // Add v-disabled also without classname prefix so
  201. // generic v-disabled CSS rules work
  202. styleName += " " + style;
  203. }
  204. styleName += " " + CLASSNAME + "-" + style;
  205. }
  206. }
  207. setStyleName(styleName);
  208. }
  209. public void updateCaption(AbstractComponentState state, boolean enabled) {
  210. // Update styles as they might have changed when the caption changed
  211. setStyles(getStylesFromState(state, enabled));
  212. boolean isEmpty = true;
  213. if (icon != null) {
  214. getElement().removeChild(icon.getElement());
  215. icon = null;
  216. }
  217. if (state.resources.containsKey(ComponentConstants.ICON_RESOURCE)) {
  218. icon = owner.getConnection().getIcon(
  219. state.resources.get(ComponentConstants.ICON_RESOURCE)
  220. .getURL());
  221. DOM.insertChild(getElement(), icon.getElement(), 0);
  222. isEmpty = false;
  223. }
  224. if (state.caption != null) {
  225. if (captionText == null) {
  226. captionText = DOM.createSpan();
  227. AriaHelper.bindCaption(owner.getWidget(), captionText);
  228. DOM.insertChild(getElement(), captionText, icon == null ? 0
  229. : 1);
  230. }
  231. String c = state.caption;
  232. if (c == null) {
  233. c = "";
  234. } else {
  235. isEmpty = false;
  236. }
  237. DOM.setInnerText(captionText, c);
  238. } else {
  239. // TODO should span also be removed
  240. }
  241. if (state.description != null && captionText != null) {
  242. addStyleDependentName("hasdescription");
  243. } else {
  244. removeStyleDependentName("hasdescription");
  245. }
  246. boolean required = owner instanceof AbstractFieldConnector
  247. && ((AbstractFieldConnector) owner).isRequired();
  248. AriaHelper.handleInputRequired(owner.getWidget(), required);
  249. if (required) {
  250. if (requiredFieldIndicator == null) {
  251. requiredFieldIndicator = DOM.createSpan();
  252. DOM.setInnerText(requiredFieldIndicator, "*");
  253. DOM.setElementProperty(requiredFieldIndicator, "className",
  254. "v-required-field-indicator");
  255. DOM.appendChild(getElement(), requiredFieldIndicator);
  256. // Hide the required indicator from screen reader, as this
  257. // information is set directly at the input field
  258. Roles.getTextboxRole().setAriaHiddenState(
  259. requiredFieldIndicator, true);
  260. }
  261. } else {
  262. if (requiredFieldIndicator != null) {
  263. DOM.removeChild(getElement(), requiredFieldIndicator);
  264. requiredFieldIndicator = null;
  265. }
  266. }
  267. // Workaround for IE weirdness, sometimes returns bad height in some
  268. // circumstances when Caption is empty. See #1444
  269. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  270. // FIXME: This could be unnecessary for IE8+
  271. if (BrowserInfo.get().isIE()) {
  272. if (isEmpty) {
  273. setHeight("0px");
  274. getElement().getStyle().setOverflow(Overflow.HIDDEN);
  275. } else {
  276. setHeight("");
  277. getElement().getStyle().clearOverflow();
  278. }
  279. }
  280. }
  281. /**
  282. * Returns Paintable for which this Caption belongs to.
  283. *
  284. * @return owner Widget
  285. */
  286. public ComponentConnector getOwner() {
  287. return owner;
  288. }
  289. }
  290. /** For internal use only. May be removed or replaced in the future. */
  291. public class ErrorFlag extends HTML {
  292. private static final String CLASSNAME = VFormLayout.CLASSNAME
  293. + "-error-indicator";
  294. Element errorIndicatorElement;
  295. private ComponentConnector owner;
  296. public ErrorFlag(ComponentConnector owner) {
  297. setStyleName(CLASSNAME);
  298. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  299. this.owner = owner;
  300. }
  301. public ComponentConnector getOwner() {
  302. return owner;
  303. }
  304. public void updateError(String errorMessage, boolean hideErrors) {
  305. boolean showError = null != errorMessage;
  306. if (hideErrors) {
  307. showError = false;
  308. }
  309. AriaHelper.handleInputInvalid(owner.getWidget(), showError);
  310. if (showError) {
  311. if (errorIndicatorElement == null) {
  312. errorIndicatorElement = DOM.createDiv();
  313. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  314. DOM.setElementProperty(errorIndicatorElement, "className",
  315. "v-errorindicator");
  316. DOM.appendChild(getElement(), errorIndicatorElement);
  317. // Hide the error indicator from screen reader, as this
  318. // information is set directly at the input field
  319. Roles.getFormRole().setAriaHiddenState(
  320. errorIndicatorElement, true);
  321. }
  322. } else if (errorIndicatorElement != null) {
  323. DOM.removeChild(getElement(), errorIndicatorElement);
  324. errorIndicatorElement = null;
  325. }
  326. }
  327. }
  328. }