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.Pithos;
40 import gr.grnet.pithos.web.client.PithosDisclosurePanel;
41 import gr.grnet.pithos.web.client.TreeView;
43 import com.google.gwt.core.client.GWT;
44 import com.google.gwt.i18n.client.NumberFormat;
45 import com.google.gwt.resources.client.ImageResource;
46 import com.google.gwt.resources.client.ImageResource.ImageOptions;
47 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
48 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
49 import com.google.gwt.safehtml.shared.SafeHtml;
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.Composite;
55 import com.google.gwt.user.client.ui.HTML;
56 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57 import com.google.gwt.user.client.ui.HorizontalPanel;
58 import com.google.gwt.user.client.ui.Tree;
59 import com.google.gwt.user.client.ui.VerticalPanel;
61 public class FolderTreeView extends Composite implements TreeView {
63 public boolean isFolderOpen(Folder folder) {
64 TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
65 return isFolderOpen(root, folder);
68 private boolean isFolderOpen(TreeNode node, Folder folder) {
69 for (int i=0; i<node.getChildCount(); i++) {
70 if (folder.equals(node.getChildValue(i))) {
71 return node.isChildOpen(i);
73 if (node.isChildOpen(i)) {
74 TreeNode n = node.setChildOpen(i, true);
75 return isFolderOpen(n, folder);
81 public void openFolder(Folder folder) {
82 TreeNode root = tree.getRootTreeNode();
83 openFolder(root, folder);
86 private void openFolder(TreeNode node, Folder folder) {
87 for (int i=0; i<node.getChildCount(); i++) {
88 if (folder.equals(node.getChildValue(i))) {
89 node.setChildOpen(i, false, true);
90 node.setChildOpen(i, true, true);
93 if (node.isChildOpen(i)) {
94 TreeNode n = node.setChildOpen(i, true);
95 openFolder(n, folder);
101 static interface BasicResources extends CellTree.Resources {
104 @ImageOptions(flipRtl = true)
105 @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
106 ImageResource cellTreeClosedItem();
109 @ImageOptions(flipRtl = true)
110 @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
111 ImageResource cellTreeLoading();
114 @ImageOptions(flipRtl = true)
115 @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
116 ImageResource cellTreeOpenItem();
119 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
120 CellTree.Style cellTreeStyle();
122 @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
123 @ImageOptions(repeatStyle=RepeatStyle.None)
124 ImageResource cellTreeLoadingBasic();
127 public static interface Images extends Tree.Resources, FolderContextMenu.Images {
129 @Source("gr/grnet/pithos/resources/home22.png")
130 ImageResource home();
132 @Source("gr/grnet/pithos/resources/2folder22.png")
133 public ImageResource folderYellow();
135 @Source("gr/grnet/pithos/resources/mimetypes/document.png")
136 ImageResource document();
138 @Source("gr/grnet/pithos/resources/myshared22.png")
139 ImageResource myShared();
141 @Source("gr/grnet/pithos/resources/folder_user.png")
142 ImageResource sharedFolder();
144 @Source("gr/grnet/pithos/resources/trash.png")
145 ImageResource trash();
148 static Images images = GWT.create(Images.class);
150 static interface Templates extends SafeHtmlTemplates {
151 public Templates INSTANCE = GWT.create(Templates.class);
153 @Template("<span style='vertical-align: middle;'>{0}</span>")
154 public SafeHtml nameSpan(String name);
156 @Template("<span class='pithos-folderLabel'>{0}</span>")
157 public SafeHtml imageSpan(String name);
160 interface Resources extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Resources {
162 @Source("gr/grnet/pithos/resources/home22.png")
163 ImageResource icon();
166 private FolderTreeViewModel model;
168 private CellTree tree;
170 private HTML usedBytes;
172 private HTML totalBytes;
174 private HTML usedPercent;
176 public FolderTreeView(FolderTreeViewModel viewModel) {
177 this.model = viewModel;
179 PithosDisclosurePanel panel = new PithosDisclosurePanel((Resources) GWT.create(Resources.class), "My Files", true);
181 VerticalPanel content = new VerticalPanel();
184 * Create the tree using the model. We use <code>null</code> as the default
185 * value of the root node. The default value will be passed to
186 * CustomTreeModel#getNodeInfo();
188 CellTree.Resources res = GWT.create(BasicResources.class);
189 tree = new CellTree(model, null, res);
190 tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
193 HorizontalPanel statistics = new HorizontalPanel();
194 statistics.addStyleName(Pithos.resources.pithosCss().statistics());
195 statistics.add(new HTML("Used: "));
196 usedBytes = new HTML();
197 statistics.add(usedBytes);
198 statistics.add(new HTML(" of "));
199 totalBytes = new HTML();
200 statistics.add(totalBytes);
201 statistics.add(new HTML(" ("));
202 usedPercent = new HTML();
203 statistics.add(usedPercent);
204 statistics.add(new HTML(")"));
205 content.add(statistics);
206 content.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_CENTER);
208 panel.setContent(content);
214 public Folder getSelection() {
215 return model.getSelection();
218 public void updateFolder(Folder folder, boolean showfiles, Command callback, final boolean openParent) {
219 model.updateFolder(folder, showfiles, callback, openParent);
222 public void showStatistics(AccountResource account) {
223 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
224 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
225 NumberFormat nf = NumberFormat.getPercentFormat();
226 usedPercent.setHTML(nf.format(account.getUsedPercentage()));