Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / Groups.java @ fd294c94

History | View | Annotate | Download (10.2 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.rest.GetCommand;
22
import gr.ebs.gss.client.rest.MultipleGetCommand;
23
import gr.ebs.gss.client.rest.resource.GroupResource;
24
import gr.ebs.gss.client.rest.resource.GroupUserResource;
25
import gr.ebs.gss.client.rest.resource.GroupsResource;
26

    
27
import java.util.List;
28

    
29
import com.google.gwt.core.client.GWT;
30
import com.google.gwt.dom.client.NativeEvent;
31
import com.google.gwt.event.dom.client.ContextMenuEvent;
32
import com.google.gwt.event.dom.client.ContextMenuHandler;
33
import com.google.gwt.event.logical.shared.OpenEvent;
34
import com.google.gwt.event.logical.shared.OpenHandler;
35
import com.google.gwt.event.logical.shared.SelectionEvent;
36
import com.google.gwt.event.logical.shared.SelectionHandler;
37
import com.google.gwt.resources.client.ClientBundle;
38
import com.google.gwt.resources.client.ImageResource;
39
import com.google.gwt.user.client.DOM;
40
import com.google.gwt.user.client.DeferredCommand;
41
import com.google.gwt.user.client.Event;
42
import com.google.gwt.user.client.ui.AbstractImagePrototype;
43
import com.google.gwt.user.client.ui.Composite;
44
import com.google.gwt.user.client.ui.HTML;
45
import com.google.gwt.user.client.ui.Tree;
46
import com.google.gwt.user.client.ui.TreeItem;
47

    
48
/**
49
 * A component that displays a list of the user's groups.
50
 */
51
public class Groups extends Composite implements SelectionHandler, OpenHandler {
52

    
53
        /**
54
         * An image bundle for this widget.
55
         */
56
        public interface Images extends Tree.Resources, ClientBundle, FileMenu.Images, EditMenu.Images, GroupMenu.Images, MessagePanel.Images {
57

    
58
                /**
59
                 * Will bundle the file 'groupevent.png' residing in the package
60
                 * 'gr.ebs.gss.resources'.
61
                 *
62
                 * @return the image prototype
63
                 */
64
                @Source("gr/ebs/gss/resources/groupevent.png")
65
                ImageResource groupImage();
66

    
67
                @Source("gr/ebs/gss/resources/editdelete.png")
68
                ImageResource delete();
69

    
70
        }
71

    
72
        /**
73
         * cached latest group selection (for selecting and expanding on refresh)
74
         */
75
        private String selectedGroup = null;
76

    
77
        /**
78
         * The tree widget that displays the groups.
79
         */
80
        private Tree tree;
81

    
82
        /**
83
         * A cached copy of the currently selected group widget.
84
         */
85
        private TreeItem current;
86

    
87
        /**
88
         * A cached copy of the previously selected group widget.
89
         */
90
        private TreeItem previous;
91

    
92
        /**
93
         * The widget's image bundle.
94
         */
95
        private final Images images;
96

    
97
        private GroupContextMenu menu;
98

    
99
        /**
100
         * Constructs a new groups widget with a bundle of images.
101
         *
102
         * @param newImages a bundle that provides the images for this widget
103
         */
104
        public Groups(final Images newImages) {
105

    
106
                images = newImages;
107
                menu = new GroupContextMenu(images);
108
                tree = new Tree(newImages);
109
                this.addHandler(new ContextMenuHandler() {
110

    
111
                        @Override
112
                        public void onContextMenu(ContextMenuEvent event) {
113
                                if(current==null) return;
114
                                int left = current.getAbsoluteLeft() + 40;
115
                                int top = current.getAbsoluteTop() + 20;
116
                                showPopup(left, top);
117

    
118
                        }
119
                }, ContextMenuEvent.getType());
120
                tree.addSelectionHandler(this);
121
                tree.addOpenHandler(this);
122
                tree.setAnimationEnabled(true);
123
                initWidget(tree);
124
                setStylePrimaryName("gss-Groups");
125
                sinkEvents(Event.ONCONTEXTMENU);
126
                sinkEvents(Event.ONMOUSEUP);
127
                sinkEvents(Event.ONDBLCLICK);
128
        }
129

    
130

    
131
        /**
132
         * Make an RPC call to retrieve the groups that belong to the specified
133
         * user.
134
         */
135
        public void updateGroups() {
136
                GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(),null){
137

    
138
                        @Override
139
                        public void onComplete() {
140
                                GroupsResource res = getResult();
141
                                MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{}), null){
142

    
143
                                        @Override
144
                                        public void onComplete() {
145
                                                List<GroupResource> groupList = getResult();
146
                                                tree.clear();
147
                                                for (int i = 0; i < groupList.size(); i++) {
148
                                                        final TreeItem item = new TreeItem();
149
                                                        item.setWidget(imageItemHTML(images.groupImage(), groupList.get(i).getName(),item));
150
                                                        item.setUserObject(groupList.get(i));
151
                                                        tree.addItem(item);
152
                                                        updateUsers(item);
153
                                                }
154
                                        }
155

    
156
                                        @Override
157
                                        public void onError(Throwable t) {
158
                                                GWT.log("", t);
159
                                        }
160

    
161
                                        @Override
162
                                        public void onError(String p, Throwable throwable) {
163
                                                GWT.log("Path:"+p, throwable);
164
                                        }
165
                                };
166
                                DeferredCommand.addCommand(ga);
167
                        }
168

    
169
                        @Override
170
                        public void onError(Throwable t) {
171

    
172
                        }
173
                };
174
                DeferredCommand.addCommand(gg);
175
        }
176

    
177
        /**
178
         *  update status panel with currently showing file stats
179
         */
