Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / Groups.java @ 3ee27ba6

History | View | Annotate | Download (10.2 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.client;
20 14ad7326 pastith
21 895035a2 pastith
import gr.ebs.gss.client.rest.GetCommand;
22 895035a2 pastith
import gr.ebs.gss.client.rest.MultipleGetCommand;
23 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupResource;
24 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupUserResource;
25 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupsResource;
26 14ad7326 pastith
27 14ad7326 pastith
import java.util.List;
28 14ad7326 pastith
29 14ad7326 pastith
import com.google.gwt.core.client.GWT;
30 d322af78 Giannis Koutsoubos
import com.google.gwt.dom.client.NativeEvent;
31 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ContextMenuEvent;
32 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ContextMenuHandler;
33 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.OpenEvent;
34 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.OpenHandler;
35 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.SelectionEvent;
36 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.SelectionHandler;
37 afd3a0ef Giannis Koutsoubos
import com.google.gwt.resources.client.ClientBundle;
38 afd3a0ef Giannis Koutsoubos
import com.google.gwt.resources.client.ImageResource;
39 d322af78 Giannis Koutsoubos
import com.google.gwt.user.client.DOM;
40 a52ea5e4 pastith
import com.google.gwt.user.client.DeferredCommand;
41 14ad7326 pastith
import com.google.gwt.user.client.Event;
42 14ad7326 pastith
import com.google.gwt.user.client.ui.AbstractImagePrototype;
43 14ad7326 pastith
import com.google.gwt.user.client.ui.Composite;
44 14ad7326 pastith
import com.google.gwt.user.client.ui.HTML;
45 14ad7326 pastith
import com.google.gwt.user.client.ui.Tree;
46 14ad7326 pastith
import com.google.gwt.user.client.ui.TreeItem;
47 14ad7326 pastith
48 14ad7326 pastith
/**
49 14ad7326 pastith
 * A component that displays a list of the user's groups.
50 14ad7326 pastith
 */
51 afd3a0ef Giannis Koutsoubos
public class Groups extends Composite implements SelectionHandler, OpenHandler {
52 14ad7326 pastith
53 14ad7326 pastith
        /**
54 14ad7326 pastith
         * An image bundle for this widget.
55 14ad7326 pastith
         */
56 afd3a0ef Giannis Koutsoubos
        public interface Images extends Tree.Resources, ClientBundle, FileMenu.Images, EditMenu.Images, GroupMenu.Images, MessagePanel.Images {
57 14ad7326 pastith
58 14ad7326 pastith
                /**
59 14ad7326 pastith
                 * Will bundle the file 'groupevent.png' residing in the package
60 14ad7326 pastith
                 * 'gr.ebs.gss.resources'.
61 14ad7326 pastith
                 *
62 14ad7326 pastith
                 * @return the image prototype
63 14ad7326 pastith
                 */
64 afd3a0ef Giannis Koutsoubos
                @Source("gr/ebs/gss/resources/groupevent.png")
65 afd3a0ef Giannis Koutsoubos
                ImageResource groupImage();
66 14ad7326 pastith
67 023f6f1e Panagiotis Astithas
                @Override
68 afd3a0ef Giannis Koutsoubos
                @Source("gr/ebs/gss/resources/editdelete.png")
69 afd3a0ef Giannis Koutsoubos
                ImageResource delete();
70 d322af78 Giannis Koutsoubos
71 14ad7326 pastith
        }
72 14ad7326 pastith
73 9db64e8c Fotis Stamatelopoulos
        /**
74 9db64e8c Fotis Stamatelopoulos
         * cached latest group selection (for selecting and expanding on refresh)
75 9db64e8c Fotis Stamatelopoulos
         */
76 9db64e8c Fotis Stamatelopoulos
        private String selectedGroup = null;
77 14ad7326 pastith
78 14ad7326 pastith
        /**
79 14ad7326 pastith
         * The tree widget that displays the groups.
80 14ad7326 pastith
         */
81 14ad7326 pastith
        private Tree tree;
82 14ad7326 pastith
83 14ad7326 pastith
        /**
84 14ad7326 pastith
         * A cached copy of the currently selected group widget.
85 14ad7326 pastith
         */
86 14ad7326 pastith
        private TreeItem current;
87 14ad7326 pastith
88 14ad7326 pastith
        /**
89 14ad7326 pastith
         * A cached copy of the previously selected group widget.
90 14ad7326 pastith
         */
91 14ad7326 pastith
        private TreeItem previous;
92 14ad7326 pastith
93 14ad7326 pastith
        /**
94 14ad7326 pastith
         * The widget's image bundle.
95 14ad7326 pastith
         */
96 14ad7326 pastith
        private final Images images;
97 14ad7326 pastith
98 14ad7326 pastith
        private GroupContextMenu menu;
99 afd3a0ef Giannis Koutsoubos
100 14ad7326 pastith
        /**
101 14ad7326 pastith
         * Constructs a new groups widget with a bundle of images.
102 14ad7326 pastith
         *
103 14ad7326 pastith
         * @param newImages a bundle that provides the images for this widget
104 14ad7326 pastith
         */
105 14ad7326 pastith
        public Groups(final Images newImages) {
106 14ad7326 pastith
107 14ad7326 pastith
                images = newImages;
108 14ad7326 pastith
                menu = new GroupContextMenu(images);
109 14ad7326 pastith
                tree = new Tree(newImages);
110 afd3a0ef Giannis Koutsoubos
                this.addHandler(new ContextMenuHandler() {
111 afd3a0ef Giannis Koutsoubos
112 afd3a0ef Giannis Koutsoubos
                        @Override
113 afd3a0ef Giannis Koutsoubos
                        public void onContextMenu(ContextMenuEvent event) {
114 afd3a0ef Giannis Koutsoubos
                                if(current==null) return;
115 afd3a0ef Giannis Koutsoubos
                                int left = current.getAbsoluteLeft() + 40;
116 afd3a0ef Giannis Koutsoubos
                                int top = current.getAbsoluteTop() + 20;
117 afd3a0ef Giannis Koutsoubos
                                showPopup(left, top);
118 afd3a0ef Giannis Koutsoubos
119 afd3a0ef Giannis Koutsoubos
                        }
120 afd3a0ef Giannis Koutsoubos
                }, ContextMenuEvent.getType());
121 afd3a0ef Giannis Koutsoubos
                tree.addSelectionHandler(this);
122 afd3a0ef Giannis Koutsoubos
                tree.addOpenHandler(this);
123 5c6b2883 Panagiotis Astithas
                tree.setAnimationEnabled(true);
124 14ad7326 pastith
                initWidget(tree);
125 14ad7326 pastith
                setStylePrimaryName("gss-Groups");
126 14ad7326 pastith
                sinkEvents(Event.ONCONTEXTMENU);
127 14ad7326 pastith
                sinkEvents(Event.ONMOUSEUP);
128 afd3a0ef Giannis Koutsoubos
                sinkEvents(Event.ONDBLCLICK);
129 14ad7326 pastith
        }
130 14ad7326 pastith
131 14ad7326 pastith
132 14ad7326 pastith
        /**
133 14ad7326 pastith
         * Make an RPC call to retrieve the groups that belong to the specified
134 14ad7326 pastith
         * user.
135 14ad7326 pastith
         */
136 a52ea5e4 pastith
        public void updateGroups() {
137 62f168b2 Giannis Koutsoubos
                GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(),null){
138 a52ea5e4 pastith
139 aa07a34b Panagiotis Astithas
                        @Override
140 a52ea5e4 pastith
                        public void onComplete() {
141 a52ea5e4 pastith
                                GroupsResource res = getResult();
142 62f168b2 Giannis Koutsoubos
                                MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{}), null){
143 a52ea5e4 pastith
144 aa07a34b Panagiotis Astithas
                                        @Override
145 a52ea5e4 pastith
                                        public void onComplete() {
146 a52ea5e4 pastith
                                                List<GroupResource> groupList = getResult();
147 a52ea5e4 pastith
                                                tree.clear();
148 a52ea5e4 pastith
                                                for (int i = 0; i < groupList.size(); i++) {
149 d322af78 Giannis Koutsoubos
                                                        final TreeItem item = new TreeItem();
150 d322af78 Giannis Koutsoubos
                                                        item.setWidget(imageItemHTML(images.groupImage(), groupList.get(i).getName(),item));
151 a52ea5e4 pastith
                                                        item.setUserObject(groupList.get(i));
152 a52ea5e4 pastith
                                                        tree.addItem(item);
153 9db64e8c Fotis Stamatelopoulos
                                                        updateUsers(item);
154 a52ea5e4 pastith
                                                }
155 a52ea5e4 pastith
                                        }
156 14ad7326 pastith
157 aa07a34b Panagiotis Astithas
                                        @Override
158 a52ea5e4 pastith
                                        public void onError(Throwable t) {
159 a52ea5e4 pastith
                                                GWT.log("", t);
160 14ad7326 pastith
                                        }
161 a52ea5e4 pastith
162 aa07a34b Panagiotis Astithas
                                        @Override
163 a52ea5e4 pastith
                                        public void onError(String p, Throwable throwable) {
164 a52ea5e4 pastith
                                                GWT.log("Path:"+p, throwable);
165 a52ea5e4 pastith
                                        }
166 a52ea5e4 pastith
                                };
167 a52ea5e4 pastith
                                DeferredCommand.addCommand(ga);
168 14ad7326 pastith
                        }
169 14ad7326 pastith
170 aa07a34b Panagiotis Astithas
                        @Override
171 a52ea5e4 pastith
                        public void onError(Throwable t) {
172 a52ea5e4 pastith
173 a52ea5e4 pastith
                        }
174 a52ea5e4 pastith
                };
