2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\r
6 * GSS is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * GSS is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
\r
19 package gr.ebs.gss.client;
\r
21 import gr.ebs.gss.client.rest.GetCommand;
\r
22 import gr.ebs.gss.client.rest.MultipleGetCommand;
\r
23 import gr.ebs.gss.client.rest.resource.GroupResource;
\r
24 import gr.ebs.gss.client.rest.resource.GroupUserResource;
\r
25 import gr.ebs.gss.client.rest.resource.GroupsResource;
\r
27 import java.util.List;
\r
29 import com.google.gwt.core.client.GWT;
\r
30 import com.google.gwt.dom.client.NativeEvent;
\r
31 import com.google.gwt.event.dom.client.ContextMenuEvent;
\r
32 import com.google.gwt.event.dom.client.ContextMenuHandler;
\r
33 import com.google.gwt.event.logical.shared.OpenEvent;
\r
34 import com.google.gwt.event.logical.shared.OpenHandler;
\r
35 import com.google.gwt.event.logical.shared.SelectionEvent;
\r
36 import com.google.gwt.event.logical.shared.SelectionHandler;
\r
37 import com.google.gwt.resources.client.ClientBundle;
\r
38 import com.google.gwt.resources.client.ImageResource;
\r
39 import com.google.gwt.user.client.DOM;
\r
40 import com.google.gwt.user.client.DeferredCommand;
\r
41 import com.google.gwt.user.client.Event;
\r
42 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
43 import com.google.gwt.user.client.ui.Composite;
\r
44 import com.google.gwt.user.client.ui.HTML;
\r
45 import com.google.gwt.user.client.ui.Tree;
\r
46 import com.google.gwt.user.client.ui.TreeItem;
\r
49 * A component that displays a list of the user's groups.
\r
51 public class Groups extends Composite implements SelectionHandler, OpenHandler {
\r
54 * An image bundle for this widget.
\r
56 public interface Images extends Tree.Resources, ClientBundle, FileMenu.Images, EditMenu.Images, GroupMenu.Images, MessagePanel.Images {
\r
59 * Will bundle the file 'groupevent.png' residing in the package
\r
60 * 'gr.ebs.gss.resources'.
\r
62 * @return the image prototype
\r
64 @Source("gr/ebs/gss/resources/groupevent.png")
\r
65 ImageResource groupImage();
\r
68 @Source("gr/ebs/gss/resources/editdelete.png")
\r
69 ImageResource delete();
\r
74 * cached latest group selection (for selecting and expanding on refresh)
\r
76 private String selectedGroup = null;
\r
79 * The tree widget that displays the groups.
\r
84 * A cached copy of the currently selected group widget.
\r
86 private TreeItem current;
\r
89 * A cached copy of the previously selected group widget.
\r
91 private TreeItem previous;
\r
94 * The widget's image bundle.
\r
96 private final Images images;
\r
98 private GroupContextMenu menu;
\r
101 * Constructs a new groups widget with a bundle of images.
\r
103 * @param newImages a bundle that provides the images for this widget
\r
105 public Groups(final Images newImages) {
\r
107 images = newImages;
\r
108 menu = new GroupContextMenu(images);
\r
109 tree = new Tree(newImages);
\r
110 this.addHandler(new ContextMenuHandler() {
\r
113 public void onContextMenu(ContextMenuEvent event) {
\r
114 if(current==null) return;
\r
115 int left = current.getAbsoluteLeft() + 40;
\r
116 int top = current.getAbsoluteTop() + 20;
\r
117 showPopup(left, top);
\r
120 }, ContextMenuEvent.getType());
\r
121 tree.addSelectionHandler(this);
\r
122 tree.addOpenHandler(this);
\r
123 tree.setAnimationEnabled(true);
\r
125 setStylePrimaryName("gss-Groups");
\r
126 sinkEvents(Event.ONCONTEXTMENU);
\r
127 sinkEvents(Event.ONMOUSEUP);
\r
128 sinkEvents(Event.ONDBLCLICK);
\r
133 * Make an RPC call to retrieve the groups that belong to the specified
\r
136 public void updateGroups() {
\r
137 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(),null){
\r
140 public void onComplete() {
\r
141 GroupsResource res = getResult();
\r
142 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{}), null){
\r
145 public void onComplete() {
\r
146 List<GroupResource> groupList = getResult();
\r
148 for (int i = 0; i < groupList.size(); i++) {
\r
149 final TreeItem item = new TreeItem();
\r
150 item.setWidget(imageItemHTML(images.groupImage(), groupList.get(i).getName(),item));
\r
151 item.setUserObject(groupList.get(i));
\r
152 tree.addItem(item);
\r
158 public void onError(Throwable t) {
\r
163 public void onError(String p, Throwable throwable) {
\r
164 GWT.log("Path:"+p, throwable);
\r
167 DeferredCommand.addCommand(ga);
\r
171 public void onError(Throwable t) {
\r
175 DeferredCommand.addCommand(gg);
\r
179 * update status panel with currently showing file stats
\r
181 public void updateCurrentlyShowingStats() {
\r
182 GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
\r
186 * A helper method to simplify adding tree items that have attached images.
\r
187 * {@link #addImageItem(TreeItem, String) code}
\r
189 * @param parent the tree item to which the new item will be added.
\r
190 * @param title the text associated with this item.
\r
191 * @param imageProto
\r
192 * @return the new tree item
\r
194 private TreeItem addImageItem(final TreeItem parent, final String title, final ImageResource imageProto) {
\r
195 final TreeItem item = new TreeItem();
\r
196 item.setWidget(imageItemHTML(imageProto, title,item));
\r
197 parent.addItem(item);
\r
202 * Generates HTML for a tree item with an attached icon.
\r
204 * @param imageProto the icon image
\r
205 * @param title the title of the item
\r
206 * @return the resultant HTML
\r
208 private HTML imageItemHTML(final ImageResource imageProto, final String title,final TreeItem item) {
\r
209 final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span>" + AbstractImagePrototype.create(imageProto).getHTML() + " " + title + "</span>" + "</a>"){
\r
211 public void onBrowserEvent(Event event) {
\r
212 switch (DOM.eventGetType(event)) {
\r
213 case Event.ONMOUSEDOWN:
\r
214 if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT || DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
\r
218 super.onBrowserEvent(event);
\r
222 link.sinkEvents(Event.ONMOUSEDOWN);
\r
228 protected void showPopup(final int x, final int y) {
\r
229 if (getCurrent() == null){
\r
230 GWT.log("[POPUP IS NULL]", null);
\r
234 menu = new GroupContextMenu(images);
\r
235 menu.setPopupPosition(x, y);
\r
242 * Generate an RPC request to retrieve the users of the specified group for
\r
245 * @param userId the ID of the current user
\r
246 * @param groupItem the TreeItem widget that corresponds to the requested
\r
249 void updateUsers(final TreeItem groupItem) {
\r
250 if(groupItem.getUserObject() instanceof GroupResource){
\r
251 GroupResource res = (GroupResource) groupItem.getUserObject();
\r
252 MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{}), null){
\r
254 public void onComplete() {
\r
255 List<GroupUserResource> users = getResult();
\r
256 groupItem.removeItems();
\r
257 for (int i = 0; i < users.size(); i++) {
\r
258 final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " <" + users.get(i).getUsername() + ">", images.permUser());
\r
259 userItem.setUserObject(users.get(i));
\r
261 if (selectedGroup != null && groupItem.getText().equals(selectedGroup)) {
\r
262 //SelectionEvent.fire(tree, groupItem);;
\r
263 onSelection(groupItem);
\r
264 groupItem.setState(true);
\r
269 public void onError(Throwable t) {
\r
274 public void onError(String p, Throwable throwable) {
\r
275 GWT.log("Path:"+p, throwable);
\r
278 DeferredCommand.addCommand(gu);
\r
284 * Retrieve the current.
\r
286 * @return the current
\r
288 TreeItem getCurrent() {
\r
293 * Modify the current.
\r
295 * @param newCurrent the current to set
\r
297 void setCurrent(final TreeItem newCurrent) {
\r
298 current = newCurrent;
\r
302 * Retrieve the previous.
\r
304 * @return the previous
\r
306 private TreeItem getPrevious() {
\r
311 * Modify the previous.
\r
313 * @param newPrevious the previous to set
\r
315 private void setPrevious(final TreeItem newPrevious) {
\r
316 previous = newPrevious;
\r
320 public void setVisible(final boolean visible) {
\r
321 super.setVisible(visible);
\r
327 public void onSelection(SelectionEvent event) {
\r
328 final TreeItem item = (TreeItem)event.getSelectedItem();
\r
333 private void onSelection(TreeItem item){
\r
334 final Object selected = item.getUserObject();
\r
335 // Preserve the previously selected item, so that the current's
\r
336 // onClick() method gets a chance to find it.
\r
337 if (getPrevious() != null)
\r
338 getPrevious().getWidget().removeStyleName("gss-SelectedRow");
\r
340 getCurrent().getWidget().addStyleName("gss-SelectedRow");
\r
341 setPrevious(getCurrent());
\r
342 GSS.get().setCurrentSelection(selected);
\r
343 //cache the latest top level node (group) for selecting and expanding on refresh
\r
344 if (item.getParentItem() == null)
\r
345 selectedGroup = item.getText();
\r
347 selectedGroup = item.getParentItem().getText();
\r
351 public void onOpen(OpenEvent event) {
\r
352 final TreeItem item = (TreeItem) event.getTarget();
\r