Change copyright notice
[pithos-web-client] / src / gr / grnet / pithos / web / client / grouptree / GroupTreeView.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.grouptree;
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 import gr.grnet.pithos.web.client.PithosDisclosurePanel.Style;
42 import gr.grnet.pithos.web.client.foldertree.Folder;
43
44 import com.google.gwt.core.client.GWT;
45 import com.google.gwt.resources.client.ImageResource;
46 import com.google.gwt.resources.client.ClientBundle.Source;
47 import com.google.gwt.resources.client.ImageResource.ImageOptions;
48 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
49 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
50 import com.google.gwt.safehtml.shared.SafeHtml;
51 import com.google.gwt.user.cellview.client.CellTree;
52 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
53 import com.google.gwt.user.cellview.client.TreeNode;
54 import com.google.gwt.user.client.ui.Composite;
55 import com.google.gwt.user.client.ui.DisclosurePanel;
56 import com.google.gwt.user.client.ui.HTML;
57 import com.google.gwt.user.client.ui.Tree;
58
59 public class GroupTreeView extends Composite implements TreeView {
60
61     public void updateChildren(Group group) {
62         TreeNode root = tree.getRootTreeNode();
63         if (group != null)
64                 updateChildren(root, group);
65         else if (root.getChildCount() > 0) {
66                 root.setChildOpen(0, false, true);
67                 root.setChildOpen(0, true, true);
68         }
69     }
70
71     private void updateChildren(TreeNode node, Group group) {
72         for (int i=0; i<node.getChildCount(); i++) {
73             if (group.equals(node.getChildValue(i))) {
74                 node.setChildOpen(i, false, true);
75                 node.setChildOpen(i, true, true);
76             }
77             else {
78                 if (node.isChildOpen(i)) {
79                         TreeNode n = node.setChildOpen(i, true);
80                         updateChildren(n, group);
81                 }       
82             }
83         }
84     }
85
86     static interface BasicResources extends CellTree.Resources {
87
88         @Override
89                 @ImageOptions(flipRtl = true)
90         @Source("gr/grnet/pithos/web/client/cellTreeClosedItem.png")
91         ImageResource cellTreeClosedItem();
92
93         @Override
94                 @ImageOptions(flipRtl = true)
95         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
96         ImageResource cellTreeLoading();
97
98         @Override
99                 @ImageOptions(flipRtl = true)
100         @Source("gr/grnet/pithos/web/client/cellTreeOpenItem.png")
101         ImageResource cellTreeOpenItem();
102
103         @Override
104                 @Source({"gr/grnet/pithos/web/client/PithosCellTreeBasic.css"})
105         CellTree.Style cellTreeStyle();
106         
107         @Source("gr/grnet/pithos/web/client/cellTreeLoadingBasic.gif")
108         @ImageOptions(repeatStyle=RepeatStyle.None)
109         ImageResource cellTreeLoadingBasic();
110     }
111
112     public static interface Images extends Tree.Resources, FolderContextMenu.Images {
113
114         @Source("gr/grnet/pithos/resources/groups22.png")
115         ImageResource groups();
116
117         @Source("gr/grnet/pithos/resources/groups22.png")
118         public ImageResource group();
119
120         @Source("gr/grnet/pithos/resources/edit_user.png")
121         ImageResource user();
122     }
123
124     static Images images = GWT.create(Images.class);
125     
126     interface Style extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Style {
127         @Override
128                 String header();
129     }
130  
131     interface Resources extends gr.grnet.pithos.web.client.PithosDisclosurePanel.Resources {
132                 @Override
133                 @Source("PithosGroupDisclosurePanel.css")
134                 Style pithosDisclosurePanelCss();
135
136                 @Override
137                 @Source("gr/grnet/pithos/resources/groups22.png")
138         ImageResource icon();
139     }
140
141     public static interface Templates extends SafeHtmlTemplates {
142         public Templates INSTANCE = GWT.create(Templates.class);
143
144         @Template("<span style='margin-left:5px;'>{0}</span>")
145         public SafeHtml nameSpan(String name);
146       }
147
148     private GroupTreeViewModel model;
149     
150     private CellTree tree;
151
152     public GroupTreeView(GroupTreeViewModel viewModel) {
153         this.model = viewModel;
154
155         PithosDisclosurePanel panel = new PithosDisclosurePanel((Resources) GWT.create(Resources.class), "Groups", false);
156         /*
157          * Create the tree using the model. We use <code>null</code> as the default
158          * value of the root node. The default value will be passed to
159          * CustomTreeModel#getNodeInfo();
160          */
161         CellTree.Resources res = GWT.create(BasicResources.class);
162         tree = new CellTree(model, null, res);
163         tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
164
165         panel.setContent(tree);
166         
167         initWidget(panel);
168     }
169
170     public void updateGroupNode(Group group) {
171         model.updateGroupNode(group);
172         updateChildren(group);
173     }
174
175         @Override
176         public Folder getSelection() {
177                 return null;
178         }
179
180         public Object getSelected() {
181                 return model.getSelectedObject();
182         }
183 }