Add a temporary hard-coded notice for the extended token validity period. This should...
[pithos] / gss / src / gr / ebs / gss / client / dnd / DnDTreeItem.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.dnd;
20
21 import gr.ebs.gss.client.Folders;
22 import gr.ebs.gss.client.GSS;
23 import gr.ebs.gss.client.rest.resource.FolderResource;
24 import gr.ebs.gss.client.rest.resource.OtherUserResource;
25 import gr.ebs.gss.client.rest.resource.OthersResource;
26 import gr.ebs.gss.client.rest.resource.SharedResource;
27 import gr.ebs.gss.client.rest.resource.TrashResource;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import com.google.gwt.user.client.DOM;
33 import com.google.gwt.user.client.ui.MouseListener;
34 import com.google.gwt.user.client.ui.SourcesMouseEvents;
35 import com.google.gwt.user.client.ui.TreeItem;
36 import com.google.gwt.user.client.ui.Widget;
37
38
39 /**
40  * @author kman
41  *
42  */
43 public class DnDTreeItem extends TreeItem implements SourcesMouseEvents {
44         public static final int FOLDER = 0;
45         public static final int SHARED = 1;
46         public static final int TRASH = 2;
47         public static final int OTHERS = 3;
48
49         private DnDFocusPanel focus;
50         private String type;
51         private Widget content;
52         private Widget toDrag;
53         private DnDDropController drop;
54         private List<DnDTreeItem> toRemove = new ArrayList();
55         private boolean draggable = false;
56
57
58         public DnDTreeItem(Widget widget,String name, boolean _draggable) {
59                 super();
60                 draggable = _draggable;
61                 content = widget;
62                 focus = new DnDFocusPanel(content,this);
63                 focus.setTabIndex(-1);
64                 setWidget(focus);
65         }
66
67         public void setFocus(){
68                 ((DnDFocusPanel)getWidget()).setFocus(true);
69         }
70
71         public void updateWidget(Widget widget){
72                 content = widget;
73                 focus.setWidget(content);
74
75         }
76
77         public void addMouseListener(MouseListener mouseListener){
78                 focus.addMouseListener(mouseListener);
79         }
80
81         public void removeMouseListener(MouseListener mouseListener){
82                 focus.removeMouseListener(mouseListener);
83         }
84
85
86         /**
87          * Retrieve the drop.
88          *
89          * @return the drop
90          */
91         public DnDDropController getDrop() {
92                 return drop;
93         }
94
95
96
97         public Widget getContent() {
98                 return content;
99         }
100
101
102         /* (non-Javadoc)
103          * @see com.google.gwt.user.client.ui.TreeItem#removeItems()
104          */
105         @Override
106         public void removeItems() {
107                 toRemove = new ArrayList();
108                 removeItems(this);
109                 for(DnDTreeItem it : toRemove)
110                         if(it.getDrop() != null)
111                                 GSS.get().getDragController().unregisterDropController(it.getDrop());
112                 toRemove.clear();
113                 super.removeItems();
114
115         }
116
117         /* (non-Javadoc)
118          * @see com.google.gwt.user.client.ui.TreeItem#removeItem(com.google.gwt.user.client.ui.TreeItem)
119          */
120         @Override
121         public void removeItem(TreeItem item) {
122                 item.removeItems();
123                 if(item instanceof DnDTreeItem){
124                         DnDTreeItem it = (DnDTreeItem) item;
125                         if(it.getDrop() != null)
126                                 GSS.get().getDragController().unregisterDropController(it.getDrop());
127                 }
128                 super.removeItem(item);
129
130         }
131         public void doDroppable(){
132                 drop = new DnDDropController(focus);
133                 GSS.get().getDragController().registerDropController(drop);
134         }
135
136         public void doDraggable(){
137                 GSS.get().getDragController().makeDraggable(getFocus(), getContent());
138                 drop = new DnDDropController(focus);
139                 GSS.get().getDragController().registerDropController(drop);
140         }
141
142         public void undoDraggable(){
143                 GSS.get().getDragController().makeNotDraggable(getFocus());
144                 if(drop!=null)
145                         GSS.get().getDragController().unregisterDropController(getDrop());
146         }
147
148         public void undoDroppable(){
149                 if(drop!=null)
150                         GSS.get().getDragController().unregisterDropController(getDrop());
151         }
152
153         protected void removeItems(DnDTreeItem item){
154                 for(int i=0;i<item.getChildCount();i++) {
155                         DnDTreeItem it = (DnDTreeItem)item.getChild(i);
156                         toRemove.add(it);
157                         removeItems(it);
158                 }
159         }
160
161
162         /**
163          * Retrieve the focus.
164          *
165          * @return the focus
166          */
167         public DnDFocusPanel getFocus() {
168                 return focus;
169         }
170
171
172         /**
173          * Retrieve the draggable.
174          *
175          * @return the draggable
176          */
177         public boolean isDraggable() {
178                 return draggable;
179         }
180
181         public DnDTreeItem getChild(FolderResource folder){
182                 for(int i=0; i< getChildCount(); i++){
183                         DnDTreeItem c = (DnDTreeItem) getChild(i);
184                         if(c.getUserObject() instanceof FolderResource)
185                                 if(((FolderResource)c.getUserObject()).getUri().equals(folder.getUri()))
186                                         return c;
187                 }
188                 return null;
189         }
190
191         public DnDTreeItem getChild(OtherUserResource user){
192                 for(int i=0; i< getChildCount(); i++){
193                         DnDTreeItem c = (DnDTreeItem) getChild(i);
194                         if(c.getUserObject() instanceof OtherUserResource)
195                                 if(((OtherUserResource)c.getUserObject()).getUri().equals(user.getUri()))
196                                         return c;
197                 }
198                 return null;
199         }
200
201         public void insertItem(TreeItem item, int position) {
202             addItem(item);
203             //if(position != 0)
204                 DOM.insertChild(getElement(), item.getElement(), position);
205           }
206
207
208         public int getItemType(){
209                 Folders f = GSS.get().getFolders();
210                 if(f.isFileItem(this))
211                         return FOLDER;
212                 if(f.isMySharedItem(this))
213                         return SHARED;
214                 if(f.isOthersSharedItem(this))
215                         return OTHERS;
216                 return TRASH;
217         }
218
219
220         /**
221          * Retrieve the folderResource.
222          *
223          * @return the folderResource
224          */
225         public FolderResource getFolderResource() {
226                 if(getUserObject() instanceof FolderResource)
227                         return (FolderResource)getUserObject();
228                 return null;
229         }
230
231
232
233
234
235         /**
236          * Retrieve the sharedResource.
237          *
238          * @return the sharedResource
239          */
240         public SharedResource getSharedResource() {
241                 if(getUserObject() instanceof SharedResource)
242                         return (SharedResource)getUserObject();
243                 return null;
244         }
245
246
247
248
249
250         /**
251          * Retrieve the trashResource.
252          *
253          * @return the trashResource
254          */
255         public TrashResource getTrashResource() {
256                 if(getUserObject() instanceof TrashResource)
257                         return (TrashResource)getUserObject();
258                 return null;
259         }
260
261
262
263
264
265         /**
266          * Retrieve the othersResource.
267          *
268          * @return the othersResource
269          */
270         public OthersResource getOthersResource() {
271                 if(getUserObject() instanceof OthersResource)
272                         return (OthersResource)getUserObject();
273                 return null;
274         }
275
276
277         /**
278          * Retrieve the otherUserResource.
279          *
280          * @return the otherUserResource
281          */
282         public OtherUserResource getOtherUserResource() {
283                 if(getUserObject() instanceof OtherUserResource)
284                         return (OtherUserResource)getUserObject();
285                 return null;
286         }
287
288
289
290
291         public boolean needExpanding(){
292                 /*if(GSS.get().getFolders().isMySharedItem(this) && ! GSS.get().getFolders().isMyShares(this)){
293                         if(getFolderResource() != null){
294                                 SharedResource sr = ((DnDTreeItem)GSS.get().getFolders().getMySharesItem()).getSharedResource();
295                                 int count =0;
296                                 for(String s : getFolderResource().getSubfolderPaths())
297                                         if(sr.getSubfolders().contains(s))
298                                                 count++;
299                                 if(count != getChildCount())
300                                         return true;
301                         }
302                 }
303                 else*/
304                 if(getFolderResource() != null){
305                         //if(equals(GSS.get().getFolders().getRootItem()))
306                                 //return false;
307                         if(getFolderResource().getFolders().size() > 0)
308                                 for(FolderResource r : getFolderResource().getFolders() )
309                                         if(r.isNeedsExpanding())
310                                                 return true;
311                         if(getFolderResource().getSubfolderPaths().size() != getChildCount())
312                                 return true;
313                 }
314                 else if (getOtherUserResource() != null)
315                         if(getOtherUserResource().getSubfolderPaths().size() != getChildCount())
316                                 return true;
317                 return false;
318         }
319
320 }