175 a52ea5e4 pastith
                DeferredCommand.addCommand(gg);
176 14ad7326 pastith
        }
177 14ad7326 pastith
178 14ad7326 pastith
        /**
179 b3c6d52e fstamatelopoulos
         *  update status panel with currently showing file stats
180 b3c6d52e fstamatelopoulos
         */
181 b3c6d52e fstamatelopoulos
        public void updateCurrentlyShowingStats() {
182 b3c6d52e fstamatelopoulos
                GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
183 b3c6d52e fstamatelopoulos
        }
184 b3c6d52e fstamatelopoulos
185 b3c6d52e fstamatelopoulos
        /**
186 14ad7326 pastith
         * A helper method to simplify adding tree items that have attached images.
187 14ad7326 pastith
         * {@link #addImageItem(TreeItem, String) code}
188 14ad7326 pastith
         *
189 14ad7326 pastith
         * @param parent the tree item to which the new item will be added.
190 14ad7326 pastith
         * @param title the text associated with this item.
191 14ad7326 pastith
         * @param imageProto
192 14ad7326 pastith
         * @return the new tree item
193 14ad7326 pastith
         */
194 afd3a0ef Giannis Koutsoubos
        private TreeItem addImageItem(final TreeItem parent, final String title, final ImageResource imageProto) {
195 d322af78 Giannis Koutsoubos
                final TreeItem item = new TreeItem();
196 d322af78 Giannis Koutsoubos
                item.setWidget(imageItemHTML(imageProto, title,item));
197 14ad7326 pastith
                parent.addItem(item);
198 14ad7326 pastith
                return item;
199 14ad7326 pastith
        }
