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