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