2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
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;
30 import java.util.Iterator;
31 import java.util.List;
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;
65 * Entry point classes define <code>onModuleLoad()</code>.
67 public class GSS implements EntryPoint, ResizeHandler {
70 * A constant that denotes the completion of an IncrementalCommand.
72 public static final boolean DONE = false;
74 public static final int VISIBLE_FILE_COUNT = 100;
77 * Instantiate an application-level image bundle. This object will provide
78 * programmatic access to all the images needed by widgets.
80 private static Images images = (Images) GWT.create(Images.class);
81 private GlassPanel glassPanel = new GlassPanel();
84 * An aggregate image bundle that pulls together all the images for this
85 * application into a single bundle.
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 {
89 @Source("gr/ebs/gss/resources/document.png")
90 ImageResource folders();
92 @Source("gr/ebs/gss/resources/edit_group_22.png")
93 ImageResource groups();
95 @Source("gr/ebs/gss/resources/search.png")
96 ImageResource search();
100 * The single GSS instance.
102 private static GSS singleton;
105 * Gets the singleton GSS instance.
107 * @return the GSS object
109 public static GSS get() {
110 if (GSS.singleton == null)
111 GSS.singleton = new GSS();
112 return GSS.singleton;
116 * The Application Clipboard implementation;
118 private Clipboard clipboard = new Clipboard();
120 private UserResource currentUserResource;
123 * The top panel that contains the menu bar.
125 private TopPanel topPanel;
128 * The panel that contains the various system messages.
130 private MessagePanel messagePanel = new MessagePanel(GSS.images);
133 * The bottom panel that contains the status bar.
135 private StatusPanel statusPanel = new StatusPanel(GSS.images);
138 * The top right panel that displays the logged in user details
140 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
143 * The file list widget.
145 private FileList fileList;
148 * The group list widget.
150 private Groups groups = new Groups(images);
153 * The search result widget.
155 private SearchResults searchResults;
158 * A widget that displays a message indicating that communication with the
159 * server is underway.
161 private LoadingIndicator loading;
164 * The tab panel that occupies the right side of the screen.
166 private TabPanel inner = new TabPanel();
169 * The split panel that will contain the left and right panels.
171 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
174 * The horizontal panel that will contain the search and status panels.
176 private DockPanel searchStatus = new DockPanel();
181 private Search search;
184 * The widget that displays the tree of folders.
186 private Folders folders = new Folders(images);
189 * The currently selected item in the application, for use by the Edit menu
190 * commands. Potential types are Folder, File, User and Group.
192 private Object currentSelection;
195 * The authentication token of the current user.
197 private String token;
200 * The WebDAV password of the current user
202 private String webDAVPassword;
204 private PickupDragController dragController;
206 public void onModuleLoad() {
207 // Initialize the singleton before calling the constructors of the
208 // various widgets that might call GSS.get().
210 RootPanel.get().add(glassPanel, 0, 0);
211 parseUserCredentials();
212 dragController = new PickupDragController(RootPanel.get(), false) {
215 public void previewDragStart() throws VetoDragException {
216 super.previewDragStart();
217 if (context.selectedWidgets.isEmpty())
218 throw new VetoDragException();
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();
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();
242 container.add(new Label("Drag ME"));
249 dragController.setBehaviorDragProxy(true);
250 dragController.setBehaviorMultipleSelection(false);
251 topPanel = new TopPanel(GSS.images);
252 topPanel.setWidth("100%");
254 messagePanel.setWidth("100%");
255 messagePanel.setVisible(false);
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%");
265 fileList = new FileList(images);
267 searchResults = new SearchResults(images);
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);
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%");
279 inner.addSelectionHandler(new SelectionHandler<Integer>() {
282 public void onSelection(SelectionEvent<Integer> event) {
283 int tabIndex= event.getSelectedItem();
286 fileList.clearSelectedRows();
287 fileList.updateCurrentlyShowingStats();
290 groups.updateCurrentlyShowingStats();
293 searchResults.clearSelectedRows();
294 searchResults.updateCurrentlyShowingStats();
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%");
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();
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);
321 loading = new LoadingIndicator();
323 // Hook the window resize event, so that we can adjust the UI.
324 Window.addResizeHandler(this);
326 // Clear out the window's built-in margin, because we want to take
327 // advantage of the entire client area.
328 Window.setMargin("0px");
330 // Finally, add the outer panel to the RootPanel, so that it will be
332 RootPanel.get().add(outer);
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());
345 * Fetches the User object for the specified username.
347 * @param username the username of the user
349 private void fetchUser(final String username) {
350 String path = getApiPath() + username + "/";
351 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path){
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);
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());
371 GSS.get().displayError("System error fetching user data:"+t.getMessage());
375 DeferredCommand.addCommand(getUserCommand);
379 * Parse and store the user credentials to the appropriate fields.
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);
390 // Redundant, but silences warnings about possible auth NPE, below.
393 int sepIndex = auth.indexOf(conf.cookieSeparator());
396 token = auth.substring(sepIndex + 1, auth.length());
397 final String username = auth.substring(0, sepIndex);
398 if (username == null)
401 refreshWebDAVPassword();
403 DeferredCommand.addCommand(new Command() {
404 public void execute() {
411 * Redirect the user to the login page for authentication.
413 protected void authenticateUser() {
414 Configuration conf = (Configuration) GWT.create(Configuration.class);
415 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
419 * Redirect the user to the logout page.
422 Configuration conf = (Configuration) GWT.create(Configuration.class);
423 Window.Location.assign(conf.logoutUrl());
427 * Creates an HTML fragment that places an image & caption together, for use
430 * @param imageProto an image prototype for an image
431 * @param caption the group caption
432 * @return the header HTML fragment
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'> " +
438 caption + "</b></td></tr></table>";
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;
448 splitPanel.setHeight("" + newHeight);
452 public void onResize(ResizeEvent event) {
453 int width=event.getWidth();
454 onWindowResized(width);
457 public boolean isFileListShowing(){
458 int tab = inner.getTabBar().getSelectedTab();
459 if(tab == 0) return true;
463 public boolean isSearchResultsShowing(){
464 int tab = inner.getTabBar().getSelectedTab();
465 if(tab == 2) return true;
470 * Make the user list visible.
472 public void showUserList() {
477 * Make the file list visible.
479 public void showFileList() {
480 fileList.updateFileCache(false, true /*clear selection*/);
485 * Make the file list visible.
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();
501 getFileList().setFiles(files);
503 fileList.updateFileCache(update, true /*clear selection*/);
508 * Make the search results visible.
509 * @param query the search query string
511 public void showSearchResults(String query) {
512 searchResults.updateFileCache(query);
513 searchResults.updateCurrentlyShowingStats();
518 * Display the 'loading' indicator.
520 public void showLoadingIndicator() {
525 * Hide the 'loading' indicator.
527 public void hideLoadingIndicator() {
532 * A native JavaScript method to reach out to the browser's window and
533 * invoke its resizeTo() method.
535 * @param x the new width
536 * @param y the new height
538 public static native void resizeTo(int x, int y) /*-{
543 * A helper method that returns true if the user's list is currently visible
544 * and false if it is hidden.
546 * @return true if the user list is visible
548 public boolean isUserListVisible() {
549 return inner.getTabBar().getSelectedTab() == 1;
553 * Display an error message.
555 * @param msg the message to display
557 public void displayError(String msg) {
558 messagePanel.displayError(msg);
562 * Display a warning message.
564 * @param msg the message to display
566 public void displayWarning(String msg) {
567 messagePanel.displayWarning(msg);
571 * Display an informational message.
573 * @param msg the message to display
575 public void displayInformation(String msg) {
576 messagePanel.displayInformation(msg);
580 * Retrieve the folders.
582 * @return the folders
584 public Folders getFolders() {
589 * Retrieve the search.
598 * Retrieve the currentSelection.
600 * @return the currentSelection
602 public Object getCurrentSelection() {
603 return currentSelection;
607 * Modify the currentSelection.
609 * @param newCurrentSelection the currentSelection to set
611 public void setCurrentSelection(Object newCurrentSelection) {
612 currentSelection = newCurrentSelection;
616 * Retrieve the groups.
620 public Groups getGroups() {
625 * Retrieve the fileList.
627 * @return the fileList
629 public FileList getFileList() {
633 public SearchResults getSearchResults(){
634 return searchResults;
638 * Retrieve the topPanel.
640 * @return the topPanel
642 TopPanel getTopPanel() {
647 * Retrieve the clipboard.
649 * @return the clipboard
651 public Clipboard getClipboard() {
656 public StatusPanel getStatusPanel(){
662 * Retrieve the userDetailsPanel.
664 * @return the userDetailsPanel
666 public UserDetailsPanel getUserDetailsPanel() {
667 return userDetailsPanel;
671 * Retrieve the dragController.
673 * @return the dragController
675 public PickupDragController getDragController() {
676 return dragController;
679 public String getToken(){
683 public String getWebDAVPassword() {
684 return webDAVPassword;
687 public void removeGlassPanel(){
688 glassPanel.removeFromParent();
692 * Retrieve the currentUserResource.
694 * @return the currentUserResource
696 public UserResource getCurrentUserResource() {
697 return currentUserResource;
701 * Modify the currentUserResource.
703 * @param newUser the new currentUserResource
705 public void setCurrentUserResource(UserResource newUser) {
706 currentUserResource = newUser;
709 public static native void preventIESelection() /*-{
710 $doc.body.onselectstart = function () { return false; };
713 public static native void enableIESelection() /*-{
714 if ($doc.body.onselectstart != null)
715 $doc.body.onselectstart = null;
719 * @return the absolute path of the API root URL
721 public String getApiPath() {
722 Configuration conf = (Configuration) GWT.create(Configuration.class);
723 return GWT.getModuleBaseURL() + conf.apiPath();
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);