200 14ad7326 pastith
201 14ad7326 pastith
        /**
202 14ad7326 pastith
         * Generates HTML for a tree item with an attached icon.
203 14ad7326 pastith
         *
204 14ad7326 pastith
         * @param imageProto the icon image
205 14ad7326 pastith
         * @param title the title of the item
206 14ad7326 pastith
         * @return the resultant HTML
207 14ad7326 pastith
         */
208 d322af78 Giannis Koutsoubos
        private HTML imageItemHTML(final ImageResource imageProto, final String title,final TreeItem item) {
209 d322af78 Giannis Koutsoubos
                final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span>" + AbstractImagePrototype.create(imageProto).getHTML() + "&nbsp;" + title + "</span>" + "</a>"){
210 d322af78 Giannis Koutsoubos
                        @Override
211 d322af78 Giannis Koutsoubos
                        public void onBrowserEvent(Event event) {
212 d322af78 Giannis Koutsoubos
                                switch (DOM.eventGetType(event)) {
213 d322af78 Giannis Koutsoubos
                                        case Event.ONMOUSEDOWN:
214 d322af78 Giannis Koutsoubos
                                                if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT || DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
215 d322af78 Giannis Koutsoubos
                                                        onSelection(item);
216 d322af78 Giannis Koutsoubos
                                                break;
217 d322af78 Giannis Koutsoubos
                                }
218 d322af78 Giannis Koutsoubos
                                super.onBrowserEvent(event);
219 d322af78 Giannis Koutsoubos
220 d322af78 Giannis Koutsoubos
                        }
221 d322af78 Giannis Koutsoubos
                };
