Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / tree / TrashSubtree.java @ a60ea262

History | View | Annotate | Download (4.3 kB)

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.Folders.Images;
22
import gr.ebs.gss.client.GSS;
23
import gr.ebs.gss.client.PopupTree;
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
import com.google.gwt.user.client.ui.Widget;
38

    
39
/**
40
 * @author kman
41
 */
42
public class TrashSubtree extends Subtree {
43

    
44
        /**
45
         * A constant that denotes the completion of an IncrementalCommand.
46
         */
47
        public static final boolean DONE = false;
48

    
49
        private DnDTreeItem rootItem;
50

    
51
        public TrashSubtree(PopupTree aTree, final Images _images) {
52
                super(aTree, _images);
53
                DeferredCommand.addCommand(new IncrementalCommand() {
54

    
55
                        @Override
56
                        public boolean execute() {
57
                                return updateInit();
58
                        }
59
                });
60
        }
61

    
62
        public boolean updateInit() {
63
                UserResource userResource = GSS.get().getCurrentUserResource();
64
                if ( userResource == null || GSS.get().getFolders().getRootItem() == null)
65
                        return !DONE;
66
                update();
67
                return DONE;
68
        }
69

    
70
        public void update() {
71
                DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
72
                        @Override
73
                        public void onComplete() {
74
                                if(rootItem == null){
75
                                        Widget rootItemWidget = imageItemHTML(images.trash(), "Trash");
76
                                        rootItemWidget.getElement().setId("tree.trash");
77
                                        rootItem = new DnDTreeItem(rootItemWidget, false,tree);
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
                        @Override
92
                        public void onError(Throwable t) {
93
                                if(t instanceof RestException){
94
                                        int statusCode = ((RestException)t).getHttpStatusCode();
95
                                        // On IE status code 1223 may be returned instead of 204.
96
                                        if(statusCode == 204 || statusCode == 1223){
97
                                                GWT.log("Trash is empty", null);
98
                                                if(rootItem == null){
99
                                                        rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), false,tree);
100
                                                        tree.addItem(rootItem);
101
                                                        rootItem.doDroppable();
102
                                                }
103
                                                rootItem.setUserObject(new TrashResource(GSS.get().getCurrentUserResource().getTrashPath()));
104
                                                rootItem.removeItems();
105
                                        } else{
106
                                                if(rootItem == null){
107
                                                        rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), false,tree);
108
                                                        tree.addItem(rootItem);
109
                                                }
110
                                                rootItem.setUserObject(new TrashResource(GSS.get().getCurrentUserResource().getTrashPath()));
111
                                                GSS.get().displayError("Unable to fetch trash folder:"+((RestException)t).getHttpStatusText());
112
                                        }
113
                                }
114
                                else{
115
                                        GWT.log("", t);
116
                                        GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
117
                                        if(rootItem == null){
118
                                                rootItem = new DnDTreeItem(imageItemHTML(images.trash(), "Trash"), false,tree);
119
                                                tree.addItem(rootItem);
120
                                                rootItem.doDroppable();
121
                                        }
122
                                        rootItem.setUserObject(new TrashResource(GSS.get().getCurrentUserResource().getTrashPath()));
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
}