Statistics
| Branch: | Tag: | Revision:

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

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