Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / grouptree / GroupTreeViewModel.java @ 1238d28d

History | View | Annotate | Download (8.4 kB)

1 ee9fbc37 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 ee9fbc37 Christos Stathis
 *
4 ee9fbc37 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 ee9fbc37 Christos Stathis
 * without modification, are permitted provided that the following
6 ee9fbc37 Christos Stathis
 * conditions are met:
7 ee9fbc37 Christos Stathis
 *
8 ee9fbc37 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 ee9fbc37 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 ee9fbc37 Christos Stathis
 *      disclaimer.
11 ee9fbc37 Christos Stathis
 *
12 ee9fbc37 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 ee9fbc37 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 ee9fbc37 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 ee9fbc37 Christos Stathis
 *      provided with the distribution.
16 ee9fbc37 Christos Stathis
 *
17 ee9fbc37 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 ee9fbc37 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ee9fbc37 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 ee9fbc37 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 ee9fbc37 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 ee9fbc37 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 ee9fbc37 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 ee9fbc37 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 ee9fbc37 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 ee9fbc37 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 ee9fbc37 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 ee9fbc37 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 ee9fbc37 Christos Stathis
 *
30 ee9fbc37 Christos Stathis
 * The views and conclusions contained in the software and
31 ee9fbc37 Christos Stathis
 * documentation are those of the authors and should not be
32 ee9fbc37 Christos Stathis
 * interpreted as representing official policies, either expressed
33 ee9fbc37 Christos Stathis
 * or implied, of GRNET S.A.
34 ee9fbc37 Christos Stathis
 */
35 ee9fbc37 Christos Stathis
36 ee9fbc37 Christos Stathis
package gr.grnet.pithos.web.client.grouptree;
37 ee9fbc37 Christos Stathis
38 ee9fbc37 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
39 54348648 Christos Stathis
import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
40 ee9fbc37 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
41 277505e5 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates;
42 ee9fbc37 Christos Stathis
43 ee9fbc37 Christos Stathis
import java.util.HashMap;
44 b162dfaa Christos Stathis
import java.util.HashSet;
45 ee9fbc37 Christos Stathis
import java.util.Map;
46 ee9fbc37 Christos Stathis
47 277505e5 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
48 277505e5 Christos Stathis
import com.google.gwt.cell.client.Cell;
49 277505e5 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
50 ee9fbc37 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
51 ee9fbc37 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
52 ee9fbc37 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
53 b162dfaa Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
54 b162dfaa Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
55 ee9fbc37 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
56 ee9fbc37 Christos Stathis
57 ee9fbc37 Christos Stathis
public class GroupTreeViewModel implements TreeViewModel {
58 ee9fbc37 Christos Stathis
59 ee9fbc37 Christos Stathis
    protected Pithos app;
60 ee9fbc37 Christos Stathis
61 4baffab1 Christos Stathis
        private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
62 ee9fbc37 Christos Stathis
63 277505e5 Christos Stathis
                @Override
64 ebead1b5 Christos Stathis
                public void render(Context context,        Group value, SafeHtmlBuilder sb) {
65 277505e5 Christos Stathis
            String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
66 a7b6e464 Christos Stathis
            sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
67 277505e5 Christos Stathis
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
68 277505e5 Christos Stathis
                }
69 4baffab1 Christos Stathis
                
70 4baffab1 Christos Stathis
        @Override
71 ebead1b5 Christos Stathis
        public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, Group group, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<Group> valueUpdater) {
72 54348648 Christos Stathis
                if (!group.equals(createGroup)) {
73 54348648 Christos Stathis
                    GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
74 54348648 Christos Stathis
                    if (event.getType().equals(ContextMenuEvent.getType().getName())) {
75 54348648 Christos Stathis
                        GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
76 54348648 Christos Stathis
                        menu.setPopupPosition(event.getClientX(), event.getClientY());
77 54348648 Christos Stathis
                        menu.show();
78 54348648 Christos Stathis
                    }
79 54348648 Christos Stathis
                }
80 4baffab1 Christos Stathis
        }
81 277505e5 Christos Stathis
        };
82 277505e5 Christos Stathis
83 4baffab1 Christos Stathis
    private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
84 277505e5 Christos Stathis
85 277505e5 Christos Stathis
                @Override
86 ebead1b5 Christos Stathis
                public void render(Context context,        User value, SafeHtmlBuilder sb) {
87 277505e5 Christos Stathis
            String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
88 a7b6e464 Christos Stathis
            sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
89 4baffab1 Christos Stathis
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
90 277505e5 Christos Stathis
                }
