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.gargoylesoftware.htmlunit.javascript.host.KeyboardEvent;
\r
30 import com.google.gwt.core.client.GWT;
\r
31 import com.google.gwt.dom.client.NativeEvent;
\r
32 import com.google.gwt.event.dom.client.ContextMenuEvent;
\r
33 import com.google.gwt.event.dom.client.ContextMenuHandler;
\r
34 import com.google.gwt.event.logical.shared.OpenEvent;
\r
35 import com.google.gwt.event.logical.shared.OpenHandler;
\r
36 import com.google.gwt.event.logical.shared.SelectionEvent;
\r
37 import com.google.gwt.event.logical.shared.SelectionHandler;
\r
38 import com.google.gwt.resources.client.ClientBundle;
\r
39 import com.google.gwt.resources.client.ImageResource;
\r
40 import com.google.gwt.user.client.DOM;
\r
41 import com.google.gwt.user.client.DeferredCommand;
\r
42 import com.google.gwt.user.client.Event;
\r
43 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
44 import com.google.gwt.user.client.ui.Composite;
\r
45 import com.google.gwt.user.client.ui.HTML;
\r
46 import com.google.gwt.user.client.ui.Tree;
\r
47 import com.google.gwt.user.client.ui.TreeItem;
\r
50 * A component that displays a list of the user's groups.
\r
52 public class Groups extends Composite implements SelectionHandler, OpenHandler {
\r
55 * An image bundle for this widget.
\r
57 public interface Images extends Tree.Resources, ClientBundle, FileMenu.Images, EditMenu.Images, GroupMenu.Images, MessagePanel.Images {
\r
60 * Will bundle the file 'groupevent.png' residing in the package
\r
61 * 'gr.ebs.gss.resources'.
\r
63 * @return the image prototype
\r
65 @Source("gr/ebs/gss/resources/groupevent.png")
\r
66 ImageResource groupImage();
\r
69 @Source("gr/ebs/gss/resources/editdelete.png")
\r
70 ImageResource delete();
\r
75 * cached latest group selection (for selecting and expanding on refresh)
\r
77 private String selectedGroup = null;
\r
80 * The tree widget that displays the groups.
\r
85 * A cached copy of the currently selected group widget.
\r
87 private TreeItem current;
\r
90 * A cached copy of the previously selected group widget.
\r
92 private TreeItem previous;
\r
95 * The widget's image bundle.
\r
97 private final Images images;
\r
99 private GroupContextMenu menu;
\r
102 * Constructs a new groups widget with a bundle of images.
\r
104 * @param newImages a bundle that provides the images for this widget
\r
106 public Groups(final Images newImages) {
\r
108 images = newImages;
\r
109 menu = new GroupContextMenu(images);
\r
110 tree = new Tree(newImages);
\r
111 this.addHandler(new ContextMenuHandler() {
\r
114 public void onContextMenu(ContextMenuEvent event) {
\r
115 if(current==null) return;
\r
116 int left = current.getAbsoluteLeft() + 40;
\r
117 int top = current.getAbsoluteTop() + 20;
\r
118 showPopup(left, top);
\r
121 }, ContextMenuEvent.getType());
\r
122 tree.getElement().setId("groupsList.tree");
\r
123 tree.addSelectionHandler(this);
\r
124 tree.addOpenHandler(this);
\r
125 tree.setAnimationEnabled(true);
\r
127 this.getElement().setAttribute("id", "CreativeFilesPanel");
\r
128 setStylePrimaryName("gss-Groups");
\r
129 sinkEvents(Event.ONCONTEXTMENU);
\r
130 sinkEvents(Event.ONMOUSEUP);
\r
131 sinkEvents(Event.ONDBLCLICK);
\r
132 sinkEvents(Event.KEYEVENTS);
\r
137 * Make an RPC call to retrieve the groups that belong to the specified
\r
140 public void updateGroups() {
\r
141 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(),null){
\r
144 public void onComplete() {
\r
145 GroupsResource res = getResult();
\r
146 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{}), null){
\r
149 public void onComplete() {
\r
150 List<GroupResource> groupList = getResult();
\r
152 for (int i = 0; i < groupList.size(); i++) {
\r
153 final TreeItem item = new TreeItem();
\r
154 item.setWidget(imageItemHTML(images.groupImage(), groupList.get(i).getName(),item));
\r
155 item.setUserObject(groupList.get(i));
\r
156 tree.addItem(item);
\r
162 public void onError(Throwable t) {
\r
167 public void onError(String p, Throwable throwable) {
\r
168 GWT.log("Path:"+p, throwable);
\r
171 DeferredCommand.addCommand(ga);
\r
175 public void onError(Throwable t) {
\r
179 DeferredCommand.addCommand(gg);
\r
183 * update status panel with currently showing file stats
\r
185 public void updateCurrentlyShowingStats() {
\r
186 GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
\r
190 * A helper method to simplify adding tree items that have attached images.
\r
191 * {@link #addImageItem(TreeItem, String) code}
\r
193 * @param parent the tree item to which the new item will be added.
\r
194 * @param title the text associated with this item.
\r
195 * @param imageProto
\r
196 * @return the new tree item
\r
198 private TreeItem addImageItem(final TreeItem parent, final String title, final ImageResource imageProto) {
\r
199 final TreeItem item = new TreeItem();
\r
200 item.setWidget(imageItemHTML(imageProto, title,item));
\r
201 parent.addItem(item);
\r
206 * Generates HTML for a tree item with an attached icon.
\r
208 * @param imageProto the icon image
\r
209 * @param title the title of the item
\r
210 * @return the resultant HTML
\r
212 private HTML imageItemHTML(final ImageResource imageProto, final String title,final TreeItem item) {
\r
213 final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span id='groupsList."+title+"'>" + AbstractImagePrototype.create(imageProto).getHTML() + " " + title + "</span>" + "</a>"){
\r
215 public void onBrowserEvent(Event event) {
\r
216 switch (DOM.eventGetType(event)) {
\r
217 case Event.ONMOUSEDOWN:
\r
218 if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT || DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
\r
222 super.onBrowserEvent(event);
\r
226 link.sinkEvents(Event.ONMOUSEDOWN);
\r
227 link.sinkEvents(Event.ONCONTEXTMENU);
\r
228 link.sinkEvents(Event.ONCLICK);
\r
229 link.sinkEvents(Event.ONKEYDOWN);
\r
235 protected void showPopup(final int x, final int y) {
\r
236 if (getCurrent() == null){
\r
237 GWT.log("[POPUP IS NULL]", null);
\r
241 menu = new GroupContextMenu(images);
\r
242 menu.setPopupPosition(x, y);
\r
249 * Generate an RPC request to retrieve the users of the specified group for
\r
252 * @param userId the ID of the current user
\r
253 * @param groupItem the TreeItem widget that corresponds to the requested
\r
256 void updateUsers(final TreeItem groupItem) {
\r
257 if(groupItem.getUserObject() instanceof GroupResource){
\r
258 GroupResource res = (GroupResource) groupItem.getUserObject();
\r
259 MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{}), null){
\r
261 public void onComplete() {
\r
262 List<GroupUserResource> users = getResult();
\r
263 groupItem.removeItems();
\r
264 for (int i = 0; i < users.size(); i++) {
\r
265 final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " <" + users.get(i).getUsername() + ">", images.permUser());
\r
266 userItem.setUserObject(users.get(i));
\r
268 if (selectedGroup != null && groupItem.getText().equals(selectedGroup)) {
\r
269 //SelectionEvent.fire(tree, groupItem);;
\r
270 onSelection(groupItem);
\r
271 groupItem.setState(true);
\r
276 public void onError(Throwable t) {
\r
281 public void onError(String p, Throwable throwable) {
\r
282 GWT.log("Path:"+p, throwable);
\r
285 DeferredCommand.addCommand(gu);
\r
291 * Retrieve the current.
\r
293 * @return the current
\r
295 TreeItem getCurrent() {
\r
300 * Modify the current.
\r
302 * @param newCurrent the current to set
\r
304 void setCurrent(final TreeItem newCurrent) {
\r
305 current = newCurrent;
\r
309 * Retrieve the previous.
\r
311 * @return the previous
\r
313 private TreeItem getPrevious() {
\r
318 * Modify the previous.
\r
320 * @param newPrevious the previous to set
\r
322 private void setPrevious(final TreeItem newPrevious) {
\r
323 previous = newPrevious;
\r
327 public void setVisible(final boolean visible) {
\r
328 super.setVisible(visible);
\r
334 public void onSelection(SelectionEvent event) {
\r
335 final TreeItem item = (TreeItem)event.getSelectedItem();
\r
340 private void onSelection(TreeItem item){
\r
341 final Object selected = item.getUserObject();
\r
342 // Preserve the previously selected item, so that the current's
\r
343 // onClick() method gets a chance to find it.
\r
344 if (getPrevious() != null)
\r
345 getPrevious().getWidget().removeStyleName("gss-SelectedRow");
\r
347 getCurrent().getWidget().addStyleName("gss-SelectedRow");
\r
348 setPrevious(getCurrent());
\r
349 GSS.get().setCurrentSelection(selected);
\r
350 //cache the latest top level node (group) for selecting and expanding on refresh
\r
351 if (item.getParentItem() == null)
\r
352 selectedGroup = item.getText();
\r
354 selectedGroup = item.getParentItem().getText();
\r
358 public void onOpen(OpenEvent event) {
\r
359 final TreeItem item = (TreeItem) event.getTarget();
\r