2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.pithos.web.client.foldertree;
38 import gr.grnet.pithos.web.client.FolderContextMenu;
39 import gr.grnet.pithos.web.client.PithosDisclosurePanel;
40 import gr.grnet.pithos.web.client.TreeView;
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;
59 public class FolderTreeView extends Composite implements TreeView {
61 public boolean isFolderOpen(Folder folder) {
62 TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
63 return isFolderOpen(root, folder);
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);
71 if (node.isChildOpen(i)) {
72 TreeNode n = node.setChildOpen(i, true);
73 return isFolderOpen(n, folder);
79 public void openFolder(Folder folder) {
80 TreeNode root = tree.getRootTreeNode();
81 openFolder(root, folder);
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);
91 if (node.isChildOpen(i)) {
92 TreeNode n = node.setChildOpen(i, true);
93 openFolder(n, folder);
99 static interface BasicResources extends CellTree.Resources {
102 @ImageOptions(flipRtl = true)
103 @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
104 ImageResource cellTreeClosedItem();
107 @ImageOptions(flipRtl = true)
108 @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
109 ImageResource cellTreeLoading();
112 @ImageOptions(flipRtl = true)
113 @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
114 ImageResource cellTreeOpenItem();
117 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
118 CellTree.Style cellTreeStyle();
120 @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
121 @ImageOptions(repeatStyle=RepeatStyle.None)
122 ImageResource cellTreeLoadingBasic();
125 public static interface Images extends Tree.Resources, FolderContextMenu.Images {
127 @Source("gr/grnet/pithos/resources/home22.png")
128 ImageResource home();
130 @Source("gr/grnet/pithos/resources/2folder22.png")
131 public ImageResource folderYellow();
133 @Source("gr/grnet/pithos/resources/mimetypes/document.png")
134 ImageResource document();
136 @Source("gr/grnet/pithos/resources/myshared22.png")
137 ImageResource myShared();
139 @Source("gr/grnet/pithos/resources/folder_user.png")
140 ImageResource sharedFolder();
142 @Source("gr/grnet/pithos/resources/trash.png")
143 ImageResource trash();
146 static Images images = GWT.create(Images.class);
148 static interface Templates extends SafeHtmlTemplates {
149 public Templates INSTANCE = GWT.create(Templates.class);
151 @Template("<span style='vertical-align: middle;'>{0}</span>")
152 public SafeHtml nameSpan(String name);
154 @Template("<span class='pithos-folderLabel'>{0}</span>")
155 public SafeHtml imageSpan(String name);
158 interface Resources extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Resources {
160 @Source("gr/grnet/pithos/resources/home22.png")
161 ImageResource icon();
164 private FolderTreeViewModel model;
166 private CellTree tree;
168 private HTML usedBytes;
170 private HTML totalBytes;
172 private HTML usedPercent;
174 public FolderTreeView(FolderTreeViewModel viewModel) {
175 this.model = viewModel;
177 PithosDisclosurePanel panel = new PithosDisclosurePanel((Resources) GWT.create(Resources.class), "My Files", true);
179 VerticalPanel content = new VerticalPanel();
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();
186 CellTree.Resources res = GWT.create(BasicResources.class);
187 tree = new CellTree(model, null, res);
188 tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
191 HorizontalPanel separator = new HorizontalPanel();
192 separator.addStyleName("pithos-statisticsSeparator");
193 content.add(separator);
195 HorizontalPanel statistics = new HorizontalPanel();
196 statistics.addStyleName("pithos-statistics");
197 statistics.add(new HTML("Used: "));
198 usedBytes = new HTML();
199 statistics.add(usedBytes);
200 statistics.add(new HTML(" of "));
201 totalBytes = new HTML();
202 statistics.add(totalBytes);
203 statistics.add(new HTML(" ("));
204 usedPercent = new HTML();
205 statistics.add(usedPercent);
206 statistics.add(new HTML(")"));
207 content.add(statistics);
209 panel.setContent(content);
215 public Folder getSelection() {
216 return model.getSelection();
219 public void updateFolder(Folder folder, boolean showfiles, Command callback, final boolean openParent) {
220 model.updateFolder(folder, showfiles, callback, openParent);
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()));