Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / client / tree / Subtree.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.PopupTree;
22 import gr.ebs.gss.client.Folders.Images;
23 import gr.ebs.gss.client.dnd.DnDTreeItem;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import com.google.gwt.user.client.ui.AbstractImagePrototype;
30 import com.google.gwt.user.client.ui.HTML;
31 import com.google.gwt.user.client.ui.TreeItem;
32
33 /**
34  * @author kman
35  */
36 public abstract class Subtree {
37
38         protected PopupTree tree;
39
40         final Images images;
41
42         public Subtree(PopupTree aTree, final Images _images) {
43                 images = _images;
44                 tree = aTree;
45         }
46
47         /**
48          * A helper method to simplify adding tree items that have attached images.
49          * {@link #addImageItem(TreeItem, String) code}
50          *
51          * @param parent the tree item to which the new item will be added.
52          * @param title the text associated with this item.
53          * @param imageProto the image of the item
54          * @return
55          */
56         protected TreeItem addImageItem(final TreeItem parent, final String title, final AbstractImagePrototype imageProto, boolean draggable) {
57                 final DnDTreeItem item = new DnDTreeItem(imageItemHTML(imageProto, title), title, draggable);
58                 parent.addItem(item);
59                 return item;
60         }
61
62         /**
63          * Generates HTML for a tree item with an attached icon.
64          *
65          * @param imageProto the image icon
66          * @param title the title of the item
67          * @return the resultant HTML
68          */
69         protected HTML imageItemHTML(final AbstractImagePrototype imageProto, final String title) {
70                 HTML html = new HTML("<a class='hidden-link' href='javascript:;'><span >" + imageProto.getHTML() + "&nbsp;" + title + "</span></a>");
71                 return html;
72         }
73
74         public void updateSubFoldersLazily(DnDTreeItem folderItem, List<FolderResource> subfolders, AbstractImagePrototype image, AbstractImagePrototype sharedImage) {
75                 for (int i = 0; i < folderItem.getChildCount(); i++) {
76                         DnDTreeItem c = (DnDTreeItem) folderItem.getChild(i);
77                         FolderResource f = (FolderResource) c.getUserObject();
78                         if (!listContainsFolder(f, subfolders)) {
79                                 c.undoDraggable();
80                                 folderItem.removeItem(c);
81                         }
82                 }
83
84                 LinkedList<DnDTreeItem> itemList = new LinkedList();
85                 for (FolderResource subfolder : subfolders) {
86                         DnDTreeItem item = folderItem.getChild(subfolder);
87                         if (item == null){
88                                 if(subfolder.isShared())
89                                         item = (DnDTreeItem) addImageItem(folderItem, subfolder.getName(), sharedImage, true);
90                                 else
91                                         item = (DnDTreeItem) addImageItem(folderItem, subfolder.getName(), image, true);
92                         } else if(subfolder.isShared())
93                                 item.updateWidget(imageItemHTML(sharedImage, subfolder.getName()));
94                         else
95                                 item.updateWidget(imageItemHTML(image, subfolder.getName()));
96                         item.setUserObject(subfolder);
97                         itemList.add(item);
98                 }
99                 for (DnDTreeItem it : itemList)
100                         it.remove();
101                 for (DnDTreeItem it : itemList)
102                         folderItem.addItem(it);
103                 for (int i = 0; i < folderItem.getChildCount(); i++) {
104                         DnDTreeItem c = (DnDTreeItem) folderItem.getChild(i);
105                         c.doDraggable();
106                 }
107         }
108
109         private boolean listContainsFolder(FolderResource folder, List<FolderResource> subfolders) {
110                 for (FolderResource f : subfolders)
111                         if (f.getUri().equals(folder.getUri()))
112                                 return true;
113                 return false;
114         }
115
116 }