9a6cc610b4a18ed975667ca359992aa3908d04d0
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / FolderTreeView.java
1 /*
2  * Copyright 2011 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.TreeView;
40
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.resources.client.ImageResource;
43 import com.google.gwt.resources.client.ImageResource.ImageOptions;
44 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
45 import com.google.gwt.safehtml.shared.SafeHtml;
46 import com.google.gwt.user.cellview.client.CellTree;
47 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
48 import com.google.gwt.user.cellview.client.TreeNode;
49 import com.google.gwt.user.client.Command;
50 import com.google.gwt.user.client.ui.Composite;
51 import com.google.gwt.user.client.ui.Tree;
52
53 public class FolderTreeView extends Composite implements TreeView {
54
55         public boolean isFolderOpen(Folder folder) {
56         TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
57         return isFolderOpen(root, folder);
58         }
59         
60         private boolean isFolderOpen(TreeNode node, Folder folder) {
61         for (int i=0; i<node.getChildCount(); i++) {
62             if (folder.equals(node.getChildValue(i))) {
63                 return node.isChildOpen(i);
64             }
65             else {
66                 if (node.isChildOpen(i)) {
67                     TreeNode n = node.setChildOpen(i, true);
68                     return isFolderOpen(n, folder);
69                 }
70             }
71         }
72         return false;
73         }
74         
75     public void openFolder(Folder folder) {
76         TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
77         openFolder(root, folder);
78     }
79
80     private void openFolder(TreeNode node, Folder folder) {
81         for (int i=0; i<node.getChildCount(); i++) {
82             if (folder.equals(node.getChildValue(i))) {
83                 node.setChildOpen(i, false, true);
84                 node.setChildOpen(i, true, true);
85                 break;
86             }
87             else {
88                 if (node.isChildOpen(i)) {
89                     TreeNode n = node.setChildOpen(i, true);
90                     openFolder(n, folder);
91                     break;
92                 }
93             }
94         }
95     }
96
97     static interface BasicResources extends CellTree.Resources {
98
99         @Override
100                 @ImageOptions(flipRtl = true)
101         @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.gif")
102         ImageResource cellTreeClosedItem();
103
104         @Override
105                 @ImageOptions(flipRtl = true)
106         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
107         ImageResource cellTreeLoading();
108
109         @Override
110                 @ImageOptions(flipRtl = true)
111         @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.gif")
112         ImageResource cellTreeOpenItem();
113
114         @Override
115                 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
116         CellTree.Style cellTreeStyle();
117     }
118
119     public static interface Images extends Tree.Resources, FolderContextMenu.Images {
120
121         @Source("gr/grnet/pithos/resources/home22.png")
122         ImageResource home();
123
124         @Source("gr/grnet/pithos/resources/folder22.png")
125         public ImageResource folderYellow();
126
127         @Source("gr/grnet/pithos/resources/mimetypes/document.png")
128         ImageResource document();
129
130         @Source("gr/grnet/pithos/resources/othersshared.png")
131         ImageResource othersShared();
132
133         @Source("gr/grnet/pithos/resources/myshared22.png")
134         ImageResource myShared();
135
136         @Source("gr/grnet/pithos/resources/folder_user.png")
137         ImageResource sharedFolder();
138
139         @Source("gr/grnet/pithos/resources/trash.png")
140         ImageResource trash();
141     }
142
143     static Images images = GWT.create(Images.class);
144
145     static interface Templates extends SafeHtmlTemplates {
146         public Templates INSTANCE = GWT.create(Templates.class);
147
148         @Template("<span class='pithos-folderLabel'>{0}</span>")
149         public SafeHtml nameSpan(String name);
150
151         @Template("<span class='pithos-folderLabel'>{0}</span>")
152         public SafeHtml imageSpan(String name);
153     }
154
155     private FolderTreeViewModel model;
156
157     public FolderTreeView(FolderTreeViewModel viewModel) {
158         this.model = viewModel;
159         /*
160          * Create the tree using the model. We use <code>null</code> as the default
161          * value of the root node. The default value will be passed to
162          * CustomTreeModel#getNodeInfo();
163          */
164         CellTree.Resources res = GWT.create(BasicResources.class);
165         CellTree tree = new CellTree(model, null, res);
166         tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
167
168         initWidget(tree);
169     }
170
171
172     @Override
173         public Folder getSelection() {
174        return model.getSelection();
175     }
176
177     public void updateFolder(Folder folder, boolean showfiles, Command callback) {
178         model.updateFolder(folder, showfiles, callback);
179     }
180 }