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.

Ticket1710.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package com.vaadin.tests.tickets;
  2. import java.util.Iterator;
  3. import com.vaadin.data.Property;
  4. import com.vaadin.data.Property.ValueChangeEvent;
  5. import com.vaadin.data.util.MethodProperty;
  6. import com.vaadin.shared.ui.AlignmentInfo.Bits;
  7. import com.vaadin.shared.ui.MarginInfo;
  8. import com.vaadin.terminal.SystemError;
  9. import com.vaadin.terminal.ThemeResource;
  10. import com.vaadin.ui.AbstractComponent;
  11. import com.vaadin.ui.Alignment;
  12. import com.vaadin.ui.Button;
  13. import com.vaadin.ui.CheckBox;
  14. import com.vaadin.ui.Component;
  15. import com.vaadin.ui.ComponentContainer;
  16. import com.vaadin.ui.DateField;
  17. import com.vaadin.ui.Form;
  18. import com.vaadin.ui.GridLayout;
  19. import com.vaadin.ui.HorizontalLayout;
  20. import com.vaadin.ui.Label;
  21. import com.vaadin.ui.Layout;
  22. import com.vaadin.ui.Layout.AlignmentHandler;
  23. import com.vaadin.ui.NativeSelect;
  24. import com.vaadin.ui.Panel;
  25. import com.vaadin.ui.Root.LegacyWindow;
  26. import com.vaadin.ui.TextField;
  27. import com.vaadin.ui.VerticalLayout;
  28. public class Ticket1710 extends com.vaadin.Application.LegacyApplication {
  29. @Override
  30. public void init() {
  31. setTheme("tests-tickets");
  32. VerticalLayout lo = new VerticalLayout();
  33. setMainWindow(new LegacyWindow("#1710", lo));
  34. lo.setMargin(true);
  35. lo.setSpacing(true);
  36. lo.setWidth("100%");
  37. // Hiding controls
  38. HorizontalLayout hidingControls = new HorizontalLayout();
  39. lo.addComponent(hidingControls);
  40. // OrderedLayout
  41. final VerticalLayout orderedLayout = new VerticalLayout();
  42. LayoutTestingPanel oltp = new LayoutTestingPanel("OrderedLayout",
  43. orderedLayout);
  44. CheckBox cb = new CheckBox("OrderedLayout",
  45. new MethodProperty<Boolean>(oltp, "visible"));
  46. cb.setImmediate(true);
  47. hidingControls.addComponent(cb);
  48. lo.addComponent(oltp);
  49. orderedLayout.setSpacing(false);
  50. addFields(orderedLayout);
  51. // GridLayout
  52. GridLayout grid = new GridLayout(1, 1);
  53. Panel g1tp = new LayoutTestingPanel("Gridlayout with 1 column", grid);
  54. cb = new CheckBox("GridLayout (1col)", new MethodProperty<Boolean>(
  55. g1tp, "visible"));
  56. cb.setImmediate(true);
  57. hidingControls.addComponent(cb);
  58. g1tp.setVisible(false);
  59. lo.addComponent(g1tp);
  60. grid.setSpacing(true);
  61. addFields(grid);
  62. GridLayout grid2 = new GridLayout(2, 1);
  63. Panel g2tp = new LayoutTestingPanel("Gridlayout with 2 columns", grid2);
  64. cb = new CheckBox("GridLayout (2cols)", new MethodProperty<Boolean>(
  65. g2tp, "visible"));
  66. cb.setImmediate(true);
  67. hidingControls.addComponent(cb);
  68. g2tp.setVisible(false);
  69. lo.addComponent(g2tp);
  70. grid2.setSpacing(true);
  71. addFields(grid2);
  72. // ExpandLayout
  73. VerticalLayout el = new VerticalLayout();
  74. Panel elp = new LayoutTestingPanel(
  75. "ExpandLayout width first component expanded", el);
  76. cb = new CheckBox("ExpandLayout (vertical)",
  77. new MethodProperty<Boolean>(elp, "visible"));
  78. cb.setImmediate(true);
  79. hidingControls.addComponent(cb);
  80. elp.setVisible(false);
  81. el.setHeight("700px");
  82. addFields(el);
  83. Component firstComponent = el.getComponentIterator().next();
  84. firstComponent.setSizeFull();
  85. el.setExpandRatio(firstComponent, 1);
  86. lo.addComponent(elp);
  87. HorizontalLayout elh = new HorizontalLayout();
  88. Panel elhp = new LayoutTestingPanel(
  89. "ExpandLayout width first component expanded; horizontal", elh);
  90. cb = new CheckBox("ExpandLayout (horizontal)",
  91. new MethodProperty<Boolean>(elhp, "visible"));
  92. cb.setImmediate(true);
  93. hidingControls.addComponent(cb);
  94. elhp.setVisible(false);
  95. elh.setWidth("2000px");
  96. elh.setHeight("100px");
  97. addFields(elh);
  98. Component firstComponentElh = elh.getComponentIterator().next();
  99. firstComponentElh.setSizeFull();
  100. elh.setExpandRatio(firstComponentElh, 1);
  101. lo.addComponent(elhp);
  102. // CustomLayout
  103. VerticalLayout cl = new VerticalLayout();
  104. Panel clp = new LayoutTestingPanel("CustomLayout", cl);
  105. cb = new CheckBox("CustomLayout", new MethodProperty<Boolean>(clp,
  106. "visible"));
  107. cb.setImmediate(true);
  108. hidingControls.addComponent(cb);
  109. clp.setVisible(false);
  110. lo.addComponent(clp);
  111. cl.addComponent(new Label("<<< Add customlayout testcase here >>>"));
  112. // Form
  113. Panel formPanel = new Panel("Form");
  114. cb = new CheckBox("Form", new MethodProperty<Boolean>(formPanel,
  115. "visible"));
  116. cb.setImmediate(true);
  117. hidingControls.addComponent(cb);
  118. formPanel.setVisible(false);
  119. formPanel.addComponent(getFormPanelExample());
  120. lo.addComponent(formPanel);
  121. for (Iterator<Component> i = hidingControls.getComponentIterator(); i
  122. .hasNext();) {
  123. ((AbstractComponent) i.next()).setImmediate(true);
  124. }
  125. }
  126. private Form getFormPanelExample() {
  127. Form f = new Form();
  128. f.setCaption("Test form");
  129. CheckBox fb2 = new CheckBox("Test button", true);
  130. fb2.setComponentError(new SystemError("Test error"));
  131. f.addField("fb2", fb2);
  132. TextField ft1 = new TextField("With caption");
  133. ft1.setComponentError(new SystemError("Error"));
  134. f.addField("ft1", ft1);
  135. TextField ft2 = new TextField();
  136. ft2.setComponentError(new SystemError("Error"));
  137. ft2.setValue("Without caption");
  138. f.addField("ft2", ft2);
  139. TextField ft3 = new TextField("With caption and required");
  140. ft3.setComponentError(new SystemError("Error"));
  141. ft3.setRequired(true);
  142. f.addField("ft3", ft3);
  143. return f;
  144. }
  145. private void addFields(ComponentContainer lo) {
  146. Button button = new Button("Test button");
  147. button.setComponentError(new SystemError("Test error"));
  148. lo.addComponent(button);
  149. CheckBox b2 = new CheckBox("Test button");
  150. b2.setComponentError(new SystemError("Test error"));
  151. lo.addComponent(b2);
  152. TextField t1 = new TextField("With caption");
  153. t1.setComponentError(new SystemError("Error"));
  154. lo.addComponent(t1);
  155. TextField t2 = new TextField("With caption and required");
  156. t2.setComponentError(new SystemError("Error"));
  157. t2.setRequired(true);
  158. lo.addComponent(t2);
  159. TextField t3 = new TextField();
  160. t3.setValue("Without caption");
  161. t3.setComponentError(new SystemError("Error"));
  162. lo.addComponent(t3);
  163. lo.addComponent(new TextField("Textfield with no error in it"));
  164. TextField tt1 = new TextField("100% wide Textfield with no error in it");
  165. tt1.setWidth("100%");
  166. lo.addComponent(tt1);
  167. TextField tt2 = new TextField();
  168. tt2.setWidth("100%");
  169. tt2.setValue("100% wide Textfield with no error in it and no caption");
  170. lo.addComponent(tt2);
  171. TextField t4 = new TextField();
  172. t4.setValue("Without caption, With required");
  173. t4.setComponentError(new SystemError("Error"));
  174. t4.setRequired(true);
  175. lo.addComponent(t4);
  176. TextField t5 = new TextField();
  177. t5.setValue("Without caption, WIDE");
  178. t5.setComponentError(new SystemError("Error"));
  179. t5.setWidth("100%");
  180. lo.addComponent(t5);
  181. TextField t6 = new TextField();
  182. t6.setValue("Without caption, With required, WIDE");
  183. t6.setComponentError(new SystemError("Error"));
  184. t6.setRequired(true);
  185. t6.setWidth("100%");
  186. lo.addComponent(t6);
  187. TextField t7 = new TextField();
  188. t7.setValue("With icon and required and icon");
  189. t7.setComponentError(new SystemError("Error"));
  190. t7.setRequired(true);
  191. t7.setIcon(new ThemeResource("../runo/icons/16/ok.png"));
  192. lo.addComponent(t7);
  193. DateField d1 = new DateField(
  194. "Datefield with caption and icon, next one without caption");
  195. d1.setComponentError(new SystemError("Error"));
  196. d1.setRequired(true);
  197. d1.setIcon(new ThemeResource("../runo/icons/16/ok.png"));
  198. lo.addComponent(d1);
  199. DateField d2 = new DateField();
  200. d2.setComponentError(new SystemError("Error"));
  201. d2.setRequired(true);
  202. lo.addComponent(d2);
  203. }
  204. public class LayoutTestingPanel extends Panel {
  205. Layout testedLayout;
  206. HorizontalLayout controls = new HorizontalLayout();
  207. CheckBox marginLeft = new CheckBox("m-left", false);
  208. CheckBox marginRight = new CheckBox("m-right", false);
  209. CheckBox marginTop = new CheckBox("m-top", false);
  210. CheckBox marginBottom = new CheckBox("m-bottom", false);
  211. CheckBox spacing = new CheckBox("spacing", false);
  212. VerticalLayout testPanelLayout = new VerticalLayout();
  213. LayoutTestingPanel(String caption, Layout layout) {
  214. super(caption);
  215. VerticalLayout internalLayout = new VerticalLayout();
  216. internalLayout.setWidth("100%");
  217. setContent(internalLayout);
  218. testedLayout = layout;
  219. testPanelLayout.setWidth("100%");
  220. Panel controlWrapper = new Panel();
  221. controlWrapper.addComponent(controls);
  222. controlWrapper.setWidth("100%");
  223. controlWrapper.setStyleName("controls");
  224. internalLayout.addComponent(controlWrapper);
  225. Panel testPanel = new Panel(testPanelLayout);
  226. testPanel.setStyleName("testarea");
  227. testPanelLayout.addComponent(testedLayout);
  228. internalLayout.addComponent(testPanel);
  229. internalLayout.setMargin(true);
  230. internalLayout.setSpacing(true);
  231. controls.setSpacing(true);
  232. controls.setMargin(false);
  233. controls.addComponent(new Label("width"));
  234. controls.addComponent(new TextField(new MethodProperty<Float>(
  235. testedLayout, "width")));
  236. controls.addComponent(new CheckBox("%",
  237. new MethodProperty<Boolean>(this, "widthPercents")));
  238. controls.addComponent(new Label("height"));
  239. controls.addComponent(new TextField(new MethodProperty<Float>(
  240. testedLayout, "height")));
  241. controls.addComponent(new CheckBox("%",
  242. new MethodProperty<Boolean>(this, "heightPercents")));
  243. controls.addComponent(marginLeft);
  244. controls.addComponent(marginRight);
  245. controls.addComponent(marginTop);
  246. controls.addComponent(marginBottom);
  247. if (testedLayout instanceof Layout.SpacingHandler) {
  248. controls.addComponent(spacing);
  249. }
  250. Property.ValueChangeListener marginSpacingListener = new Property.ValueChangeListener() {
  251. @Override
  252. public void valueChange(ValueChangeEvent event) {
  253. updateMarginsAndSpacing();
  254. }
  255. };
  256. marginBottom.addListener(marginSpacingListener);
  257. marginTop.addListener(marginSpacingListener);
  258. marginLeft.addListener(marginSpacingListener);
  259. marginRight.addListener(marginSpacingListener);
  260. spacing.addListener(marginSpacingListener);
  261. updateMarginsAndSpacing();
  262. addAlignmentControls();
  263. testedLayout.setStyleName("tested-layout");
  264. setStyleName("layout-testing-panel");
  265. for (Iterator<Component> i = controls.getComponentIterator(); i
  266. .hasNext();) {
  267. ((AbstractComponent) i.next()).setImmediate(true);
  268. }
  269. }
  270. @SuppressWarnings("deprecation")
  271. private void addAlignmentControls() {
  272. if (!(testedLayout instanceof Layout.AlignmentHandler)) {
  273. return;
  274. }
  275. @SuppressWarnings("unused")
  276. final Layout.AlignmentHandler ah = (AlignmentHandler) testedLayout;
  277. final NativeSelect vAlign = new NativeSelect();
  278. final NativeSelect hAlign = new NativeSelect();
  279. controls.addComponent(new Label("component alignment"));
  280. controls.addComponent(hAlign);
  281. controls.addComponent(vAlign);
  282. hAlign.setNullSelectionAllowed(false);
  283. vAlign.setNullSelectionAllowed(false);
  284. vAlign.addItem(new Integer(Bits.ALIGNMENT_TOP));
  285. vAlign.setItemCaption(new Integer(Bits.ALIGNMENT_TOP), "top");
  286. vAlign.addItem(new Integer(Bits.ALIGNMENT_VERTICAL_CENTER));
  287. vAlign.setItemCaption(new Integer(Bits.ALIGNMENT_VERTICAL_CENTER),
  288. "center");
  289. vAlign.addItem(new Integer(Bits.ALIGNMENT_BOTTOM));
  290. vAlign.setItemCaption(new Integer(Bits.ALIGNMENT_BOTTOM), "bottom");
  291. hAlign.addItem(new Integer(Bits.ALIGNMENT_LEFT));
  292. hAlign.setItemCaption(new Integer(Bits.ALIGNMENT_LEFT), "left");
  293. hAlign.addItem(new Integer(Bits.ALIGNMENT_HORIZONTAL_CENTER));
  294. hAlign.setItemCaption(
  295. new Integer(Bits.ALIGNMENT_HORIZONTAL_CENTER), "center");
  296. hAlign.addItem(new Integer(Bits.ALIGNMENT_RIGHT));
  297. hAlign.setItemCaption(new Integer(Bits.ALIGNMENT_RIGHT), "right");
  298. Property.ValueChangeListener alignmentChangeListener = new Property.ValueChangeListener() {
  299. @Override
  300. public void valueChange(ValueChangeEvent event) {
  301. Integer h = ((Integer) hAlign.getValue()).intValue();
  302. int v = ((Integer) vAlign.getValue()).intValue();
  303. updateAlignments(new Alignment(h + v));
  304. }
  305. };
  306. hAlign.setValue(new Integer(Bits.ALIGNMENT_LEFT));
  307. vAlign.addListener(alignmentChangeListener);
  308. hAlign.addListener(alignmentChangeListener);
  309. vAlign.setValue(new Integer(Bits.ALIGNMENT_TOP));
  310. controls.addComponent(new Label("layout alignment"));
  311. final NativeSelect lAlign = new NativeSelect();
  312. controls.addComponent(lAlign);
  313. lAlign.setNullSelectionAllowed(false);
  314. lAlign.addItem(new Integer(Bits.ALIGNMENT_LEFT));
  315. lAlign.setItemCaption(new Integer(Bits.ALIGNMENT_LEFT), "left");
  316. lAlign.addItem(new Integer(Bits.ALIGNMENT_HORIZONTAL_CENTER));
  317. lAlign.setItemCaption(
  318. new Integer(Bits.ALIGNMENT_HORIZONTAL_CENTER), "center");
  319. lAlign.addItem(new Integer(Bits.ALIGNMENT_RIGHT));
  320. lAlign.setItemCaption(new Integer(Bits.ALIGNMENT_RIGHT), "right");
  321. lAlign.addListener(new Property.ValueChangeListener() {
  322. @Override
  323. public void valueChange(ValueChangeEvent event) {
  324. testPanelLayout.setComponentAlignment(
  325. testedLayout,
  326. new Alignment(((Integer) lAlign.getValue())
  327. .intValue() + Bits.ALIGNMENT_TOP));
  328. }
  329. });
  330. }
  331. private void updateAlignments(Alignment a) {
  332. for (Iterator<Component> i = testedLayout.getComponentIterator(); i
  333. .hasNext();) {
  334. ((Layout.AlignmentHandler) testedLayout).setComponentAlignment(
  335. i.next(), a);
  336. }
  337. }
  338. private void updateMarginsAndSpacing() {
  339. if (testedLayout instanceof Layout.MarginHandler) {
  340. ((Layout.MarginHandler) testedLayout).setMargin(new MarginInfo(
  341. marginTop.getValue().booleanValue(), marginRight
  342. .getValue().booleanValue(), marginBottom
  343. .getValue().booleanValue(), marginLeft
  344. .getValue().booleanValue()));
  345. }
  346. if (testedLayout instanceof Layout.SpacingHandler) {
  347. ((Layout.SpacingHandler) testedLayout).setSpacing(spacing
  348. .getValue().booleanValue());
  349. }
  350. }
  351. }
  352. }