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