Remove one constructor of PithosDisclosurePanel
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / FolderTreeView.java
1 /*
2  * Copyright 2011-2013 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.Pithos;
40 import gr.grnet.pithos.web.client.PithosDisclosurePanel;
41 import gr.grnet.pithos.web.client.TreeView;
42
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;
60
61 public class FolderTreeView extends Composite implements TreeView {
62
63         public boolean isFolderOpen(Folder folder) {
64         TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
65         return isFolderOpen(root, folder);
66         }
67         
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);
72             }
73                         if (node.isChildOpen(i)) {
74                             TreeNode n = node.setChildOpen(i, true);
75                             return isFolderOpen(n, folder);
76                         }
77         }
78         return false;
79         }
80         
81     public void openFolder(Folder folder) {
82         TreeNode root = tree.getRootTreeNode();
83         openFolder(root, folder);
84     }
85
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);
91                 break;
92             }
93                         if (node.isChildOpen(i)) {
94                             TreeNode n = node.setChildOpen(i, true);
95                             openFolder(n, folder);
96                             break;
97                         }
98         }
99     }
100
101     static interface BasicResources extends CellTree.Resources {
102
103         @Override
104                 @ImageOptions(flipRtl = true)
105         @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
106         ImageResource cellTreeClosedItem();
107
108         @Override
109                 @ImageOptions(flipRtl = true)
110         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
111         ImageResource cellTreeLoading();
112
113         @Override
114                 @ImageOptions(flipRtl = true)
115         @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
116         ImageResource cellTreeOpenItem();
117
118         @Override
119                 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
120         CellTree.Style cellTreeStyle();
121         
122         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
123         @ImageOptions(repeatStyle=RepeatStyle.None)
124         ImageResource cellTreeLoadingBasic();
125     }
126
127     public static interface Images extends Tree.Resources, FolderContextMenu.Images {
128
129         @Source("gr/grnet/pithos/resources/home22.png")
130         ImageResource home();
131
132         @Source("gr/grnet/pithos/resources/2folder22.png")
133         public ImageResource folderYellow();
134
135         @Source("gr/grnet/pithos/resources/mimetypes/document.png")
136         ImageResource document();
137
138         @Source("gr/grnet/pithos/resources/myshared22.png")
139         ImageResource myShared();
140
141         @Source("gr/grnet/pithos/resources/folder_user.png")
142         ImageResource sharedFolder();
143
144         @Source("gr/grnet/pithos/resources/trash.png")
145         ImageResource trash();
146     }
147
148     static Images images = GWT.create(Images.class);
149
150     static interface Templates extends SafeHtmlTemplates {
151         public Templates INSTANCE = GWT.create(Templates.class);
152
153         @Template("<span style='vertical-align: middle;'>{0}</span>")
154         public SafeHtml nameSpan(String name);
155
156         @Template("<span class='pithos-folderLabel'>{0}</span>")
157         public SafeHtml imageSpan(String name);
158     }
159
160     interface Resources extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Resources {
161                 @Override
162                 @Source("gr/grnet/pithos/resources/home22.png")
163         ImageResource icon();
164     }
165
166     private FolderTreeViewModel model;
167     
168     CellTree tree;
169     
170     private HTML usedBytes;
171     
172     private HTML totalBytes;
173     
174     private HTML usedPercent;
175
176     public FolderTreeView(FolderTreeViewModel viewModel) {
177         this.model = viewModel;
178
179         PithosDisclosurePanel panel = new PithosDisclosurePanel((Resources) GWT.create(Resources.class), "My Files", false, true);
180
181         VerticalPanel content = new VerticalPanel();
182         
183         /*
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();
187          */
188         CellTree.Resources res = GWT.create(BasicResources.class);
189         tree = new CellTree(model, null, res);
190         tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
191         tree.setDefaultNodeSize(5000);
192         content.add(tree);
193
194         HorizontalPanel statistics = new HorizontalPanel();
195             statistics.addStyleName(Pithos.resources.pithosCss().statistics());
196             statistics.add(new HTML("Used:&nbsp;"));
197             usedBytes = new HTML();
198             statistics.add(usedBytes);
199             statistics.add(new HTML("&nbsp;of&nbsp;"));
200             totalBytes = new HTML();
201             statistics.add(totalBytes);
202             statistics.add(new HTML("&nbsp;("));
203             usedPercent = new HTML();
204             statistics.add(usedPercent);
205             statistics.add(new HTML(")"));
206             content.add(statistics);
207             content.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_CENTER);
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 }