source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/com/gs3/testGXT/client/tree/MyTreeStoreExt.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 5.2 KB
Line 
1package com.gs3.testGXT.client.tree;
2
3import java.util.ArrayList;
4import java.util.LinkedList;
5import java.util.List;
6
7import com.google.gwt.core.client.GWT;
8import com.google.gwt.dom.client.Style.Cursor;
9import com.google.gwt.user.client.DOM;
10import com.google.gwt.user.client.Element;
11import com.google.gwt.user.client.rpc.AsyncCallback;
12import com.gs3.testGXT.client.Keys.FileSystemKey;
13import com.gs3.testGXT.client.services.ListDirectoryService;
14import com.gs3.testGXT.client.services.ListDirectoryServiceAsync;
15import com.sencha.gxt.data.shared.ModelKeyProvider;
16import com.sencha.gxt.data.shared.SortDir;
17import com.sencha.gxt.data.shared.Store;
18import com.sencha.gxt.data.shared.TreeStore;
19import 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 */
35public 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}
Note: See TracBrowser for help on using the repository browser.