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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright 2000-2016 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.event.dom.client.ClickEvent;
  23. import com.google.gwt.event.dom.client.ClickHandler;
  24. import com.google.gwt.user.client.DOM;
  25. import com.google.gwt.user.client.ui.FlexTable;
  26. import com.google.gwt.user.client.ui.HTML;
  27. import com.google.gwt.user.client.ui.SimplePanel;
  28. import com.google.gwt.user.client.ui.Widget;
  29. import com.vaadin.client.BrowserInfo;
  30. import com.vaadin.client.ComponentConnector;
  31. import com.vaadin.client.Focusable;
  32. import com.vaadin.client.StyleConstants;
  33. import com.vaadin.client.VTooltip;
  34. import com.vaadin.client.WidgetUtil.ErrorUtil;
  35. import com.vaadin.client.ui.aria.AriaHelper;
  36. import com.vaadin.shared.AbstractComponentState;
  37. import com.vaadin.shared.ComponentConstants;
  38. import com.vaadin.shared.ui.ComponentStateUtil;
  39. import com.vaadin.shared.ui.ErrorLevel;
  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 static final 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<>();
  66. if (ComponentStateUtil.hasStyles(state)) {
  67. for (String name : state.styles) {
  68. styles.add(name);
  69. }
  70. }
  71. if (!enabled) {
  72. styles.add(StyleConstants.DISABLED);
  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. public static final int COLUMN_WIDGET = 2;
  80. private HashMap<Widget, Caption> widgetToCaption = new HashMap<>();
  81. private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<>();
  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
  101. .getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  102. ((com.google.gwt.user.client.ui.Focusable) caption
  103. .getOwner()).setFocus(true);
  104. }
  105. }
  106. }
  107. public void setMargins(MarginInfo margins) {
  108. Element margin = getElement();
  109. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  110. margins.hasTop());
  111. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  112. margins.hasRight());
  113. setStyleName(margin, 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. ErrorLevel errorLevel, boolean hideErrors) {
  172. final ErrorFlag e = widgetToError.get(widget);
  173. if (e != null) {
  174. e.updateError(errorMessage, errorLevel, 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 captionContent;
  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 (StyleConstants.DISABLED.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,
  210. boolean enabled) {
  211. // Update styles as they might have changed when the caption changed
  212. setStyles(getStylesFromState(state, enabled));
  213. boolean isEmpty = true;
  214. if (icon != null) {
  215. getElement().removeChild(icon.getElement());
  216. icon = null;
  217. }
  218. if (state.resources.containsKey(ComponentConstants.ICON_RESOURCE)) {
  219. icon = owner.getConnection().getIcon(state.resources
  220. .get(ComponentConstants.ICON_RESOURCE).getURL());
  221. DOM.insertChild(getElement(), icon.getElement(), 0);
  222. isEmpty = false;
  223. }
  224. if (state.caption != null) {
  225. if (captionContent == null) {
  226. captionContent = DOM.createSpan();
  227. AriaHelper.bindCaption(owner.getWidget(), captionContent);
  228. DOM.insertChild(getElement(), captionContent,
  229. icon == null ? 0 : 1);
  230. }
  231. String c = state.caption;
  232. if (c == null) {
  233. c = "";
  234. } else {
  235. isEmpty = false;
  236. }
  237. if (state.captionAsHtml) {
  238. captionContent.setInnerHTML(c);
  239. } else {
  240. captionContent.setInnerText(c);
  241. }
  242. } else {
  243. // TODO should span also be removed
  244. }
  245. if (state.description != null && captionContent != null) {
  246. addStyleDependentName("hasdescription");
  247. } else {
  248. removeStyleDependentName("hasdescription");
  249. }
  250. boolean required = owner instanceof HasRequiredIndicator
  251. && ((HasRequiredIndicator) owner)
  252. .isRequiredIndicatorVisible();
  253. AriaHelper.handleInputRequired(owner.getWidget(), required);
  254. if (required) {
  255. if (requiredFieldIndicator == null) {
  256. requiredFieldIndicator = DOM.createSpan();
  257. DOM.setInnerText(requiredFieldIndicator, "*");
  258. DOM.setElementProperty(requiredFieldIndicator, "className",
  259. "v-required-field-indicator");
  260. DOM.appendChild(getElement(), requiredFieldIndicator);
  261. // Hide the required indicator from screen reader, as this
  262. // information is set directly at the input field
  263. Roles.getTextboxRole()
  264. .setAriaHiddenState(requiredFieldIndicator, true);
  265. }
  266. } else {
  267. if (requiredFieldIndicator != null) {
  268. DOM.removeChild(getElement(), requiredFieldIndicator);
  269. requiredFieldIndicator = null;
  270. }
  271. }
  272. }
  273. /**
  274. * Returns Paintable for which this Caption belongs to.
  275. *
  276. * @return owner Widget
  277. */
  278. public ComponentConnector getOwner() {
  279. return owner;
  280. }
  281. }
  282. /** For internal use only. May be removed or replaced in the future. */
  283. public class ErrorFlag extends HTML implements HasErrorIndicatorElement {
  284. private static final String CLASSNAME = VFormLayout.CLASSNAME
  285. + "-error-indicator";
  286. Element errorIndicatorElement;
  287. private ComponentConnector owner;
  288. public ErrorFlag(ComponentConnector owner) {
  289. setStyleName(CLASSNAME);
  290. if (!BrowserInfo.get().isTouchDevice()) {
  291. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  292. }
  293. this.owner = owner;
  294. }
  295. public ComponentConnector getOwner() {
  296. return owner;
  297. }
  298. public void updateError(String errorMessage, ErrorLevel errorLevel,
  299. boolean hideErrors) {
  300. boolean showError = null != errorMessage;
  301. if (hideErrors) {
  302. showError = false;
  303. }
  304. AriaHelper.handleInputInvalid(owner.getWidget(), showError);
  305. if (showError) {
  306. setErrorIndicatorElementVisible(true);
  307. // Hide the error indicator from screen reader, as this
  308. // information is set directly at the input field
  309. Roles.getFormRole()
  310. .setAriaHiddenState(errorIndicatorElement, true);
  311. ErrorUtil.setErrorLevelStyle(errorIndicatorElement,
  312. StyleConstants.STYLE_NAME_ERROR_INDICATOR, errorLevel);
  313. } else {
  314. setErrorIndicatorElementVisible(false);
  315. }
  316. }
  317. @Override
  318. public Element getErrorIndicatorElement() {
  319. return errorIndicatorElement;
  320. }
  321. @Override
  322. public void setErrorIndicatorElementVisible(boolean visible) {
  323. if (visible) {
  324. if (errorIndicatorElement == null) {
  325. errorIndicatorElement = ErrorUtil
  326. .createErrorIndicatorElement();
  327. getElement().appendChild(errorIndicatorElement);
  328. }
  329. } else if (errorIndicatorElement != null) {
  330. getElement().removeChild(errorIndicatorElement);
  331. errorIndicatorElement = null;
  332. }
  333. }
  334. }
  335. }