180
        public void updateCurrentlyShowingStats() {
181
                GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
182
        }
183

    
184
        /**
185
         * A helper method to simplify adding tree items that have attached images.
186
         * {@link #addImageItem(TreeItem, String) code}
187
         *
188
         * @param parent the tree item to which the new item will be added.
189
         * @param title the text associated with this item.
190
         * @param imageProto
191
         * @return the new tree item
192
         */
193
        private TreeItem addImageItem(final TreeItem parent, final String title, final ImageResource imageProto) {
194
                final TreeItem item = new TreeItem();
195
                item.setWidget(imageItemHTML(imageProto, title,item));
196
                parent.addItem(item);
197
                return item;
198
        }
199

    
200
        /**
201
         * Generates HTML for a tree item with an attached icon.
202
         *
203
         * @param imageProto the icon image
204
         * @param title the title of the item
205
         * @return the resultant HTML
206
         */
207
        private HTML imageItemHTML(final ImageResource imageProto, final String title,final TreeItem item) {
208
                final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span>" + AbstractImagePrototype.create(imageProto).getHTML() + "&nbsp;" + title + "</span>" + "</a>"){
209
                        @Override
210
                        public void onBrowserEvent(Event event) {
211
                                switch (DOM.eventGetType(event)) {
212
                                        case Event.ONMOUSEDOWN:
213
                                                if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT || DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
214
                                                        onSelection(item);
215
                                                break;
216
                                }
217
                                super.onBrowserEvent(event);
218

    
219
                        }
220
                };
221
                link.sinkEvents(Event.ONMOUSEDOWN);
222
                return link;
223
        }
224

    
225

    
226

    
227
        protected void showPopup(final int x, final int y) {
228
                if (getCurrent() == null){
229
                        GWT.log("[POPUP IS NULL]", null);
230
                        return;
231
                }
232
                menu.hide();
233
                menu = new GroupContextMenu(images);
234
                menu.setPopupPosition(x, y);
235
                menu.show();
236
        }
237

    
238

    
239

    
240
        /**
241
         * Generate an RPC request to retrieve the users of the specified group for
242
         * display.
243
         *
244
         * @param userId the ID of the current user
245
         * @param groupItem the TreeItem widget that corresponds to the requested
246
         *            group
247
         */
248
        void updateUsers(final TreeItem groupItem) {
249
                if(groupItem.getUserObject() instanceof GroupResource){
250
                        GroupResource res = (GroupResource) groupItem.getUserObject();
251
                        MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{}), null){
252
                                @Override
253
                                public void onComplete() {
254
                                        List<GroupUserResource> users = getResult();
255
                                        groupItem.removeItems();
256
                                        for (int i = 0; i < users.size(); i++) {
257
                                                final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " &lt;" + users.get(i).getUsername() + "&gt;", images.permUser());
258
                                                userItem.setUserObject(users.get(i));
259
                                        }
260
                                        if (selectedGroup != null && groupItem.getText().equals(selectedGroup)) {
261
                                                //SelectionEvent.fire(tree, groupItem);;
262
                                                onSelection(groupItem);
263
                                                groupItem.setState(true);
264
                                        }
265
                                }
266

    
267
                                @Override
268
                                public void onError(Throwable t) {
269
                                        GWT.log("", t);
270
                                }
271

    
272
                                @Override
273
                                public void onError(String p, Throwable throwable) {
274
                                        GWT.log("Path:"+p, throwable);
275
                                }
276
                        };
277
                        DeferredCommand.addCommand(gu);
278
                }
279

    
280
        }
281

    
282
        /**
283
         * Retrieve the current.
284
         *
285
         * @return the current
286
         */
287
        TreeItem getCurrent() {
288
                return current;
289
        }
290

    
291
        /**
292
         * Modify the current.
293
         *
294
         * @param newCurrent the current to set
295
         */
296
        void setCurrent(final TreeItem newCurrent) {
297
                current = newCurrent;
298
        }
299

    
300
        /**
301
         * Retrieve the previous.
302
         *
303
         * @return the previous
304
         */
305
        private TreeItem getPrevious() {
306
                return previous;
307
        }
308

    
309
        /**
310
         * Modify the previous.
311
         *
312
         * @param newPrevious the previous to set
313
         */
314
        private void setPrevious(final TreeItem newPrevious) {
315
                previous = newPrevious;
316
        }
317

    
318
        @Override
319
        public void setVisible(final boolean visible) {
320
                super.setVisible(visible);
321
                if (visible)
322
                        updateGroups();
323
        }
324

    
325
        @Override
326
        public void onSelection(SelectionEvent event) {
327
                final TreeItem item = (TreeItem)event.getSelectedItem();
328
                onSelection(item);
329

    
330
        }
331

    
332
        private void onSelection(TreeItem item){
333
                final Object selected = item.getUserObject();
334
                // Preserve the previously selected item, so that the current's
335
                // onClick() method gets a chance to find it.
336
                if (getPrevious() != null)
337
                        getPrevious().getWidget().removeStyleName("gss-SelectedRow");
338
                setCurrent(item);
339
                getCurrent().getWidget().addStyleName("gss-SelectedRow");
340
                setPrevious(getCurrent());
341
                GSS.get().setCurrentSelection(selected);
342
                //cache the latest top level node (group) for selecting and expanding on refresh
343
                if (item.getParentItem() == null)
344
                        selectedGroup = item.getText();
345
                else
346
                        selectedGroup = item.getParentItem().getText();
347
        }
348

    
349
        @Override
350
        public void onOpen(OpenEvent event) {
351
                final TreeItem item = (TreeItem) event.getTarget();
352
                updateUsers(item);
353
        }
354
}