Fix confusion over width and height.
[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 = 100;
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.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 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 height = event.getHeight();
454                 onWindowResized(height);
455         }
456
457         public boolean isFileListShowing(){
458                 int tab = inner.getTabBar().getSelectedTab();
459                 if(tab == 0) return true;
460                 return false;
461         }
462
463         public boolean isSearchResultsShowing(){
464                 int tab = inner.getTabBar().getSelectedTab();
465                 if(tab == 2) return true;
466                 return false;
467         }
468
469         /**
470          * Make the user list visible.
471          */
472         public void showUserList() {
473                 inner.selectTab(1);
474         }
475
476         /**
477          * Make the file list visible.
478          */
479         public void showFileList() {
480                 fileList.updateFileCache(false, true /*clear selection*/);
481                 inner.selectTab(0);
482         }
483
484         /**
485          * Make the file list visible.
486          * @param update
487          */
488         public void showFileList(boolean update) {
489                 TreeItem currentFolder = getFolders().getCurrent();
490                 if (currentFolder != null) {
491                         List<FileResource> files = null;
492                         Object cachedObject = currentFolder.getUserObject();
493                         if (cachedObject instanceof FolderResource) {
494                                 FolderResource folder = (FolderResource) cachedObject;
495                                 files = folder.getFiles();
496                         } else if (cachedObject instanceof TrashResource) {
497                                 TrashResource folder = (TrashResource) cachedObject;
498                                 files = folder.getFiles();
499                         }
500                         if (files != null)
501                                 getFileList().setFiles(files);
502                 }
503                 fileList.updateFileCache(update, true /*clear selection*/);
504                 inner.selectTab(0);
505         }
506
507         /**
508          * Make the search results visible.
509          * @param query the search query string
510          */
511         public void showSearchResults(String query) {
512                 searchResults.updateFileCache(query);
513                 searchResults.updateCurrentlyShowingStats();
514                 inner.selectTab(2);
515         }
516
517         /**
518          * Display the 'loading' indicator.
519          */
520         public void showLoadingIndicator() {
521                 loading.center();
522         }
523
524         /**
525          * Hide the 'loading' indicator.
526          */
527         public void hideLoadingIndicator() {
528                 loading.hide();
529         }
530
531         /**
532          * A native JavaScript method to reach out to the browser's window and
533          * invoke its resizeTo() method.
534          *
535          * @param x the new width
536          * @param y the new height
537          */
538         public static native void resizeTo(int x, int y) /*-{
539          $wnd.resizeTo(x,y);
540         }-*/;
541
542         /**
543          * A helper method that returns true if the user's list is currently visible
544          * and false if it is hidden.
545          *
546          * @return true if the user list is visible
547          */
548         public boolean isUserListVisible() {
549                 return inner.getTabBar().getSelectedTab() == 1;
550         }
551
552         /**
553          * Display an error message.
554          *
555          * @param msg the message to display
556          */
557         public void displayError(String msg) {
558                 messagePanel.displayError(msg);
559         }
560
561         /**
562          * Display a warning message.
563          *
564          * @param msg the message to display
565          */
566         public void displayWarning(String msg) {
567                 messagePanel.displayWarning(msg);
568         }
569
570         /**
571          * Display an informational message.
572          *
573          * @param msg the message to display
574          */
575         public void displayInformation(String msg) {
576                 messagePanel.displayInformation(msg);
577         }
578
579         /**
580          * Retrieve the folders.
581          *
582          * @return the folders
583          */
584         public Folders getFolders() {
585                 return folders;
586         }
587
588         /**
589          * Retrieve the search.
590          *
591          * @return the search
592          */
593         Search getSearch() {
594                 return search;
595         }
596
597         /**
598          * Retrieve the currentSelection.
599          *
600          * @return the currentSelection
601          */
602         public Object getCurrentSelection() {
603                 return currentSelection;
604         }
605
606         /**
607          * Modify the currentSelection.
608          *
609          * @param newCurrentSelection the currentSelection to set
610          */
611         public void setCurrentSelection(Object newCurrentSelection) {
612                 currentSelection = newCurrentSelection;
613         }
614
615         /**
616          * Retrieve the groups.
617          *
618          * @return the groups
619          */
620         public Groups getGroups() {
621                 return groups;
622         }
623
624         /**
625          * Retrieve the fileList.
626          *
627          * @return the fileList
628          */
629         public FileList getFileList() {
630                 return fileList;
631         }
632
633         public SearchResults getSearchResults(){
634                 return searchResults;
635         }
636
637         /**
638          * Retrieve the topPanel.
639          *
640          * @return the topPanel
641          */
642         TopPanel getTopPanel() {
643                 return topPanel;
644         }
645
646         /**
647          * Retrieve the clipboard.
648          *
649          * @return the clipboard
650          */
651         public Clipboard getClipboard() {
652                 return clipboard;
653         }
654
655
656         public StatusPanel getStatusPanel(){
657                 return statusPanel;
658         }
659
660
661         /**
662          * Retrieve the userDetailsPanel.
663          *
664          * @return the userDetailsPanel
665          */
666         public UserDetailsPanel getUserDetailsPanel() {
667                 return userDetailsPanel;
668         }
669
670         /**
671          * Retrieve the dragController.
672          *
673          * @return the dragController
674          */
675         public PickupDragController getDragController() {
676                 return dragController;
677         }
678
679         public String getToken(){
680                 return token;
681         }
682
683         public String getWebDAVPassword() {
684                 return webDAVPassword;
685         }
686
687         public void removeGlassPanel(){
688                 glassPanel.removeFromParent();
689         }
690
691         /**
692          * Retrieve the currentUserResource.
693          *
694          * @return the currentUserResource
695          */
696         public UserResource getCurrentUserResource() {
697                 return currentUserResource;
698         }
699
700         /**
701          * Modify the currentUserResource.
702          *
703          * @param newUser the new currentUserResource
704          */
705         public void setCurrentUserResource(UserResource newUser) {
706                 currentUserResource = newUser;
707         }
708
709         public static native void preventIESelection() /*-{
710         $doc.body.onselectstart = function () { return false; };
711         }-*/;
712
713         public static native void enableIESelection() /*-{
714                 if ($doc.body.onselectstart != null)
715                         $doc.body.onselectstart = null;
716         }-*/;
717
718         /**
719          * @return the absolute path of the API root URL
720          */
721         public String getApiPath() {
722                 Configuration conf = (Configuration) GWT.create(Configuration.class);
723                 return GWT.getModuleBaseURL() + conf.apiPath();
724         }
725
726         public void refreshWebDAVPassword() {
727                 Configuration conf = (Configuration) GWT.create(Configuration.class);
728                 String domain = Window.Location.getHostName();
729                 String path = Window.Location.getPath();
730                 String cookie = conf.webdavCookie();
731                 webDAVPassword = Cookies.getCookie(cookie);
732                 Cookies.setCookie(cookie, "", null, domain, path, false);
733         }
734
735
736
737 }