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