222 d322af78 Giannis Koutsoubos
                link.sinkEvents(Event.ONMOUSEDOWN);
223 14ad7326 pastith
                return link;
224 14ad7326 pastith
        }
225 14ad7326 pastith
226 afd3a0ef Giannis Koutsoubos
227 14ad7326 pastith
228 14ad7326 pastith
        protected void showPopup(final int x, final int y) {
229 afd3a0ef Giannis Koutsoubos
                if (getCurrent() == null){
230 afd3a0ef Giannis Koutsoubos
                        GWT.log("[POPUP IS NULL]", null);
231 14ad7326 pastith
                        return;
232 afd3a0ef Giannis Koutsoubos
                }
233 14ad7326 pastith
                menu.hide();
234 14ad7326 pastith
                menu = new GroupContextMenu(images);
235 14ad7326 pastith
                menu.setPopupPosition(x, y);
236 14ad7326 pastith
                menu.show();
237 14ad7326 pastith
        }
238 b3c6d52e fstamatelopoulos
239 14ad7326 pastith
240 14ad7326 pastith
241 14ad7326 pastith
        /**
242 14ad7326 pastith
         * Generate an RPC request to retrieve the users of the specified group for
243 14ad7326 pastith
         * display.
244 14ad7326 pastith
         *
245 14ad7326 pastith
         * @param userId the ID of the current user
246 14ad7326 pastith
         * @param groupItem the TreeItem widget that corresponds to the requested
247 14ad7326 pastith
         *            group
248 14ad7326 pastith
         */
249 a52ea5e4 pastith
        void updateUsers(final TreeItem groupItem) {
250 a52ea5e4 pastith
                if(groupItem.getUserObject() instanceof GroupResource){
251 a52ea5e4 pastith
                        GroupResource res = (GroupResource) groupItem.getUserObject();
252 62f168b2 Giannis Koutsoubos
                        MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{}), null){
253 aa07a34b Panagiotis Astithas
                                @Override
254 a52ea5e4 pastith
                                public void onComplete() {
255 a52ea5e4 pastith
                                        List<GroupUserResource> users = getResult();
256 a52ea5e4 pastith
                                        groupItem.removeItems();
257 a52ea5e4 pastith
                                        for (int i = 0; i < users.size(); i++) {
258 a52ea5e4 pastith
                                                final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " &lt;" + users.get(i).getUsername() + "&gt;", images.permUser());
259 a52ea5e4 pastith
                                                userItem.setUserObject(users.get(i));
260 a52ea5e4 pastith
                                        }
261 9db64e8c Fotis Stamatelopoulos
                                        if (selectedGroup != null && groupItem.getText().equals(selectedGroup)) {
262 afd3a0ef Giannis Koutsoubos
                                                //SelectionEvent.fire(tree, groupItem);;
263 afd3a0ef Giannis Koutsoubos
                                                onSelection(groupItem);
264 9db64e8c Fotis Stamatelopoulos
                                                groupItem.setState(true);
265 9db64e8c Fotis Stamatelopoulos
                                        }
266 a52ea5e4 pastith
                                }
267 a52ea5e4 pastith
268 aa07a34b Panagiotis Astithas
                                @Override
269 a52ea5e4 pastith
                                public void onError(Throwable t) {
270 a52ea5e4 pastith
                                        GWT.log("", t);
271 a52ea5e4 pastith
                                }
272 a52ea5e4 pastith
273 aa07a34b Panagiotis Astithas
                                @Override
274 a52ea5e4 pastith
                                public void onError(String p, Throwable throwable) {
275 a52ea5e4 pastith
                                        GWT.log("Path:"+p, throwable);
276 a52ea5e4 pastith
                                }
277 a52ea5e4 pastith
                        };
278 a52ea5e4 pastith
                        DeferredCommand.addCommand(gu);
279 a52ea5e4 pastith
                }
280 14ad7326 pastith
281 14ad7326 pastith
        }
282 14ad7326 pastith
283 14ad7326 pastith
        /**
284 14ad7326 pastith
         * Retrieve the current.
285 14ad7326 pastith
         *
286 14ad7326 pastith
         * @return the current
287 14ad7326 pastith
         */
288 14ad7326 pastith
        TreeItem getCurrent() {
289 14ad7326 pastith
                return current;
290 14ad7326 pastith
        }
