1 | package com.gs3.testGXT.client.tree;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.LinkedList;
|
---|
5 |
|
---|
6 | import com.google.gwt.core.client.GWT;
|
---|
7 | import com.google.gwt.user.client.rpc.AsyncCallback;
|
---|
8 | import com.gs3.testGXT.client.Keys.FileSystemKey;
|
---|
9 | import com.gs3.testGXT.client.services.ListDirectoryService;
|
---|
10 | import com.gs3.testGXT.client.services.ListDirectoryServiceAsync;
|
---|
11 | import com.sencha.gxt.core.client.ValueProvider;
|
---|
12 | import com.sencha.gxt.data.shared.TreeStore;
|
---|
13 | import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
|
---|
14 | import com.sencha.gxt.widget.core.client.event.BeforeExpandItemEvent;
|
---|
15 | import com.sencha.gxt.widget.core.client.event.BeforeExpandItemEvent.BeforeExpandItemHandler;
|
---|
16 | import com.sencha.gxt.widget.core.client.tree.Tree;
|
---|
17 |
|
---|
18 | public class MyLocalFileSystemTree extends Tree<FileSystemKey, FileSystemKey> {
|
---|
19 | protected ListDirectoryServiceAsync listDirService = null;
|
---|
20 | AsyncCallback<LinkedList<String[]>> callback = null;
|
---|
21 |
|
---|
22 | public MyLocalFileSystemTree(MyTreeStoreExt store,
|
---|
23 | ValueProvider<? super FileSystemKey, FileSystemKey> valueProvider) {
|
---|
24 | super(store, valueProvider);
|
---|
25 | this.addBeforeExpandHandler(new myOtherBeforeExtendHandler(store));
|
---|
26 |
|
---|
27 | listDirService = GWT.create(ListDirectoryService.class);
|
---|
28 | }
|
---|
29 |
|
---|
30 | class myOtherBeforeExtendHandler implements BeforeExpandItemHandler<FileSystemKey> {
|
---|
31 | TreeStore<FileSystemKey> ts;
|
---|
32 | public myOtherBeforeExtendHandler(TreeStore<FileSystemKey> ts) {
|
---|
33 | this.ts = ts;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public void onBeforeExpand(BeforeExpandItemEvent<FileSystemKey> event) {
|
---|
38 | final FileSystemKey parent = event.getItem();
|
---|
39 | if(!parent.Walked()) {
|
---|
40 | final String key = parent.toKey();
|
---|
41 | final String path = parent.toPath();
|
---|
42 | ts.removeChildren(parent);
|
---|
43 |
|
---|
44 | callback = new AsyncCallback<LinkedList<String[]>>() {
|
---|
45 | public void onFailure(Throwable caught) {
|
---|
46 | AlertMessageBox amb = new AlertMessageBox("Error", "Error: " + caught.getMessage()); //TODO: this is going to be messy - fix
|
---|
47 | amb.show();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void onSuccess(LinkedList<String[]> result) {
|
---|
51 | updateStore(result, ts, parent, key, path);
|
---|
52 | parent.setWalked();
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | listDirService.getDirectoryContents(path, callback);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void refreshTree(FileSystemKey k) {
|
---|
62 | if(store instanceof MyTreeStoreExt) {
|
---|
63 | ((MyTreeStoreExt)store).Refresh(k, this);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void updateStore(LinkedList<String[]> result, TreeStore<FileSystemKey> ts, FileSystemKey parent, String key, String path) {
|
---|
68 | ArrayList<FileSystemKey> keylist = new ArrayList<FileSystemKey>(result.size());
|
---|
69 | for(String[] set : result) {
|
---|
70 | //structure:
|
---|
71 | // File/Folder
|
---|
72 | // Name
|
---|
73 | // Canonical Path
|
---|
74 | FileSystemKey child = null;
|
---|
75 | if(set[0].equals("File")) {
|
---|
76 | child = new FileSystemKey(key, set[1], false, true, set[2]); //key, name, file, realized, path
|
---|
77 | }
|
---|
78 | else if (set[0].equals("Folder")) {
|
---|
79 | child = new FileSystemKey(key, set[1], true, false, set[2]); //key, name, folder, not realized, path
|
---|
80 | }
|
---|
81 | keylist.add(child);
|
---|
82 | }
|
---|
83 | ts.add(parent, keylist);
|
---|
84 | //ts.update(parent);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected boolean hasChildren(FileSystemKey model) {
|
---|
89 | // This will control the leaf behavior: true for looking like a folder and false to look like an item
|
---|
90 | return model.isFolder();
|
---|
91 | }
|
---|
92 | }
|
---|