e7c5290c95b0f2ffb539711b734566ff3f584e9d
[pithos] / gss / src / gr / ebs / gss / client / tree / TrashSubtree.java
1 /*
2  * Copyright 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.tree;
20
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.PopupTree;
23 import gr.ebs.gss.client.Folders.Images;
24 import gr.ebs.gss.client.dnd.DnDTreeItem;
25 import gr.ebs.gss.client.rest.GetCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.TrashResource;
29 import gr.ebs.gss.client.rest.resource.UserResource;
30
31 import java.util.List;
32
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.user.client.DeferredCommand;
35 import com.google.gwt.user.client.IncrementalCommand;
36 import com.google.gwt.user.client.ui.TreeItem;
37
38
39
40 /**
41  * @author kman
42  */
43 public class TrashSubtree extends Subtree {
44
45         /**
46          * A constant that denotes the completion of an IncrementalCommand.
47          */
48         public static final boolean DONE = false;
49
50         private DnDTreeItem rootItem;
51
52         public TrashSubtree(PopupTree tree, final Images _images) {
53                 super(tree, _images);
54
55                 DeferredCommand.addCommand(new IncrementalCommand() {
56
57                         public boolean execute() {
58                                 return updateInit();
59                         }
60                 });
61         }
62
63         public boolean updateInit() {
64
65                 UserResource userResource = GSS.get().getCurrentUserResource();
66                 if ( userResource == null || GSS.get().getFolders().getRootItem() == null)
67                         return !DONE;
68                 update();
69                 return DONE;
70
71         }
72
73         public void update() {
74                 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath()) {
75                         public void onComplete() {
76                                 if(rootItem == null){
77                                         rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), "Trash", false);
78                                         tree.addItem(rootItem);
79                                         rootItem.doDroppable();
80                                 }
81                                 rootItem.setUserObject(getResult());
82                                 rootItem.removeItems();
83                                 List<FolderResource> res = rootItem.getTrashResource().getTrashedFolders();
84                                 for (FolderResource r : res) {
85                                         DnDTreeItem child = (DnDTreeItem) addImageItem(rootItem, r.getName(), images.folderYellow(), true);
86                                         child.setUserObject(r);
87                                         child.setState(false);
88                                 }
89                         }
90
91                         public void onError(Throwable t) {
92                                 if(t instanceof RestException){
93                                         int statusCode = ((RestException)t).getHttpStatusCode();
94                                         //empty trash ie returns 1223 status code instead of 204
95                                         if(statusCode == 204 || statusCode == 1223){
96                                                 GWT.log("Trash is empty", null);
97                                                 if(rootItem == null){
98                                                         rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), "Trash", false);
99                                                         tree.addItem(rootItem);
100                                                         rootItem.doDroppable();
101                                                 }
102                                                 rootItem.setUserObject(new TrashResource(GSS.get().getCurrentUserResource().getTrashPath()));
103                                                 rootItem.removeItems();
104                                         } else{
105                                                 if(rootItem == null){
106                                                         rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), "Trash", false);
107                                                         tree.addItem(rootItem);
108                                                 }
109                                                 rootItem.setUserObject(new TrashResource(GSS.get().getCurrentUserResource().getTrashPath()));
110                                                 GSS.get().displayError("Unable to fetch trash folder:"+((RestException)t).getHttpStatusText());
111                                         }
112                                 }
113                                 else{
114                                         GWT.log("", t);
115                                         GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
116                                         if(rootItem == null){
117                                                 rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), "Trash", false);
118                                                 tree.addItem(rootItem);
119                                                 rootItem.doDroppable();
120                                         }
121                                         rootItem.setUserObject(new TrashResource(GSS.get().getCurrentUserResource().getTrashPath()));
122                                 }
123                         }
124                 });
125
126         }
127
128         /**
129          * Retrieve the rootItem.
130          *
131          * @return the rootItem
132          */
133         public TreeItem getRootItem() {
134                 return rootItem;
135         }
136
137 }