291 14ad7326 pastith
292 14ad7326 pastith
        /**
293 14ad7326 pastith
         * Modify the current.
294 14ad7326 pastith
         *
295 14ad7326 pastith
         * @param newCurrent the current to set
296 14ad7326 pastith
         */
297 14ad7326 pastith
        void setCurrent(final TreeItem newCurrent) {
298 14ad7326 pastith
                current = newCurrent;
299 14ad7326 pastith
        }
300 14ad7326 pastith
301 14ad7326 pastith
        /**
302 14ad7326 pastith
         * Retrieve the previous.
303 14ad7326 pastith
         *
304 14ad7326 pastith
         * @return the previous
305 14ad7326 pastith
         */
306 14ad7326 pastith
        private TreeItem getPrevious() {
307 14ad7326 pastith
                return previous;
308 14ad7326 pastith
        }
309 14ad7326 pastith
310 14ad7326 pastith
        /**
311 14ad7326 pastith
         * Modify the previous.
312 14ad7326 pastith
         *
313 14ad7326 pastith
         * @param newPrevious the previous to set
314 14ad7326 pastith
         */
315 14ad7326 pastith
        private void setPrevious(final TreeItem newPrevious) {
316 14ad7326 pastith
                previous = newPrevious;
317 14ad7326 pastith
        }
318 14ad7326 pastith
319 aa07a34b Panagiotis Astithas
        @Override
320 14ad7326 pastith
        public void setVisible(final boolean visible) {
321 14ad7326 pastith
                super.setVisible(visible);
322 14ad7326 pastith
                if (visible)
323 a52ea5e4 pastith
                        updateGroups();
324 14ad7326 pastith
        }
325 afd3a0ef Giannis Koutsoubos
326 afd3a0ef Giannis Koutsoubos
        @Override
327 afd3a0ef Giannis Koutsoubos
        public void onSelection(SelectionEvent event) {
328 afd3a0ef Giannis Koutsoubos
                final TreeItem item = (TreeItem)event.getSelectedItem();
329 afd3a0ef Giannis Koutsoubos
                onSelection(item);
330 afd3a0ef Giannis Koutsoubos
331 afd3a0ef Giannis Koutsoubos
        }
332 afd3a0ef Giannis Koutsoubos
333 afd3a0ef Giannis Koutsoubos
        private void onSelection(TreeItem item){
334 afd3a0ef Giannis Koutsoubos
                final Object selected = item.getUserObject();
335 afd3a0ef Giannis Koutsoubos
                // Preserve the previously selected item, so that the current's
336 afd3a0ef Giannis Koutsoubos
                // onClick() method gets a chance to find it.
337 afd3a0ef Giannis Koutsoubos
                if (getPrevious() != null)
338 afd3a0ef Giannis Koutsoubos
                        getPrevious().getWidget().removeStyleName("gss-SelectedRow");
339 afd3a0ef Giannis Koutsoubos
                setCurrent(item);
340 afd3a0ef Giannis Koutsoubos
                getCurrent().getWidget().addStyleName("gss-SelectedRow");
341 afd3a0ef Giannis Koutsoubos
                setPrevious(getCurrent());
342 afd3a0ef Giannis Koutsoubos
                GSS.get().setCurrentSelection(selected);
343 afd3a0ef Giannis Koutsoubos
                //cache the latest top level node (group) for selecting and expanding on refresh
344 afd3a0ef Giannis Koutsoubos
                if (item.getParentItem() == null)
345 afd3a0ef Giannis Koutsoubos
                        selectedGroup = item.getText();
346 afd3a0ef Giannis Koutsoubos
                else
347 afd3a0ef Giannis Koutsoubos
                        selectedGroup = item.getParentItem().getText();
348 afd3a0ef Giannis Koutsoubos
        }
349 afd3a0ef Giannis Koutsoubos
350 afd3a0ef Giannis Koutsoubos
        @Override
351 afd3a0ef Giannis Koutsoubos
        public void onOpen(OpenEvent event) {
352 afd3a0ef Giannis Koutsoubos
                final TreeItem item = (TreeItem) event.getTarget();
353 afd3a0ef Giannis Koutsoubos
                updateUsers(item);
354 afd3a0ef Giannis Koutsoubos
        }
355 14ad7326 pastith
}