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