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.

TouchScrollables.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package com.vaadin.tests.components;
  2. import java.util.Collection;
  3. import com.vaadin.data.Item;
  4. import com.vaadin.data.util.IndexedContainer;
  5. import com.vaadin.event.Action;
  6. import com.vaadin.event.Action.Handler;
  7. import com.vaadin.event.DataBoundTransferable;
  8. import com.vaadin.event.dd.DragAndDropEvent;
  9. import com.vaadin.event.dd.DropHandler;
  10. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  11. import com.vaadin.event.dd.acceptcriteria.SourceIs;
  12. import com.vaadin.shared.ui.dd.VerticalDropLocation;
  13. import com.vaadin.tests.util.Person;
  14. import com.vaadin.tests.util.PersonContainer;
  15. import com.vaadin.tests.util.TestUtils;
  16. import com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails;
  17. import com.vaadin.ui.Accordion;
  18. import com.vaadin.ui.Button;
  19. import com.vaadin.ui.Button.ClickEvent;
  20. import com.vaadin.ui.Component;
  21. import com.vaadin.ui.CssLayout;
  22. import com.vaadin.ui.HorizontalSplitPanel;
  23. import com.vaadin.ui.Label;
  24. import com.vaadin.ui.Layout;
  25. import com.vaadin.ui.Notification;
  26. import com.vaadin.ui.Panel;
  27. import com.vaadin.ui.TabSheet;
  28. import com.vaadin.ui.Table;
  29. import com.vaadin.ui.VerticalLayout;
  30. import com.vaadin.ui.Window;
  31. public class TouchScrollables extends TestBase {
  32. java.util.Random r = new java.util.Random(1);
  33. private TabSheet testSelector = new TabSheet();
  34. @Override
  35. public void setup() {
  36. getLayout().addComponent(testSelector);
  37. testSelector.setHeight("500px");
  38. addTest(getPanelTest());
  39. addTest(getSimpleTableTest());
  40. addTest(getDDSortableTableTest());
  41. addTest(getTabSheetTest());
  42. addTest(getSplitPanelTest());
  43. addTest(getAccordionTest());
  44. addTest(getSubWindowTest());
  45. TestUtils
  46. .injectCSS(
  47. getLayout().getUI(),
  48. "body * {-webkit-user-select: none;} .v-table-row-drag-middle .v-table-cell-content {"
  49. + " background-color: inherit ; border-bottom: 1px solid cyan;"
  50. + "}"
  51. + ".v-table-row-drag-middle .v-table-cell-wrapper {"
  52. + " margin-bottom: -1px;" + "}" + ""
  53. );
  54. }
  55. private Component getPanelTest() {
  56. Layout cssLayout = new CssLayout();
  57. cssLayout.setCaption("Panel");
  58. final VerticalLayout pl = new VerticalLayout();
  59. pl.setMargin(true);
  60. final Panel p = new Panel(pl);
  61. p.setHeight("400px");
  62. Label l50 = null;
  63. for (int i = 0; i < 100; i++) {
  64. Label c = new Label("Label" + i);
  65. pl.addComponent(c);
  66. if (i == 50) {
  67. l50 = c;
  68. }
  69. }
  70. final Label l = l50;
  71. Button button = new Button("Scroll to label 50",
  72. new Button.ClickListener() {
  73. @Override
  74. public void buttonClick(ClickEvent event) {
  75. getLayout().getUI().scrollIntoView(l);
  76. }
  77. });
  78. cssLayout.addComponent(button);
  79. button = new Button("Scroll to 100px", new Button.ClickListener() {
  80. @Override
  81. public void buttonClick(ClickEvent event) {
  82. p.setScrollTop(100);
  83. }
  84. });
  85. cssLayout.addComponent(button);
  86. cssLayout.addComponent(p);
  87. return cssLayout;
  88. }
  89. private Component getTabSheetTest() {
  90. TabSheet ts = new TabSheet();
  91. ts.setCaption("Tabsheet");
  92. ts.setHeight("100%");
  93. ts.addTab(getBigComponent(), "Tab 1");
  94. ts.addTab(getBigComponent(), "Tab 2");
  95. return ts;
  96. }
  97. private Component getSplitPanelTest() {
  98. HorizontalSplitPanel sp = new HorizontalSplitPanel();
  99. sp.setCaption("Splitpanel");
  100. sp.addComponent(getBigComponent());
  101. sp.addComponent(getBigComponent());
  102. return sp;
  103. }
  104. private Component getSimpleTableTest() {
  105. CssLayout cssLayout = new CssLayout();
  106. final Table table = new Table();
  107. Button button = new Button("Toggle lazyloading");
  108. button.addListener(new Button.ClickListener() {
  109. @Override
  110. public void buttonClick(ClickEvent event) {
  111. if (table.getCacheRate() == 100) {
  112. table.setCacheRate(2);
  113. table.setPageLength(15);
  114. } else {
  115. table.setCacheRate(100);
  116. table.setHeight("400px");
  117. }
  118. }
  119. });
  120. cssLayout.addComponent(button);
  121. button = new Button("Toggle selectable");
  122. button.addListener(new Button.ClickListener() {
  123. @Override
  124. public void buttonClick(ClickEvent event) {
  125. table.setSelectable(!table.isSelectable());
  126. }
  127. });
  128. cssLayout.addComponent(button);
  129. table.addContainerProperty("foo", String.class, "bar");
  130. table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
  131. for (int i = 0; i < 1000; i++) {
  132. table.addItem();
  133. }
  134. cssLayout.addComponent(table);
  135. cssLayout.setCaption("Table");
  136. return cssLayout;
  137. }
  138. private Component getAccordionTest() {
  139. Accordion a = new Accordion();
  140. a.setCaption("Accordion");
  141. a.setHeight("100%");
  142. a.addTab(getBigComponent(), "Tab 1");
  143. a.addTab(getBigComponent(), "Tab 2");
  144. a.addTab(getBigComponent(), "Tab 3");
  145. return a;
  146. }
  147. private Component getSubWindowTest() {
  148. Button b = new Button("Open subwindow", new Button.ClickListener() {
  149. @Override
  150. public void buttonClick(ClickEvent event) {
  151. VerticalLayout layout = new VerticalLayout();
  152. layout.setMargin(true);
  153. Window w = new Window("Subwindow", layout);
  154. w.center();
  155. w.setHeight("200px");
  156. layout.addComponent(getBigComponent());
  157. getMainWindow().addWindow(w);
  158. }
  159. });
  160. return b;
  161. }
  162. private Component getDDSortableTableTest() {
  163. final Table table;
  164. table = new Table();
  165. table.setCaption("DD sortable table with context menus");
  166. // table.setWidth("100%");
  167. table.setPageLength(10);
  168. table.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  169. table.setSelectable(true);
  170. table.setMultiSelect(true);
  171. table.addActionHandler(new Handler() {
  172. Action[] actions = new Action[] { new Action("FOO"),
  173. new Action("BAR"), new Action("CAR") };
  174. @Override
  175. public Action[] getActions(Object target, Object sender) {
  176. return actions;
  177. }
  178. @Override
  179. public void handleAction(Action action, Object sender, Object target) {
  180. Notification.show(action.getCaption());
  181. }
  182. });
  183. populateTable(table);
  184. /*
  185. * Make table rows draggable
  186. */
  187. table.setDragMode(Table.TableDragMode.ROW);
  188. table.setDropHandler(new DropHandler() {
  189. // accept only drags from this table
  190. AcceptCriterion crit = new SourceIs(table);
  191. @Override
  192. public AcceptCriterion getAcceptCriterion() {
  193. return crit;
  194. }
  195. @Override
  196. public void drop(DragAndDropEvent dropEvent) {
  197. AbstractSelectTargetDetails dropTargetData = (AbstractSelectTargetDetails) dropEvent
  198. .getTargetDetails();
  199. DataBoundTransferable transferable = (DataBoundTransferable) dropEvent
  200. .getTransferable();
  201. Object itemIdOver = dropTargetData.getItemIdOver();
  202. Object itemId = transferable.getItemId();
  203. if (itemId == null || itemIdOver == null
  204. || itemId.equals(itemIdOver)) {
  205. return; // no move happened
  206. }
  207. // IndexedContainer goodies... (hint: don't use it in real apps)
  208. IndexedContainer containerDataSource = (IndexedContainer) table
  209. .getContainerDataSource();
  210. int newIndex = containerDataSource.indexOfId(itemIdOver) - 1;
  211. if (dropTargetData.getDropLocation() != VerticalDropLocation.TOP) {
  212. newIndex++;
  213. }
  214. if (newIndex < 0) {
  215. newIndex = 0;
  216. }
  217. Object idAfter = containerDataSource.getIdByIndex(newIndex);
  218. Collection<?> selections = (Collection<?>) table.getValue();
  219. if (selections != null && selections.contains(itemId)) {
  220. // dragged a selected item, if multiple rows selected, drag
  221. // them too (functionality similar to apple mail)
  222. for (Object object : selections) {
  223. moveAfter(containerDataSource, object, idAfter);
  224. }
  225. } else {
  226. // move just the dragged row, not considering selection at
  227. // all
  228. moveAfter(containerDataSource, itemId, idAfter);
  229. }
  230. }
  231. private void moveAfter(IndexedContainer containerDataSource,
  232. Object itemId, Object idAfter) {
  233. try {
  234. IndexedContainer clone = null;
  235. clone = (IndexedContainer) containerDataSource.clone();
  236. containerDataSource.removeItem(itemId);
  237. Item newItem = containerDataSource.addItemAfter(idAfter,
  238. itemId);
  239. Item item = clone.getItem(itemId);
  240. for (Object propId : item.getItemPropertyIds()) {
  241. newItem.getItemProperty(propId).setValue(
  242. item.getItemProperty(propId).getValue());
  243. }
  244. // TODO Auto-generated method stub
  245. } catch (CloneNotSupportedException e) {
  246. // TODO Auto-generated catch block
  247. e.printStackTrace();
  248. }
  249. }
  250. });
  251. return table;
  252. }
  253. private void populateTable(Table table) {
  254. table.addContainerProperty("Name", String.class, "");
  255. table.addContainerProperty("Weight", Integer.class, 0);
  256. PersonContainer testData = PersonContainer.createWithTestData();
  257. for (int i = 0; i < 40; i++) {
  258. Item addItem = table.addItem("Item" + i);
  259. Person p = testData.getIdByIndex(i);
  260. addItem.getItemProperty("Name").setValue(
  261. p.getFirstName() + " " + p.getLastName());
  262. addItem.getItemProperty("Weight").setValue(50 + r.nextInt(60));
  263. }
  264. }
  265. private void addTest(final Component t) {
  266. testSelector.addComponent(t);
  267. }
  268. private Component getBigComponent() {
  269. Layout l = new VerticalLayout();
  270. for (int i = 0; i < 100; i++) {
  271. Label c = new Label("Label" + i);
  272. l.addComponent(c);
  273. }
  274. return l;
  275. }
  276. @Override
  277. protected String getDescription() {
  278. return "Various components and setups suitable for testing scrolling on touch devices.";
  279. }
  280. @Override
  281. protected Integer getTicketNumber() {
  282. return null;
  283. }
  284. }