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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 java.util.Set;
  10. import com.google.gwt.event.dom.client.ClickEvent;
  11. import com.google.gwt.event.dom.client.ClickHandler;
  12. import com.google.gwt.user.client.DOM;
  13. import com.google.gwt.user.client.Element;
  14. import com.google.gwt.user.client.Event;
  15. import com.google.gwt.user.client.ui.FlexTable;
  16. import com.google.gwt.user.client.ui.HTML;
  17. import com.google.gwt.user.client.ui.SimplePanel;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  20. import com.vaadin.terminal.gwt.client.BrowserInfo;
  21. import com.vaadin.terminal.gwt.client.ComponentState;
  22. import com.vaadin.terminal.gwt.client.Container;
  23. import com.vaadin.terminal.gwt.client.Focusable;
  24. import com.vaadin.terminal.gwt.client.RenderSpace;
  25. import com.vaadin.terminal.gwt.client.StyleConstants;
  26. import com.vaadin.terminal.gwt.client.UIDL;
  27. import com.vaadin.terminal.gwt.client.Util;
  28. import com.vaadin.terminal.gwt.client.VPaintableMap;
  29. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  30. import com.vaadin.terminal.gwt.client.VTooltip;
  31. /**
  32. * Two col Layout that places caption on left col and field on right col
  33. */
  34. public class VFormLayout extends SimplePanel implements Container {
  35. private final static String CLASSNAME = "v-formlayout";
  36. ApplicationConnection client;
  37. VFormLayoutTable table;
  38. private String width = "";
  39. private String height = "";
  40. boolean rendering = false;
  41. public VFormLayout() {
  42. super();
  43. setStyleName(CLASSNAME);
  44. table = new VFormLayoutTable();
  45. setWidget(table);
  46. }
  47. /**
  48. * Parses the stylenames from shared state
  49. *
  50. * @param state
  51. * shared state of the component
  52. * @return An array of stylenames
  53. */
  54. private String[] getStylesFromState(ComponentState state) {
  55. List<String> styles = new ArrayList<String>();
  56. if (state.hasStyles()) {
  57. String[] stylesnames = state.getStyle().split(" ");
  58. for (String name : stylesnames) {
  59. styles.add(name);
  60. }
  61. }
  62. if (state.isDisabled()) {
  63. styles.add(ApplicationConnection.DISABLED_CLASSNAME);
  64. }
  65. return styles.toArray(new String[styles.size()]);
  66. }
  67. public class VFormLayoutTable extends FlexTable implements ClickHandler {
  68. private static final int COLUMN_CAPTION = 0;
  69. private static final int COLUMN_ERRORFLAG = 1;
  70. private static final int COLUMN_WIDGET = 2;
  71. private HashMap<Widget, Caption> widgetToCaption = new HashMap<Widget, Caption>();
  72. private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<Widget, ErrorFlag>();
  73. public VFormLayoutTable() {
  74. DOM.setElementProperty(getElement(), "cellPadding", "0");
  75. DOM.setElementProperty(getElement(), "cellSpacing", "0");
  76. }
  77. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  78. final VMarginInfo margins = new VMarginInfo(
  79. uidl.getIntAttribute("margins"));
  80. Element margin = getElement();
  81. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  82. margins.hasTop());
  83. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  84. margins.hasRight());
  85. setStyleName(margin,
  86. CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  87. margins.hasBottom());
  88. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  89. margins.hasLeft());
  90. setStyleName(margin, CLASSNAME + "-" + "spacing",
  91. uidl.hasAttribute("spacing"));
  92. int i = 0;
  93. for (final Iterator<?> it = uidl.getChildIterator(); it.hasNext(); i++) {
  94. prepareCell(i, 1);
  95. final UIDL childUidl = (UIDL) it.next();
  96. final VPaintableWidget childPaintable = client
  97. .getPaintable(childUidl);
  98. Widget childWidget = childPaintable.getWidgetForPaintable();
  99. Caption caption = widgetToCaption.get(childWidget);
  100. if (caption == null) {
  101. caption = new Caption(childPaintable, client);
  102. caption.addClickHandler(this);
  103. widgetToCaption.put(childWidget, caption);
  104. }
  105. ErrorFlag error = widgetToError.get(childWidget);
  106. if (error == null) {
  107. error = new ErrorFlag();
  108. widgetToError.put(childWidget, error);
  109. }
  110. prepareCell(i, COLUMN_WIDGET);
  111. Widget oldWidget = getWidget(i, COLUMN_WIDGET);
  112. if (oldWidget == null) {
  113. setWidget(i, COLUMN_WIDGET, childWidget);
  114. } else if (oldWidget != childWidget) {
  115. final VPaintableWidget oldPaintable = VPaintableMap.get(
  116. client).getPaintable(oldWidget);
  117. client.unregisterPaintable(oldPaintable);
  118. setWidget(i, COLUMN_WIDGET, childWidget);
  119. }
  120. getCellFormatter().setStyleName(i, COLUMN_WIDGET,
  121. CLASSNAME + "-contentcell");
  122. getCellFormatter().setStyleName(i, COLUMN_CAPTION,
  123. CLASSNAME + "-captioncell");
  124. setWidget(i, COLUMN_CAPTION, caption);
  125. setContentWidth(i);
  126. getCellFormatter().setStyleName(i, COLUMN_ERRORFLAG,
  127. CLASSNAME + "-errorcell");
  128. setWidget(i, COLUMN_ERRORFLAG, error);
  129. childPaintable.updateFromUIDL(childUidl, client);
  130. String rowstyles = CLASSNAME + "-row";
  131. if (i == 0) {
  132. rowstyles += " " + CLASSNAME + "-firstrow";
  133. }
  134. if (!it.hasNext()) {
  135. rowstyles += " " + CLASSNAME + "-lastrow";
  136. }
  137. getRowFormatter().setStyleName(i, rowstyles);
  138. }
  139. while (getRowCount() > i) {
  140. Widget w = getWidget(i, COLUMN_WIDGET);
  141. final VPaintableWidget p = VPaintableMap.get(client)
  142. .getPaintable(w);
  143. client.unregisterPaintable(p);
  144. widgetToCaption.remove(w);
  145. removeRow(i);
  146. }
  147. /*
  148. * Must update relative sized fields last when it is clear how much
  149. * space they are allowed to use
  150. */
  151. for (Widget p : widgetToCaption.keySet()) {
  152. client.handleComponentRelativeSize(p);
  153. }
  154. }
  155. public void setContentWidths() {
  156. for (int row = 0; row < getRowCount(); row++) {
  157. setContentWidth(row);
  158. }
  159. }
  160. private void setContentWidth(int row) {
  161. String width = "";
  162. if (!isDynamicWidth()) {
  163. width = "100%";
  164. }
  165. getCellFormatter().setWidth(row, COLUMN_WIDGET, width);
  166. }
  167. public void replaceChildComponent(Widget oldComponent,
  168. Widget newComponent) {
  169. int i;
  170. for (i = 0; i < getRowCount(); i++) {
  171. Widget candidate = getWidget(i, COLUMN_WIDGET);
  172. if (oldComponent == candidate) {
  173. VPaintableMap paintableMap = VPaintableMap.get(client);
  174. VPaintableWidget oldPaintable = paintableMap
  175. .getPaintable(oldComponent);
  176. VPaintableWidget newPaintable = paintableMap
  177. .getPaintable(newComponent);
  178. Caption oldCap = widgetToCaption.get(oldComponent);
  179. final Caption newCap = new Caption(newPaintable, client);
  180. newCap.addClickHandler(this);
  181. newCap.setStyleName(oldCap.getStyleName());
  182. widgetToCaption.put(newComponent, newCap);
  183. ErrorFlag error = widgetToError.get(newComponent);
  184. if (error == null) {
  185. error = new ErrorFlag();
  186. widgetToError.put(newComponent, error);
  187. }
  188. setWidget(i, COLUMN_CAPTION, newCap);
  189. setWidget(i, COLUMN_ERRORFLAG, error);
  190. setWidget(i, COLUMN_WIDGET, newComponent);
  191. break;
  192. }
  193. }
  194. }
  195. public boolean hasChildComponent(Widget component) {
  196. return widgetToCaption.containsKey(component);
  197. }
  198. public void updateCaption(VPaintableWidget paintable, UIDL uidl) {
  199. final Caption c = widgetToCaption.get(paintable
  200. .getWidgetForPaintable());
  201. if (c != null) {
  202. c.updateCaption(uidl, paintable.getState());
  203. }
  204. final ErrorFlag e = widgetToError.get(paintable
  205. .getWidgetForPaintable());
  206. if (e != null) {
  207. e.updateFromUIDL(uidl, paintable);
  208. }
  209. }
  210. public int getAllocatedWidth(Widget child, int availableWidth) {
  211. Caption caption = widgetToCaption.get(child);
  212. ErrorFlag error = widgetToError.get(child);
  213. int width = availableWidth;
  214. if (caption != null) {
  215. width -= DOM.getParent(caption.getElement()).getOffsetWidth();
  216. }
  217. if (error != null) {
  218. width -= DOM.getParent(error.getElement()).getOffsetWidth();
  219. }
  220. return width;
  221. }
  222. /*
  223. * (non-Javadoc)
  224. *
  225. * @see
  226. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  227. * .event.dom.client.ClickEvent)
  228. */
  229. public void onClick(ClickEvent event) {
  230. Caption caption = (Caption) event.getSource();
  231. if (caption.getOwner() != null) {
  232. if (caption.getOwner() instanceof Focusable) {
  233. ((Focusable) caption.getOwner()).focus();
  234. } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  235. ((com.google.gwt.user.client.ui.Focusable) caption
  236. .getOwner()).setFocus(true);
  237. }
  238. }
  239. }
  240. }
  241. public boolean isDynamicWidth() {
  242. return width.equals("");
  243. }
  244. public boolean hasChildComponent(Widget component) {
  245. return table.hasChildComponent(component);
  246. }
  247. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  248. table.replaceChildComponent(oldComponent, newComponent);
  249. }
  250. // TODO why duplicated here?
  251. public class Caption extends HTML {
  252. public static final String CLASSNAME = "v-caption";
  253. private final VPaintableWidget owner;
  254. private Element requiredFieldIndicator;
  255. private Icon icon;
  256. private Element captionText;
  257. private final ApplicationConnection client;
  258. /**
  259. *
  260. * @param component
  261. * optional owner of caption. If not set, getOwner will
  262. * return null
  263. * @param client
  264. */
  265. public Caption(VPaintableWidget component, ApplicationConnection client) {
  266. super();
  267. this.client = client;
  268. owner = component;
  269. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  270. }
  271. private void setStyles(String[] styles) {
  272. String styleName = CLASSNAME;
  273. if (styles != null) {
  274. for (String style : styles) {
  275. if (ApplicationConnection.DISABLED_CLASSNAME.equals(style)) {
  276. // Add v-disabled also without classname prefix so
  277. // generic v-disabled CSS rules work
  278. styleName += " " + style;
  279. }
  280. styleName += " " + CLASSNAME + "-" + style;
  281. }
  282. }
  283. setStyleName(styleName);
  284. }
  285. public void updateCaption(UIDL uidl, ComponentState state) {
  286. setVisible(!uidl.getBooleanAttribute("invisible"));
  287. // Update styles as they might have changed when the caption changed
  288. setStyles(getStylesFromState(state));
  289. boolean isEmpty = true;
  290. if (uidl.hasAttribute(VAbstractPaintableWidget.ATTRIBUTE_ICON)) {
  291. if (icon == null) {
  292. icon = new Icon(client);
  293. DOM.insertChild(getElement(), icon.getElement(), 0);
  294. }
  295. icon.setUri(uidl
  296. .getStringAttribute(VAbstractPaintableWidget.ATTRIBUTE_ICON));
  297. isEmpty = false;
  298. } else {
  299. if (icon != null) {
  300. DOM.removeChild(getElement(), icon.getElement());
  301. icon = null;
  302. }
  303. }
  304. if (state.getCaption() != null) {
  305. if (captionText == null) {
  306. captionText = DOM.createSpan();
  307. DOM.insertChild(getElement(), captionText, icon == null ? 0
  308. : 1);
  309. }
  310. String c = state.getCaption();
  311. if (c == null) {
  312. c = "";
  313. } else {
  314. isEmpty = false;
  315. }
  316. DOM.setInnerText(captionText, c);
  317. } else {
  318. // TODO should span also be removed
  319. }
  320. if (state.hasDescription() && captionText != null) {
  321. addStyleDependentName("hasdescription");
  322. } else {
  323. removeStyleDependentName("hasdescription");
  324. }
  325. if (uidl.getBooleanAttribute(VAbstractPaintableWidget.ATTRIBUTE_REQUIRED)) {
  326. if (requiredFieldIndicator == null) {
  327. requiredFieldIndicator = DOM.createSpan();
  328. DOM.setInnerText(requiredFieldIndicator, "*");
  329. DOM.setElementProperty(requiredFieldIndicator, "className",
  330. "v-required-field-indicator");
  331. DOM.appendChild(getElement(), requiredFieldIndicator);
  332. }
  333. } else {
  334. if (requiredFieldIndicator != null) {
  335. DOM.removeChild(getElement(), requiredFieldIndicator);
  336. requiredFieldIndicator = null;
  337. }
  338. }
  339. // Workaround for IE weirdness, sometimes returns bad height in some
  340. // circumstances when Caption is empty. See #1444
  341. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  342. // FIXME: This could be unnecessary for IE8+
  343. if (BrowserInfo.get().isIE()) {
  344. if (isEmpty) {
  345. setHeight("0px");
  346. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  347. } else {
  348. setHeight("");
  349. DOM.setStyleAttribute(getElement(), "overflow", "");
  350. }
  351. }
  352. }
  353. /**
  354. * Returns Paintable for which this Caption belongs to.
  355. *
  356. * @return owner Widget
  357. */
  358. public VPaintableWidget getOwner() {
  359. return owner;
  360. }
  361. @Override
  362. public void onBrowserEvent(Event event) {
  363. super.onBrowserEvent(event);
  364. if (client != null) {
  365. client.handleTooltipEvent(event, owner);
  366. }
  367. }
  368. }
  369. private class ErrorFlag extends HTML {
  370. private static final String CLASSNAME = VFormLayout.CLASSNAME
  371. + "-error-indicator";
  372. Element errorIndicatorElement;
  373. private VPaintableWidget owner;
  374. public ErrorFlag() {
  375. setStyleName(CLASSNAME);
  376. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  377. }
  378. public void updateFromUIDL(UIDL uidl, VPaintableWidget component) {
  379. owner = component;
  380. if (uidl.hasAttribute("error")
  381. && !uidl.getBooleanAttribute(VAbstractPaintableWidget.ATTRIBUTE_HIDEERRORS)) {
  382. if (errorIndicatorElement == null) {
  383. errorIndicatorElement = DOM.createDiv();
  384. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  385. DOM.setElementProperty(errorIndicatorElement, "className",
  386. "v-errorindicator");
  387. DOM.appendChild(getElement(), errorIndicatorElement);
  388. }
  389. } else if (errorIndicatorElement != null) {
  390. DOM.removeChild(getElement(), errorIndicatorElement);
  391. errorIndicatorElement = null;
  392. }
  393. }
  394. @Override
  395. public void onBrowserEvent(Event event) {
  396. super.onBrowserEvent(event);
  397. if (owner != null) {
  398. client.handleTooltipEvent(event, owner);
  399. }
  400. }
  401. }
  402. public boolean requestLayout(Set<Widget> children) {
  403. if (height.equals("") || width.equals("")) {
  404. // A dynamic size might change due to children changes
  405. return false;
  406. }
  407. return true;
  408. }
  409. public RenderSpace getAllocatedSpace(Widget child) {
  410. int width = 0;
  411. int height = 0;
  412. if (!this.width.equals("")) {
  413. int availableWidth = getOffsetWidth();
  414. width = table.getAllocatedWidth(child, availableWidth);
  415. }
  416. return new RenderSpace(width, height, false);
  417. }
  418. @Override
  419. public void setHeight(String height) {
  420. if (this.height.equals(height)) {
  421. return;
  422. }
  423. this.height = height;
  424. super.setHeight(height);
  425. }
  426. @Override
  427. public void setWidth(String width) {
  428. if (this.width.equals(width)) {
  429. return;
  430. }
  431. this.width = width;
  432. super.setWidth(width);
  433. if (!rendering) {
  434. table.setContentWidths();
  435. if (height.equals("")) {
  436. // Width might affect height
  437. Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, this,
  438. this);
  439. }
  440. }
  441. }
  442. }