Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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.ui.aria.AriaHelper;
  35. import com.vaadin.shared.AbstractComponentState;
  36. import com.vaadin.shared.ComponentConstants;
  37. import com.vaadin.shared.ui.ComponentStateUtil;
  38. import com.vaadin.shared.ui.MarginInfo;
  39. /**
  40. * Two col Layout that places caption on left col and field on right col
  41. */
  42. public class VFormLayout extends SimplePanel {
  43. private final static String CLASSNAME = "v-formlayout";
  44. /** For internal use only. May be removed or replaced in the future. */
  45. public VFormLayoutTable table;
  46. public VFormLayout() {
  47. super();
  48. setStyleName(CLASSNAME);
  49. addStyleName(StyleConstants.UI_LAYOUT);
  50. table = new VFormLayoutTable();
  51. setWidget(table);
  52. }
  53. /**
  54. * Parses the stylenames from shared state
  55. *
  56. * @param state
  57. * shared state of the component
  58. * @param enabled
  59. * @return An array of stylenames
  60. */
  61. private String[] getStylesFromState(AbstractComponentState state,
  62. boolean enabled) {
  63. List<String> styles = new ArrayList<>();
  64. if (ComponentStateUtil.hasStyles(state)) {
  65. for (String name : state.styles) {
  66. styles.add(name);
  67. }
  68. }
  69. if (!enabled) {
  70. styles.add(StyleConstants.DISABLED);
  71. }
  72. return styles.toArray(new String[styles.size()]);
  73. }
  74. public class VFormLayoutTable extends FlexTable implements ClickHandler {
  75. private static final int COLUMN_CAPTION = 0;
  76. private static final int COLUMN_ERRORFLAG = 1;
  77. public static final int COLUMN_WIDGET = 2;
  78. private HashMap<Widget, Caption> widgetToCaption = new HashMap<>();
  79. private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<>();
  80. public VFormLayoutTable() {
  81. DOM.setElementProperty(getElement(), "cellPadding", "0");
  82. DOM.setElementProperty(getElement(), "cellSpacing", "0");
  83. Roles.getPresentationRole().set(getElement());
  84. }
  85. /*
  86. * (non-Javadoc)
  87. *
  88. * @see
  89. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  90. * .event.dom.client.ClickEvent)
  91. */
  92. @Override
  93. public void onClick(ClickEvent event) {
  94. Caption caption = (Caption) event.getSource();
  95. if (caption.getOwner() != null) {
  96. if (caption.getOwner() instanceof Focusable) {
  97. ((Focusable) caption.getOwner()).focus();
  98. } else if (caption
  99. .getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  100. ((com.google.gwt.user.client.ui.Focusable) caption
  101. .getOwner()).setFocus(true);
  102. }
  103. }
  104. }
  105. public void setMargins(MarginInfo margins) {
  106. Element margin = getElement();
  107. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  108. margins.hasTop());
  109. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  110. margins.hasRight());
  111. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  112. margins.hasBottom());
  113. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  114. margins.hasLeft());
  115. }
  116. public void setSpacing(boolean spacing) {
  117. setStyleName(getElement(), CLASSNAME + "-" + "spacing", spacing);
  118. }
  119. public void setRowCount(int rowNr) {
  120. for (int i = 0; i < rowNr; i++) {
  121. prepareCell(i, COLUMN_CAPTION);
  122. getCellFormatter().setStyleName(i, COLUMN_CAPTION,
  123. CLASSNAME + "-captioncell");
  124. prepareCell(i, 1);
  125. getCellFormatter().setStyleName(i, COLUMN_ERRORFLAG,
  126. CLASSNAME + "-errorcell");
  127. prepareCell(i, 2);
  128. getCellFormatter().setStyleName(i, COLUMN_WIDGET,
  129. CLASSNAME + "-contentcell");
  130. String rowstyles = CLASSNAME + "-row";
  131. if (i == 0) {
  132. rowstyles += " " + CLASSNAME + "-firstrow";
  133. }
  134. if (i == rowNr - 1) {
  135. rowstyles += " " + CLASSNAME + "-lastrow";
  136. }
  137. getRowFormatter().setStyleName(i, rowstyles);
  138. }
  139. while (getRowCount() != rowNr) {
  140. removeRow(rowNr);
  141. }
  142. }
  143. public void setChild(int rowNr, Widget childWidget, Caption caption,
  144. ErrorFlag error) {
  145. setWidget(rowNr, COLUMN_WIDGET, childWidget);
  146. setWidget(rowNr, COLUMN_CAPTION, caption);
  147. setWidget(rowNr, COLUMN_ERRORFLAG, error);
  148. widgetToCaption.put(childWidget, caption);
  149. widgetToError.put(childWidget, error);
  150. }
  151. public Caption getCaption(Widget childWidget) {
  152. return widgetToCaption.get(childWidget);
  153. }
  154. public ErrorFlag getError(Widget childWidget) {
  155. return widgetToError.get(childWidget);
  156. }
  157. public void cleanReferences(Widget oldChildWidget) {
  158. widgetToError.remove(oldChildWidget);
  159. widgetToCaption.remove(oldChildWidget);
  160. }
  161. public void updateCaption(Widget widget, AbstractComponentState state,
  162. boolean enabled) {
  163. final Caption c = widgetToCaption.get(widget);
  164. if (c != null) {
  165. c.updateCaption(state, enabled);
  166. }
  167. }
  168. public void updateError(Widget widget, String errorMessage,
  169. boolean hideErrors) {
  170. final ErrorFlag e = widgetToError.get(widget);
  171. if (e != null) {
  172. e.updateError(errorMessage, hideErrors);
  173. }
  174. }
  175. }
  176. // TODO why duplicated here?
  177. public class Caption extends HTML {
  178. public static final String CLASSNAME = "v-caption";
  179. private final ComponentConnector owner;
  180. private Element requiredFieldIndicator;
  181. private Icon icon;
  182. private Element captionContent;
  183. /**
  184. *
  185. * @param component
  186. * optional owner of caption. If not set, getOwner will
  187. * return null
  188. */
  189. public Caption(ComponentConnector component) {
  190. super();
  191. owner = component;
  192. }
  193. private void setStyles(String[] styles) {
  194. String styleName = CLASSNAME;
  195. if (styles != null) {
  196. for (String style : styles) {
  197. if (StyleConstants.DISABLED.equals(style)) {
  198. // Add v-disabled also without classname prefix so
  199. // generic v-disabled CSS rules work
  200. styleName += " " + style;
  201. }
  202. styleName += " " + CLASSNAME + "-" + style;
  203. }
  204. }
  205. setStyleName(styleName);
  206. }
  207. public void updateCaption(AbstractComponentState state,
  208. boolean enabled) {
  209. // Update styles as they might have changed when the caption changed
  210. setStyles(getStylesFromState(state, enabled));
  211. boolean isEmpty = true;
  212. if (icon != null) {
  213. getElement().removeChild(icon.getElement());
  214. icon = null;
  215. }
  216. if (state.resources.containsKey(ComponentConstants.ICON_RESOURCE)) {
  217. icon = owner.getConnection().getIcon(state.resources
  218. .get(ComponentConstants.ICON_RESOURCE).getURL());
  219. DOM.insertChild(getElement(), icon.getElement(), 0);
  220. isEmpty = false;
  221. }
  222. if (state.caption != null) {
  223. if (captionContent == null) {
  224. captionContent = DOM.createSpan();
  225. AriaHelper.bindCaption(owner.getWidget(), captionContent);
  226. DOM.insertChild(getElement(), captionContent,
  227. icon == null ? 0 : 1);
  228. }
  229. String c = state.caption;
  230. if (c == null) {
  231. c = "";
  232. } else {
  233. isEmpty = false;
  234. }
  235. if (state.captionAsHtml) {
  236. captionContent.setInnerHTML(c);
  237. } else {
  238. captionContent.setInnerText(c);
  239. }
  240. } else {
  241. // TODO should span also be removed
  242. }
  243. if (state.description != null && captionContent != null) {
  244. addStyleDependentName("hasdescription");
  245. } else {
  246. removeStyleDependentName("hasdescription");
  247. }
  248. boolean required = owner instanceof HasRequiredIndicator
  249. && ((HasRequiredIndicator) owner)
  250. .isRequiredIndicatorVisible();
  251. AriaHelper.handleInputRequired(owner.getWidget(), required);
  252. if (required) {
  253. if (requiredFieldIndicator == null) {
  254. requiredFieldIndicator = DOM.createSpan();
  255. DOM.setInnerText(requiredFieldIndicator, "*");
  256. DOM.setElementProperty(requiredFieldIndicator, "className",
  257. "v-required-field-indicator");
  258. DOM.appendChild(getElement(), requiredFieldIndicator);
  259. // Hide the required indicator from screen reader, as this
  260. // information is set directly at the input field
  261. Roles.getTextboxRole()
  262. .setAriaHiddenState(requiredFieldIndicator, true);
  263. }
  264. } else {
  265. if (requiredFieldIndicator != null) {
  266. DOM.removeChild(getElement(), requiredFieldIndicator);
  267. requiredFieldIndicator = null;
  268. }
  269. }
  270. }
  271. /**
  272. * Returns Paintable for which this Caption belongs to.
  273. *
  274. * @return owner Widget
  275. */
  276. public ComponentConnector getOwner() {
  277. return owner;
  278. }
  279. }
  280. /** For internal use only. May be removed or replaced in the future. */
  281. public class ErrorFlag extends HTML {
  282. private static final String CLASSNAME = VFormLayout.CLASSNAME
  283. + "-error-indicator";
  284. Element errorIndicatorElement;
  285. private ComponentConnector owner;
  286. public ErrorFlag(ComponentConnector owner) {
  287. setStyleName(CLASSNAME);
  288. if (!BrowserInfo.get().isTouchDevice()) {
  289. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  290. }
  291. this.owner = owner;
  292. }
  293. public ComponentConnector getOwner() {
  294. return owner;
  295. }
  296. public void updateError(String errorMessage, boolean hideErrors) {
  297. boolean showError = null != errorMessage;
  298. if (hideErrors) {
  299. showError = false;
  300. }
  301. AriaHelper.handleInputInvalid(owner.getWidget(), showError);
  302. if (showError) {
  303. if (errorIndicatorElement == null) {
  304. errorIndicatorElement = DOM.createDiv();
  305. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  306. DOM.setElementProperty(errorIndicatorElement, "className",
  307. "v-errorindicator");
  308. DOM.appendChild(getElement(), errorIndicatorElement);
  309. // Hide the error indicator from screen reader, as this
  310. // information is set directly at the input field
  311. Roles.getFormRole()
  312. .setAriaHiddenState(errorIndicatorElement, true);
  313. }
  314. } else if (errorIndicatorElement != null) {
  315. DOM.removeChild(getElement(), errorIndicatorElement);
  316. errorIndicatorElement = null;
  317. }
  318. }
  319. }
  320. }