Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / tree / Subtree.java @ 0fc071d9

History | View | Annotate | Download (4.1 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.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.resources.client.ImageResource;
30
import com.google.gwt.user.client.ui.AbstractImagePrototype;
31
import com.google.gwt.user.client.ui.HTML;
32
import com.google.gwt.user.client.ui.TreeItem;
33

    
34
/**
35
 * @author kman
36
 */
37
public abstract class Subtree {
38

    
39
        protected PopupTree tree;
40

    
41
        final Images images;
42

    
43
        public Subtree(PopupTree aTree, final Images _images) {
44
                images = _images;
45
                tree = aTree;
46
        }
47

    
48
        /**
49
         * A helper method to simplify adding tree items that have attached images.
50
         * {@link #addImageItem(TreeItem, String) code}
51
         *
52
         * @param parent the tree item to which the new item will be added.
53
         * @param title the text associated with this item.
54
         * @param imageProto the image of the item
55
         * @return
56
         */
57
        protected TreeItem addImageItem(final TreeItem parent, final String title, final ImageResource imageProto, boolean draggable) {
58
                final DnDTreeItem item = new DnDTreeItem(imageItemHTML(imageProto, title), draggable,tree,true);
59
                parent.addItem(item);
60
                return item;
61
        }
62

    
63
        /**
64
         * Generates HTML for a tree item with an attached icon.
65
         *
66
         * @param imageProto the image icon
67
         * @param title the title of the item
68
         * @return the resultant HTML
69
         */
70
        protected HTML imageItemHTML(final ImageResource imageProto, final String title) {
71
                HTML html = new HTML("<a class='hidden-link' href='javascript:;'><span >" + AbstractImagePrototype.create(imageProto).getHTML() + "&nbsp;" + title + "</span></a>");
72
                return html;
73
        }
74

    
75
        public void updateSubFoldersLazily(DnDTreeItem folderItem, List<FolderResource> subfolders, ImageResource image, ImageResource sharedImage) {
76
                for (int i = 0; i < folderItem.getChildCount(); i++) {
77
                        TreeItem initialItem = folderItem.getChild(i);
78
                        if(initialItem instanceof DnDTreeItem){
79
                                DnDTreeItem c = (DnDTreeItem)initialItem;
80
                                FolderResource f = (FolderResource) c.getUserObject();
81
                                if (!listContainsFolder(f, subfolders)) {
82
                                        c.undoDraggable();
83
                                        folderItem.removeItem(c);
84
                                }
85
                        }
86
                        else
87
                                folderItem.removeItem(initialItem);
88
                }
89

    
90
                LinkedList<DnDTreeItem> itemList = new LinkedList();
91
                for (FolderResource subfolder : subfolders) {
92
                        DnDTreeItem item = folderItem.getChild(subfolder);
93
                        if (item == null){
94
                                if(subfolder.isShared() || subfolder.isReadForAll())
95
                                        item = (DnDTreeItem) addImageItem(folderItem, subfolder.getName(), sharedImage, true);
96
                                else
97
                                        item = (DnDTreeItem) addImageItem(folderItem, subfolder.getName(), image, true);
98
                        } else if(subfolder.isShared() || subfolder.isReadForAll())
99
                                item.updateWidget(imageItemHTML(sharedImage, subfolder.getName()));
100
                        else
101
                                item.updateWidget(imageItemHTML(image, subfolder.getName()));
102
                        item.setUserObject(subfolder);
103
                        itemList.add(item);
104
                }
105
                for (DnDTreeItem it : itemList)
106
                        it.remove();
107
                for (DnDTreeItem it : itemList)
108
                        folderItem.addItem(it);
109
                for (int i = 0; i < folderItem.getChildCount(); i++) {
110
                        DnDTreeItem c = (DnDTreeItem) folderItem.getChild(i);
111
                        c.doDraggable();
112
                }
113
        }
114

    
115
        private boolean listContainsFolder(FolderResource folder, List<FolderResource> subfolders) {
116
                for (FolderResource f : subfolders)
117
                        if (f.getUri().equals(folder.getUri()))
118
                                return true;
119
                return false;
120
        }
121

    
122
}