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.grnet.pithos.web.client;
\r
21 import gr.grnet.pithos.web.client.rest.GetCommand;
\r
22 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
\r
23 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
\r
24 import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
\r
25 import gr.grnet.pithos.web.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.grnet.pithos.web.resources'.
\r
62 * @return the image prototype
\r
64 @Source("gr/grnet/pithos/resources/groupevent.png")
\r
65 ImageResource groupImage();
\r
68 @Source("gr/grnet/pithos/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.getElement().setId("groupsList.tree");
\r
122 tree.addSelectionHandler(this);
\r
123 tree.addOpenHandler(this);
\r
124 tree.setAnimationEnabled(true);
\r
126 this.getElement().setAttribute("id", "CreativeFilesPanel");
\r
127 setStylePrimaryName("pithos-Groups");
\r
128 sinkEvents(Event.ONCONTEXTMENU);
\r
129 sinkEvents(Event.ONMOUSEUP);
\r
130 sinkEvents(Event.ONDBLCLICK);
\r
131 sinkEvents(Event.KEYEVENTS);
\r
136 * Make an RPC call to retrieve the groups that belong to the specified
\r
139 public void updateGroups() {
\r
140 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(),null){
\r
143 public void onComplete() {
\r
144 GroupsResource res = getResult();
\r
145 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{}), null){
\r
148 public void onComplete() {
\r
149 List<GroupResource> groupList = getResult();
\r
151 for (int i = 0; i < groupList.size(); i++) {
\r
152 final TreeItem item = new TreeItem();
\r
153 item.setWidget(imageItemHTML(images.groupImage(), groupList.get(i).getName(),item));
\r
154 item.setUserObject(groupList.get(i));
\r
155 tree.addItem(item);
\r
161 public void onError(Throwable t) {
\r
166 public void onError(String p, Throwable throwable) {
\r
167 GWT.log("Path:"+p, throwable);
\r
170 DeferredCommand.addCommand(ga);
\r
174 public void onError(Throwable t) {
\r
178 DeferredCommand.addCommand(gg);
\r
182 * update status panel with currently showing file stats
\r
184 public void updateCurrentlyShowingStats() {
\r
185 GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
\r
189 * A helper method to simplify adding tree items that have attached images.
\r
191 * @param parent the tree item to which the new item will be added.
\r
192 * @param title the text associated with this item.
\r
193 * @param imageProto
\r
194 * @return the new tree item
\r
196 private TreeItem addImageItem(final TreeItem parent, final String title, final ImageResource imageProto) {
\r
197 final TreeItem item = new TreeItem();
\r
198 item.setWidget(imageItemHTML(imageProto, title,item));
\r
199 parent.addItem(item);
\r
204 * Generates HTML for a tree item with an attached icon.
\r
206 * @param imageProto the icon image
\r
207 * @param title the title of the item
\r
208 * @return the resultant HTML
\r
210 private HTML imageItemHTML(final ImageResource imageProto, final String title,final TreeItem item) {
\r
211 final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span id='groupsList."+title+"'>" + AbstractImagePrototype.create(imageProto).getHTML() + " " + title + "</span>" + "</a>"){
\r
213 public void onBrowserEvent(Event event) {
\r
214 switch (DOM.eventGetType(event)) {
\r
215 case Event.ONMOUSEDOWN:
\r
216 if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT || DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
\r
219 case Event.ONCONTEXTMENU:
\r
220 showPopup(event.getClientX(), event.getClientY());
\r
221 event.preventDefault();
\r
222 event.stopPropagation();
\r
225 super.onBrowserEvent(event);
\r
229 link.sinkEvents(Event.ONMOUSEDOWN);
\r
230 link.sinkEvents(Event.ONCONTEXTMENU);
\r
231 link.sinkEvents(Event.ONCLICK);
\r
232 link.sinkEvents(Event.ONKEYDOWN);
\r
238 protected void showPopup(final int x, final int y) {
\r
240 menu = new GroupContextMenu(images);
\r
241 menu.setPopupPosition(x, y);
\r
248 * Generate an RPC request to retrieve the users of the specified group for
\r
251 * @param groupItem the TreeItem widget that corresponds to the requested
\r
254 void updateUsers(final TreeItem groupItem) {
\r
255 if(groupItem.getUserObject() instanceof GroupResource){
\r
256 GroupResource res = (GroupResource) groupItem.getUserObject();
\r
257 MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{}), null){
\r
259 public void onComplete() {
\r
260 List<GroupUserResource> users = getResult();
\r
261 groupItem.removeItems();
\r
262 for (int i = 0; i < users.size(); i++) {
\r
263 final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " <" + users.get(i).getUsername() + ">", images.permUser());
\r
264 userItem.setUserObject(users.get(i));
\r
266 if (selectedGroup != null && groupItem.getText().equals(selectedGroup)) {
\r
267 //SelectionEvent.fire(tree, groupItem);;
\r
268 onSelection(groupItem);
\r
269 groupItem.setState(true);
\r
274 public void onError(Throwable t) {
\r
279 public void onError(String p, Throwable throwable) {
\r
280 GWT.log("Path:"+p, throwable);
\r
283 DeferredCommand.addCommand(gu);
\r
289 * Retrieve the current.
\r
291 * @return the current
\r
293 TreeItem getCurrent() {
\r
298 * Modify the current.
\r
300 * @param newCurrent the current to set
\r
302 void setCurrent(final TreeItem newCurrent) {
\r
303 current = newCurrent;
\r
307 * Retrieve the previous.
\r
309 * @return the previous
\r
311 private TreeItem getPrevious() {
\r
316 * Modify the previous.
\r
318 * @param newPrevious the previous to set
\r
320 private void setPrevious(final TreeItem newPrevious) {
\r
321 previous = newPrevious;
\r
325 public void setVisible(final boolean visible) {
\r
326 super.setVisible(visible);
\r
332 public void onSelection(SelectionEvent event) {
\r
333 final TreeItem item = (TreeItem)event.getSelectedItem();
\r
338 private void onSelection(TreeItem item){
\r
339 final Object selected = item.getUserObject();
\r
340 // Preserve the previously selected item, so that the current's
\r
341 // onClick() method gets a chance to find it.
\r
342 if (getPrevious() != null)
\r
343 getPrevious().getWidget().removeStyleName("pithos-SelectedRow");
\r
345 getCurrent().getWidget().addStyleName("pithos-SelectedRow");
\r
346 setPrevious(getCurrent());
\r
347 GSS.get().setCurrentSelection(selected);
\r
348 //cache the latest top level node (group) for selecting and expanding on refresh
\r
349 if (item.getParentItem() == null)
\r
350 selectedGroup = item.getText();
\r
352 selectedGroup = item.getParentItem().getText();
\r
356 public void onOpen(OpenEvent event) {
\r
357 final TreeItem item = (TreeItem) event.getTarget();
\r