1 | package com.gs3.testGXT.client.tree;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import com.google.gwt.core.client.GWT;
|
---|
8 | import com.google.gwt.dom.client.Style.Cursor;
|
---|
9 | import com.google.gwt.user.client.DOM;
|
---|
10 | import com.google.gwt.user.client.Element;
|
---|
11 | import com.google.gwt.user.client.rpc.AsyncCallback;
|
---|
12 | import com.gs3.testGXT.client.Keys.FileSystemKey;
|
---|
13 | import com.gs3.testGXT.client.services.ListDirectoryService;
|
---|
14 | import com.gs3.testGXT.client.services.ListDirectoryServiceAsync;
|
---|
15 | import com.sencha.gxt.data.shared.ModelKeyProvider;
|
---|
16 | import com.sencha.gxt.data.shared.SortDir;
|
---|
17 | import com.sencha.gxt.data.shared.Store;
|
---|
18 | import com.sencha.gxt.data.shared.TreeStore;
|
---|
19 | import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * A {@link Store} for hierarchical data. Parent-Child relationships are tracked
|
---|
23 | * internally, and can be accessed through the {@link #getParent(Object)} and
|
---|
24 | * {@link #getChildren(Object)} calls, and modified through the many add()
|
---|
25 | * overrides or the {@link #remove(Object)} method.
|
---|
26 | *
|
---|
27 | * As with {@link ListStore}, all changes and data are relative to what is
|
---|
28 | * currently visible, due to the {@link Store.StoreFilter}s. As such, if filters
|
---|
29 | * are active and structural changes are required, it might be necessary to
|
---|
30 | * disable filters to make the change, re-enabling them when finished.
|
---|
31 | *
|
---|
32 | * @param <FileSystemKey>
|
---|
33 | * the model type
|
---|
34 | */
|
---|
35 | public class MyTreeStoreExt extends TreeStore<FileSystemKey> {
|
---|
36 |
|
---|
37 | protected ListDirectoryServiceAsync listDirService = null;
|
---|
38 | AsyncCallback<LinkedList<String[]>> callback = null;
|
---|
39 |
|
---|
40 |
|
---|
41 | public MyTreeStoreExt(ModelKeyProvider<? super FileSystemKey> keyProvider) {
|
---|
42 | super(keyProvider);
|
---|
43 |
|
---|
44 | StoreSortInfo<FileSystemKey> info = new StoreSortInfo<FileSystemKey>(FileSystemKey.getComparator(), SortDir.ASC);
|
---|
45 | this.addSortInfo(info);
|
---|
46 |
|
---|
47 | listDirService = GWT.create(ListDirectoryService.class);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void Refresh(final FileSystemKey fsk, final MyLocalFileSystemTree tree) {
|
---|
51 | final String key = fsk.toKey();
|
---|
52 | final String path = fsk.toPath();
|
---|
53 |
|
---|
54 | final boolean wasExpanded = fsk.isFolder() && tree.isExpanded(fsk);
|
---|
55 |
|
---|
56 | AsyncCallback<LinkedList<String[]>> callback = new AsyncCallback<LinkedList<String[]>>() {
|
---|
57 | public void onFailure(Throwable caught) {
|
---|
58 | AlertMessageBox amb = new AlertMessageBox("Error", "Error: " + caught.getMessage()); //TODO: this is going to be messy - fix
|
---|
59 | amb.show();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void onSuccess(LinkedList<String[]> result) {
|
---|
63 |
|
---|
64 | //no need for this check I think
|
---|
65 | if(fsk.isFolder()) {
|
---|
66 | conservativeUpdateChildren(result, fsk, key, path, tree);
|
---|
67 |
|
---|
68 | fsk.setWalked();
|
---|
69 | tree.setExpanded(fsk, wasExpanded);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | listDirService.getDirectoryContents(fsk.toPath(), callback);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | //essentially just recursive descent model checking
|
---|
79 | private void conservativeUpdateChildren(LinkedList<String[]> result, FileSystemKey parent, String key, String path, final MyLocalFileSystemTree tree) {
|
---|
80 | List<FileSystemKey> children = this.getChildren(parent);
|
---|
81 | ArrayList<FileSystemKey> itemsToAdd = new ArrayList<FileSystemKey>();
|
---|
82 | ArrayList<FileSystemKey> itemsToRefresh = new ArrayList<FileSystemKey>();
|
---|
83 | ArrayList<FileSystemKey> itemsToRemove = new ArrayList<FileSystemKey>();
|
---|
84 |
|
---|
85 | itemsToRemove.addAll(children);
|
---|
86 |
|
---|
87 | for(String[] set : result) {
|
---|
88 | //structure:
|
---|
89 | // File/Folder
|
---|
90 | // Name
|
---|
91 | // Canonical Path
|
---|
92 | FileSystemKey child = null;
|
---|
93 |
|
---|
94 | if(set[0].equals("File")) {
|
---|
95 | child = new FileSystemKey(key, set[1], false, true, set[2]); //key, name, file, realized, path
|
---|
96 | }
|
---|
97 | else if (set[0].equals("Folder")) {
|
---|
98 | child = new FileSystemKey(key, set[1], true, false, set[2]); //key, name, folder, not realized, path
|
---|
99 | }
|
---|
100 |
|
---|
101 | //AlertMessageBox test = new AlertMessageBox("test", "test: " + children.size());
|
---|
102 | //test.show();
|
---|
103 |
|
---|
104 | int ind;
|
---|
105 | if((ind = children.indexOf(child)) == -1) {
|
---|
106 | //then we add the item
|
---|
107 | itemsToAdd.add(child);
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | child = children.get(ind);
|
---|
111 | if(child.Walked()) {//only update items that we expect to be up to date anyway
|
---|
112 | itemsToRefresh.add(child);
|
---|
113 | }
|
---|
114 |
|
---|
115 | //the item already exists - we should remove it from our list of exclusions
|
---|
116 | itemsToRemove.remove(child);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*delete all items that were removed from the set -
|
---|
121 | * we know this because they didn't show up in the list of items from the server*/
|
---|
122 | for(FileSystemKey child : itemsToRemove) {
|
---|
123 | remove(child);
|
---|
124 |
|
---|
125 | //we should probably do some sort of check here
|
---|
126 | }
|
---|
127 |
|
---|
128 | add(parent, itemsToAdd);
|
---|
129 |
|
---|
130 | for(FileSystemKey f: itemsToRefresh) {
|
---|
131 | Refresh(f, tree);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | //this is the bad of way of doing this - it is preserved just incase there is some issue with the good way
|
---|
136 | private void updateChildren(LinkedList<String[]> result, FileSystemKey parent, String key, String path) {
|
---|
137 | ArrayList<FileSystemKey> keylist = new ArrayList<FileSystemKey>(result.size());
|
---|
138 | for(String[] set : result) {
|
---|
139 | //structure:
|
---|
140 | // File/Folder
|
---|
141 | // Name
|
---|
142 | // Canonical Path
|
---|
143 | FileSystemKey child = null;
|
---|
144 | if(set[0].equals("File")) {
|
---|
145 | child = new FileSystemKey(key, set[1], false, true, set[2]); //key, name, file, realized, path
|
---|
146 | }
|
---|
147 | else if (set[0].equals("Folder")) {
|
---|
148 | child = new FileSystemKey(key, set[1], true, false, set[2]); //key, name, folder, not realized, path
|
---|
149 | }
|
---|
150 | keylist.add(child);
|
---|
151 | }
|
---|
152 | add(parent, keylist);
|
---|
153 | }
|
---|
154 | } |
---|