root / gss / src / gr / ebs / gss / client / tree / MyShareSubtree.java @ 895035a2
History | View | Annotate | Download (8.4 kB)
1 | 14ad7326 | pastith | /*
|
---|---|---|---|
2 | 14ad7326 | pastith | * Copyright 2008, 2009 Electronic Business Systems Ltd.
|
3 | 14ad7326 | pastith | *
|
4 | 14ad7326 | pastith | * This file is part of GSS.
|
5 | 14ad7326 | pastith | *
|
6 | 14ad7326 | pastith | * GSS is free software: you can redistribute it and/or modify
|
7 | 14ad7326 | pastith | * it under the terms of the GNU General Public License as published by
|
8 | 14ad7326 | pastith | * the Free Software Foundation, either version 3 of the License, or
|
9 | 14ad7326 | pastith | * (at your option) any later version.
|
10 | 14ad7326 | pastith | *
|
11 | 14ad7326 | pastith | * GSS is distributed in the hope that it will be useful,
|
12 | 14ad7326 | pastith | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 14ad7326 | pastith | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 14ad7326 | pastith | * GNU General Public License for more details.
|
15 | 14ad7326 | pastith | *
|
16 | 14ad7326 | pastith | * You should have received a copy of the GNU General Public License
|
17 | 14ad7326 | pastith | * along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 | 14ad7326 | pastith | */
|
19 | 14ad7326 | pastith | package gr.ebs.gss.client.tree; |
20 | 14ad7326 | pastith | |
21 | 14ad7326 | pastith | import gr.ebs.gss.client.GSS; |
22 | 14ad7326 | pastith | import gr.ebs.gss.client.PopupTree; |
23 | 14ad7326 | pastith | import gr.ebs.gss.client.Folders.Images; |
24 | 14ad7326 | pastith | import gr.ebs.gss.client.dnd.DnDTreeItem; |
25 | 895035a2 | pastith | import gr.ebs.gss.client.rest.GetCommand; |
26 | 895035a2 | pastith | import gr.ebs.gss.client.rest.MultipleGetCommand; |
27 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.FolderResource; |
28 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.SharedResource; |
29 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.UserResource; |
30 | 14ad7326 | pastith | |
31 | a52ea5e4 | pastith | import java.util.ArrayList; |
32 | 14ad7326 | pastith | import java.util.List; |
33 | 14ad7326 | pastith | |
34 | 14ad7326 | pastith | import com.google.gwt.core.client.GWT; |
35 | 14ad7326 | pastith | import com.google.gwt.user.client.DeferredCommand; |
36 | 14ad7326 | pastith | import com.google.gwt.user.client.IncrementalCommand; |
37 | 14ad7326 | pastith | import com.google.gwt.user.client.ui.TreeItem; |
38 | 14ad7326 | pastith | /**
|
39 | 14ad7326 | pastith | * @author kman
|
40 | 14ad7326 | pastith | */
|
41 | 14ad7326 | pastith | public class MyShareSubtree extends Subtree { |
42 | 14ad7326 | pastith | |
43 | 14ad7326 | pastith | /**
|
44 | 14ad7326 | pastith | * A constant that denotes the completion of an IncrementalCommand.
|
45 | 14ad7326 | pastith | */
|
46 | 14ad7326 | pastith | public static final boolean DONE = false; |
47 | 14ad7326 | pastith | |
48 | 14ad7326 | pastith | private DnDTreeItem rootItem;
|
49 | 14ad7326 | pastith | |
50 | 14ad7326 | pastith | public MyShareSubtree(PopupTree tree, final Images _images) { |
51 | a52ea5e4 | pastith | super(tree, _images);
|
52 | 14ad7326 | pastith | |
53 | 14ad7326 | pastith | DeferredCommand.addCommand(new IncrementalCommand() {
|
54 | 14ad7326 | pastith | |
55 | 14ad7326 | pastith | public boolean execute() { |
56 | 14ad7326 | pastith | return updateInit();
|
57 | 14ad7326 | pastith | } |
58 | 14ad7326 | pastith | }); |
59 | 14ad7326 | pastith | } |
60 | 14ad7326 | pastith | |
61 | 14ad7326 | pastith | public boolean updateInit() { |
62 | a52ea5e4 | pastith | UserResource userResource = GSS.get().getCurrentUserResource(); |
63 | a52ea5e4 | pastith | if (userResource == null || GSS.get().getFolders().getRootItem() == null || GSS.get().getFolders().getTrashItem() == null) |
64 | 14ad7326 | pastith | return !DONE;
|
65 | 14ad7326 | pastith | |
66 | 895035a2 | pastith | GetCommand<SharedResource> gs = new GetCommand<SharedResource>(SharedResource.class, userResource.getSharedPath()) {
|
67 | 14ad7326 | pastith | |
68 | a52ea5e4 | pastith | public void onComplete() { |
69 | a52ea5e4 | pastith | rootItem = new DnDTreeItem(imageItemHTML(images.myShared(), "My Shared"), "My Shared", false); |
70 | a52ea5e4 | pastith | rootItem.setUserObject(getResult()); |
71 | 14ad7326 | pastith | tree.addItem(rootItem); |
72 | 14ad7326 | pastith | rootItem.removeItems(); |
73 | a52ea5e4 | pastith | rootItem.doDroppable(); |
74 | a52ea5e4 | pastith | update(rootItem); |
75 | 14ad7326 | pastith | } |
76 | 14ad7326 | pastith | |
77 | a52ea5e4 | pastith | public void onError(Throwable t) { |
78 | a52ea5e4 | pastith | GWT.log("Error fetching Shared Root folder", t);
|
79 | a52ea5e4 | pastith | GSS.get().displayError("Unable to fetch Shared Root folder");
|
80 | a52ea5e4 | pastith | if(rootItem != null){ |
81 | a52ea5e4 | pastith | rootItem = new DnDTreeItem(imageItemHTML(images.myShared(), "ERROR"), "ERROR", false); |
82 | a52ea5e4 | pastith | tree.addItem(rootItem); |
83 | a52ea5e4 | pastith | } |
84 | 14ad7326 | pastith | } |
85 | a52ea5e4 | pastith | }; |
86 | a52ea5e4 | pastith | DeferredCommand.addCommand(gs); |
87 | 14ad7326 | pastith | return DONE;
|
88 | 14ad7326 | pastith | |
89 | 14ad7326 | pastith | } |
90 | 14ad7326 | pastith | |
91 | a52ea5e4 | pastith | public void update(final DnDTreeItem folderItem) { |
92 | a52ea5e4 | pastith | if (folderItem.getFolderResource() != null) { |
93 | a52ea5e4 | pastith | folderItem.removeItems(); |
94 | a52ea5e4 | pastith | List<String> newPaths = new ArrayList<String>(); |
95 | a52ea5e4 | pastith | for (String s : folderItem.getFolderResource().getSubfolderPaths()) { |
96 | 14ad7326 | pastith | |
97 | a52ea5e4 | pastith | if (!s.endsWith("/")) |
98 | a52ea5e4 | pastith | s = s + "/";
|
99 | a52ea5e4 | pastith | if (rootItem.getSharedResource().getSubfolderPaths().contains(s))
|
100 | a52ea5e4 | pastith | newPaths.add(s); |
101 | a52ea5e4 | pastith | } |
102 | a52ea5e4 | pastith | String parentName = ""; |
103 | a52ea5e4 | pastith | if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null) |
104 | a52ea5e4 | pastith | parentName = ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getName()+"->";
|
105 | a52ea5e4 | pastith | parentName = parentName+folderItem.getFolderResource().getName(); |
106 | a52ea5e4 | pastith | folderItem.getFolderResource().setSubfolderPaths(newPaths); |
107 | 895035a2 | pastith | MultipleGetCommand<FolderResource> gf = new MultipleGetCommand<FolderResource>(FolderResource.class, newPaths.toArray(new String[] {})) { |
108 | 14ad7326 | pastith | |
109 | a52ea5e4 | pastith | public void onComplete() { |
110 | a52ea5e4 | pastith | List<FolderResource> res = getResult();
|
111 | a52ea5e4 | pastith | for (FolderResource r : res) {
|
112 | a52ea5e4 | pastith | |
113 | a52ea5e4 | pastith | DnDTreeItem child = (DnDTreeItem) addImageItem(folderItem, r.getName(), images.folderYellow(), true);
|
114 | a52ea5e4 | pastith | child.setUserObject(r); |
115 | a52ea5e4 | pastith | child.setState(false);
|
116 | a52ea5e4 | pastith | child.doDraggable(); |
117 | 903c2182 | koutsoub | if(folderItem.getState())
|
118 | 903c2182 | koutsoub | update(child); |
119 | a52ea5e4 | pastith | } |
120 | a52ea5e4 | pastith | |
121 | a52ea5e4 | pastith | } |
122 | a52ea5e4 | pastith | |
123 | a52ea5e4 | pastith | public void onError(Throwable t) { |
124 | a52ea5e4 | pastith | GSS.get().displayError("Unable to fetch subfolders");
|
125 | a52ea5e4 | pastith | GWT.log("Unable to fetch subfolders", t);
|
126 | 14ad7326 | pastith | } |
127 | 14ad7326 | pastith | |
128 | a52ea5e4 | pastith | @Override
|
129 | a52ea5e4 | pastith | public void onError(String p, Throwable throwable) { |
130 | a52ea5e4 | pastith | GWT.log("Path:"+p, throwable);
|
131 | 14ad7326 | pastith | } |
132 | a52ea5e4 | pastith | }; |
133 | a52ea5e4 | pastith | DeferredCommand.addCommand(gf); |
134 | 14ad7326 | pastith | } |
135 | a52ea5e4 | pastith | if (folderItem.getSharedResource() != null) { |
136 | a52ea5e4 | pastith | folderItem.removeItems(); |
137 | a52ea5e4 | pastith | List<String> paths = folderItem.getSharedResource().getSubfolderPaths(); |
138 | a52ea5e4 | pastith | List<String> newPaths = new ArrayList<String>(); |
139 | a52ea5e4 | pastith | for (String r : paths) |
140 | a52ea5e4 | pastith | if (isRoot(r, paths))
|
141 | a52ea5e4 | pastith | newPaths.add(r); |
142 | 895035a2 | pastith | MultipleGetCommand<FolderResource> gf = new MultipleGetCommand<FolderResource>(FolderResource.class, newPaths.toArray(new String[] {})) { |
143 | a52ea5e4 | pastith | |
144 | a52ea5e4 | pastith | public void onComplete() { |
145 | a52ea5e4 | pastith | List<FolderResource> res = getResult();
|
146 | a52ea5e4 | pastith | for (FolderResource r : res) {
|
147 | a52ea5e4 | pastith | DnDTreeItem child = (DnDTreeItem) addImageItem(folderItem, r.getName(), images.folderYellow(), true);
|
148 | a52ea5e4 | pastith | child.setUserObject(r); |
149 | a52ea5e4 | pastith | child.setState(false);
|
150 | a52ea5e4 | pastith | child.doDraggable(); |
151 | 903c2182 | koutsoub | update(child); |
152 | a52ea5e4 | pastith | } |
153 | a52ea5e4 | pastith | |
154 | 14ad7326 | pastith | } |
155 | 14ad7326 | pastith | |
156 | a52ea5e4 | pastith | public void onError(Throwable t) { |
157 | a52ea5e4 | pastith | GSS.get().displayError("Unable to fetch subfolders");
|
158 | a52ea5e4 | pastith | GWT.log("Unable to fetch subfolders", t);
|
159 | 14ad7326 | pastith | } |
160 | a52ea5e4 | pastith | |
161 | a52ea5e4 | pastith | @Override
|
162 | a52ea5e4 | pastith | public void onError(String p, Throwable throwable) { |
163 | a52ea5e4 | pastith | GWT.log("Path:"+p, throwable);
|
164 | a52ea5e4 | pastith | } |
165 | a52ea5e4 | pastith | }; |
166 | a52ea5e4 | pastith | DeferredCommand.addCommand(gf); |
167 | 14ad7326 | pastith | } |
168 | a52ea5e4 | pastith | } |
169 | a52ea5e4 | pastith | |
170 | a52ea5e4 | pastith | private void handleFolders(DnDTreeItem child, FolderResource ff, List<FolderResource> folders) { |
171 | 14ad7326 | pastith | |
172 | 14ad7326 | pastith | } |
173 | 14ad7326 | pastith | |
174 | a52ea5e4 | pastith | private boolean isRoot(FolderResource f, List<FolderResource> folders) { |
175 | a52ea5e4 | pastith | for (FolderResource t : folders)
|
176 | 555e8e59 | pastith | if (!f.getUri().equals(t.getUri()) && f.getUri().startsWith(t.getUri()))
|
177 | a52ea5e4 | pastith | return false; |
178 | a52ea5e4 | pastith | return true; |
179 | a52ea5e4 | pastith | } |
180 | 14ad7326 | pastith | |
181 | a52ea5e4 | pastith | private boolean isRoot(String f, List<String> folders) { |
182 | a52ea5e4 | pastith | for (String t : folders) |
183 | a52ea5e4 | pastith | if (!f.equals(t) && f.startsWith(t))
|
184 | a52ea5e4 | pastith | return false; |
185 | a52ea5e4 | pastith | return true; |
186 | a52ea5e4 | pastith | } |
187 | a52ea5e4 | pastith | |
188 | a52ea5e4 | pastith | |
189 | a52ea5e4 | pastith | |
190 | a52ea5e4 | pastith | public void updateFolderAndSubfolders(final DnDTreeItem folderItem) { |
191 | a52ea5e4 | pastith | if (folderItem.getFolderResource() != null) { |
192 | 555e8e59 | pastith | final String path = folderItem.getFolderResource().getUri(); |
193 | 895035a2 | pastith | GetCommand<SharedResource> gs = new GetCommand<SharedResource>(SharedResource.class, GSS.get().getCurrentUserResource().getSharedPath()) {
|
194 | a52ea5e4 | pastith | |
195 | a52ea5e4 | pastith | public void onComplete() { |
196 | a52ea5e4 | pastith | rootItem.setUserObject(getResult()); |
197 | 895035a2 | pastith | GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path) {
|
198 | a52ea5e4 | pastith | |
199 | a52ea5e4 | pastith | public void onComplete() { |
200 | a52ea5e4 | pastith | FolderResource rootResource = getResult(); |
201 | 555e8e59 | pastith | if(rootItem.getSharedResource().getSubfolderPaths().contains(rootResource.getUri())){
|
202 | a52ea5e4 | pastith | folderItem.undoDraggable(); |
203 | a52ea5e4 | pastith | folderItem.updateWidget(imageItemHTML(images.folderYellow(), rootResource.getName())); |
204 | a52ea5e4 | pastith | folderItem.setUserObject(rootResource); |
205 | a52ea5e4 | pastith | folderItem.doDraggable(); |
206 | a52ea5e4 | pastith | update(folderItem); |
207 | a52ea5e4 | pastith | } else
|
208 | a52ea5e4 | pastith | folderItem.getParentItem().removeItem(folderItem); |
209 | a52ea5e4 | pastith | } |
210 | a52ea5e4 | pastith | |
211 | a52ea5e4 | pastith | public void onError(Throwable t) { |
212 | a52ea5e4 | pastith | GWT.log("Error fetching folder", t);
|
213 | a52ea5e4 | pastith | GSS.get().displayError("Unable to fetch folder:" + folderItem.getFolderResource().getName());
|
214 | a52ea5e4 | pastith | } |
215 | a52ea5e4 | pastith | }; |
216 | a52ea5e4 | pastith | DeferredCommand.addCommand(gf); |
217 | a52ea5e4 | pastith | } |
218 | a52ea5e4 | pastith | |
219 | a52ea5e4 | pastith | public void onError(Throwable t) { |
220 | a52ea5e4 | pastith | GWT.log("Error fetching Shared Root folder", t);
|
221 | a52ea5e4 | pastith | GSS.get().displayError("Unable to fetch Shared Root folder");
|
222 | a52ea5e4 | pastith | } |
223 | a52ea5e4 | pastith | }; |
224 | a52ea5e4 | pastith | DeferredCommand.addCommand(gs); |
225 | a52ea5e4 | pastith | |
226 | a52ea5e4 | pastith | } |
227 | a52ea5e4 | pastith | else if( folderItem.getSharedResource() != null){ |
228 | 895035a2 | pastith | GetCommand<SharedResource> gs = new GetCommand<SharedResource>(SharedResource.class, GSS.get().getCurrentUserResource().getSharedPath()) {
|
229 | a52ea5e4 | pastith | |
230 | a52ea5e4 | pastith | public void onComplete() { |
231 | a52ea5e4 | pastith | rootItem.setUserObject(getResult()); |
232 | a52ea5e4 | pastith | rootItem.removeItems(); |
233 | a52ea5e4 | pastith | update(rootItem); |
234 | a52ea5e4 | pastith | } |
235 | a52ea5e4 | pastith | |
236 | a52ea5e4 | pastith | public void onError(Throwable t) { |
237 | a52ea5e4 | pastith | GWT.log("Error fetching Shared Root folder", t);
|
238 | a52ea5e4 | pastith | GSS.get().displayError("Unable to fetch Shared Root folder");
|
239 | a52ea5e4 | pastith | } |
240 | a52ea5e4 | pastith | }; |
241 | a52ea5e4 | pastith | DeferredCommand.addCommand(gs); |
242 | a52ea5e4 | pastith | } |
243 | 14ad7326 | pastith | |
244 | 14ad7326 | pastith | } |
245 | a52ea5e4 | pastith | |
246 | 14ad7326 | pastith | /**
|
247 | 14ad7326 | pastith | * Retrieve the rootItem.
|
248 | 14ad7326 | pastith | *
|
249 | 14ad7326 | pastith | * @return the rootItem
|
250 | 14ad7326 | pastith | */
|
251 | 14ad7326 | pastith | public TreeItem getRootItem() {
|
252 | 14ad7326 | pastith | return rootItem;
|
253 | 14ad7326 | pastith | } |
254 | 14ad7326 | pastith | |
255 | a52ea5e4 | pastith | |
256 | a52ea5e4 | pastith | |
257 | a52ea5e4 | pastith | |
258 | a52ea5e4 | pastith | public void updateNode(TreeItem node, FolderResource folder) { |
259 | 14ad7326 | pastith | node.getWidget().removeStyleName("gss-SelectedRow");
|
260 | a52ea5e4 | pastith | if (node instanceof DnDTreeItem) { |
261 | 14ad7326 | pastith | ((DnDTreeItem) node).undoDraggable(); |
262 | a52ea5e4 | pastith | ((DnDTreeItem) node).updateWidget(imageItemHTML(images.folderYellow(), folder.getName())); |
263 | 14ad7326 | pastith | ((DnDTreeItem) node).doDraggable(); |
264 | a52ea5e4 | pastith | } else
|
265 | a52ea5e4 | pastith | node.setWidget(imageItemHTML(images.folderYellow(), folder.getName())); |
266 | 14ad7326 | pastith | node.setUserObject(folder); |
267 | 14ad7326 | pastith | } |
268 | 14ad7326 | pastith | } |