select row on right click
[pithos] / src / gr / ebs / gss / client / Folders.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.dnd.DnDTreeItem;
22 import gr.ebs.gss.client.rest.resource.FolderResource;
23 import gr.ebs.gss.client.rest.resource.OtherUserResource;
24 import gr.ebs.gss.client.rest.resource.RestResource;
25 import gr.ebs.gss.client.tree.FolderSubtree;
26 import gr.ebs.gss.client.tree.MyShareSubtree;
27 import gr.ebs.gss.client.tree.OthersSharesSubtree;
28 import gr.ebs.gss.client.tree.TrashSubtree;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import com.google.gwt.resources.client.ClientBundle;
34 import com.google.gwt.resources.client.ImageResource;
35 import com.google.gwt.user.client.Command;
36 import com.google.gwt.user.client.DeferredCommand;
37 import com.google.gwt.user.client.ui.Composite;
38 import com.google.gwt.user.client.ui.Tree;
39 import com.google.gwt.user.client.ui.TreeItem;
40 /**
41  * A tree displaying the folders in the user's file space.
42  */
43 public class Folders extends Composite {
44
45         /**
46          * A constant that denotes the completion of an IncrementalCommand.
47          */
48         public static final boolean DONE = false;
49
50         /**
51          * Specifies the images that will be bundled for this Composite and other
52          * inherited images that will be included in the same bundle.
53          */
54         public interface Images extends ClientBundle,Tree.Resources, FolderContextMenu.Images {
55
56                 @Source("gr/ebs/gss/resources/folder_home.png")
57                 ImageResource home();
58
59                 @Source("gr/ebs/gss/resources/folder_yellow.png")
60                 ImageResource folderYellow();
61
62                 @Source("gr/ebs/gss/resources/mimetypes/document.png")
63                 ImageResource document();
64
65                 @Source("gr/ebs/gss/resources/internet.png")
66                 ImageResource othersShared();
67
68                 @Source("gr/ebs/gss/resources/edit_user.png")
69                 ImageResource myShared();
70
71                 @Source("gr/ebs/gss/resources/folder_user.png")
72                 ImageResource sharedFolder();
73
74                 @Source("gr/ebs/gss/resources/trashcan_empty.png")
75                 ImageResource trash();
76         }
77
78         /**
79          * The widget's image bundle.
80          */
81         private final Images images;
82
83         /**
84          * The tree widget that displays the folder namespace.
85          */
86         private PopupTree tree;
87
88         /**
89          * A cached copy of the currently selected folder widget.
90          */
91         private FolderSubtree folderSubtree;
92
93         private TrashSubtree trashSubtree;
94
95         private MyShareSubtree myShareSubtree;
96
97         private OthersSharesSubtree othersSharesSubtree;
98
99         /**
100          * Constructs a new folders widget with a bundle of images.
101          *
102          * @param _images a bundle that provides the images for this widget
103          */
104         public Folders(final Images _images) {
105                 images = _images;
106                 tree = new PopupTree(images);
107                 tree.setAnimationEnabled(true);
108                 initWidget(tree);
109                 folderSubtree = new FolderSubtree(tree, images);
110                 myShareSubtree = new MyShareSubtree(tree, images);
111                 trashSubtree = new TrashSubtree(tree, images);
112                 othersSharesSubtree = new OthersSharesSubtree(tree, images);
113         }
114
115         public Images getImages() {
116                 return images;
117         }
118
119         public void select(TreeItem item) {
120                 tree.processItemSelected(item);
121         }
122
123         public void clearSelection() {
124                 tree.clearSelection();
125         }
126
127         public void update(TreeItem item) {
128                 if (isFileItem(item))
129                         folderSubtree.updateSubfolders((DnDTreeItem) item);
130                 else if (isTrash(item))
131                         trashSubtree.update();
132                 else if (isMySharedItem(item))
133                         myShareSubtree.update((DnDTreeItem) item);
134                 else if (isOthersSharedItem(item))
135                         othersSharesSubtree.update((DnDTreeItem) item);
136         }
137
138         public void updateFolder(final DnDTreeItem folderItem) {
139                 if (isFileItem(folderItem)){
140                         folderSubtree.updateFolderAndSubfolders(folderItem);
141                         myShareSubtree.updateFolderAndSubfolders((DnDTreeItem) getMySharesItem());
142                 }
143                 else if (isMySharedItem(folderItem)){
144                         myShareSubtree.updateFolderAndSubfolders(folderItem);
145                         if (folderItem.getFolderResource() != null) {
146                                 DnDTreeItem fitem = (DnDTreeItem) getUserItem(getRootItem(), folderItem.getFolderResource().getUri());
147                                 if (fitem != null)
148                                         folderSubtree.updateFolderAndSubfolders(fitem);
149                                 else
150                                         folderSubtree.updateFolderAndSubfolders((DnDTreeItem) getRootItem());
151                         }
152
153                 }
154                 else if (isTrashItem(folderItem))
155                         trashSubtree.update();
156                 else if (isOthersSharedItem(folderItem))
157                         othersSharesSubtree.updateFolderAndSubfolders(folderItem);
158         }
159
160         /**
161          * Retrieve the current.
162          *
163          * @return the current
164          */
165         public TreeItem getCurrent() {
166                 return tree.getTreeSelectedItem();
167         }
168
169         /**
170          * Modify the current.
171          *
172          * @param _current the current to set
173          */
174         void setCurrent(final TreeItem _current) {
175                 tree.setTreeSelectedItem(_current);
176         }
177
178         /**
179          * Checks whether a TreeItem is contained in the root folder structure
180          *
181          * @param item The TreeItem to check
182          */
183         public boolean isFileItem(TreeItem item) {
184                 if (getRootOfItem(item).equals(getRootItem()))
185                         return true;
186                 return false;
187         }
188
189         /**
190          * Checks whether a TreeItem is contained in the trash folder structure
191          *
192          * @param item The TreeItem to check
193          */
194         public boolean isTrashItem(TreeItem item) {
195                 if (getRootOfItem(item).equals(getTrashItem()))
196                         return true;
197                 return false;
198         }
199
200         /**
201          * Checks whether a TreeItem is contained in the trash folder structure
202          *
203          * @param item The TreeItem to check
204          */
205         public boolean isOthersSharedItem(TreeItem item) {
206                 if (getRootOfItem(item).equals(getSharesItem()))
207                         return true;
208                 return false;
209         }
210
211         /**
212          * Checks whether a TreeItem is contained in the trash folder structure
213          *
214          * @param item The TreeItem to check
215          */
216         public boolean isMySharedItem(TreeItem item) {
217                 if (getRootOfItem(item).equals(getMySharesItem()))
218                         return true;
219                 return false;
220         }
221
222         private TreeItem getRootOfItem(TreeItem item) {
223                 if (item.getParentItem() == null)
224                         return item;
225                 TreeItem toCheck = item;
226                 while (toCheck.getParentItem() != null) {
227                         toCheck = toCheck.getParentItem();
228                         toCheck = getRootOfItem(toCheck);
229                 }
230                 return toCheck;
231         }
232
233         public TreeItem getUserOfSharedItem(TreeItem item) {
234                 if (item.getUserObject() instanceof OtherUserResource)
235                         return item;
236                 TreeItem test = item;
237                 while (test.getParentItem() != null) {
238                         test = test.getParentItem();
239                         if (test.getUserObject() instanceof OtherUserResource)
240                                 return test;
241                 }
242                 return null;
243         }
244
245         public boolean isTrash(TreeItem item) {
246                 return item.equals(getTrashItem());
247         }
248
249         public boolean isMyShares(TreeItem item) {
250                 return item.equals(getMySharesItem());
251         }
252
253         public boolean isOthersShared(TreeItem item) {
254                 return item.equals(getSharesItem());
255         }
256
257         /*
258          * Returns the Tree Item corresponding to the FolderDTO object
259          * since we need to update main file structure for untrashed folders
260          */
261         public TreeItem getUserItem(FolderResource folder) {
262                 return getUserItem(getRootItem(), folder);
263         }
264
265         public TreeItem getOtherSharedItem(FolderResource folder) {
266                 return getUserItem(getSharesItem(), folder);
267         }
268
269         private TreeItem getUserItem(TreeItem parent, FolderResource folder) {
270                 TreeItem tmp = null;
271                 if (parent.getUserObject() instanceof FolderResource &&
272                                         (parent.getUserObject().equals(folder) ||
273                                         ((FolderResource) parent.getUserObject()).getUri().equals(folder.getUri())))
274                         return parent;
275                 for (int i = 0; i < parent.getChildCount(); i++) {
276                         TreeItem child = parent.getChild(i);
277                         if (child.getUserObject() instanceof FolderResource) {
278                                 FolderResource dto = (FolderResource) child.getUserObject();
279                                 if (dto.equals(folder) || dto.getUri().equals(folder.getUri()))
280                                         return child;
281                         }
282                         tmp = getUserItem(child, folder);
283                         if (tmp != null)
284                                 return tmp;
285                 }
286                 return null;
287         }
288
289         /**
290          * Retrieve the trashItem.
291          *
292          * @return the trashItem
293          */
294         public TreeItem getTrashItem() {
295                 return trashSubtree.getRootItem();
296         }
297
298         /**
299          * Retrieve the rootItem.
300          *
301          * @return the rootItem
302          */
303         public TreeItem getRootItem() {
304                 return folderSubtree.getRootItem();
305         }
306
307         /**
308          * Retrieve the mySharesItem.
309          *
310          * @return the mySharesItem
311          */
312         public TreeItem getMySharesItem() {
313                 return myShareSubtree.getRootItem();
314         }
315
316         /**
317          * Retrieve the sharesItem.
318          *
319          * @return the sharesItem
320          */
321         public TreeItem getSharesItem() {
322                 return othersSharesSubtree.getRootItem();
323         }
324
325         public void onFolderTrash(TreeItem folder) {
326                 if (folder.getParentItem().getUserObject() instanceof FolderResource) {
327                         FolderResource folderDTO = (FolderResource) folder.getParentItem().getUserObject();
328                         updateFileAndShareNodes(folderDTO);
329                 } else
330                         update(getMySharesItem());
331                 update(getTrashItem());
332                 clearSelection();
333                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
334         }
335
336         public void onFolderDelete(TreeItem folder) {
337                 if (folder.getParentItem().getUserObject() instanceof FolderResource) {
338                         FolderResource folderDTO = (FolderResource) folder.getParentItem().getUserObject();
339                         updateFileAndShareNodes(folderDTO);
340                 } else
341                         update(getMySharesItem());
342                 GSS.get().getStatusPanel().updateStats();
343                 clearSelection();
344                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
345         }
346
347         public void onFolderCopy(TreeItem folder) {
348                 if (!updateFileAndShareNodes((FolderResource) folder.getUserObject()))
349                         update(folder);
350                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
351                 GSS.get().getStatusPanel().updateStats();
352         }
353
354         public void onFolderMove(TreeItem folder, FolderResource initialParent) {
355                 updateFileAndShareNodes(initialParent);
356                 updateFileAndShareNodes((FolderResource) folder.getUserObject());
357                 update(folder);
358                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
359                 GSS.get().getStatusPanel().updateStats();
360                 clearSelection();
361         }
362
363         private boolean updateFileAndShareNodes(FolderResource folder) {
364                 boolean updated = false;
365                 TreeItem sharesFolder = getUserItem(getMySharesItem(), folder);
366                 if (sharesFolder != null) {
367                         update(sharesFolder);
368                         updated = true;
369                 }
370                 TreeItem fileFolder = getUserItem(getRootItem(), folder);
371                 if (fileFolder != null) {
372                         update(fileFolder);
373                         updated = true;
374                 }
375                 return updated;
376         }
377
378         public void initialize() {
379                 DeferredCommand.addCommand(new Command() {
380
381                         @Override
382                         public void execute() {
383                                 GSS.get().showLoadingIndicator();
384                                 folderSubtree.getRootItem().removeItems();
385                                 trashSubtree.getRootItem().removeItems();
386                                 myShareSubtree.getRootItem().removeItems();
387                                 othersSharesSubtree.getRootItem().removeItems();
388                                 update(folderSubtree.getRootItem());
389                                 update(trashSubtree.getRootItem());
390                                 update(myShareSubtree.getRootItem());
391                                 update(othersSharesSubtree.getRootItem());
392                                 GSS.get().setCurrentSelection(null);
393                                 clearSelection();
394                                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
395                                 GSS.get().hideLoadingIndicator();
396                         }
397
398                 });
399         }
400
401         /* NEW HANDLING METHODS */
402         public TreeItem getUserItem(TreeItem parent, String path) {
403                 TreeItem tmp = null;
404                 if (parent.getUserObject() instanceof RestResource && ((RestResource) parent.getUserObject()).getUri().equals(path))
405                         return parent;
406                 for (int i = 0; i < parent.getChildCount(); i++) {
407                         TreeItem child = parent.getChild(i);
408                         if (child.getUserObject() instanceof RestResource) {
409                                 RestResource dto = (RestResource) child.getUserObject();
410                                 if (dto.getUri().equals(path))
411                                         return child;
412                         }
413                         tmp = getUserItem(child, path);
414                         if (tmp != null)
415                                 return tmp;
416                 }
417                 return null;
418         }
419
420         public List<TreeItem> getItemsOfTreeForPath(String path) {
421                 List<TreeItem> result = new ArrayList<TreeItem>();
422                 TreeItem item = null;
423                 item = getUserItem(getRootItem(), path);
424                 if (item != null)
425                         result.add(item);
426                 item = getUserItem(getMySharesItem(), path);
427                 if (item != null)
428                         result.add(item);
429                 item = getUserItem(getTrashItem(), path);
430                 if (item != null)
431                         result.add(item);
432                 item = getUserItem(getSharesItem(), path);
433                 if (item != null)
434                         result.add(item);
435                 return result;
436         }
437         /**
438          * This method returns the current Popup Tree
439          * @return the popuptree
440          */
441         public PopupTree getPopupTree(){
442                 return tree;
443         }
444 }