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.user.client.DOM;
\r
31 import com.google.gwt.user.client.DeferredCommand;
\r
32 import com.google.gwt.user.client.Event;
\r
33 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
34 import com.google.gwt.user.client.ui.Composite;
\r
35 import com.google.gwt.user.client.ui.HTML;
\r
36 import com.google.gwt.user.client.ui.KeyboardListener;
\r
37 import com.google.gwt.user.client.ui.Tree;
\r
38 import com.google.gwt.user.client.ui.TreeImages;
\r
39 import com.google.gwt.user.client.ui.TreeItem;
\r
40 import com.google.gwt.user.client.ui.TreeListener;
\r
43 * A component that displays a list of the user's groups.
\r
45 public class Groups extends Composite implements TreeListener {
\r
48 * An image bundle for this widget.
\r
50 public interface Images extends TreeImages, FileMenu.Images, EditMenu.Images, GroupMenu.Images, MessagePanel.Images {
\r
53 * Will bundle the file 'groupevent.png' residing in the package
\r
54 * 'gr.ebs.gss.resources'.
\r
56 * @return the image prototype
\r
58 @Resource("gr/ebs/gss/resources/groupevent.png")
\r
59 AbstractImagePrototype groupImage();
\r
61 @Resource("gr/ebs/gss/resources/editdelete.png")
\r
62 AbstractImagePrototype delete();
\r
64 private boolean ctrlKeyPressed = false;
\r
66 private boolean leftClicked = false;
\r
68 private boolean rightClicked = false;
\r
72 * The tree widget that displays the groups.
\r
77 * A cached copy of the currently selected group widget.
\r
79 private TreeItem current;
\r
82 * A cached copy of the previously selected group widget.
\r
84 private TreeItem previous;
\r
87 * A cached copy of the currently changed group widget.
\r
89 private TreeItem changed;
\r
92 * The widget's image bundle.
\r
94 private final Images images;
\r
96 private GroupContextMenu menu;
\r
98 * Constructs a new groups widget with a bundle of images.
\r
100 * @param newImages a bundle that provides the images for this widget
\r
102 public Groups(final Images newImages) {
\r
104 images = newImages;
\r
105 menu = new GroupContextMenu(images);
\r
106 tree = new Tree(newImages);
\r
107 tree.addTreeListener(this);
\r
109 setStylePrimaryName("gss-Groups");
\r
110 sinkEvents(Event.ONCONTEXTMENU);
\r
111 sinkEvents(Event.ONMOUSEUP);
\r
114 public void onBrowserEvent(Event event) {
\r
115 switch (DOM.eventGetType(event)) {
\r
116 case Event.ONKEYDOWN:
\r
117 int key = DOM.eventGetKeyCode(event);
\r
118 if (key == KeyboardListener.KEY_CTRL)
\r
119 ctrlKeyPressed = true;
\r
122 case Event.ONKEYUP:
\r
123 key = DOM.eventGetKeyCode(event);
\r
124 if (key == KeyboardListener.KEY_CTRL)
\r
125 ctrlKeyPressed = false;
\r
128 case Event.ONMOUSEDOWN:
\r
129 if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT)
\r
130 rightClicked = true;
\r
131 else if (DOM.eventGetButton(event) == Event.BUTTON_LEFT)
\r
132 leftClicked = true;
\r
135 case Event.ONMOUSEUP:
\r
136 if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT)
\r
137 rightClicked = false;
\r
138 else if (DOM.eventGetButton(event) == Event.BUTTON_LEFT)
\r
139 leftClicked = false;
\r
143 super.onBrowserEvent(event);
\r
146 * Make an RPC call to retrieve the groups that belong to the specified
\r
149 * @param userId the user ID
\r
151 public void updateGroups() {
\r
152 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath()){
\r
154 public void onComplete() {
\r
155 GroupsResource res = getResult();
\r
156 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{})){
\r
158 public void onComplete() {
\r
159 List<GroupResource> groupList = getResult();
\r
161 for (int i = 0; i < groupList.size(); i++) {
\r
162 final TreeItem item = new TreeItem(imageItemHTML(images.groupImage(), groupList.get(i).getName()));
\r
163 item.setUserObject(groupList.get(i));
\r
164 tree.addItem(item);
\r
165 updateUsers( item);
\r
170 public void onError(Throwable t) {
\r
174 public void onError(String p, Throwable throwable) {
\r
175 GWT.log("Path:"+p, throwable);
\r
178 DeferredCommand.addCommand(ga);
\r
182 public void onError(Throwable t) {
\r
186 DeferredCommand.addCommand(gg);
\r
188 GetGroupsCommand gg = new GetGroupsCommand(GSS.get().getCurrentUserResource().getGroupsPath()){
\r
190 public void onComplete() {
\r
191 GroupsResource res = getResource();
\r
192 GetAllGroupsCommand ga = new GetAllGroupsCommand(res.getGroupPaths().toArray(new String[]{})){
\r
194 public void onComplete() {
\r
195 List<GroupResource> groupList = getList();
\r
197 for (int i = 0; i < groupList.size(); i++) {
\r
198 final TreeItem item = new TreeItem(imageItemHTML(images.groupImage(), groupList.get(i).getName()));
\r
199 item.setUserObject(groupList.get(i));
\r
200 tree.addItem(item);
\r
201 updateUsers( item);
\r
205 DeferredCommand.addCommand(ga);
\r
208 DeferredCommand.addCommand(gg);*/
\r
213 * update status panel with currently showing file stats
\r
215 public void updateCurrentlyShowingStats() {
\r
216 GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
\r
221 * A helper method to simplify adding tree items that have attached images.
\r
222 * {@link #addImageItem(TreeItem, String) code}
\r
224 * @param parent the tree item to which the new item will be added.
\r
225 * @param title the text associated with this item.
\r
226 * @param imageProto
\r
227 * @return the new tree item
\r
229 private TreeItem addImageItem(final TreeItem parent, final String title, final AbstractImagePrototype imageProto) {
\r
230 final TreeItem item = new TreeItem(imageItemHTML(imageProto, title));
\r
231 parent.addItem(item);
\r
236 * Generates HTML for a tree item with an attached icon.
\r
238 * @param imageProto the icon image
\r
239 * @param title the title of the item
\r
240 * @return the resultant HTML
\r
242 private HTML imageItemHTML(final AbstractImagePrototype imageProto, final String title) {
\r
243 final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span>" + imageProto.getHTML() + " " + title + "</span>" + "</a>");
\r
250 * @see com.google.gwt.user.client.ui.TreeListener#onTreeItemSelected(com.google.gwt.user.client.ui.TreeItem)
\r
252 public void onTreeItemSelected(final TreeItem item) {
\r
253 final Object selected = item.getUserObject();
\r
254 // Preserve the previously selected item, so that the current's
\r
255 // onClick() method gets a chance to find it.
\r
256 if (getPrevious() != null)
\r
257 getPrevious().getWidget().removeStyleName("gss-SelectedRow");
\r
259 getCurrent().getWidget().addStyleName("gss-SelectedRow");
\r
260 setPrevious(getCurrent());
\r
261 GSS.get().setCurrentSelection(selected);
\r
262 if (rightClicked) {
\r
263 int left = item.getAbsoluteLeft() + 40;
\r
264 int top = item.getAbsoluteTop() + 20;
\r
265 showPopup(left, top);
\r
269 protected void showPopup(final int x, final int y) {
\r
270 if (getCurrent() == null)
\r
273 menu = new GroupContextMenu(images);
\r
274 menu.setPopupPosition(x, y);
\r
281 * @see com.google.gwt.user.client.ui.TreeListener#onTreeItemStateChanged(com.google.gwt.user.client.ui.TreeItem)
\r
283 public void onTreeItemStateChanged(final TreeItem item) {
\r
284 // Ignore closed items.
\r
285 if (!item.getState())
\r
289 updateUsers( item);
\r
293 * Generate an RPC request to retrieve the users of the specified group for
\r
296 * @param userId the ID of the current user
\r
297 * @param groupItem the TreeItem widget that corresponds to the requested
\r
300 void updateUsers(final TreeItem groupItem) {
\r
301 if(groupItem.getUserObject() instanceof GroupResource){
\r
302 GroupResource res = (GroupResource) groupItem.getUserObject();
\r
303 MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{})){
\r
304 public void onComplete() {
\r
305 List<GroupUserResource> users = getResult();
\r
306 groupItem.removeItems();
\r
307 for (int i = 0; i < users.size(); i++) {
\r
308 final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " <" + users.get(i).getUsername() + ">", images.permUser());
\r
309 userItem.setUserObject(users.get(i));
\r
314 public void onError(Throwable t) {
\r
318 public void onError(String p, Throwable throwable) {
\r
319 GWT.log("Path:"+p, throwable);
\r
322 DeferredCommand.addCommand(gu);
\r
328 * Retrieve the current.
\r
330 * @return the current
\r
332 TreeItem getCurrent() {
\r
337 * Modify the current.
\r
339 * @param newCurrent the current to set
\r
341 void setCurrent(final TreeItem newCurrent) {
\r
342 current = newCurrent;
\r
346 * Modify the changed.
\r
348 * @param newChanged the changed to set
\r
350 private void setChanged(final TreeItem newChanged) {
\r
351 changed = newChanged;
\r
355 * Retrieve the previous.
\r
357 * @return the previous
\r
359 private TreeItem getPrevious() {
\r
364 * Modify the previous.
\r
366 * @param newPrevious the previous to set
\r
368 private void setPrevious(final TreeItem newPrevious) {
\r
369 previous = newPrevious;
\r
375 * @see com.google.gwt.user.client.ui.UIObject#setVisible(boolean)
\r
377 public void setVisible(final boolean visible) {
\r
378 super.setVisible(visible);
\r