Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.5 kB)

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.grouptree;
37

    
38
import gr.grnet.pithos.web.client.Pithos;
39
import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
40
import gr.grnet.pithos.web.client.foldertree.File;
41
import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates;
42

    
43
import java.util.HashMap;
44
import java.util.HashSet;
45
import java.util.Map;
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 Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
62

    
63
                @Override
64
                public void render(Context context,        Group value, SafeHtmlBuilder sb) {
65
            String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
66
            sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
67
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
68
                }
69
                
70
        @Override
71
        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
                if (!group.equals(createGroup)) {
73
                    GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
74
                    if (event.getType().equals(ContextMenuEvent.getType().getName())) {
75
                        GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
76
                        menu.setPopupPosition(event.getClientX(), event.getClientY());
77
                        menu.show();
78
                    }
79
                }
80
        }
81
        };
82

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

    
85
                @Override
86
                public void render(Context context,        User value, SafeHtmlBuilder sb) {
87
            String html = AbstractImagePrototype.create(GroupTreeView.images.user()).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, User user, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<User> valueUpdater) {
94
            GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
95
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
96
                UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
97
                menu.setPopupPosition(event.getClientX(), event.getClientY());
98
                menu.show();
99
            }
100
        }
101
    };
102

    
103
    protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
104

    
105
    protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
106
    
107
    SingleSelectionModel<Group> groupSelectionModel;
108
    SingleSelectionModel<User> userSelectionModel;
109
    
110
    final Group createGroup = new Group("Create new group...");
111

    
112
    public GroupTreeViewModel(Pithos _app) {
113
        app = _app;
114

    
115
            groupSelectionModel = new SingleSelectionModel<Group>();
116
            app.addSelectionModel(groupSelectionModel);
117
            groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
118
                        
119
                        @Override
120
                        public void onSelectionChange(SelectionChangeEvent event) {
121
                                Group selected = groupSelectionModel.getSelectedObject();
122
                                if (selected != null) {
123
                                        app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
124
                                        app.showFiles(new HashSet<File>());
125
                                        app.disableUploadArea();
126
                                        app.upload.setEnabled(false);
127
                                        app.showRelevantToolbarButtons();
128
                                        if (selected.equals(createGroup)) {
129
                                                new CreateGroupCommand(app, null).execute();
130
                                                groupSelectionModel.setSelected(createGroup, false);
131
                                        }
132
                                }
133
                                else {
134
                                        if (app.getSelectedTree().equals(app.getGroupTreeView()))
135
                                                app.setSelectedTree(null);
136
                                        if (app.getSelectedTree() == null)
137
                                                app.showRelevantToolbarButtons();
138
                                }
139
                        }
140
                });
141

    
142
            userSelectionModel = new SingleSelectionModel<User>();
143
            app.addSelectionModel(userSelectionModel);
144
            userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
145
                        
146
                        @Override
147
                        public void onSelectionChange(SelectionChangeEvent event) {
148
                                if (userSelectionModel.getSelectedObject() != null) {
149
                                        app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
150
                                        app.showFiles(new HashSet<File>());
151
                                        app.showRelevantToolbarButtons();
152
                                }
153
                                else {
154
                                        if (app.getSelectedTree().equals(app.getGroupTreeView()))
155
                                                app.setSelectedTree(null);
156
                                        if (app.getSelectedTree() == null)
157
                                                app.showRelevantToolbarButtons();
158
                                }
159
                        }
160
                });
161
}
162

    
163
    @Override
164
    public <T> NodeInfo<?> getNodeInfo(T value) {
165
        if (value == null) {
166
                groupsDataProvider.getList().clear();
167
                       groupsDataProvider.getList().addAll(app.getAccount().getGroups());
168
                       groupsDataProvider.getList().add(createGroup);
169
            return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null);
170
        }
171
            Group g = (Group) value;
172
                if (userDataProviderMap.get(g) == null) {
173
                        userDataProviderMap.put(g, new ListDataProvider<User>());
174
                }
175
                final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
176
                dataProvider.getList().clear();
177
                for (String u : g.getMembers())
178
                        dataProvider.getList().add(new User(u, g.getName()));
179
            return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
180
    }
181

    
182
        @Override
183
    public boolean isLeaf(Object o) {
184
        if (o instanceof User) {
185
                       return true;
186
        }
187
        else if (o instanceof Group)
188
                return ((Group) o).getMembers().isEmpty();
189
        return false;
190
    }
191
        
192
        public void updateGroupNode(Group group) {
193
                if (group == null) {
194
                        groupsDataProvider.getList().clear();
195
                        groupsDataProvider.getList().addAll(app.getAccount().getGroups());
196
                       groupsDataProvider.getList().add(createGroup);
197
                }
198
                else {
199
                        if (userDataProviderMap.get(group) == null) {
200
                                userDataProviderMap.put(group, new ListDataProvider<User>());
201
                        }
202
                        final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
203
                        dataProvider.getList().clear();
204
                        for (String u : group.getMembers())
205
                                dataProvider.getList().add(new User(u, group.getName()));
206
                }
207
        }
208

    
209
        public Object getSelectedObject() {
210
                if (groupSelectionModel.getSelectedObject() != null)
211
                        return groupSelectionModel.getSelectedObject();
212
                if (userSelectionModel.getSelectedObject() != null)
213
                        return userSelectionModel.getSelectedObject();
214
                return null;
215
        }
216
}