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.

BaseLayoutTestUI.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright 2000-2016 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.tests.layouts.layouttester;
  17. import com.vaadin.server.Resource;
  18. import com.vaadin.server.ThemeResource;
  19. import com.vaadin.server.UserError;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.shared.ui.ContentMode;
  22. import com.vaadin.tests.components.AbstractReindeerTestUI;
  23. import com.vaadin.ui.AbstractComponent;
  24. import com.vaadin.ui.AbstractLayout;
  25. import com.vaadin.ui.AbstractOrderedLayout;
  26. import com.vaadin.ui.Alignment;
  27. import com.vaadin.ui.Button;
  28. import com.vaadin.ui.Button.ClickEvent;
  29. import com.vaadin.ui.Button.ClickListener;
  30. import com.vaadin.ui.Component;
  31. import com.vaadin.ui.HorizontalLayout;
  32. import com.vaadin.ui.Label;
  33. import com.vaadin.ui.TabSheet;
  34. import com.vaadin.ui.VerticalLayout;
  35. import com.vaadin.v7.ui.Table;
  36. import com.vaadin.v7.ui.TextField;
  37. /**
  38. *
  39. * Base class for Layout tests.
  40. */
  41. public abstract class BaseLayoutTestUI extends AbstractReindeerTestUI {
  42. protected static final String FOLDER_16_PNG = "../icons/runo/16/folder.png";
  43. protected static final String CALENDAR_32_PNG = "../runo/icons/16/calendar.png";
  44. protected static final String LOCK_16_PNG = "../runo/icons/16/lock.png";
  45. protected static final String GLOBE_16_PNG = "../runo/icons/16/globe.png";
  46. public Alignment[] alignments = new Alignment[] { Alignment.TOP_CENTER,
  47. Alignment.TOP_LEFT, Alignment.TOP_RIGHT, Alignment.BOTTOM_CENTER,
  48. Alignment.BOTTOM_LEFT, Alignment.BOTTOM_RIGHT,
  49. Alignment.MIDDLE_CENTER, Alignment.MIDDLE_LEFT,
  50. Alignment.MIDDLE_RIGHT };
  51. public final String[] CAPTIONS = new String[] { "",
  52. "VeryLongOneWordCaption",
  53. "Very long caption of 50 approximately symbols aaaaaaaaaaaa aaaaaa aaa " };
  54. Resource[] ICONS = new Resource[] { new ThemeResource(CALENDAR_32_PNG),
  55. new ThemeResource(LOCK_16_PNG), new ThemeResource(GLOBE_16_PNG) };
  56. public AbstractComponent[] components = new AbstractComponent[alignments.length];
  57. protected AbstractOrderedLayout l1;
  58. protected AbstractOrderedLayout l2;
  59. protected Class<? extends AbstractLayout> layoutClass;
  60. protected VerticalLayout mainLayout = new VerticalLayout();
  61. public BaseLayoutTestUI(Class<? extends AbstractLayout> layoutClass) {
  62. super();
  63. fillComponents();
  64. this.layoutClass = layoutClass;
  65. }
  66. protected void init() {
  67. try {
  68. l1 = (AbstractOrderedLayout) layoutClass.newInstance();
  69. l2 = (AbstractOrderedLayout) layoutClass.newInstance();
  70. l1.setMargin(false);
  71. l1.setSpacing(false);
  72. l2.setMargin(false);
  73. l2.setSpacing(false);
  74. } catch (InstantiationException e1) {
  75. // TODO Auto-generated catch block
  76. e1.printStackTrace();
  77. } catch (IllegalAccessException e1) {
  78. // TODO Auto-generated catch block
  79. e1.printStackTrace();
  80. }
  81. if (layoutClass.equals(HorizontalLayout.class)) {
  82. setLayoutMeasures(l1, l2, "600px", "400px");
  83. } else if (layoutClass.equals(VerticalLayout.class)) {
  84. setLayoutMeasures(l1, l2, "400px", "400px");
  85. } else {
  86. setDefaultForVertical(l1, l2);
  87. }
  88. }
  89. private void fillComponents() {
  90. for (int i = 0; i < components.length; i++) {
  91. String name = "Field" + i;
  92. TextField field = new TextField();
  93. field.setValue(name);
  94. components[i] = field;
  95. }
  96. }
  97. protected AbstractLayout createLabelsFields(
  98. Class<? extends AbstractComponent> compType) {
  99. return createLabelsFields(compType, false, null);
  100. }
  101. protected void getLayoutForLayoutSizing(final String compType) {
  102. l2.setSpacing(false);
  103. l2.setMargin(false);
  104. final AbstractComponent c1 = getTestTable();
  105. c1.setSizeFull();
  106. final AbstractComponent c2 = getTestTable();
  107. c2.setSizeFull();
  108. class SetSizeButton extends Button {
  109. SetSizeButton(final String size) {
  110. super();
  111. setCaption("Set size " + size);
  112. addClickListener(new ClickListener() {
  113. @Override
  114. public void buttonClick(ClickEvent event) {
  115. if (compType == "layout") {
  116. l2.setHeight(size);
  117. l2.setWidth(size);
  118. } else if (compType == "component") {
  119. c2.setHeight(size);
  120. c2.setWidth(size);
  121. } else {
  122. }
  123. }
  124. });
  125. }
  126. }
  127. Button btn1 = new SetSizeButton("350px");
  128. Button btn2 = new SetSizeButton("-1px");
  129. Button btn3 = new SetSizeButton("75%");
  130. Button btn4 = new SetSizeButton("100%");
  131. Label spacer = new Label(
  132. "<div style='height: 1px'></div><hr /><div style='height: 1px'></div>",
  133. ContentMode.HTML);
  134. spacer.setWidth("100%");
  135. l1.addComponent(btn1);
  136. l1.addComponent(btn2);
  137. l1.addComponent(btn3);
  138. l1.addComponent(btn4);
  139. l2.addComponent(c1);
  140. l2.addComponent(spacer);
  141. l2.addComponent(c2);
  142. l2.setExpandRatio(c1, 0.5f);
  143. l2.setExpandRatio(c2, 0.5f);
  144. btn2.addClickListener(new ClickListener() {
  145. @Override
  146. public void buttonClick(ClickEvent event) {
  147. Label newLabel = new Label("--- NEW LABEL ---");
  148. newLabel.setSizeUndefined();
  149. l2.addComponent(newLabel);
  150. }
  151. });
  152. }
  153. protected Table getTestTable() {
  154. Table t = new Table();
  155. t.setPageLength(5);
  156. t.addContainerProperty("test", String.class, null);
  157. t.addItem(new Object[] { "qwertyuiop asdfghjköäxccvbnm,m,." }, 1);
  158. t.addItem(new Object[] { "YGVYTCTCTRXRXRXRX" }, 2);
  159. return t;
  160. }
  161. protected AbstractLayout createLabelsFields(
  162. Class<? extends AbstractComponent> compType, boolean useIcon,
  163. String ErrorMessage) {
  164. AbstractOrderedLayout mainLayout = new VerticalLayout();
  165. mainLayout.setSpacing(false);
  166. mainLayout.setMargin(false);
  167. AbstractLayout curLayout = null;
  168. try {
  169. curLayout = layoutClass.newInstance();
  170. if (curLayout instanceof AbstractOrderedLayout) {
  171. ((AbstractOrderedLayout) curLayout).setMargin(false);
  172. ((AbstractOrderedLayout) curLayout).setSpacing(false);
  173. }
  174. } catch (InstantiationException e1) {
  175. // TODO Auto-generated catch block
  176. e1.printStackTrace();
  177. } catch (IllegalAccessException e1) {
  178. // TODO Auto-generated catch block
  179. e1.printStackTrace();
  180. }
  181. final Component[] components = new Component[CAPTIONS.length];
  182. for (int i = 0; i < components.length; i++) {
  183. AbstractComponent comp = null;
  184. try {
  185. comp = compType.newInstance();
  186. } catch (InstantiationException e) {
  187. // TODO Auto-generated catch block
  188. e.printStackTrace();
  189. } catch (IllegalAccessException e) {
  190. // TODO Auto-generated catch block
  191. e.printStackTrace();
  192. }
  193. components[i] = comp;
  194. comp.setCaption(CAPTIONS[i]);
  195. if (useIcon) {
  196. comp.setIcon(ICONS[i]);
  197. }
  198. if (ErrorMessage != null) {
  199. if (ErrorMessage.length() == 0) {
  200. comp.setComponentError(new UserError(null));
  201. } else {
  202. comp.setComponentError(new UserError(ErrorMessage));
  203. }
  204. }
  205. // if component is a tab sheet add two tabs for it
  206. if (comp instanceof TabSheet) {
  207. comp.setSizeUndefined();
  208. TabSheet tab = (TabSheet) comp;
  209. tab.addTab(new UndefWideLabel("TAB1"), "TAB1",
  210. new ThemeResource(GLOBE_16_PNG));
  211. tab.addTab(new UndefWideLabel("TAB2"), "TAB2", null);
  212. }
  213. curLayout.addComponent(comp);
  214. mainLayout.addComponent(curLayout);
  215. }
  216. return mainLayout;
  217. }
  218. /*
  219. * (non-Javadoc)
  220. *
  221. * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
  222. * VaadinRequest)
  223. */
  224. @Override
  225. protected void setup(VaadinRequest request) {
  226. mainLayout.setMargin(false);
  227. mainLayout.setSpacing(false);
  228. mainLayout.addComponent(l1);
  229. mainLayout.addComponent(l2);
  230. addComponent(mainLayout);
  231. }
  232. /*
  233. * (non-Javadoc)
  234. *
  235. * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription()
  236. */
  237. @Override
  238. protected String getTestDescription() {
  239. return null;
  240. }
  241. protected void setLayoutMeasures(AbstractOrderedLayout l1,
  242. AbstractOrderedLayout l2, String w, String h) {
  243. l1.setWidth(w);
  244. l1.setHeight(h);
  245. l2.setWidth(h);
  246. l2.setHeight(w);
  247. }
  248. protected void setDefaultForVertical(AbstractOrderedLayout l1,
  249. AbstractOrderedLayout l2) {
  250. l1.setWidth("800px");
  251. l1.setHeight("600px");
  252. l2.setWidth("800px");
  253. l2.setHeight("600px");
  254. }
  255. protected void setDefaultForHorizontal(AbstractOrderedLayout l1,
  256. AbstractOrderedLayout l2) {
  257. l1.setWidth("600px");
  258. l1.setHeight("600px");
  259. l2.setWidth("600px");
  260. l2.setHeight("600px");
  261. }
  262. /*
  263. * (non-Javadoc)
  264. *
  265. * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber()
  266. */
  267. @Override
  268. protected Integer getTicketNumber() {
  269. // TODO Auto-generated method stub
  270. return null;
  271. }
  272. }