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
115 public void onBrowserEvent(Event event) {
\r
116 switch (DOM.eventGetType(event)) {
\r
117 case Event.ONKEYDOWN:
\r
118 int key = DOM.eventGetKeyCode(event);
\r
119 if (key == KeyboardListener.KEY_CTRL)
\r
120 ctrlKeyPressed = true;
\r
123 case Event.ONKEYUP:
\r
124 key = DOM.eventGetKeyCode(event);
\r
125 if (key == KeyboardListener.KEY_CTRL)
\r
126 ctrlKeyPressed = false;
\r
129 case Event.ONMOUSEDOWN:
\r
130 if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT)
\r
131 rightClicked = true;
\r
132 else if (DOM.eventGetButton(event) == Event.BUTTON_LEFT)
\r
133 leftClicked = true;
\r
136 case Event.ONMOUSEUP:
\r
137 if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT)
\r
138 rightClicked = false;
\r
139 else if (DOM.eventGetButton(event) == Event.BUTTON_LEFT)
\r
140 leftClicked = false;
\r
144 super.onBrowserEvent(event);
\r
147 * Make an RPC call to retrieve the groups that belong to the specified
\r
150 public void updateGroups() {
\r
151 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
159 public void onComplete() {
\r
160 List<GroupResource> groupList = getResult();
\r
162 for (int i = 0; i < groupList.size(); i++) {
\r
163 final TreeItem item = new TreeItem(imageItemHTML(images.groupImage(), groupList.get(i).getName()));
\r
164 item.setUserObject(groupList.get(i));
\r
165 tree.addItem(item);
\r
166 updateUsers( item);
\r
171 public void onError(Throwable t) {
\r
176 public void onError(String p, Throwable throwable) {
\r
177 GWT.log("Path:"+p, throwable);
\r
180 DeferredCommand.addCommand(ga);
\r
184 public void onError(Throwable t) {
\r
188 DeferredCommand.addCommand(gg);
\r
192 * update status panel with currently showing file stats
\r
194 public void updateCurrentlyShowingStats() {
\r
195 GSS.get().getStatusPanel().updateCurrentlyShowing(null); //clear stats - nothing to show for the groups tab
\r
199 * A helper method to simplify adding tree items that have attached images.
\r
200 * {@link #addImageItem(TreeItem, String) code}
\r
202 * @param parent the tree item to which the new item will be added.
\r
203 * @param title the text associated with this item.
\r
204 * @param imageProto
\r
205 * @return the new tree item
\r
207 private TreeItem addImageItem(final TreeItem parent, final String title, final AbstractImagePrototype imageProto) {
\r
208 final TreeItem item = new TreeItem(imageItemHTML(imageProto, title));
\r
209 parent.addItem(item);
\r
214 * Generates HTML for a tree item with an attached icon.
\r
216 * @param imageProto the icon image
\r
217 * @param title the title of the item
\r
218 * @return the resultant HTML
\r
220 private HTML imageItemHTML(final AbstractImagePrototype imageProto, final String title) {
\r
221 final HTML link = new HTML("<a class='hidden-link' href='javascript:;'>" + "<span>" + imageProto.getHTML() + " " + title + "</span>" + "</a>");
\r
228 * @see com.google.gwt.user.client.ui.TreeListener#onTreeItemSelected(com.google.gwt.user.client.ui.TreeItem)
\r
230 public void onTreeItemSelected(final TreeItem item) {
\r
231 final Object selected = item.getUserObject();
\r
232 // Preserve the previously selected item, so that the current's
\r
233 // onClick() method gets a chance to find it.
\r
234 if (getPrevious() != null)
\r
235 getPrevious().getWidget().removeStyleName("gss-SelectedRow");
\r
237 getCurrent().getWidget().addStyleName("gss-SelectedRow");
\r
238 setPrevious(getCurrent());
\r
239 GSS.get().setCurrentSelection(selected);
\r
240 if (rightClicked) {
\r
241 int left = item.getAbsoluteLeft() + 40;
\r
242 int top = item.getAbsoluteTop() + 20;
\r
243 showPopup(left, top);
\r
247 protected void showPopup(final int x, final int y) {
\r
248 if (getCurrent() == null)
\r
251 menu = new GroupContextMenu(images);
\r
252 menu.setPopupPosition(x, y);
\r
259 * @see com.google.gwt.user.client.ui.TreeListener#onTreeItemStateChanged(com.google.gwt.user.client.ui.TreeItem)
\r
261 public void onTreeItemStateChanged(final TreeItem item) {
\r
262 // Ignore closed items.
\r
263 if (!item.getState())
\r
267 updateUsers( item);
\r
271 * Generate an RPC request to retrieve the users of the specified group for
\r
274 * @param userId the ID of the current user
\r
275 * @param groupItem the TreeItem widget that corresponds to the requested
\r
278 void updateUsers(final TreeItem groupItem) {
\r
279 if(groupItem.getUserObject() instanceof GroupResource){
\r
280 GroupResource res = (GroupResource) groupItem.getUserObject();
\r
281 MultipleGetCommand<GroupUserResource> gu = new MultipleGetCommand<GroupUserResource>(GroupUserResource.class, res.getUserPaths().toArray(new String[]{})){
\r
283 public void onComplete() {
\r
284 List<GroupUserResource> users = getResult();
\r
285 groupItem.removeItems();
\r
286 for (int i = 0; i < users.size(); i++) {
\r
287 final TreeItem userItem = addImageItem(groupItem, users.get(i).getName() + " <" + users.get(i).getUsername() + ">", images.permUser());
\r
288 userItem.setUserObject(users.get(i));
\r
293 public void onError(Throwable t) {
\r
298 public void onError(String p, Throwable throwable) {
\r
299 GWT.log("Path:"+p, throwable);
\r
302 DeferredCommand.addCommand(gu);
\r
308 * Retrieve the current.
\r
310 * @return the current
\r
312 TreeItem getCurrent() {
\r
317 * Modify the current.
\r
319 * @param newCurrent the current to set
\r
321 void setCurrent(final TreeItem newCurrent) {
\r
322 current = newCurrent;
\r
326 * Modify the changed.
\r
328 * @param newChanged the changed to set
\r
330 private void setChanged(final TreeItem newChanged) {
\r
331 changed = newChanged;
\r
335 * Retrieve the previous.
\r
337 * @return the previous
\r
339 private TreeItem getPrevious() {
\r
344 * Modify the previous.
\r
346 * @param newPrevious the previous to set
\r
348 private void setPrevious(final TreeItem newPrevious) {
\r
349 previous = newPrevious;
\r
353 public void setVisible(final boolean visible) {
\r
354 super.setVisible(visible);
\r