Remove the redundant gss top-level directory.
[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         private PickupDragController dragController;
197
198         public void onModuleLoad() {
199                 // Initialize the singleton before calling the constructors of the
200                 // various widgets that might call GSS.get().
201                 singleton = this;
202                 RootPanel.get().add(glassPanel, 0, 0);
203                 parseUserCredentials();
204                 dragController = new PickupDragController(RootPanel.get(), false) {
205
206                         @Override
207                         public void previewDragStart() throws VetoDragException {
208                             super.previewDragStart();
209                             if (context.selectedWidgets.isEmpty())
210                                         throw new VetoDragException();
211
212                             if(context.draggable != null){
213                                         DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
214                                         //prevent drag and drop for trashed files and for unselected tree items
215                                         if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
216                                                 throw new VetoDragException();
217                                         else if(toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
218                                                 throw new VetoDragException();
219                                         else if(toDrop.getItem() != null && !toDrop.getItem().isDraggable())
220                                                 throw new VetoDragException();
221
222                             }
223                           }
224
225                         @Override
226                         protected Widget newDragProxy(DragContext aContext) {
227                                 AbsolutePanel container = new AbsolutePanel();
228                                 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
229                                 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
230                                         Widget widget = (Widget) iterator.next();
231                                         DnDFocusPanel book = (DnDFocusPanel) widget;
232                                         HTML html = book.cloneHTML();
233                                         if(html == null)
234                                                 container.add(new Label("Drag ME"));
235                                         else
236                                                 container.add(html);
237                                 }
238                                 return container;
239                         }
240                 };
241                 dragController.setBehaviorDragProxy(true);
242                 dragController.setBehaviorMultipleSelection(false);
243                 topPanel = new TopPanel(GSS.images);
244                 topPanel.setWidth("100%");
245
246                 messagePanel.setWidth("100%");
247                 messagePanel.setVisible(false);
248
249                 search = new Search(images);
250                 searchStatus.add(search, DockPanel.WEST);
251                 searchStatus.add(userDetailsPanel, DockPanel.EAST);
252                 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
253                 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
254                 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
255                 searchStatus.setWidth("100%");
256
257                 fileList = new FileList(images);
258
259                 searchResults = new SearchResults(images);
260
261                 // Inner contains the various lists.
262                 inner.getTabBar().setStyleName("gss-TabBar");
263                 inner.setStyleName("gss-TabPanel");
264                 inner.add(fileList, createHeaderHTML(images.folders(), "Files"), true);
265
266                 inner.add(groups, createHeaderHTML(images.groups(), "Groups"), true);
267                 inner.add(searchResults, createHeaderHTML(images.search(), "Search Results"), true);
268                 inner.setWidth("100%");
269                 inner.selectTab(0);
270
271                 inner.addTabListener(new TabListener() {
272                         public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
273                         switch (tabIndex) {
274                                 case 0:
275                                         fileList.updateCurrentlyShowingStats();
276                                         break;
277                                 case 1:
278                                         groups.updateCurrentlyShowingStats();
279                                         break;
280                                 case 2:
281                                         searchResults.updateCurrentlyShowingStats();
282                                         break;
283                         }
284                         }
285
286                         public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
287                                 return true;
288                         }
289                 });
290
291                 // Add the left and right panels to the split panel.
292                 splitPanel.setLeftWidget(folders);
293                 splitPanel.setRightWidget(inner);
294                 splitPanel.setSplitPosition("25%");
295                 splitPanel.setSize("100%", "100%");
296
297                 // Create a dock panel that will contain the menu bar at the top,
298                 // the shortcuts to the left, the status bar at the bottom and the
299                 // right panel taking the rest.
300                 VerticalPanel outer = new VerticalPanel();
301                 outer.add(topPanel);
302                 outer.add(searchStatus);
303                 outer.add(messagePanel);
304                 outer.add(splitPanel);
305                 outer.add(statusPanel);
306                 outer.setWidth("100%");
307                 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
308
309                 outer.setSpacing(4);
310
311                 loading = new LoadingIndicator();
312
313                 // Hook the window resize event, so that we can adjust the UI.
314                 Window.addWindowResizeListener(this);
315
316                 // Clear out the window's built-in margin, because we want to take
317                 // advantage of the entire client area.
318                 Window.setMargin("0px");
319
320                 // Finally, add the outer panel to the RootPanel, so that it will be
321                 // displayed.
322                 RootPanel.get().add(outer);
323
324                 // Call the window resized handler to get the initial sizes setup. Doing
325                 // this in a deferred command causes it to occur after all widgets'
326                 // sizes have been computed by the browser.
327                 DeferredCommand.addCommand(new Command() {
328                         public void execute() {
329                                 onWindowResized(Window.getClientWidth(), Window.getClientHeight());
330                         }
331                 });
332         }
333
334         /**
335          * Fetches the User object for the specified username.
336          *
337          * @param username the username of the user
338          */
339         private void fetchUser(final String username) {
340                 String path = getApiPath() + username + "/";
341                 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path){
342
343                         @Override
344                         public void onComplete() {
345                                 currentUserResource = getResult();
346                                 final String announcement = currentUserResource.getAnnouncement();
347                                 if (announcement != null)
348                                         DeferredCommand.addCommand(new Command() {
349                                                 public void execute() {
350                                                         displayInformation(announcement);
351                                                 }
352                                         });
353                         }
354
355                         @Override
356                         public void onError(Throwable t) {
357                                 GWT.log("Fetching user error", t);
358                                 if(t instanceof RestException)
359                                         GSS.get().displayError("No user found:"+((RestException)t).getHttpStatusText());
360                                 else
361                                         GSS.get().displayError("System error fetching user data:"+t.getMessage());
362                                 authenticateUser();
363                         }
364                 };
365                 DeferredCommand.addCommand(getUserCommand);
366         }
367
368         /**
369          * Parse and store the user credentials to the appropriate fields.
370          */
371         private void parseUserCredentials() {
372                 //------------------------
373                 // XXX This part is only for development environments!
374                 // XXX Remove/comment for production deployment!
375                 final String _username = Window.Location.getParameter("user");
376                 token = Window.Location.getParameter("token");
377                 if (_username != null) {
378                         DeferredCommand.addCommand(new Command() {
379                                 public void execute() {
380                                         fetchUser(_username);
381                                 }
382                         });
383                         return;
384                 }
385                 //------------------------
386                 Configuration conf = (Configuration) GWT.create(Configuration.class);
387                 String cookie = conf.authCookie();
388                 String auth = Cookies.getCookie(cookie);
389                 String domain = Window.Location.getHostName();
390                 String path = Window.Location.getPath();
391                 Cookies.setCookie(cookie, "", null, domain, path, false);
392                 if (auth == null) {
393                         authenticateUser();
394                         // Redundant, but silences warnings about possible auth NPE, below.
395                         return;
396                 }
397                 int sepIndex = auth.indexOf(conf.cookieSeparator());
398                 if (sepIndex == -1)
399                         authenticateUser();
400                 token = auth.substring(sepIndex + 1, auth.length());
401                 final String username = auth.substring(0, sepIndex);
402                 if (username == null)
403                         authenticateUser();
404                 DeferredCommand.addCommand(new Command() {
405                         public void execute() {
406                                 fetchUser(username);
407                         }
408                 });
409         }
410
411         /**
412          * Redirect the user to the login page for authentication.
413          */
414         protected void authenticateUser() {
415                 Configuration conf = (Configuration) GWT.create(Configuration.class);
416                 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
417         }
418
419         /**
420          * Redirect the user to the logout page.
421          */
422         void logout() {
423                 Configuration conf = (Configuration) GWT.create(Configuration.class);
424                 Window.Location.assign(conf.logoutUrl());
425         }
426
427         /**
428          * Creates an HTML fragment that places an image & caption together, for use
429          * in a group header.
430          *
431          * @param imageProto an image prototype for an image
432          * @param caption the group caption
433          * @return the header HTML fragment
434          */
435         private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
436                 String captionHTML = "<table class='caption' cellpadding='0' " +
437                                 "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() +
438                                 "</td><td class='rcaption'><b style='white-space:nowrap'>&nbsp;" +
439                                 caption + "</b></td></tr></table>";
440                 return captionHTML;
441         }
442
443         /*
444          * (non-Javadoc)
445          *
446          * @see com.google.gwt.user.client.WindowResizeListener#onWindowResized(int,int)
447          */
448         public void onWindowResized(int width, int height) {
449                 // Adjust the split panel to take up the available room in the window.
450                 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
451                 if (newHeight < 1)
452                         newHeight = 1;
453                 splitPanel.setHeight("" + newHeight);
454         }
455
456         public boolean isFileListShowing(){
457                 int tab = inner.getTabBar().getSelectedTab();
458                 if(tab == 0) return true;
459                 return false;
460         }
461
462         public boolean isSearchResultsShowing(){
463                 int tab = inner.getTabBar().getSelectedTab();
464                 if(tab == 2) return true;
465                 return false;
466         }
467
468         /**
469          * Make the user list visible.
470          */
471         public void showUserList() {
472                 inner.selectTab(1);
473         }
474
475         /**
476          * Make the file list visible.
477          */
478         public void showFileList() {
479                 fileList.updateFileCache(false, true /*clear selection*/);
480                 inner.selectTab(0);
481         }
482
483         /**
484          * Make the file list visible.
485          * @param update
486          */
487         public void showFileList(boolean update) {
488                 TreeItem currentFolder = getFolders().getCurrent();
489                 if (currentFolder != null) {
490                         List<FileResource> files = null;
491                         Object cachedObject = currentFolder.getUserObject();
492                         if (cachedObject instanceof FolderResource) {
493                                 FolderResource folder = (FolderResource) cachedObject;
494                                 files = folder.getFiles();
495                         } else if (cachedObject instanceof TrashResource) {
496                                 TrashResource folder = (TrashResource) cachedObject;
497                                 files = folder.getFiles();
498                         }
499                         if (files != null)
500                                 getFileList().setFiles(files);
501                 }
502                 fileList.updateFileCache(update, true /*clear selection*/);
503                 inner.selectTab(0);
504         }
505
506         /**
507          * Make the search results visible.
508          * @param query the search query string
509          */
510         public void showSearchResults(String query) {
511                 searchResults.updateFileCache(query);
512                 searchResults.updateCurrentlyShowingStats();
513                 inner.selectTab(2);
514         }
515
516         /**
517          * Display the 'loading' indicator.
518          */
519         public void showLoadingIndicator() {
520                 loading.center();
521         }
522
523         /**
524          * Hide the 'loading' indicator.
525          */
526         public void hideLoadingIndicator() {
527                 loading.hide();
528         }
529
530         /**
531          * A native JavaScript method to reach out to the browser's window and
532          * invoke its resizeTo() method.
533          *
534          * @param x the new width
535          * @param y the new height
536          */
537         public static native void resizeTo(int x, int y) /*-{
538          $wnd.resizeTo(x,y);
539         }-*/;
540
541         /**
542          * A helper method that returns true if the user's list is currently visible
543          * and false if it is hidden.
544          *
545          * @return true if the user list is visible
546          */
547         public boolean isUserListVisible() {
548                 return inner.getTabBar().getSelectedTab() == 1;
549         }
550
551         /**
552          * Display an error message.
553          *
554          * @param msg the message to display
555          */
556         public void displayError(String msg) {
557                 messagePanel.displayError(msg);
558         }
559
560         /**
561          * Display a warning message.
562          *
563          * @param msg the message to display
564          */
565         public void displayWarning(String msg) {
566                 messagePanel.displayWarning(msg);
567         }
568
569         /**
570          * Display an informational message.
571          *
572          * @param msg the message to display
573          */
574         public void displayInformation(String msg) {
575                 messagePanel.displayInformation(msg);
576         }
577
578         /**
579          * Retrieve the folders.
580          *
581          * @return the folders
582          */
583         public Folders getFolders() {
584                 return folders;
585         }
586
587         /**
588          * Retrieve the search.
589          *
590          * @return the search
591          */
592         Search getSearch() {
593                 return search;
594         }
595
596         /**
597          * Retrieve the currentSelection.
598          *
599          * @return the currentSelection
600          */
601         public Object getCurrentSelection() {
602                 return currentSelection;
603         }
604
605         /**
606          * Modify the currentSelection.
607          *
608          * @param newCurrentSelection the currentSelection to set
609          */
610         public void setCurrentSelection(Object newCurrentSelection) {
611                 currentSelection = newCurrentSelection;
612         }
613
614         /**
615          * Retrieve the groups.
616          *
617          * @return the groups
618          */
619         public Groups getGroups() {
620                 return groups;
621         }
622
623         /**
624          * Retrieve the fileList.
625          *
626          * @return the fileList
627          */
628         public FileList getFileList() {
629                 return fileList;
630         }
631
632         public SearchResults getSearchResults(){
633                 return searchResults;
634         }
635
636         /**
637          * Retrieve the topPanel.
638          *
639          * @return the topPanel
640          */
641         TopPanel getTopPanel() {
642                 return topPanel;
643         }
644
645         /**
646          * Retrieve the clipboard.
647          *
648          * @return the clipboard
649          */
650         public Clipboard getClipboard() {
651                 return clipboard;
652         }
653
654
655         public StatusPanel getStatusPanel(){
656                 return statusPanel;
657         }
658
659         /**
660          * Retrieve the dragController.
661          *
662          * @return the dragController
663          */
664         public PickupDragController getDragController() {
665                 return dragController;
666         }
667
668         public String getToken(){
669                 return token;
670         }
671
672         public void removeGlassPanel(){
673                 glassPanel.removeFromParent();
674         }
675
676         /**
677          * Retrieve the currentUserResource.
678          *
679          * @return the currentUserResource
680          */
681         public UserResource getCurrentUserResource() {
682                 return currentUserResource;
683         }
684
685         public static native void preventIESelection() /*-{
686         $doc.body.onselectstart = function () { return false; };
687         }-*/;
688
689         public static native void enableIESelection() /*-{
690                 if ($doc.body.onselectstart != null)
691                         $doc.body.onselectstart = null;
692         }-*/;
693
694         /**
695          * @return the absolute path of the API root URL
696          */
697         public String getApiPath() {
698                 Configuration conf = (Configuration) GWT.create(Configuration.class);
699                 return GWT.getModuleBaseURL() + conf.apiPath();
700         }
701
702 }