Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / foldertree / FolderTreeView.java @ e56be452

History | View | Annotate | Download (8 kB)

1
/*
2
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35

    
36
package gr.grnet.pithos.web.client.foldertree;
37

    
38
import gr.grnet.pithos.web.client.FolderContextMenu;
39
import gr.grnet.pithos.web.client.PithosDisclosurePanel;
40
import gr.grnet.pithos.web.client.TreeView;
41
import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates;
42

    
43
import com.google.gwt.core.client.GWT;
44
import com.google.gwt.resources.client.ImageResource;
45
import com.google.gwt.resources.client.ImageResource.ImageOptions;
46
import com.google.gwt.resources.client.ImageResource.RepeatStyle;
47
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
48
import com.google.gwt.safehtml.shared.SafeHtml;
49
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
50
import com.google.gwt.user.cellview.client.CellTree;
51
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
52
import com.google.gwt.user.cellview.client.TreeNode;
53
import com.google.gwt.user.client.Command;
54
import com.google.gwt.user.client.ui.AbstractImagePrototype;
55
import com.google.gwt.user.client.ui.Composite;
56
import com.google.gwt.user.client.ui.FlowPanel;
57
import com.google.gwt.user.client.ui.HTML;
58
import com.google.gwt.user.client.ui.HasVerticalAlignment;
59
import com.google.gwt.user.client.ui.HorizontalPanel;
60
import com.google.gwt.user.client.ui.Image;
61
import com.google.gwt.user.client.ui.Tree;
62
import com.google.gwt.user.client.ui.VerticalPanel;
63

    
64
public class FolderTreeView extends Composite implements TreeView {
65

    
66
        public boolean isFolderOpen(Folder folder) {
67
        TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
68
        return isFolderOpen(root, folder);
69
        }
70
        
71
        private boolean isFolderOpen(TreeNode node, Folder folder) {
72
        for (int i=0; i<node.getChildCount(); i++) {
73
            if (folder.equals(node.getChildValue(i))) {
74
                return node.isChildOpen(i);
75
            }
76
                        if (node.isChildOpen(i)) {
77
                            TreeNode n = node.setChildOpen(i, true);
78
                            return isFolderOpen(n, folder);
79
                        }
80
            }
81
        return false;
82
        }
83
        
84
    public void openFolder(Folder folder) {
85
        TreeNode root = tree.getRootTreeNode();
86
        openFolder(root, folder);
87
    }
88

    
89
    private void openFolder(TreeNode node, Folder folder) {
90
        for (int i=0; i<node.getChildCount(); i++) {
91
            if (folder.equals(node.getChildValue(i))) {
92
                    node.setChildOpen(i, false, true);
93
                    node.setChildOpen(i, true, true);
94
                    break;
95
            }
96
                        if (node.isChildOpen(i)) {
97
                            TreeNode n = node.setChildOpen(i, true);
98
                            openFolder(n, folder);
99
                            break;
100
                        }
101
            }
102
    }
103

    
104
    static interface BasicResources extends CellTree.Resources {
105

    
106
        @Override
107
                @ImageOptions(flipRtl = true)
108
        @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
109
        ImageResource cellTreeClosedItem();
110

    
111
        @Override
112
                @ImageOptions(flipRtl = true)
113
        @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
114
        ImageResource cellTreeLoading();
115

    
116
        @Override
117
                @ImageOptions(flipRtl = true)
118
        @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
119
        ImageResource cellTreeOpenItem();
120

    
121
        @Override
122
                @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
123
        CellTree.Style cellTreeStyle();
124
        
125
        @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
126
        @ImageOptions(repeatStyle=RepeatStyle.None)
127
        ImageResource cellTreeLoadingBasic();
128
    }
129

    
130
    public static interface Images extends Tree.Resources, FolderContextMenu.Images {
131

    
132
        @Source("gr/grnet/pithos/resources/home22.png")
133
        ImageResource home();
134

    
135
        @Source("gr/grnet/pithos/resources/2folder22.png")
136
        public ImageResource folderYellow();
137

    
138
        @Source("gr/grnet/pithos/resources/mimetypes/document.png")
139
        ImageResource document();
140

    
141
        @Source("gr/grnet/pithos/resources/myshared22.png")
142
        ImageResource myShared();
143

    
144
        @Source("gr/grnet/pithos/resources/folder_user.png")
145
        ImageResource sharedFolder();
146

    
147
        @Source("gr/grnet/pithos/resources/trash.png")
148
        ImageResource trash();
149
    }
150

    
151
    static Images images = GWT.create(Images.class);
152

    
153
    static interface Templates extends SafeHtmlTemplates {
154
        public Templates INSTANCE = GWT.create(Templates.class);
155

    
156
        @Template("<span style='vertical-align: middle;'>{0}</span>")
157
        public SafeHtml nameSpan(String name);
158

    
159
        @Template("<span class='pithos-folderLabel'>{0}</span>")
160
        public SafeHtml imageSpan(String name);
161
    }
162

    
163
    interface Resources extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Resources {
164
            @Override
165
                @Source("gr/grnet/pithos/resources/home22.png")
166
            ImageResource icon();
167
    }
168

    
169
    private FolderTreeViewModel model;
170
    
171
    private CellTree tree;
172

    
173
    public FolderTreeView(FolderTreeViewModel viewModel) {
174
        this.model = viewModel;
175

    
176
        VerticalPanel panel = new VerticalPanel();
177
        panel.addStyleName("pithos-folderTreeSection");
178
        Resources resources = GWT.create(Resources.class);
179
        
180
        HorizontalPanel header = new HorizontalPanel();
181
        
182
//        SafeHtmlBuilder sb = new SafeHtmlBuilder();
183
        Image img = new Image(resources.icon());
184
        header.add(img);
185
        header.setCellVerticalAlignment(img, HasVerticalAlignment.ALIGN_MIDDLE);
186
        header.setCellWidth(img, "32px");
187
        HTML title = new HTML("My Files");
188
        header.add(title);
189
        header.setCellVerticalAlignment(title, HasVerticalAlignment.ALIGN_MIDDLE);
190
        
191
//        sb.appendHtmlConstant(AbstractImagePrototype.create(resources.icon()).getHTML());
192
 //       sb.append(Templates.INSTANCE.nameSpan("My Files"));
193
//        HTML header = new HTML(sb.toSafeHtml());
194
        header.addStyleName("pithos-folderTreeSectionHeader");
195
        panel.add(header);
196

    
197

    
198
        /*
199
         * Create the tree using the model. We use <code>null</code> as the default
200
         * value of the root node. The default value will be passed to
201
         * CustomTreeModel#getNodeInfo();
202
         */
203
        CellTree.Resources res = GWT.create(BasicResources.class);
204
        tree = new CellTree(model, null, res);
205
        tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
206
        tree.addStyleName("pithos-folderTreeSectionContent");
207
        panel.add(tree);
208
        
209
        initWidget(panel);
210
    }
211

    
212

    
213
    @Override
214
        public Folder getSelection() {
215
       return model.getSelection();
216
    }
217

    
218
    public void updateFolder(Folder folder, boolean showfiles, Command callback, final boolean openParent) {
219
        model.updateFolder(folder, showfiles, callback, openParent);
220
    }
221
}