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.

BoxLayoutTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package com.vaadin.tests.components.orderedlayout;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import com.vaadin.annotations.PreserveOnRefresh;
  5. import com.vaadin.annotations.Theme;
  6. import com.vaadin.event.LayoutEvents.LayoutClickEvent;
  7. import com.vaadin.event.LayoutEvents.LayoutClickListener;
  8. import com.vaadin.server.ThemeResource;
  9. import com.vaadin.server.UserError;
  10. import com.vaadin.server.VaadinRequest;
  11. import com.vaadin.shared.ui.ContentMode;
  12. import com.vaadin.tests.components.AbstractReindeerTestUI;
  13. import com.vaadin.ui.AbstractComponent;
  14. import com.vaadin.ui.AbstractOrderedLayout;
  15. import com.vaadin.ui.Alignment;
  16. import com.vaadin.ui.Button;
  17. import com.vaadin.ui.Button.ClickEvent;
  18. import com.vaadin.ui.CheckBox;
  19. import com.vaadin.ui.Component;
  20. import com.vaadin.ui.GridLayout;
  21. import com.vaadin.ui.HorizontalLayout;
  22. import com.vaadin.ui.Label;
  23. import com.vaadin.ui.VerticalLayout;
  24. import com.vaadin.v7.data.Property.ValueChangeEvent;
  25. import com.vaadin.v7.data.Property.ValueChangeListener;
  26. import com.vaadin.v7.ui.AbstractField;
  27. import com.vaadin.v7.ui.NativeSelect;
  28. import com.vaadin.v7.ui.TextField;
  29. import com.vaadin.v7.ui.themes.Reindeer;
  30. @Theme("tests-components")
  31. @PreserveOnRefresh
  32. public class BoxLayoutTest extends AbstractReindeerTestUI {
  33. protected AbstractOrderedLayout view;
  34. protected AbstractOrderedLayout l;
  35. protected AbstractComponent target;
  36. protected NativeSelect componentWidth;
  37. protected NativeSelect componentHeight;
  38. protected NativeSelect componentCaption;
  39. protected NativeSelect componentIcon;
  40. protected TextField componentDescription;
  41. protected CheckBox componentError;
  42. protected CheckBox componentRequired;
  43. protected NativeSelect align;
  44. protected CheckBox expand;
  45. @Override
  46. protected void setup(VaadinRequest request) {
  47. view = new VerticalLayout();
  48. view.setMargin(true);
  49. view.setSpacing(true);
  50. for (int i = 0; i < 20; i++) {
  51. view.addComponent(createHorizontalTest());
  52. }
  53. setContent(view);
  54. }
  55. private Component createHorizontalTest() {
  56. HorizontalLayout l = new HorizontalLayout();
  57. l.setWidth("100%");
  58. l.setSpacing(true);
  59. Label exp;
  60. // l.addComponent(new Embedded(null, new ThemeResource(
  61. // "../runo/icons/32/document.png")));
  62. l.addComponent(exp = new Label(
  63. "Mauris iaculis porttitor posuere. Praesent id metus massa, ut blandit odio. Proin quis tortor orci. Etiam at risus et justo dignissim congue. Donec."));
  64. // exp.setWidth("300px");
  65. l.addComponent(new Button("Edit"));
  66. l.addComponent(new Button("Delete"));
  67. l.setExpandRatio(exp, 1);
  68. for (int i = 0; i < l.getComponentCount(); i++) {
  69. l.setComponentAlignment(l.getComponent(i), Alignment.MIDDLE_LEFT);
  70. }
  71. return l;
  72. }
  73. protected AbstractOrderedLayout createControls(boolean horizontal) {
  74. VerticalLayout root = new VerticalLayout();
  75. root.setSpacing(true);
  76. // First row
  77. HorizontalLayout header = new HorizontalLayout();
  78. header.setSpacing(true);
  79. root.addComponent(header);
  80. Label title = new Label("BoxLayout Test");
  81. title.addStyleName(Reindeer.LABEL_H1);
  82. header.addComponent(title);
  83. final CheckBox vertical = new CheckBox("Vertical", !horizontal);
  84. vertical.addValueChangeListener(event -> {
  85. view.removeAllComponents();
  86. view.addComponent(
  87. createControls(!vertical.getValue().booleanValue()));
  88. view.addComponent(
  89. createTestLayout(!vertical.getValue().booleanValue()));
  90. view.setExpandRatio(view.getComponent(1), 1);
  91. });
  92. header.addComponent(vertical);
  93. Button addComponent = new Button("Add Component",
  94. new Button.ClickListener() {
  95. @Override
  96. public void buttonClick(ClickEvent event) {
  97. GridLayout grid = new GridLayout(2, 2);
  98. Button grow = new Button("Grow Me",
  99. new Button.ClickListener() {
  100. @Override
  101. public void buttonClick(ClickEvent event) {
  102. if (event.getButton()
  103. .getWidth() == -1) {
  104. event.getButton().setHeight("50px");
  105. event.getButton().setWidth("200px");
  106. } else {
  107. event.getButton()
  108. .setSizeUndefined();
  109. }
  110. }
  111. });
  112. grid.addComponent(new Label("Grid cell 1"));
  113. grid.addComponent(new Label("Grid cell 2"));
  114. grid.addComponent(grow);
  115. grid.addComponent(new Label("Grid cell 4"));
  116. l.addComponent(grid);
  117. // l.addComponent(new TextField("Some field"));
  118. }
  119. });
  120. header.addComponent(addComponent);
  121. Button removeComponent = new Button("Remove Component",
  122. new Button.ClickListener() {
  123. @Override
  124. public void buttonClick(ClickEvent event) {
  125. Component last = l
  126. .getComponent(l.getComponentCount() - 1);
  127. l.removeComponent(last);
  128. }
  129. });
  130. header.addComponent(removeComponent);
  131. // Second row
  132. HorizontalLayout controls = new HorizontalLayout();
  133. controls.setSpacing(true);
  134. root.addComponent(controls);
  135. // Layout controls
  136. HorizontalLayout layout = new HorizontalLayout();
  137. layout.addStyleName("fieldset");
  138. layout.setSpacing(true);
  139. controls.addComponent(layout);
  140. layout.addComponent(new Label("Layout"));
  141. ArrayList<String> sizes = new ArrayList<>();
  142. sizes.addAll(Arrays.asList("100px", "30em", "100%"));
  143. final NativeSelect width = new NativeSelect(null, sizes);
  144. width.setImmediate(true);
  145. width.addListener(new ValueChangeListener() {
  146. @Override
  147. public void valueChange(ValueChangeEvent event) {
  148. if (width.getValue() != null) {
  149. l.setWidth(width.getValue().toString());
  150. } else {
  151. l.setWidth(null);
  152. }
  153. }
  154. });
  155. layout.addComponent(width);
  156. layout.addComponent(new Label("&times;", ContentMode.HTML));
  157. final NativeSelect height = new NativeSelect(null, sizes);
  158. height.setImmediate(true);
  159. height.addListener(new ValueChangeListener() {
  160. @Override
  161. public void valueChange(ValueChangeEvent event) {
  162. if (height.getValue() != null) {
  163. l.setHeight(height.getValue().toString());
  164. } else {
  165. l.setHeight(null);
  166. }
  167. }
  168. });
  169. layout.addComponent(height);
  170. final CheckBox margin = new CheckBox("Margin", false);
  171. margin.addValueChangeListener(event -> l.setMargin(event.getValue()));
  172. layout.addComponent(margin);
  173. layout.addComponent(margin);
  174. final CheckBox spacing = new CheckBox("Spacing", false);
  175. spacing.addValueChangeListener(event -> l.setSpacing(event.getValue()));
  176. layout.addComponent(spacing);
  177. // Cell controls
  178. HorizontalLayout cell = new HorizontalLayout();
  179. cell.addStyleName("fieldset");
  180. cell.setSpacing(true);
  181. controls.addComponent(cell);
  182. cell.addComponent(new Label("Cell"));
  183. ArrayList<Alignment> alignments = new ArrayList<>();
  184. alignments.addAll(Arrays.asList(Alignment.TOP_LEFT,
  185. Alignment.MIDDLE_LEFT, Alignment.BOTTOM_LEFT,
  186. Alignment.TOP_CENTER, Alignment.MIDDLE_CENTER,
  187. Alignment.BOTTOM_CENTER, Alignment.TOP_RIGHT,
  188. Alignment.MIDDLE_RIGHT, Alignment.BOTTOM_RIGHT));
  189. align = new NativeSelect(null, alignments);
  190. for (Alignment a : alignments) {
  191. align.setItemCaption(a, a.getVerticalAlignment() + "-"
  192. + a.getHorizontalAlignment());
  193. }
  194. align.setImmediate(true);
  195. align.setEnabled(false);
  196. align.setNullSelectionAllowed(false);
  197. align.select(Alignment.TOP_LEFT);
  198. align.addListener(new ValueChangeListener() {
  199. @Override
  200. public void valueChange(ValueChangeEvent event) {
  201. if (target == null) {
  202. return;
  203. }
  204. l.setComponentAlignment(target, (Alignment) align.getValue());
  205. }
  206. });
  207. cell.addComponent(align);
  208. expand = new CheckBox("Expand");
  209. expand.setEnabled(false);
  210. expand.addValueChangeListener(event -> {
  211. if (target != null) {
  212. l.setExpandRatio(target, expand.getValue() ? 1 : 0);
  213. }
  214. });
  215. cell.addComponent(expand);
  216. // Component controls
  217. HorizontalLayout component = new HorizontalLayout();
  218. component.addStyleName("fieldset");
  219. component.setSpacing(true);
  220. root.addComponent(component);
  221. component.addComponent(new Label("Component"));
  222. sizes = new ArrayList<>();
  223. sizes.addAll(Arrays.asList("50px", "200px", "10em", "50%", "100%"));
  224. componentWidth = new NativeSelect(null, sizes);
  225. componentWidth.setImmediate(true);
  226. componentWidth.setEnabled(false);
  227. componentWidth.addListener(new ValueChangeListener() {
  228. @Override
  229. public void valueChange(ValueChangeEvent event) {
  230. if (target == null) {
  231. return;
  232. }
  233. if (componentWidth.getValue() != null) {
  234. target.setWidth(componentWidth.getValue().toString());
  235. } else {
  236. target.setWidth(null);
  237. }
  238. }
  239. });
  240. component.addComponent(componentWidth);
  241. component.addComponent(new Label("&times;", ContentMode.HTML));
  242. componentHeight = new NativeSelect(null, sizes);
  243. componentHeight.setImmediate(true);
  244. componentHeight.setEnabled(false);
  245. componentHeight.addListener(new ValueChangeListener() {
  246. @Override
  247. public void valueChange(ValueChangeEvent event) {
  248. if (componentHeight.getValue() != null) {
  249. target.setHeight(componentHeight.getValue().toString());
  250. } else {
  251. target.setHeight(null);
  252. }
  253. }
  254. });
  255. component.addComponent(componentHeight);
  256. componentCaption = new NativeSelect("Caption",
  257. Arrays.asList("Short", "Slightly Longer Caption"));
  258. componentCaption.setImmediate(true);
  259. componentCaption.setEnabled(false);
  260. componentCaption.addListener(new ValueChangeListener() {
  261. @Override
  262. public void valueChange(ValueChangeEvent event) {
  263. if (componentCaption.getValue() != null) {
  264. target.setCaption(componentCaption.getValue().toString());
  265. } else {
  266. target.setCaption(null);
  267. }
  268. }
  269. });
  270. component.addComponent(componentCaption);
  271. componentIcon = new NativeSelect("Icon",
  272. Arrays.asList("../runo/icons/16/folder.png",
  273. "../runo/icons/32/document.png"));
  274. componentIcon.setImmediate(true);
  275. componentIcon.setEnabled(false);
  276. componentIcon.addListener(new ValueChangeListener() {
  277. @Override
  278. public void valueChange(ValueChangeEvent event) {
  279. if (componentIcon.getValue() != null) {
  280. target.setIcon(new ThemeResource(
  281. componentIcon.getValue().toString()));
  282. } else {
  283. target.setIcon(null);
  284. }
  285. }
  286. });
  287. component.addComponent(componentIcon);
  288. componentDescription = new TextField("Description");
  289. componentDescription.setImmediate(true);
  290. componentDescription.setEnabled(false);
  291. componentDescription.addListener(new ValueChangeListener() {
  292. @Override
  293. public void valueChange(ValueChangeEvent event) {
  294. target.setDescription(componentDescription.getValue());
  295. }
  296. });
  297. component.addComponent(componentDescription);
  298. componentError = new CheckBox("Error");
  299. componentError.setEnabled(false);
  300. componentError.addValueChangeListener(event -> {
  301. if (target != null) {
  302. target.setComponentError(componentError.getValue()
  303. ? new UserError("Error message") : null);
  304. }
  305. });
  306. component.addComponent(componentError);
  307. componentRequired = new CheckBox("Required");
  308. componentRequired.setEnabled(false);
  309. componentRequired.addValueChangeListener(event -> {
  310. if (target != null && target instanceof AbstractField) {
  311. ((AbstractField<?>) target)
  312. .setRequired(componentRequired.getValue());
  313. }
  314. });
  315. component.addComponent(componentRequired);
  316. for (int i = 0; i < component.getComponentCount(); i++) {
  317. component.setComponentAlignment(component.getComponent(i),
  318. Alignment.MIDDLE_LEFT);
  319. }
  320. return root;
  321. }
  322. protected AbstractOrderedLayout createTestLayout(boolean horizontal) {
  323. l = horizontal ? new HorizontalLayout() : new VerticalLayout();
  324. l.setSizeUndefined();
  325. l.addStyleName("test");
  326. Label label = new Label("Component 1");
  327. l.addComponent(label);
  328. l.addComponent(new Button("Component 2"));
  329. l.addLayoutClickListener(new LayoutClickListener() {
  330. @Override
  331. public void layoutClick(LayoutClickEvent event) {
  332. if (event.getChildComponent() == null
  333. || target == event.getChildComponent()) {
  334. if (target != null) {
  335. target.removeStyleName("target");
  336. }
  337. target = null;
  338. } else if (target != event.getChildComponent()) {
  339. if (target != null) {
  340. target.removeStyleName("target");
  341. }
  342. target = (AbstractComponent) event.getChildComponent();
  343. target.addStyleName("target");
  344. }
  345. componentWidth.setEnabled(target != null);
  346. componentHeight.setEnabled(target != null);
  347. componentCaption.setEnabled(target != null);
  348. componentIcon.setEnabled(target != null);
  349. componentDescription.setEnabled(target != null);
  350. componentError.setEnabled(target != null);
  351. componentRequired.setEnabled(
  352. target != null && target instanceof AbstractField);
  353. align.setEnabled(target != null);
  354. expand.setEnabled(target != null);
  355. if (target != null) {
  356. if (target.getWidth() > -1) {
  357. componentWidth
  358. .select(new Float(target.getWidth()).intValue()
  359. + target.getWidthUnits().getSymbol());
  360. } else {
  361. componentWidth.select(null);
  362. }
  363. if (target.getHeight() > -1) {
  364. componentHeight
  365. .select(new Float(target.getHeight()).intValue()
  366. + target.getHeightUnits().getSymbol());
  367. } else {
  368. componentHeight.select(null);
  369. }
  370. align.select(l.getComponentAlignment(target));
  371. expand.setValue(new Boolean(l.getExpandRatio(target) > 0));
  372. componentCaption.select(target.getCaption());
  373. if (target.getIcon() != null) {
  374. componentIcon.select(((ThemeResource) target.getIcon())
  375. .getResourceId());
  376. } else {
  377. componentIcon.select(null);
  378. }
  379. componentDescription.setValue(target.getDescription());
  380. componentError.setValue(target.getComponentError() != null);
  381. if (target instanceof AbstractField) {
  382. componentRequired.setValue(
  383. ((AbstractField<?>) target).isRequired());
  384. }
  385. }
  386. }
  387. });
  388. target = null;
  389. return l;
  390. }
  391. @Override
  392. protected String getTestDescription() {
  393. // TODO Auto-generated method stub
  394. return null;
  395. }
  396. @Override
  397. protected Integer getTicketNumber() {
  398. // TODO Auto-generated method stub
  399. return null;
  400. }
  401. }