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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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.Container;
  22. import com.vaadin.terminal.gwt.client.Focusable;
  23. import com.vaadin.terminal.gwt.client.RenderSpace;
  24. import com.vaadin.terminal.gwt.client.StyleConstants;
  25. import com.vaadin.terminal.gwt.client.UIDL;
  26. import com.vaadin.terminal.gwt.client.Util;
  27. import com.vaadin.terminal.gwt.client.VPaintableMap;
  28. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  29. import com.vaadin.terminal.gwt.client.VTooltip;
  30. /**
  31. * Two col Layout that places caption on left col and field on right col
  32. */
  33. public class VFormLayout extends SimplePanel implements Container {
  34. private final static String CLASSNAME = "v-formlayout";
  35. private ApplicationConnection client;
  36. private VFormLayoutTable table;
  37. private String width = "";
  38. private String height = "";
  39. private boolean rendering = false;
  40. public VFormLayout() {
  41. super();
  42. setStyleName(CLASSNAME);
  43. table = new VFormLayoutTable();
  44. setWidget(table);
  45. }
  46. /**
  47. * Parses the stylenames from an uidl
  48. *
  49. * @param uidl
  50. * The uidl to get the stylenames from
  51. * @return An array of stylenames
  52. */
  53. private String[] getStylesFromUIDL(UIDL uidl) {
  54. List<String> styles = new ArrayList<String>();
  55. if (uidl.hasAttribute("style")) {
  56. String[] stylesnames = uidl.getStringAttribute("style").split(" ");
  57. for (String name : stylesnames) {
  58. styles.add(name);
  59. }
  60. }
  61. if (uidl.hasAttribute("disabled")) {
  62. styles.add(ApplicationConnection.DISABLED_CLASSNAME);
  63. }
  64. return styles.toArray(new String[styles.size()]);
  65. }
  66. public class VFormLayoutTable extends FlexTable implements ClickHandler {
  67. private static final int COLUMN_CAPTION = 0;
  68. private static final int COLUMN_ERRORFLAG = 1;
  69. private static final int COLUMN_WIDGET = 2;
  70. private HashMap<Widget, Caption> widgetToCaption = new HashMap<Widget, Caption>();
  71. private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<Widget, ErrorFlag>();
  72. public VFormLayoutTable() {
  73. DOM.setElementProperty(getElement(), "cellPadding", "0");
  74. DOM.setElementProperty(getElement(), "cellSpacing", "0");
  75. }
  76. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  77. final VMarginInfo margins = new VMarginInfo(
  78. uidl.getIntAttribute("margins"));
  79. Element margin = getElement();
  80. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
  81. margins.hasTop());
  82. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT,
  83. margins.hasRight());
  84. setStyleName(margin,
  85. CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM,
  86. margins.hasBottom());
  87. setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
  88. margins.hasLeft());
  89. setStyleName(margin, CLASSNAME + "-" + "spacing",
  90. uidl.hasAttribute("spacing"));
  91. int i = 0;
  92. for (final Iterator<?> it = uidl.getChildIterator(); it.hasNext(); i++) {
  93. prepareCell(i, 1);
  94. final UIDL childUidl = (UIDL) it.next();
  95. final VPaintableWidget childPaintable = client
  96. .getPaintable(childUidl);
  97. Widget childWidget = childPaintable.getWidgetForPaintable();
  98. Caption caption = widgetToCaption.get(childWidget);
  99. if (caption == null) {
  100. caption = new Caption(childPaintable, client,
  101. getStylesFromUIDL(childUidl));
  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. null);
  181. newCap.addClickHandler(this);
  182. newCap.setStyleName(oldCap.getStyleName());
  183. widgetToCaption.put(newComponent, newCap);
  184. ErrorFlag error = widgetToError.get(newComponent);
  185. if (error == null) {
  186. error = new ErrorFlag();
  187. widgetToError.put(newComponent, error);
  188. }
  189. setWidget(i, COLUMN_CAPTION, newCap);
  190. setWidget(i, COLUMN_ERRORFLAG, error);
  191. setWidget(i, COLUMN_WIDGET, newComponent);
  192. break;
  193. }
  194. }
  195. }
  196. public boolean hasChildComponent(Widget component) {
  197. return widgetToCaption.containsKey(component);
  198. }
  199. public void updateCaption(VPaintableWidget paintable, UIDL uidl) {
  200. final Caption c = widgetToCaption.get(paintable
  201. .getWidgetForPaintable());
  202. if (c != null) {
  203. c.updateCaption(uidl);
  204. }
  205. final ErrorFlag e = widgetToError.get(paintable
  206. .getWidgetForPaintable());
  207. if (e != null) {
  208. e.updateFromUIDL(uidl, paintable);
  209. }
  210. }
  211. public int getAllocatedWidth(Widget child, int availableWidth) {
  212. Caption caption = widgetToCaption.get(child);
  213. ErrorFlag error = widgetToError.get(child);
  214. int width = availableWidth;
  215. if (caption != null) {
  216. width -= DOM.getParent(caption.getElement()).getOffsetWidth();
  217. }
  218. if (error != null) {
  219. width -= DOM.getParent(error.getElement()).getOffsetWidth();
  220. }
  221. return width;
  222. }
  223. /*
  224. * (non-Javadoc)
  225. *
  226. * @see
  227. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  228. * .event.dom.client.ClickEvent)
  229. */
  230. public void onClick(ClickEvent event) {
  231. Caption caption = (Caption) event.getSource();
  232. if (caption.getOwner() != null) {
  233. if (caption.getOwner() instanceof Focusable) {
  234. ((Focusable) caption.getOwner()).focus();
  235. } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
  236. ((com.google.gwt.user.client.ui.Focusable) caption
  237. .getOwner()).setFocus(true);
  238. }
  239. }
  240. }
  241. }
  242. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  243. rendering = true;
  244. this.client = client;
  245. if (client.updateComponent(this, uidl, true)) {
  246. rendering = false;
  247. return;
  248. }
  249. table.updateFromUIDL(uidl, client);
  250. rendering = false;
  251. }
  252. public boolean isDynamicWidth() {
  253. return width.equals("");
  254. }
  255. public boolean hasChildComponent(Widget component) {
  256. return table.hasChildComponent(component);
  257. }
  258. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  259. table.replaceChildComponent(oldComponent, newComponent);
  260. }
  261. public void updateCaption(VPaintableWidget component, UIDL uidl) {
  262. table.updateCaption(component, uidl);
  263. }
  264. public class Caption extends HTML {
  265. public static final String CLASSNAME = "v-caption";
  266. private final VPaintableWidget owner;
  267. private Element requiredFieldIndicator;
  268. private Icon icon;
  269. private Element captionText;
  270. private final ApplicationConnection client;
  271. /**
  272. *
  273. * @param component
  274. * optional owner of caption. If not set, getOwner will
  275. * return null
  276. * @param client
  277. */
  278. public Caption(VPaintableWidget component,
  279. ApplicationConnection client, String[] styles) {
  280. super();
  281. this.client = client;
  282. owner = component;
  283. String style = CLASSNAME;
  284. if (styles != null) {
  285. for (int i = 0; i < styles.length; i++) {
  286. style += " " + CLASSNAME + "-" + styles[i];
  287. }
  288. }
  289. setStyleName(style);
  290. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  291. }
  292. public void updateCaption(UIDL uidl) {
  293. setVisible(!uidl.getBooleanAttribute("invisible"));
  294. setStyleName(getElement(),
  295. ApplicationConnection.DISABLED_CLASSNAME,
  296. uidl.hasAttribute("disabled"));
  297. boolean isEmpty = true;
  298. if (uidl.hasAttribute("icon")) {
  299. if (icon == null) {
  300. icon = new Icon(client);
  301. DOM.insertChild(getElement(), icon.getElement(), 0);
  302. }
  303. icon.setUri(uidl.getStringAttribute("icon"));
  304. isEmpty = false;
  305. } else {
  306. if (icon != null) {
  307. DOM.removeChild(getElement(), icon.getElement());
  308. icon = null;
  309. }
  310. }
  311. if (uidl.hasAttribute("caption")) {
  312. if (captionText == null) {
  313. captionText = DOM.createSpan();
  314. DOM.insertChild(getElement(), captionText, icon == null ? 0
  315. : 1);
  316. }
  317. String c = uidl.getStringAttribute("caption");
  318. if (c == null) {
  319. c = "";
  320. } else {
  321. isEmpty = false;
  322. }
  323. DOM.setInnerText(captionText, c);
  324. } else {
  325. // TODO should span also be removed
  326. }
  327. if (uidl.hasAttribute("description")) {
  328. if (captionText != null) {
  329. addStyleDependentName("hasdescription");
  330. } else {
  331. removeStyleDependentName("hasdescription");
  332. }
  333. }
  334. if (uidl.getBooleanAttribute("required")) {
  335. if (requiredFieldIndicator == null) {
  336. requiredFieldIndicator = DOM.createSpan();
  337. DOM.setInnerText(requiredFieldIndicator, "*");
  338. DOM.setElementProperty(requiredFieldIndicator, "className",
  339. "v-required-field-indicator");
  340. DOM.appendChild(getElement(), requiredFieldIndicator);
  341. }
  342. } else {
  343. if (requiredFieldIndicator != null) {
  344. DOM.removeChild(getElement(), requiredFieldIndicator);
  345. requiredFieldIndicator = null;
  346. }
  347. }
  348. // Workaround for IE weirdness, sometimes returns bad height in some
  349. // circumstances when Caption is empty. See #1444
  350. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  351. // FIXME: This could be unnecessary for IE8+
  352. if (BrowserInfo.get().isIE()) {
  353. if (isEmpty) {
  354. setHeight("0px");
  355. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  356. } else {
  357. setHeight("");
  358. DOM.setStyleAttribute(getElement(), "overflow", "");
  359. }
  360. }
  361. }
  362. /**
  363. * Returns Paintable for which this Caption belongs to.
  364. *
  365. * @return owner Widget
  366. */
  367. public VPaintableWidget getOwner() {
  368. return owner;
  369. }
  370. @Override
  371. public void onBrowserEvent(Event event) {
  372. super.onBrowserEvent(event);
  373. if (client != null) {
  374. client.handleTooltipEvent(event, owner);
  375. }
  376. }
  377. }
  378. private class ErrorFlag extends HTML {
  379. private static final String CLASSNAME = VFormLayout.CLASSNAME
  380. + "-error-indicator";
  381. Element errorIndicatorElement;
  382. private VPaintableWidget owner;
  383. public ErrorFlag() {
  384. setStyleName(CLASSNAME);
  385. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  386. }
  387. public void updateFromUIDL(UIDL uidl, VPaintableWidget component) {
  388. owner = component;
  389. if (uidl.hasAttribute("error")
  390. && !uidl.getBooleanAttribute("hideErrors")) {
  391. if (errorIndicatorElement == null) {
  392. errorIndicatorElement = DOM.createDiv();
  393. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  394. DOM.setElementProperty(errorIndicatorElement, "className",
  395. "v-errorindicator");
  396. DOM.appendChild(getElement(), errorIndicatorElement);
  397. }
  398. } else if (errorIndicatorElement != null) {
  399. DOM.removeChild(getElement(), errorIndicatorElement);
  400. errorIndicatorElement = null;
  401. }
  402. }
  403. @Override
  404. public void onBrowserEvent(Event event) {
  405. super.onBrowserEvent(event);
  406. if (owner != null) {
  407. client.handleTooltipEvent(event, owner);
  408. }
  409. }
  410. }
  411. public boolean requestLayout(Set<Widget> children) {
  412. if (height.equals("") || width.equals("")) {
  413. // A dynamic size might change due to children changes
  414. return false;
  415. }
  416. return true;
  417. }
  418. public RenderSpace getAllocatedSpace(Widget child) {
  419. int width = 0;
  420. int height = 0;
  421. if (!this.width.equals("")) {
  422. int availableWidth = getOffsetWidth();
  423. width = table.getAllocatedWidth(child, availableWidth);
  424. }
  425. return new RenderSpace(width, height, false);
  426. }
  427. @Override
  428. public void setHeight(String height) {
  429. if (this.height.equals(height)) {
  430. return;
  431. }
  432. this.height = height;
  433. super.setHeight(height);
  434. }
  435. @Override
  436. public void setWidth(String width) {
  437. if (this.width.equals(width)) {
  438. return;
  439. }
  440. this.width = width;
  441. super.setWidth(width);
  442. if (!rendering) {
  443. table.setContentWidths();
  444. if (height.equals("")) {
  445. // Width might affect height
  446. Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, this,
  447. this);
  448. }
  449. }
  450. }
  451. public Widget getWidgetForPaintable() {
  452. return this;
  453. }
  454. }