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.event.Action;
  4. import com.vaadin.event.Action.Handler;
  5. import com.vaadin.event.dd.DragAndDropEvent;
  6. import com.vaadin.event.dd.DropHandler;
  7. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  8. import com.vaadin.event.dd.acceptcriteria.SourceIs;
  9. import com.vaadin.shared.ui.dd.VerticalDropLocation;
  10. import com.vaadin.tests.util.Person;
  11. import com.vaadin.tests.util.PersonContainer;
  12. import com.vaadin.tests.util.TestUtils;
  13. import com.vaadin.ui.Accordion;
  14. import com.vaadin.ui.Button;
  15. import com.vaadin.ui.Button.ClickEvent;
  16. import com.vaadin.ui.Component;
  17. import com.vaadin.ui.CssLayout;
  18. import com.vaadin.ui.HorizontalSplitPanel;
  19. import com.vaadin.ui.Label;
  20. import com.vaadin.ui.Layout;
  21. import com.vaadin.ui.Notification;
  22. import com.vaadin.ui.Panel;
  23. import com.vaadin.ui.TabSheet;
  24. import com.vaadin.ui.VerticalLayout;
  25. import com.vaadin.ui.Window;
  26. import com.vaadin.v7.data.Item;
  27. import com.vaadin.v7.data.util.IndexedContainer;
  28. import com.vaadin.v7.event.DataBoundTransferable;
  29. import com.vaadin.v7.ui.AbstractSelect.AbstractSelectTargetDetails;
  30. import com.vaadin.v7.ui.Table;
  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.injectCSS(getLayout().getUI(),
  46. "body * {-webkit-user-select: none;} .v-table-row-drag-middle .v-table-cell-content {"
  47. + " background-color: inherit ; border-bottom: 1px solid cyan;"
  48. + "}"
  49. + ".v-table-row-drag-middle .v-table-cell-wrapper {"
  50. + " margin-bottom: -1px;" + "}" + ""
  51. );
  52. }
  53. private Component getPanelTest() {
  54. Layout cssLayout = new CssLayout();
  55. cssLayout.setCaption("Panel");
  56. final VerticalLayout pl = new VerticalLayout();
  57. pl.setMargin(true);
  58. final Panel p = new Panel(pl);
  59. p.setHeight("400px");
  60. Label l50 = null;
  61. for (int i = 0; i < 100; i++) {
  62. Label c = new Label("Label" + i);
  63. pl.addComponent(c);
  64. if (i == 50) {
  65. l50 = c;
  66. }
  67. }
  68. final Label l = l50;
  69. Button button = new Button("Scroll to label 50",
  70. new Button.ClickListener() {
  71. @Override
  72. public void buttonClick(ClickEvent event) {
  73. getLayout().getUI().scrollIntoView(l);
  74. }
  75. });
  76. cssLayout.addComponent(button);
  77. button = new Button("Scroll to 100px", new Button.ClickListener() {
  78. @Override
  79. public void buttonClick(ClickEvent event) {
  80. p.setScrollTop(100);
  81. }
  82. });
  83. cssLayout.addComponent(button);
  84. cssLayout.addComponent(p);
  85. return cssLayout;
  86. }
  87. private Component getTabSheetTest() {
  88. TabSheet ts = new TabSheet();
  89. ts.setCaption("Tabsheet");
  90. ts.setHeight("100%");
  91. ts.addTab(getBigComponent(), "Tab 1");
  92. ts.addTab(getBigComponent(), "Tab 2");
  93. return ts;
  94. }
  95. private Component getSplitPanelTest() {
  96. HorizontalSplitPanel sp = new HorizontalSplitPanel();
  97. sp.setCaption("Splitpanel");
  98. sp.addComponent(getBigComponent());
  99. sp.addComponent(getBigComponent());
  100. return sp;
  101. }
  102. private Component getSimpleTableTest() {
  103. CssLayout cssLayout = new CssLayout();
  104. final Table table = new Table();
  105. Button button = new Button("Toggle lazyloading");
  106. button.addClickListener(new Button.ClickListener() {
  107. @Override
  108. public void buttonClick(ClickEvent event) {
  109. if (table.getCacheRate() == 100) {
  110. table.setCacheRate(2);
  111. table.setPageLength(15);
  112. } else {
  113. table.setCacheRate(100);
  114. table.setHeight("400px");
  115. }
  116. }
  117. });
  118. cssLayout.addComponent(button);
  119. button = new Button("Toggle selectable");
  120. button.addClickListener(new Button.ClickListener() {
  121. @Override
  122. public void buttonClick(ClickEvent event) {
  123. table.setSelectable(!table.isSelectable());
  124. }
  125. });
  126. cssLayout.addComponent(button);
  127. table.addContainerProperty("foo", String.class, "bar");
  128. table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
  129. for (int i = 0; i < 1000; i++) {
  130. table.addItem();
  131. }
  132. cssLayout.addComponent(table);
  133. cssLayout.setCaption("Table");
  134. return cssLayout;
  135. }
  136. private Component getAccordionTest() {
  137. Accordion a = new Accordion();
  138. a.setCaption("Accordion");
  139. a.setHeight("100%");
  140. a.addTab(getBigComponent(), "Tab 1");
  141. a.addTab(getBigComponent(), "Tab 2");
  142. a.addTab(getBigComponent(), "Tab 3");
  143. return a;
  144. }
  145. private Component getSubWindowTest() {
  146. Button b = new Button("Open subwindow", new Button.ClickListener() {
  147. @Override
  148. public void buttonClick(ClickEvent event) {
  149. VerticalLayout layout = new VerticalLayout();
  150. layout.setMargin(true);
  151. Window w = new Window("Subwindow", layout);
  152. w.center();
  153. w.setHeight("200px");
  154. layout.addComponent(getBigComponent());
  155. getMainWindow().addWindow(w);
  156. }
  157. });
  158. return b;
  159. }
  160. private Component getDDSortableTableTest() {
  161. final Table table;
  162. table = new Table();
  163. table.setCaption("DD sortable table with context menus");
  164. // table.setWidth("100%");
  165. table.setPageLength(10);
  166. table.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  167. table.setSelectable(true);
  168. table.setMultiSelect(true);
  169. table.addActionHandler(new Handler() {
  170. Action[] actions = new Action[] { new Action("FOO"),
  171. new Action("BAR"), new Action("CAR") };
  172. @Override
  173. public Action[] getActions(Object target, Object sender) {
  174. return actions;
  175. }
  176. @Override
  177. public void handleAction(Action action, Object sender,
  178. Object target) {
  179. Notification.show(action.getCaption());
  180. }
  181. });
  182. populateTable(table);
  183. /*
  184. * Make table rows draggable
  185. */
  186. table.setDragMode(Table.TableDragMode.ROW);
  187. table.setDropHandler(new DropHandler() {
  188. // accept only drags from this table
  189. AcceptCriterion crit = new SourceIs(table);
  190. @Override
  191. public AcceptCriterion getAcceptCriterion() {
  192. return crit;
  193. }
  194. @Override
  195. public void drop(DragAndDropEvent dropEvent) {
  196. AbstractSelectTargetDetails dropTargetData = (AbstractSelectTargetDetails) dropEvent
  197. .getTargetDetails();
  198. DataBoundTransferable transferable = (DataBoundTransferable) dropEvent
  199. .getTransferable();
  200. Object itemIdOver = dropTargetData.getItemIdOver();
  201. Object itemId = transferable.getItemId();
  202. if (itemId == null || itemIdOver == null
  203. || itemId.equals(itemIdOver)) {
  204. return; // no move happened
  205. }
  206. // IndexedContainer goodies... (hint: don't use it in real apps)
  207. IndexedContainer containerDataSource = (IndexedContainer) table
  208. .getContainerDataSource();
  209. int newIndex = containerDataSource.indexOfId(itemIdOver) - 1;
  210. if (dropTargetData
  211. .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")
  261. .setValue(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. }