Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / grouptree / GroupTreeViewModel.java @ 2e3e007c

History | View | Annotate | Download (7.7 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.Map;
44
import java.util.Set;
45

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

    
54
public class GroupTreeViewModel implements TreeViewModel {
55

    
56
    protected Pithos app;
57

    
58
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
59
    
60
    private Cell<String> rootCell = new AbstractCell<String>(ContextMenuEvent.getType().getName()) {
61

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

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

    
81
                @Override
82
                public void render(@SuppressWarnings("unused") Context context,        Group value, SafeHtmlBuilder sb) {
83
            String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
84
            sb.appendHtmlConstant(html);
85
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
86
                }
87
                
88
        @Override
89
        public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Group group, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<Group> valueUpdater) {
90
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
91
                GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
92
                menu.setPopupPosition(event.getClientX(), event.getClientY());
93
                menu.show();
94
            }
95
        }
96
        };
97

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

    
100
                @Override
101
                public void render(@SuppressWarnings("unused") Context context,        User value, SafeHtmlBuilder sb) {
102
            String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
103
            sb.appendHtmlConstant(html);
104
            sb.append(Templates.INSTANCE.nameSpan(value.getName()));
105
                }
106

    
107
        @Override
108
        public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, User user, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<User> valueUpdater) {
109
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
110
                UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
111
                menu.setPopupPosition(event.getClientX(), event.getClientY());
112
                menu.show();
113
            }
114
        }
115
    };
116

    
117
    protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
118

    
119
    protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
120
    
121
    protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
122

    
123
    public GroupTreeViewModel(Pithos _app) {
124
        app = _app;
125
    }
126

    
127
    @Override
128
    public <T> NodeInfo<?> getNodeInfo(T value) {
129
        if (value == null) {
130
                rootDataProvider.getList().add("");
131
            return new DefaultNodeInfo<String>(rootDataProvider, rootCell,  null, null);
132
        }
133
        else if (value instanceof String) {
134
                groupsDataProvider.getList().clear();
135
                groupsDataProvider.getList().addAll(app.getAccount().getGroups());
136
            return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, null, null);
137
        }
138
        else { //Group
139
                Group g = (Group) value;
140
                        if (userDataProviderMap.get(g) == null) {
141
                                userDataProviderMap.put(g, new ListDataProvider<User>());
142
                        }
143
                        final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
144
                        dataProvider.getList().clear();
145
                        for (String u : g.getMembers())
146
                                dataProvider.getList().add(new User(u, g));
147
                return new DefaultNodeInfo<User>(dataProvider, userCell, null, null);
148
        }
149
    }
150

    
151
        @Override
152
    public boolean isLeaf(Object o) {
153
        if (o instanceof String) {
154
                       return ((String) o).length() == 0 || app.getAccount().getGroups().isEmpty();
155
        }
156
        else if (o instanceof Group)
157
                return ((Group) o).getMembers().isEmpty();
158
        else if (o != null)
159
                return true;
160
        return false;
161
    }
162
        
163
        public void initialize() {
164
            rootDataProvider.getList().clear();
165
            rootDataProvider.getList().add("Groups");
166
        }
167

    
168
        public void updateGroupNode(Group group) {
169
                if (group == null) {
170
                        groupsDataProvider.getList().clear();
171
                        groupsDataProvider.getList().addAll(app.getAccount().getGroups());
172
                }
173
                else {
174
                        if (userDataProviderMap.get(group) == null) {
175
                                userDataProviderMap.put(group, new ListDataProvider<User>());
176
                        }
177
                        final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
178
                        dataProvider.getList().clear();
179
                        for (String u : group.getMembers())
180
                                dataProvider.getList().add(new User(u, group));
181
                }
182
        }
183
}