Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.9 kB)

1
/*
2
 * Copyright 2011 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.Pithos;
39
import gr.grnet.pithos.web.client.foldertree.File;
40
import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates;
41

    
42
import java.util.HashMap;
43
import java.util.HashSet;
44
import java.util.Map;
45
import java.util.Set;
46

    
47
import com.google.gwt.cell.client.AbstractCell;
48
import com.google.gwt.cell.client.Cell;
49
import com.google.gwt.event.dom.client.ContextMenuEvent;
50
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
51
import com.google.gwt.user.client.ui.AbstractImagePrototype;
52
import com.google.gwt.view.client.ListDataProvider;
53
import com.google.gwt.view.client.SelectionChangeEvent;
54
import com.google.gwt.view.client.SingleSelectionModel;
55
import com.google.gwt.view.client.TreeViewModel;
56

    
57
public class GroupTreeViewModel implements TreeViewModel {
58

    
59
    protected Pithos app;
60

    
61
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
62
    
63
    private Cell<String> rootCell = new AbstractCell<String>(ContextMenuEvent.getType().getName()) {
64

    
65
                @Override
66
                public void render(Context context,        String value, SafeHtmlBuilder sb) {
67
            String html = AbstractImagePrototype.create(GroupTreeView.images.groups()).getHTML();
68
            sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
69
            sb.append(Templates.INSTANCE.nameSpan(value));
70
                }
71
                
72
        @Override
73
        public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, String s, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<String> valueUpdater) {
74
            GroupTreeViewModel.this.rootSelectionModel.setSelected(s, true);
75
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
76
                GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, null);
77
                menu.setPopupPosition(event.getClientX(), event.getClientY());
78
                menu.show();
79
            }
80
        }
81
        };
82

    
83
        private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
84

    
85
                @Override
86
                public void render(Context context,        Group value, SafeHtmlBuilder sb) {
87
            String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
88
            sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
89
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
90
                }
91
                
92
        @Override
93
        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) {
94
            GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
95
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
96
                GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
97
                menu.setPopupPosition(event.getClientX(), event.getClientY());
98
                menu.show();
99
            }
100
        }
101
        };
102

    
103
    private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
104

    
105
                @Override
106
                public void render(Context context,        User value, SafeHtmlBuilder sb) {
107
            String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
108
            sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
109
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
110
                }
111

    
112
        @Override
113
        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) {
114
            GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
115
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
116
                UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
117
                menu.setPopupPosition(event.getClientX(), event.getClientY());
118
                menu.show();
119
            }
120
        }
121
    };
122

    
123
    protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
124

    
125
    protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
126
    
127
    protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
128

    
129
    SingleSelectionModel<String> rootSelectionModel;
130
    SingleSelectionModel<Group> groupSelectionModel;
131
    SingleSelectionModel<User> userSelectionModel;
132

    
133
    public GroupTreeViewModel(Pithos _app) {
134
        app = _app;
135
        rootSelectionModel = new SingleSelectionModel<String>();
136
            app.addSelectionModel(rootSelectionModel);
137
            rootSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
138
                        
139
                        @Override
140
                        public void onSelectionChange(SelectionChangeEvent event) {
141
                                if (rootSelectionModel.getSelectedObject() != null) {
142
                                        app.deselectOthers(app.getGroupTreeView(), rootSelectionModel);
143
                                        app.showFiles(new HashSet<File>());
144
                                }
145
                                app.showRelevantToolbarButtons();
146
                        }
147
                });
148

    
149
            groupSelectionModel = new SingleSelectionModel<Group>();
150
            app.addSelectionModel(groupSelectionModel);
151
            groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
152
                        
153
                        @Override
154
                        public void onSelectionChange(SelectionChangeEvent event) {
155
                                if (groupSelectionModel.getSelectedObject() != null) {
156
                                        app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
157
                                        app.showFiles(new HashSet<File>());
158
                                }
159
                                app.showRelevantToolbarButtons();
160
                        }
161
                });
162

    
163
            userSelectionModel = new SingleSelectionModel<User>();
164
            app.addSelectionModel(userSelectionModel);
165
            userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
166
                        
167
                        @Override
168
                        public void onSelectionChange(SelectionChangeEvent event) {
169
                                if (userSelectionModel.getSelectedObject() != null) {
170
                                        app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
171
                                        app.showFiles(new HashSet<File>());
172
                                }
173
                                app.showRelevantToolbarButtons();
174
                        }
175
                });
176
}
177

    
178
    @Override
179
    public <T> NodeInfo<?> getNodeInfo(T value) {
180
        if (value == null) {
181
                rootDataProvider.getList().add("");
182
            return new DefaultNodeInfo<String>(rootDataProvider, rootCell,  rootSelectionModel, null);
183
        }
184
        else if (value instanceof String) {
185
                groupsDataProvider.getList().clear();
186
                groupsDataProvider.getList().addAll(app.getAccount().getGroups());
187
            return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null);
188
        }
189
        else { //Group
190
                Group g = (Group) value;
191
                        if (userDataProviderMap.get(g) == null) {
192
                                userDataProviderMap.put(g, new ListDataProvider<User>());
193
                        }
194
                        final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
195
                        dataProvider.getList().clear();
196
                        for (String u : g.getMembers())
197
                                dataProvider.getList().add(new User(u, g));
198
                return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
199
        }
200
    }
201

    
202
        @Override
203
    public boolean isLeaf(Object o) {
204
        if (o instanceof String) {
205
                       return ((String) o).length() == 0 || app.getAccount().getGroups().isEmpty();
206
        }
207
        else if (o instanceof Group)
208
                return ((Group) o).getMembers().isEmpty();
209
        else if (o != null)
210
                return true;
211
        return false;
212
    }
213
        
214
        public void initialize() {
215
            rootDataProvider.getList().clear();
216
            rootDataProvider.getList().add("Groups");
217
        }
218

    
219
        public void updateGroupNode(Group group) {
220
                if (group == null) {
221
                        groupsDataProvider.getList().clear();
222
                        groupsDataProvider.getList().addAll(app.getAccount().getGroups());
223
                }
224
                else {
225
                        if (userDataProviderMap.get(group) == null) {
226
                                userDataProviderMap.put(group, new ListDataProvider<User>());
227
                        }
228
                        final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
229
                        dataProvider.getList().clear();
230
                        for (String u : group.getMembers())
231
                                dataProvider.getList().add(new User(u, group));
232
                }
233
        }
234

    
235
        public Object getSelectedObject() {
236
                if (rootSelectionModel.getSelectedObject() != null)
237
                        return rootSelectionModel.getSelectedObject();
238
                if (groupSelectionModel.getSelectedObject() != null)
239
                        return groupSelectionModel.getSelectedObject();
240
                if (userSelectionModel.getSelectedObject() != null)
241
                        return userSelectionModel.getSelectedObject();
242
                return null;
243
        }
244
}