right click selection for folder tree
[pithos] / src / gr / ebs / gss / client / GSS.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.clipboard.Clipboard;
22 import gr.ebs.gss.client.dnd.DnDFocusPanel;
23 import gr.ebs.gss.client.rest.GetCommand;
24 import gr.ebs.gss.client.rest.RestException;
25 import gr.ebs.gss.client.rest.resource.FileResource;
26 import gr.ebs.gss.client.rest.resource.FolderResource;
27 import gr.ebs.gss.client.rest.resource.TrashResource;
28 import gr.ebs.gss.client.rest.resource.UserResource;
29
30 import java.util.Iterator;
31 import java.util.List;
32
33 import com.allen_sauer.gwt.dnd.client.DragContext;
34 import com.allen_sauer.gwt.dnd.client.PickupDragController;
35 import com.allen_sauer.gwt.dnd.client.VetoDragException;
36 import com.google.gwt.core.client.EntryPoint;
37 import com.google.gwt.core.client.GWT;
38 import com.google.gwt.event.logical.shared.ResizeEvent;
39 import com.google.gwt.event.logical.shared.ResizeHandler;
40 import com.google.gwt.event.logical.shared.SelectionEvent;
41 import com.google.gwt.event.logical.shared.SelectionHandler;
42 import com.google.gwt.resources.client.ClientBundle;
43 import com.google.gwt.resources.client.ImageResource;
44 import com.google.gwt.user.client.Command;
45 import com.google.gwt.user.client.Cookies;
46 import com.google.gwt.user.client.DOM;
47 import com.google.gwt.user.client.DeferredCommand;
48 import com.google.gwt.user.client.Window;
49 import com.google.gwt.user.client.ui.AbsolutePanel;
50 import com.google.gwt.user.client.ui.AbstractImagePrototype;
51 import com.google.gwt.user.client.ui.DecoratedTabPanel;
52 import com.google.gwt.user.client.ui.DockPanel;
53 import com.google.gwt.user.client.ui.HTML;
54 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
55 import com.google.gwt.user.client.ui.HasVerticalAlignment;
56 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
57 import com.google.gwt.user.client.ui.Label;
58 import com.google.gwt.user.client.ui.RootPanel;
59 import com.google.gwt.user.client.ui.TabPanel;
60 import com.google.gwt.user.client.ui.TreeItem;
61 import com.google.gwt.user.client.ui.VerticalPanel;
62 import com.google.gwt.user.client.ui.Widget;
63
64
65 /**
66  * Entry point classes define <code>onModuleLoad()</code>.
67  */
68 public class GSS implements EntryPoint, ResizeHandler {
69
70         /**
71          * A constant that denotes the completion of an IncrementalCommand.
72          */
73         public static final boolean DONE = false;
74
75         public static final int VISIBLE_FILE_COUNT = 100;
76
77         /**
78          * Instantiate an application-level image bundle. This object will provide
79          * programmatic access to all the images needed by widgets.
80          */
81         private static Images images = (Images) GWT.create(Images.class);
82         private GlassPanel glassPanel = new GlassPanel();
83
84         /**
85          * An aggregate image bundle that pulls together all the images for this
86          * application into a single bundle.
87          */
88         public interface Images extends ClientBundle,TopPanel.Images, StatusPanel.Images, FileMenu.Images, EditMenu.Images, SettingsMenu.Images, GroupMenu.Images, FilePropertiesDialog.Images, MessagePanel.Images, FileList.Images,  SearchResults.Images, Search.Images, Groups.Images, Folders.Images {
89
90                 @Source("gr/ebs/gss/resources/document.png")
91                 ImageResource folders();
92
93                 @Source("gr/ebs/gss/resources/edit_group_22.png")
94                 ImageResource groups();
95
96                 @Source("gr/ebs/gss/resources/search.png")
97                 ImageResource search();
98         }
99
100         /**
101          * The single GSS instance.
102          */
103         private static GSS singleton;
104
105         /**
106          * Gets the singleton GSS instance.
107          *
108          * @return the GSS object
109          */
110         public static GSS get() {
111                 if (GSS.singleton == null)
112                         GSS.singleton = new GSS();
113                 return GSS.singleton;
114         }
115
116         /**
117          * The Application Clipboard implementation;
118          */
119         private Clipboard clipboard = new Clipboard();
120
121         private UserResource currentUserResource;
122
123         /**
124          * The top panel that contains the menu bar.
125          */
126         private TopPanel topPanel;
127
128         /**
129          * The panel that contains the various system messages.
130          */
131         private MessagePanel messagePanel = new MessagePanel(GSS.images);
132
133         /**
134          * The bottom panel that contains the status bar.
135          */
136         private StatusPanel statusPanel = new StatusPanel(GSS.images);
137
138         /**
139          * The top right panel that displays the logged in user details
140          */
141         private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
142
143         /**
144          * The file list widget.
145          */
146         private FileList fileList;
147
148         /**
149          * The group list widget.
150          */
151         private Groups groups  = new Groups(images);
152
153         /**
154          * The search result widget.
155          */
156         private SearchResults searchResults;
157
158         /**
159          * A widget that displays a message indicating that communication with the
160          * server is underway.
161          */
162         private LoadingIndicator loading;
163
164         /**
165          * The tab panel that occupies the right side of the screen.
166          */
167         private TabPanel inner = new DecoratedTabPanel();
168
169         /**
170          * The split panel that will contain the left and right panels.
171          */
172         private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
173
174         /**
175          * The horizontal panel that will contain the search and status panels.
176          */
177         private DockPanel searchStatus = new DockPanel();
178
179         /**
180          * The search widget.
181          */
182         private Search search;
183
184         /**
185          * The widget that displays the tree of folders.
186          */
187         private Folders folders = new Folders(images);
188
189         /**
190          * The currently selected item in the application, for use by the Edit menu
191          * commands. Potential types are Folder, File, User and Group.
192          */
193         private Object currentSelection;
194
195         /**
196          * The authentication token of the current user.
197          */
198         private String token;
199
200         /**
201          * The WebDAV password of the current user
202          */
203         private String webDAVPassword;
204
205         private PickupDragController dragController;
206
207         public void onModuleLoad() {
208                 // Initialize the singleton before calling the constructors of the
209                 // various widgets that might call GSS.get().
210                 singleton = this;
211                 RootPanel.get().add(glassPanel, 0, 0);
212                 parseUserCredentials();
213                 dragController = new PickupDragController(RootPanel.get(), false) {
214
215                         @Override
216                         public void previewDragStart() throws VetoDragException {
217                             super.previewDragStart();
218                             if (context.selectedWidgets.isEmpty())
219                                         throw new VetoDragException();
220
221                             if(context.draggable != null){
222                                         DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
223                                         //prevent drag and drop for trashed files and for unselected tree items
224                                         if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
225                                                 throw new VetoDragException();
226                                         else if(toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
227                                                 throw new VetoDragException();
228                                         else if(toDrop.getItem() != null && !toDrop.getItem().isDraggable())
229                                                 throw new VetoDragException();
230
231                             }
232                           }
233
234                         @Override
235                         protected Widget newDragProxy(DragContext aContext) {
236                                 AbsolutePanel container = new AbsolutePanel();
237                                 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
238                                 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
239                                         Widget widget = (Widget) iterator.next();
240                                         DnDFocusPanel book = (DnDFocusPanel) widget;
241                                         HTML html = book.cloneHTML();
242                                         if(html == null)
243                                                 container.add(new Label("Drag ME"));
244                                         else
245                                                 container.add(html);
246                                 }
247                                 return container;
248                         }
249                 };
250                 dragController.setBehaviorDragProxy(true);
251                 dragController.setBehaviorMultipleSelection(false);
252                 topPanel = new TopPanel(GSS.images);
253                 topPanel.setWidth("100%");
254
255                 messagePanel.setWidth("100%");
256                 messagePanel.setVisible(false);
257
258                 search = new Search(images);
259                 searchStatus.add(search, DockPanel.WEST);
260                 searchStatus.add(userDetailsPanel, DockPanel.EAST);
261                 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
262                 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
263                 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
264                 searchStatus.setWidth("100%");
265
266                 fileList = new FileList(images);
267
268                 searchResults = new SearchResults(images);
269
270                 // Inner contains the various lists.
271                 inner.setAnimationEnabled(true);
272                 inner.getTabBar().addStyleName("gss-MainTabBar");
273                 inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
274                 inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
275
276                 inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
277                 inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
278                 inner.setWidth("100%");
279                 inner.selectTab(0);
280
281                 inner.addSelectionHandler(new SelectionHandler<Integer>() {
282
283                         @Override
284                         public void onSelection(SelectionEvent<Integer> event) {
285                                 int tabIndex= event.getSelectedItem();
286                                 switch (tabIndex) {
287                                 case 0:
288                                         fileList.clearSelectedRows();
289                                         fileList.updateCurrentlyShowingStats();
290                                         break;
291                                 case 1:
292                                         groups.updateCurrentlyShowingStats();
293                                         break;
294                                 case 2:
295                                         searchResults.clearSelectedRows();
296                                         searchResults.updateCurrentlyShowingStats();
297                                         break;
298                         }
299                         }
300                 });
301
302                 // Add the left and right panels to the split panel.
303                 splitPanel.setLeftWidget(folders);
304                 splitPanel.setRightWidget(inner);
305                 splitPanel.setSplitPosition("25%");
306                 splitPanel.setSize("100%", "100%");
307                 splitPanel.addStyleName("gss-splitPanel");
308
309                 // Create a dock panel that will contain the menu bar at the top,
310                 // the shortcuts to the left, the status bar at the bottom and the
311                 // right panel taking the rest.
312                 VerticalPanel outer = new VerticalPanel();
313                 outer.add(topPanel);
314                 outer.add(searchStatus);
315                 outer.add(messagePanel);
316                 outer.add(splitPanel);
317                 outer.add(statusPanel);
318                 outer.setWidth("100%");
319                 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
320
321                 outer.setSpacing(4);
322
323                 loading = new LoadingIndicator();
324                 // Hook the window resize event, so that we can adjust the UI.
325                 Window.addResizeHandler(this);
326                 // Clear out the window's built-in margin, because we want to take
327                 // advantage of the entire client area.
328                 Window.setMargin("0px");
329                 // Finally, add the outer panel to the RootPanel, so that it will be
330                 // displayed.
331                 RootPanel.get().add(outer);
332                 // Call the window resized handler to get the initial sizes setup. Doing
333                 // this in a deferred command causes it to occur after all widgets'
334                 // sizes have been computed by the browser.
335                 DeferredCommand.addCommand(new Command() {
336                         public void execute() {
337                                 onWindowResized(Window.getClientHeight());
338                         }
339                 });
340         }
341
342         /**
343          * Fetches the User object for the specified username.
344          *
345          * @param username the username of the user
346          */
347         private void fetchUser(final String username) {
348                 String path = getApiPath() + username + "/";
349                 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path){
350
351                         @Override
352                         public void onComplete() {
353                                 currentUserResource = getResult();
354                                 final String announcement = currentUserResource.getAnnouncement();
355                                 if (announcement != null)
356                                         DeferredCommand.addCommand(new Command() {
357                                                 public void execute() {
358                                                         displayInformation(announcement);
359                                                 }
360                                         });
361                         }
362
363                         @Override
364                         public void onError(Throwable t) {
365                                 GWT.log("Fetching user error", t);
366                                 if(t instanceof RestException)
367                                         GSS.get().displayError("No user found:"+((RestException)t).getHttpStatusText());
368                                 else
369                                         GSS.get().displayError("System error fetching user data:"+t.getMessage());
370                                 authenticateUser();
371                         }
372                 };
373                 DeferredCommand.addCommand(getUserCommand);
374         }
375
376         /**
377          * Parse and store the user credentials to the appropriate fields.
378          */
379         private void parseUserCredentials() {
380                 Configuration conf = (Configuration) GWT.create(Configuration.class);
381                 String cookie = conf.authCookie();
382                 String auth = Cookies.getCookie(cookie);
383                 String domain = Window.Location.getHostName();
384                 String path = Window.Location.getPath();
385                 Cookies.setCookie(cookie, "", null, domain, path, false);
386                 if (auth == null) {
387                         authenticateUser();
388                         // Redundant, but silences warnings about possible auth NPE, below.
389                         return;
390                 }
391                 int sepIndex = auth.indexOf(conf.cookieSeparator());
392                 if (sepIndex == -1)
393                         authenticateUser();
394                 token = auth.substring(sepIndex + 1, auth.length());
395                 final String username = auth.substring(0, sepIndex);
396                 if (username == null)
397                         authenticateUser();
398
399                 refreshWebDAVPassword();
400
401                 DeferredCommand.addCommand(new Command() {
402                         public void execute() {
403                                 fetchUser(username);
404                         }
405                 });
406         }
407
408         /**
409          * Redirect the user to the login page for authentication.
410          */
411         protected void authenticateUser() {
412                 Configuration conf = (Configuration) GWT.create(Configuration.class);
413                 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
414         }
415
416         /**
417          * Redirect the user to the logout page.
418          */
419         void logout() {
420                 Configuration conf = (Configuration) GWT.create(Configuration.class);
421                 Window.Location.assign(conf.logoutUrl());
422         }
423
424         /**
425          * Creates an HTML fragment that places an image & caption together, for use
426          * in a group header.
427          *
428          * @param imageProto an image prototype for an image
429          * @param caption the group caption
430          * @return the header HTML fragment
431          */
432         private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
433                 String captionHTML = "<table class='caption' cellpadding='0' " +
434                                 "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() +
435                                 "</td><td class='rcaption'><b style='white-space:nowrap'>&nbsp;" +
436                                 caption + "</b></td></tr></table>";
437                 return captionHTML;
438         }
439
440
441         private void onWindowResized(int height) {
442                 // Adjust the split panel to take up the available room in the window.
443                 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
444                 if (newHeight < 1)
445                         newHeight = 1;
446                 splitPanel.setHeight("" + newHeight);
447         }
448
449         @Override
450         public void onResize(ResizeEvent event) {
451                 int height = event.getHeight();
452                 onWindowResized(height);
453         }
454
455         public boolean isFileListShowing(){
456                 int tab = inner.getTabBar().getSelectedTab();
457                 if(tab == 0) return true;
458                 return false;
459         }
460
461         public boolean isSearchResultsShowing(){
462                 int tab = inner.getTabBar().getSelectedTab();
463                 if(tab == 2) return true;
464                 return false;
465         }
466
467         /**
468          * Make the user list visible.
469          */
470         public void showUserList() {
471                 inner.selectTab(1);
472         }
473
474         /**
475          * Make the file list visible.
476          */
477         public void showFileList() {
478                 fileList.updateFileCache(false, true /*clear selection*/);
479                 inner.selectTab(0);
480         }
481
482         /**
483          * Make the file list visible.
484          * @param update
485          */
486         public void showFileList(boolean update) {
487                 TreeItem currentFolder = getFolders().getCurrent();
488                 if (currentFolder != null) {
489                         List<FileResource> files = null;
490                         Object cachedObject = currentFolder.getUserObject();
491                         if (cachedObject instanceof FolderResource) {
492                                 FolderResource folder = (FolderResource) cachedObject;
493                                 files = folder.getFiles();
494                         } else if (cachedObject instanceof TrashResource) {
495                                 TrashResource folder = (TrashResource) cachedObject;
496                                 files = folder.getFiles();
497                         }
498                         if (files != null)
499                                 getFileList().setFiles(files);
500                 }
501                 fileList.updateFileCache(update, true /*clear selection*/);
502                 inner.selectTab(0);
503         }
504
505         /**
506          * Make the search results visible.
507          * @param query the search query string
508          */
509         public void showSearchResults(String query) {
510                 searchResults.updateFileCache(query);
511                 searchResults.updateCurrentlyShowingStats();
512                 inner.selectTab(2);
513         }
514
515         /**
516          * Display the 'loading' indicator.
517          */
518         public void showLoadingIndicator() {
519                 loading.center();
520         }
521
522         /**
523          * Hide the 'loading' indicator.
524          */
525         public void hideLoadingIndicator() {
526                 loading.hide();
527         }
528
529         /**
530          * A native JavaScript method to reach out to the browser's window and
531          * invoke its resizeTo() method.
532          *
533          * @param x the new width
534          * @param y the new height
535          */
536         public static native void resizeTo(int x, int y) /*-{
537          $wnd.resizeTo(x,y);
538         }-*/;
539
540         /**
541          * A helper method that returns true if the user's list is currently visible
542          * and false if it is hidden.
543          *
544          * @return true if the user list is visible
545          */
546         public boolean isUserListVisible() {
547                 return inner.getTabBar().getSelectedTab() == 1;
548         }
549
550         /**
551          * Display an error message.
552          *
553          * @param msg the message to display
554          */
555         public void displayError(String msg) {
556                 messagePanel.displayError(msg);
557         }
558
559         /**
560          * Display a warning message.
561          *
562          * @param msg the message to display
563          */
564         public void displayWarning(String msg) {
565                 messagePanel.displayWarning(msg);
566         }
567
568         /**
569          * Display an informational message.
570          *
571          * @param msg the message to display
572          */
573         public void displayInformation(String msg) {
574                 messagePanel.displayInformation(msg);
575         }
576
577         /**
578          * Retrieve the folders.
579          *
580          * @return the folders
581          */
582         public Folders getFolders() {
583                 return folders;
584         }
585
586         /**
587          * Retrieve the search.
588          *
589          * @return the search
590          */
591         Search getSearch() {
592                 return search;
593         }
594
595         /**
596          * Retrieve the currentSelection.
597          *
598          * @return the currentSelection
599          */
600         public Object getCurrentSelection() {
601                 return currentSelection;
602         }
603
604         /**
605          * Modify the currentSelection.
606          *
607          * @param newCurrentSelection the currentSelection to set
608          */
609         public void setCurrentSelection(Object newCurrentSelection) {
610                 currentSelection = newCurrentSelection;
611         }
612
613         /**
614          * Retrieve the groups.
615          *
616          * @return the groups
617          */
618         public Groups getGroups() {
619                 return groups;
620         }
621
622         /**
623          * Retrieve the fileList.
624          *
625          * @return the fileList
626          */
627         public FileList getFileList() {
628                 return fileList;
629         }
630
631         public SearchResults getSearchResults(){
632                 return searchResults;
633         }
634
635         /**
636          * Retrieve the topPanel.
637          *
638          * @return the topPanel
639          */
640         TopPanel getTopPanel() {
641                 return topPanel;
642         }
643
644         /**
645          * Retrieve the clipboard.
646          *
647          * @return the clipboard
648          */
649         public Clipboard getClipboard() {
650                 return clipboard;
651         }
652
653
654         public StatusPanel getStatusPanel(){
655                 return statusPanel;
656         }
657
658
659         /**
660          * Retrieve the userDetailsPanel.
661          *
662          * @return the userDetailsPanel
663          */
664         public UserDetailsPanel getUserDetailsPanel() {
665                 return userDetailsPanel;
666         }
667
668         /**
669          * Retrieve the dragController.
670          *
671          * @return the dragController
672          */
673         public PickupDragController getDragController() {
674                 return dragController;
675         }
676
677         public String getToken(){
678                 return token;
679         }
680
681         public String getWebDAVPassword() {
682                 return webDAVPassword;
683         }
684
685         public void removeGlassPanel(){
686                 glassPanel.removeFromParent();
687         }
688
689         /**
690          * Retrieve the currentUserResource.
691          *
692          * @return the currentUserResource
693          */
694         public UserResource getCurrentUserResource() {
695                 return currentUserResource;
696         }
697
698         /**
699          * Modify the currentUserResource.
700          *
701          * @param newUser the new currentUserResource
702          */
703         public void setCurrentUserResource(UserResource newUser) {
704                 currentUserResource = newUser;
705         }
706
707         public static native void preventIESelection() /*-{
708         $doc.body.onselectstart = function () { return false; };
709         }-*/;
710
711         public static native void enableIESelection() /*-{
712                 if ($doc.body.onselectstart != null)
713                         $doc.body.onselectstart = null;
714         }-*/;
715
716         /**
717          * @return the absolute path of the API root URL
718          */
719         public String getApiPath() {
720                 Configuration conf = (Configuration) GWT.create(Configuration.class);
721                 return GWT.getModuleBaseURL() + conf.apiPath();
722         }
723
724         public void refreshWebDAVPassword() {
725                 Configuration conf = (Configuration) GWT.create(Configuration.class);
726                 String domain = Window.Location.getHostName();
727                 String path = Window.Location.getPath();
728                 String cookie = conf.webdavCookie();
729                 webDAVPassword = Cookies.getCookie(cookie);
730                 Cookies.setCookie(cookie, "", null, domain, path, false);
731         }
732
733
734
735 }