Fixed auto-refresh to update only the files of the current folder
[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.TreeView;
40
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.resources.client.ImageResource;
43 import com.google.gwt.resources.client.ImageResource.ImageOptions;
44 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
45 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
46 import com.google.gwt.safehtml.shared.SafeHtml;
47 import com.google.gwt.user.cellview.client.CellTree;
48 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
49 import com.google.gwt.user.cellview.client.TreeNode;
50 import com.google.gwt.user.client.Command;
51 import com.google.gwt.user.client.ui.Composite;
52 import com.google.gwt.user.client.ui.Tree;
53
54 public class FolderTreeView extends Composite implements TreeView {
55
56         public boolean isFolderOpen(Folder folder) {
57         TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
58         return isFolderOpen(root, folder);
59         }
60         
61         private boolean isFolderOpen(TreeNode node, Folder folder) {
62         for (int i=0; i<node.getChildCount(); i++) {
63             if (folder.equals(node.getChildValue(i))) {
64                 return node.isChildOpen(i);
65             }
66                         if (node.isChildOpen(i)) {
67                             TreeNode n = node.setChildOpen(i, true);
68                             return isFolderOpen(n, folder);
69                         }
70         }
71         return false;
72         }
73         
74     public void openFolder(Folder folder) {
75         TreeNode root = ((CellTree) getWidget()).getRootTreeNode();
76         openFolder(root, folder);
77     }
78
79     private void openFolder(TreeNode node, Folder folder) {
80         for (int i=0; i<node.getChildCount(); i++) {
81             if (folder.equals(node.getChildValue(i))) {
82                 node.setChildOpen(i, false, true);
83                 node.setChildOpen(i, true, true);
84                 break;
85             }
86                         if (node.isChildOpen(i)) {
87                             TreeNode n = node.setChildOpen(i, true);
88                             openFolder(n, folder);
89                             break;
90                         }
91         }
92     }
93
94     static interface BasicResources extends CellTree.Resources {
95
96         @Override
97                 @ImageOptions(flipRtl = true)
98         @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
99         ImageResource cellTreeClosedItem();
100
101         @Override
102                 @ImageOptions(flipRtl = true)
103         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
104         ImageResource cellTreeLoading();
105
106         @Override
107                 @ImageOptions(flipRtl = true)
108         @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
109         ImageResource cellTreeOpenItem();
110
111         @Override
112                 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
113         CellTree.Style cellTreeStyle();
114         
115         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
116         @ImageOptions(repeatStyle=RepeatStyle.None)
117         ImageResource cellTreeLoadingBasic();
118     }
119
120     public static interface Images extends Tree.Resources, FolderContextMenu.Images {
121
122         @Source("gr/grnet/pithos/resources/home22.png")
123         ImageResource home();
124
125         @Source("gr/grnet/pithos/resources/folder22.png")
126         public ImageResource folderYellow();
127
128         @Source("gr/grnet/pithos/resources/mimetypes/document.png")
129         ImageResource document();
130
131         @Source("gr/grnet/pithos/resources/othersshared.png")
132         ImageResource othersShared();
133
134         @Source("gr/grnet/pithos/resources/myshared22.png")
135         ImageResource myShared();
136
137         @Source("gr/grnet/pithos/resources/folder_user.png")
138         ImageResource sharedFolder();
139
140         @Source("gr/grnet/pithos/resources/trash.png")
141         ImageResource trash();
142     }
143
144     static Images images = GWT.create(Images.class);
145
146     static interface Templates extends SafeHtmlTemplates {
147         public Templates INSTANCE = GWT.create(Templates.class);
148
149         @Template("<span style='vertical-align: middle;'>{0}</span>")
150         public SafeHtml nameSpan(String name);
151
152         @Template("<span class='pithos-folderLabel'>{0}</span>")
153         public SafeHtml imageSpan(String name);
154     }
155
156     private FolderTreeViewModel model;
157
158     public FolderTreeView(FolderTreeViewModel viewModel) {
159         this.model = viewModel;
160         /*
161          * Create the tree using the model. We use <code>null</code> as the default
162          * value of the root node. The default value will be passed to
163          * CustomTreeModel#getNodeInfo();
164          */
165         CellTree.Resources res = GWT.create(BasicResources.class);
166         CellTree tree = new CellTree(model, null, res);
167         tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
168
169         initWidget(tree);
170     }
171
172
173     @Override
174         public Folder getSelection() {
175        return model.getSelection();
176     }
177
178     public void updateFolder(Folder folder, boolean showfiles, Command callback, final boolean openParent) {
179         model.updateFolder(folder, showfiles, callback, openParent);
180     }
181 }