91 277505e5 Christos Stathis
92 4baffab1 Christos Stathis
        @Override
93 ebead1b5 Christos Stathis
        public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, User user, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<User> valueUpdater) {
94 b162dfaa Christos Stathis
            GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
95 4baffab1 Christos Stathis
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
96 4baffab1 Christos Stathis
                UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
97 4baffab1 Christos Stathis
                menu.setPopupPosition(event.getClientX(), event.getClientY());
98 4baffab1 Christos Stathis
                menu.show();
99 4baffab1 Christos Stathis
            }
100 4baffab1 Christos Stathis
        }
101 4baffab1 Christos Stathis
    };
102 4baffab1 Christos Stathis
103 277505e5 Christos Stathis
    protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
104 277505e5 Christos Stathis
105 4baffab1 Christos Stathis
    protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
106 ee9fbc37 Christos Stathis
    
107 b162dfaa Christos Stathis
    SingleSelectionModel<Group> groupSelectionModel;
108 b162dfaa Christos Stathis
    SingleSelectionModel<User> userSelectionModel;
109 54348648 Christos Stathis
    
110 54348648 Christos Stathis
    final Group createGroup = new Group("Create new group...");
111 b162dfaa Christos Stathis
112 4baffab1 Christos Stathis
    public GroupTreeViewModel(Pithos _app) {
113 ee9fbc37 Christos Stathis
        app = _app;
114 b162dfaa Christos Stathis
115 b162dfaa Christos Stathis
            groupSelectionModel = new SingleSelectionModel<Group>();
116 b162dfaa Christos Stathis
            app.addSelectionModel(groupSelectionModel);
117 b162dfaa Christos Stathis
            groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
118 b162dfaa Christos Stathis
                        
119 b162dfaa Christos Stathis
                        @Override
120 b162dfaa Christos Stathis
                        public void onSelectionChange(SelectionChangeEvent event) {
121 54348648 Christos Stathis
                                Group selected = groupSelectionModel.getSelectedObject();
122 54348648 Christos Stathis
                                if (selected != null) {
123 b162dfaa Christos Stathis
                                        app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
124 b162dfaa Christos Stathis
                                        app.showFiles(new HashSet<File>());
125 1238d28d Christos Stathis
                                        app.hideUploadArea();
126 1238d28d Christos Stathis
                                        app.upload.setEnabled(false);
127 80a65a5f Christos Stathis
                                        app.showRelevantToolbarButtons();
128 54348648 Christos Stathis
                                        if (selected.equals(createGroup)) {
129 54348648 Christos Stathis
                                                new CreateGroupCommand(app, null).execute();
130 54348648 Christos Stathis
                                                groupSelectionModel.setSelected(createGroup, false);
131 54348648 Christos Stathis
                                        }
132 80a65a5f Christos Stathis
                                }
133 80a65a5f Christos Stathis
                                else {
134 80a65a5f Christos Stathis
                                        if (app.getSelectedTree().equals(app.getGroupTreeView()))
135 80a65a5f Christos Stathis
                                                app.setSelectedTree(null);
136 80a65a5f Christos Stathis
                                        if (app.getSelectedTree() == null)
137 80a65a5f Christos Stathis
                                                app.showRelevantToolbarButtons();
138 b162dfaa Christos Stathis
                                }
139 b162dfaa Christos Stathis
                        }
140 b162dfaa Christos Stathis
                });
141 b162dfaa Christos Stathis
142 b162dfaa Christos Stathis
            userSelectionModel = new SingleSelectionModel<User>();
143 b162dfaa Christos Stathis
            app.addSelectionModel(userSelectionModel);
144 b162dfaa Christos Stathis
            userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
145 b162dfaa Christos Stathis
                        
146 b162dfaa Christos Stathis
                        @Override
147 b162dfaa Christos Stathis
                        public void onSelectionChange(SelectionChangeEvent event) {
148 b162dfaa Christos Stathis
                                if (userSelectionModel.getSelectedObject() != null) {
149 b162dfaa Christos Stathis
                                        app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
150 b162dfaa Christos Stathis
                                        app.showFiles(new HashSet<File>());
151 80a65a5f Christos Stathis
                                        app.showRelevantToolbarButtons();
152 80a65a5f Christos Stathis
                                }
153 80a65a5f Christos Stathis
                                else {
154 80a65a5f Christos Stathis
                                        if (app.getSelectedTree().equals(app.getGroupTreeView()))
155 80a65a5f Christos Stathis
                                                app.setSelectedTree(null);
156 80a65a5f Christos Stathis
                                        if (app.getSelectedTree() == null)
157 80a65a5f Christos Stathis
                                                app.showRelevantToolbarButtons();
158 b162dfaa Christos Stathis
                                }
159 b162dfaa Christos Stathis
                        }
