]> source.dussan.org Git - vaadin-framework.git/blob
c8b3604fd65d97a7b17b2a819a446d159cddd4b6
[vaadin-framework.git] /
1 package com.vaadin.tests.containers.filesystemcontainer;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6
7 import com.vaadin.tests.components.TestBase;
8 import com.vaadin.tests.util.Log;
9 import com.vaadin.ui.Button;
10 import com.vaadin.ui.Button.ClickEvent;
11 import com.vaadin.ui.HorizontalLayout;
12 import com.vaadin.v7.data.Container;
13 import com.vaadin.v7.data.Container.Ordered;
14 import com.vaadin.v7.data.util.FilesystemContainer;
15 import com.vaadin.v7.ui.Table;
16 import com.vaadin.v7.ui.Tree.CollapseEvent;
17 import com.vaadin.v7.ui.Tree.CollapseListener;
18 import com.vaadin.v7.ui.Tree.ExpandEvent;
19 import com.vaadin.v7.ui.Tree.ExpandListener;
20 import com.vaadin.v7.ui.TreeTable;
21
22 public class FileSystemContainerInTreeTable extends TestBase {
23
24     private Log log = new Log(5);
25     private TreeTable treeTable;
26
27     @Override
28     protected void setup() {
29         setTheme("reindeer-tests");
30
31         final File folder;
32         try {
33             File tempFile = File.createTempFile("fsc-tt", "");
34             tempFile.delete();
35             folder = new File(tempFile.getParent(), tempFile.getName());
36             folder.mkdir();
37             System.out.println(folder.getPath());
38             folder.deleteOnExit();
39
40             populate(folder, 3, 10);
41
42             FilesystemContainer fsc = new FilesystemContainer(folder);
43
44             treeTable = new TreeTable();
45             treeTable.addStyleName("table-equal-rowheight");
46             treeTable.setWidth("450px");
47             treeTable.setHeight("550px");
48             treeTable.setContainerDataSource(fsc);
49             treeTable.setItemIconPropertyId(FilesystemContainer.PROPERTY_ICON);
50             treeTable.setVisibleColumns(new String[] { "Name" });
51             treeTable.setColumnWidth("Name", 400);
52             treeTable.addListener(new ExpandListener() {
53
54                 @Override
55                 public void nodeExpand(ExpandEvent event) {
56                     logExpandCollapse(event.getItemId(), "expanded");
57
58                 }
59             });
60             treeTable.addListener(new CollapseListener() {
61
62                 @Override
63                 public void nodeCollapse(CollapseEvent event) {
64                     logExpandCollapse(event.getItemId(), "collapsed");
65
66                 }
67             });
68
69             addComponent(log);
70             addComponent(treeTable);
71
72             HorizontalLayout buttonLayout = new HorizontalLayout();
73             buttonLayout.setSpacing(true);
74             buttonLayout.addComponent(
75                     new Button("Create dir11", new Button.ClickListener() {
76                         @Override
77                         public void buttonClick(ClickEvent event) {
78                             new File(folder, "dir11").mkdir();
79                             log.log("Row dir11 created");
80                         }
81                     }));
82             buttonLayout.addComponent(
83                     new Button("Delete dir11", new Button.ClickListener() {
84                         @Override
85                         public void buttonClick(ClickEvent event) {
86                             new File(folder, "dir11").delete();
87                             log.log("Row dir11 deleted");
88                         }
89                     }));
90             // to clean up explicitly before ending an automated test
91             buttonLayout.addComponent(
92                     new Button("Clean all files", new Button.ClickListener() {
93                         @Override
94                         public void buttonClick(ClickEvent event) {
95                             folder.delete();
96                         }
97                     }));
98             addComponent(buttonLayout);
99         } catch (IOException e) {
100             e.printStackTrace();
101         }
102     }
103
104     private void populate(File folder, int subDirectories, int files)
105             throws IOException {
106         for (int i = 1; i <= files; i++) {
107             File f = new File(folder, "file" + i + ".txt");
108             f.createNewFile();
109         }
110
111         for (int i = 1; i <= subDirectories; i++) {
112             File f = new File(folder, "dir" + i);
113             f.mkdir();
114             populate(f, 0, 2);
115         }
116     }
117
118     protected int indexOfId(Table source, Object itemId) {
119         Container.Ordered c = (Ordered) source.getContainerDataSource();
120         if (c instanceof Container.Indexed) {
121             return ((Container.Indexed) source).indexOfId(itemId);
122         } else {
123             ArrayList<Object> list = new ArrayList<Object>(source.getItemIds());
124             return list.indexOf(itemId);
125         }
126     }
127
128     protected void logExpandCollapse(Object itemId, String operation) {
129         File file = (File) itemId;
130         // do not use the variable part (path) of file name
131         log.log("Row " + file.getName() + " " + operation + ". Row index: "
132                 + indexOfId(treeTable, itemId));
133
134     }
135
136     @Override
137     protected String getDescription() {
138         return "TreeTable partial updates can only be used with a container that notifies the TreeTable of item set changes";
139     }
140
141     @Override
142     protected Integer getTicketNumber() {
143         return 7837;
144     }
145
146 }