Statistics
| Branch: | Tag: | Revision:

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

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
                @Override
68
                @Source("gr/ebs/gss/resources/editdelete.png")
69
                ImageResource delete();
70

    
71
        }
72

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

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

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

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

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

    
98
        private GroupContextMenu menu;
99

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

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

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

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

    
131

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

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

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

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

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

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

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

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

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

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

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

    
226

    
227

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

    
239

    
240

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

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

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

    
281
        }
282

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

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

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

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

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

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

    
331
        }
332

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

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