My files section made expand/collapse-able (issue #2346)
[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.i18n.client.NumberFormat;
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.user.cellview.client.CellTree;
50 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
51 import com.google.gwt.user.cellview.client.TreeNode;
52 import com.google.gwt.user.client.Command;
53 import com.google.gwt.user.client.ui.Composite;
54 import com.google.gwt.user.client.ui.HTML;
55 import com.google.gwt.user.client.ui.HorizontalPanel;
56 import com.google.gwt.user.client.ui.Tree;
57 import com.google.gwt.user.client.ui.VerticalPanel;
58
59 public class FolderTreeView extends Composite implements TreeView {
60
61         public boolean isFolderOpen(Folder folder) {
62         TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
63         return isFolderOpen(root, folder);
64         }
65         
66         private boolean isFolderOpen(TreeNode node, Folder folder) {
67         for (int i=0; i<node.getChildCount(); i++) {
68             if (folder.equals(node.getChildValue(i))) {
69                 return node.isChildOpen(i);
70             }
71                         if (node.isChildOpen(i)) {
72                             TreeNode n = node.setChildOpen(i, true);
73                             return isFolderOpen(n, folder);
74                         }
75         }
76         return false;
77         }
78         
79     public void openFolder(Folder folder) {
80         TreeNode root = tree.getRootTreeNode();
81         openFolder(root, folder);
82     }
83
84     private void openFolder(TreeNode node, Folder folder) {
85         for (int i=0; i<node.getChildCount(); i++) {
86             if (folder.equals(node.getChildValue(i))) {
87                 node.setChildOpen(i, false, true);
88                 node.setChildOpen(i, true, true);
89                 break;
90             }
91                         if (node.isChildOpen(i)) {
92                             TreeNode n = node.setChildOpen(i, true);
93                             openFolder(n, folder);
94                             break;
95                         }
96         }
97     }
98
99     static interface BasicResources extends CellTree.Resources {
100
101         @Override
102                 @ImageOptions(flipRtl = true)
103         @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
104         ImageResource cellTreeClosedItem();
105
106         @Override
107                 @ImageOptions(flipRtl = true)
108         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
109         ImageResource cellTreeLoading();
110
111         @Override
112                 @ImageOptions(flipRtl = true)
113         @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
114         ImageResource cellTreeOpenItem();
115
116         @Override
117                 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
118         CellTree.Style cellTreeStyle();
119         
120         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
121         @ImageOptions(repeatStyle=RepeatStyle.None)
122         ImageResource cellTreeLoadingBasic();
123     }
124
125     public static interface Images extends Tree.Resources, FolderContextMenu.Images {
126
127         @Source("gr/grnet/pithos/resources/home22.png")
128         ImageResource home();
129
130         @Source("gr/grnet/pithos/resources/2folder22.png")
131         public ImageResource folderYellow();
132
133         @Source("gr/grnet/pithos/resources/mimetypes/document.png")
134         ImageResource document();
135
136         @Source("gr/grnet/pithos/resources/myshared22.png")
137         ImageResource myShared();
138
139         @Source("gr/grnet/pithos/resources/folder_user.png")
140         ImageResource sharedFolder();
141
142         @Source("gr/grnet/pithos/resources/trash.png")
143         ImageResource trash();
144     }
145
146     static Images images = GWT.create(Images.class);
147
148     static interface Templates extends SafeHtmlTemplates {
149         public Templates INSTANCE = GWT.create(Templates.class);
150
151         @Template("<span style='vertical-align: middle;'>{0}</span>")
152         public SafeHtml nameSpan(String name);
153
154         @Template("<span class='pithos-folderLabel'>{0}</span>")
155         public SafeHtml imageSpan(String name);
156     }
157
158     interface Resources extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Resources {
159         @Override
160                 @Source("gr/grnet/pithos/resources/home22.png")
161         ImageResource icon();
162     }
163
164     private FolderTreeViewModel model;
165     
166     private CellTree tree;
167     
168     private HTML usedBytes;
169     
170     private HTML totalBytes;
171     
172     private HTML usedPercent;
173
174     public FolderTreeView(FolderTreeViewModel viewModel) {
175         this.model = viewModel;
176
177         PithosDisclosurePanel panel = new PithosDisclosurePanel((Resources) GWT.create(Resources.class), "My Files", true);
178
179         VerticalPanel content = new VerticalPanel();
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         content.add(tree);
190
191         HorizontalPanel separator = new HorizontalPanel();
192         separator.addStyleName("pithos-statisticsSeparator");
193         content.add(separator);
194
195         HorizontalPanel statistics = new HorizontalPanel();
196             statistics.addStyleName("pithos-statistics");
197             statistics.add(new HTML("Used:&nbsp;"));
198             usedBytes = new HTML();
199             statistics.add(usedBytes);
200             statistics.add(new HTML("&nbsp;of&nbsp;"));
201             totalBytes = new HTML();
202             statistics.add(totalBytes);
203             statistics.add(new HTML("&nbsp;("));
204             usedPercent = new HTML();
205             statistics.add(usedPercent);
206             statistics.add(new HTML(")"));
207             content.add(statistics);
208
209         panel.setContent(content);
210         initWidget(panel);
211     }
212
213
214     @Override
215         public Folder getSelection() {
216        return model.getSelection();
217     }
218
219     public void updateFolder(Folder folder, boolean showfiles, Command callback, final boolean openParent) {
220         model.updateFolder(folder, showfiles, callback, openParent);
221     }
222     
223         public void showStatistics(AccountResource account) {
224         usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
225         totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
226         NumberFormat nf = NumberFormat.getPercentFormat();
227         usedPercent.setHTML(nf.format(account.getUsedPercentage()));
228         }
229 }