160 b162dfaa Christos Stathis
                });
161 b162dfaa Christos Stathis
}
162 ee9fbc37 Christos Stathis
163 ee9fbc37 Christos Stathis
    @Override
164 ee9fbc37 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
165 ee9fbc37 Christos Stathis
        if (value == null) {
166 61f8605d Christos Stathis
                groupsDataProvider.getList().clear();
167 8c6e9848 Christos Stathis
                       groupsDataProvider.getList().addAll(app.getAccount().getGroups());
168 54348648 Christos Stathis
                       groupsDataProvider.getList().add(createGroup);
169 b162dfaa Christos Stathis
            return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null);
170 ee9fbc37 Christos Stathis
        }
171 c46c9e31 Christos Stathis
            Group g = (Group) value;
172 c46c9e31 Christos Stathis
                if (userDataProviderMap.get(g) == null) {
173 c46c9e31 Christos Stathis
                        userDataProviderMap.put(g, new ListDataProvider<User>());
174 c46c9e31 Christos Stathis
                }
175 c46c9e31 Christos Stathis
                final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
176 c46c9e31 Christos Stathis
                dataProvider.getList().clear();
177 c46c9e31 Christos Stathis
                for (String u : g.getMembers())
178 c46c9e31 Christos Stathis
                        dataProvider.getList().add(new User(u, g));
179 c46c9e31 Christos Stathis
            return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
180 ee9fbc37 Christos Stathis
    }
181 ee9fbc37 Christos Stathis
182 ee9fbc37 Christos Stathis
        @Override
183 ee9fbc37 Christos Stathis
    public boolean isLeaf(Object o) {
184 8c6e9848 Christos Stathis
        if (o instanceof User) {
185 8c6e9848 Christos Stathis
                       return true;
186 4baffab1 Christos Stathis
        }
187 4baffab1 Christos Stathis
        else if (o instanceof Group)
188 4baffab1 Christos Stathis
                return ((Group) o).getMembers().isEmpty();
189 8c6e9848 Christos Stathis
        return false;
190 ee9fbc37 Christos Stathis
    }
191 4baffab1 Christos Stathis
        
192 61f8605d Christos Stathis
        public void updateGroupNode(Group group) {
193 61f8605d Christos Stathis
                if (group == null) {
194 61f8605d Christos Stathis
                        groupsDataProvider.getList().clear();
195 61f8605d Christos Stathis
                        groupsDataProvider.getList().addAll(app.getAccount().getGroups());
196 54348648 Christos Stathis
                       groupsDataProvider.getList().add(createGroup);
197 61f8605d Christos Stathis
                }
198 61f8605d Christos Stathis
                else {
199 61f8605d Christos Stathis
                        if (userDataProviderMap.get(group) == null) {
200 61f8605d Christos Stathis
                                userDataProviderMap.put(group, new ListDataProvider<User>());
201 61f8605d Christos Stathis
                        }
202 61f8605d Christos Stathis
                        final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
203 61f8605d Christos Stathis
                        dataProvider.getList().clear();
204 61f8605d Christos Stathis
                        for (String u : group.getMembers())
205 61f8605d Christos Stathis
                                dataProvider.getList().add(new User(u, group));
206 61f8605d Christos Stathis
                }
207 61f8605d Christos Stathis
        }
208 b162dfaa Christos Stathis
209 b162dfaa Christos Stathis
        public Object getSelectedObject() {
210 b162dfaa Christos Stathis
                if (groupSelectionModel.getSelectedObject() != null)
211 b162dfaa Christos Stathis
                        return groupSelectionModel.getSelectedObject();
212 b162dfaa Christos Stathis
                if (userSelectionModel.getSelectedObject() != null)
213 b162dfaa Christos Stathis
                        return userSelectionModel.getSelectedObject();
214 b162dfaa Christos Stathis
                return null;
215 b162dfaa Christos Stathis
        }
216 ee9fbc37 Christos Stathis
}