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.DecoratedTabPanel;
52 import com.google.gwt.user.client.ui.DockPanel;
53 import com.google.gwt.user.client.ui.HTML;
54 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
55 import com.google.gwt.user.client.ui.HasVerticalAlignment;
56 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
57 import com.google.gwt.user.client.ui.Label;
58 import com.google.gwt.user.client.ui.RootPanel;
59 import com.google.gwt.user.client.ui.TabPanel;
60 import com.google.gwt.user.client.ui.TreeItem;
61 import com.google.gwt.user.client.ui.VerticalPanel;
62 import com.google.gwt.user.client.ui.Widget;
66 * Entry point classes define <code>onModuleLoad()</code>.
68 public class GSS implements EntryPoint, ResizeHandler {
71 * A constant that denotes the completion of an IncrementalCommand.
73 public static final boolean DONE = false;
75 public static final int VISIBLE_FILE_COUNT = 100;
78 * Instantiate an application-level image bundle. This object will provide
79 * programmatic access to all the images needed by widgets.
81 private static Images images = (Images) GWT.create(Images.class);
82 private GlassPanel glassPanel = new GlassPanel();
85 * An aggregate image bundle that pulls together all the images for this
86 * application into a single bundle.
88 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 {
90 @Source("gr/ebs/gss/resources/document.png")
91 ImageResource folders();
93 @Source("gr/ebs/gss/resources/edit_group_22.png")
94 ImageResource groups();
96 @Source("gr/ebs/gss/resources/search.png")
97 ImageResource search();
101 * The single GSS instance.
103 private static GSS singleton;
106 * Gets the singleton GSS instance.
108 * @return the GSS object
110 public static GSS get() {
111 if (GSS.singleton == null)
112 GSS.singleton = new GSS();
113 return GSS.singleton;
117 * The Application Clipboard implementation;
119 private Clipboard clipboard = new Clipboard();
121 private UserResource currentUserResource;
124 * The top panel that contains the menu bar.
126 private TopPanel topPanel;
129 * The panel that contains the various system messages.
131 private MessagePanel messagePanel = new MessagePanel(GSS.images);
134 * The bottom panel that contains the status bar.
136 private StatusPanel statusPanel = new StatusPanel(GSS.images);
139 * The top right panel that displays the logged in user details
141 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
144 * The file list widget.
146 private FileList fileList;
149 * The group list widget.
151 private Groups groups = new Groups(images);
154 * The search result widget.
156 private SearchResults searchResults;
159 * A widget that displays a message indicating that communication with the
160 * server is underway.
162 private LoadingIndicator loading;
165 * The tab panel that occupies the right side of the screen.
167 private TabPanel inner = new DecoratedTabPanel();
170 * The split panel that will contain the left and right panels.
172 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
175 * The horizontal panel that will contain the search and status panels.
177 private DockPanel searchStatus = new DockPanel();
182 private Search search;
185 * The widget that displays the tree of folders.
187 private Folders folders = new Folders(images);
190 * The currently selected item in the application, for use by the Edit menu
191 * commands. Potential types are Folder, File, User and Group.
193 private Object currentSelection;
196 * The authentication token of the current user.
198 private String token;
201 * The WebDAV password of the current user
203 private String webDAVPassword;
205 private PickupDragController dragController;
207 public void onModuleLoad() {
208 // Initialize the singleton before calling the constructors of the
209 // various widgets that might call GSS.get().
211 RootPanel.get().add(glassPanel, 0, 0);
212 parseUserCredentials();
213 dragController = new PickupDragController(RootPanel.get(), false) {
216 public void previewDragStart() throws VetoDragException {
217 super.previewDragStart();
218 if (context.selectedWidgets.isEmpty())
219 throw new VetoDragException();
221 if(context.draggable != null){
222 DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
223 //prevent drag and drop for trashed files and for unselected tree items
224 if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
225 throw new VetoDragException();
226 else if(toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
227 throw new VetoDragException();
228 else if(toDrop.getItem() != null && !toDrop.getItem().isDraggable())
229 throw new VetoDragException();
235 protected Widget newDragProxy(DragContext aContext) {
236 AbsolutePanel container = new AbsolutePanel();
237 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
238 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
239 Widget widget = (Widget) iterator.next();
240 DnDFocusPanel book = (DnDFocusPanel) widget;
241 HTML html = book.cloneHTML();
243 container.add(new Label("Drag ME"));
250 dragController.setBehaviorDragProxy(true);
251 dragController.setBehaviorMultipleSelection(false);
252 topPanel = new TopPanel(GSS.images);
253 topPanel.setWidth("100%");
255 messagePanel.setWidth("100%");
256 messagePanel.setVisible(false);
258 search = new Search(images);
259 searchStatus.add(search, DockPanel.WEST);
260 searchStatus.add(userDetailsPanel, DockPanel.EAST);
261 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
262 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
263 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
264 searchStatus.setWidth("100%");
266 fileList = new FileList(images);
268 searchResults = new SearchResults(images);
270 // Inner contains the various lists.
271 inner.setAnimationEnabled(true);
272 inner.getTabBar().addStyleName("gss-MainTabBar");
273 inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
274 inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
276 inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
277 inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
278 inner.setWidth("100%");
281 inner.addSelectionHandler(new SelectionHandler<Integer>() {
284 public void onSelection(SelectionEvent<Integer> event) {
285 int tabIndex= event.getSelectedItem();
288 fileList.clearSelectedRows();
289 fileList.updateCurrentlyShowingStats();
292 groups.updateCurrentlyShowingStats();
295 searchResults.clearSelectedRows();
296 searchResults.updateCurrentlyShowingStats();
302 // Add the left and right panels to the split panel.
303 splitPanel.setLeftWidget(folders);
304 splitPanel.setRightWidget(inner);
305 splitPanel.setSplitPosition("25%");
306 splitPanel.setSize("100%", "100%");
307 splitPanel.addStyleName("gss-splitPanel");
309 // Create a dock panel that will contain the menu bar at the top,
310 // the shortcuts to the left, the status bar at the bottom and the
311 // right panel taking the rest.
312 VerticalPanel outer = new VerticalPanel();
314 outer.add(searchStatus);
315 outer.add(messagePanel);
316 outer.add(splitPanel);
317 outer.add(statusPanel);
318 outer.setWidth("100%");
319 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
323 loading = new LoadingIndicator();
324 // Hook the window resize event, so that we can adjust the UI.
325 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");
329 // Finally, add the outer panel to the RootPanel, so that it will be
331 RootPanel.get().add(outer);
332 // Call the window resized handler to get the initial sizes setup. Doing
333 // this in a deferred command causes it to occur after all widgets'
334 // sizes have been computed by the browser.
335 DeferredCommand.addCommand(new Command() {
336 public void execute() {
337 onWindowResized(Window.getClientHeight());
343 * Fetches the User object for the specified username.
345 * @param username the username of the user
347 private void fetchUser(final String username) {
348 String path = getApiPath() + username + "/";
349 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path){
352 public void onComplete() {
353 currentUserResource = getResult();
354 final String announcement = currentUserResource.getAnnouncement();
355 if (announcement != null)
356 DeferredCommand.addCommand(new Command() {
357 public void execute() {
358 displayInformation(announcement);
364 public void onError(Throwable t) {
365 GWT.log("Fetching user error", t);
366 if(t instanceof RestException)
367 GSS.get().displayError("No user found:"+((RestException)t).getHttpStatusText());
369 GSS.get().displayError("System error fetching user data:"+t.getMessage());
373 DeferredCommand.addCommand(getUserCommand);
377 * Parse and store the user credentials to the appropriate fields.
379 private void parseUserCredentials() {
380 Configuration conf = (Configuration) GWT.create(Configuration.class);
381 String cookie = conf.authCookie();
382 String auth = Cookies.getCookie(cookie);
383 String domain = Window.Location.getHostName();
384 String path = Window.Location.getPath();
385 Cookies.setCookie(cookie, "", null, domain, path, false);
388 // Redundant, but silences warnings about possible auth NPE, below.
391 int sepIndex = auth.indexOf(conf.cookieSeparator());
394 token = auth.substring(sepIndex + 1, auth.length());
395 final String username = auth.substring(0, sepIndex);
396 if (username == null)
399 refreshWebDAVPassword();
401 DeferredCommand.addCommand(new Command() {
402 public void execute() {
409 * Redirect the user to the login page for authentication.
411 protected void authenticateUser() {
412 Configuration conf = (Configuration) GWT.create(Configuration.class);
413 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
417 * Redirect the user to the logout page.
420 Configuration conf = (Configuration) GWT.create(Configuration.class);
421 Window.Location.assign(conf.logoutUrl());
425 * Creates an HTML fragment that places an image & caption together, for use
428 * @param imageProto an image prototype for an image
429 * @param caption the group caption
430 * @return the header HTML fragment
432 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
433 String captionHTML = "<table class='caption' cellpadding='0' " +
434 "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() +
435 "</td><td class='rcaption'><b style='white-space:nowrap'> " +
436 caption + "</b></td></tr></table>";
441 private void onWindowResized(int height) {
442 // Adjust the split panel to take up the available room in the window.
443 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
446 splitPanel.setHeight("" + newHeight);
450 public void onResize(ResizeEvent event) {
451 int height = event.getHeight();
452 onWindowResized(height);
455 public boolean isFileListShowing(){
456 int tab = inner.getTabBar().getSelectedTab();
457 if(tab == 0) return true;
461 public boolean isSearchResultsShowing(){
462 int tab = inner.getTabBar().getSelectedTab();
463 if(tab == 2) return true;
468 * Make the user list visible.
470 public void showUserList() {
475 * Make the file list visible.
477 public void showFileList() {
478 fileList.updateFileCache(false, true /*clear selection*/);
483 * Make the file list visible.
486 public void showFileList(boolean update) {
487 TreeItem currentFolder = getFolders().getCurrent();
488 if (currentFolder != null) {
489 List<FileResource> files = null;
490 Object cachedObject = currentFolder.getUserObject();
491 if (cachedObject instanceof FolderResource) {
492 FolderResource folder = (FolderResource) cachedObject;
493 files = folder.getFiles();
494 } else if (cachedObject instanceof TrashResource) {
495 TrashResource folder = (TrashResource) cachedObject;
496 files = folder.getFiles();
499 getFileList().setFiles(files);
501 fileList.updateFileCache(update, true /*clear selection*/);
506 * Make the search results visible.
507 * @param query the search query string
509 public void showSearchResults(String query) {
510 searchResults.updateFileCache(query);
511 searchResults.updateCurrentlyShowingStats();
516 * Display the 'loading' indicator.
518 public void showLoadingIndicator() {
523 * Hide the 'loading' indicator.
525 public void hideLoadingIndicator() {
530 * A native JavaScript method to reach out to the browser's window and
531 * invoke its resizeTo() method.
533 * @param x the new width
534 * @param y the new height
536 public static native void resizeTo(int x, int y) /*-{
541 * A helper method that returns true if the user's list is currently visible
542 * and false if it is hidden.
544 * @return true if the user list is visible
546 public boolean isUserListVisible() {
547 return inner.getTabBar().getSelectedTab() == 1;
551 * Display an error message.
553 * @param msg the message to display
555 public void displayError(String msg) {
556 messagePanel.displayError(msg);
560 * Display a warning message.
562 * @param msg the message to display
564 public void displayWarning(String msg) {
565 messagePanel.displayWarning(msg);
569 * Display an informational message.
571 * @param msg the message to display
573 public void displayInformation(String msg) {
574 messagePanel.displayInformation(msg);
578 * Retrieve the folders.
580 * @return the folders
582 public Folders getFolders() {
587 * Retrieve the search.
596 * Retrieve the currentSelection.
598 * @return the currentSelection
600 public Object getCurrentSelection() {
601 return currentSelection;
605 * Modify the currentSelection.
607 * @param newCurrentSelection the currentSelection to set
609 public void setCurrentSelection(Object newCurrentSelection) {
610 currentSelection = newCurrentSelection;
614 * Retrieve the groups.
618 public Groups getGroups() {
623 * Retrieve the fileList.
625 * @return the fileList
627 public FileList getFileList() {
631 public SearchResults getSearchResults(){
632 return searchResults;
636 * Retrieve the topPanel.
638 * @return the topPanel
640 TopPanel getTopPanel() {
645 * Retrieve the clipboard.
647 * @return the clipboard
649 public Clipboard getClipboard() {
654 public StatusPanel getStatusPanel(){
660 * Retrieve the userDetailsPanel.
662 * @return the userDetailsPanel
664 public UserDetailsPanel getUserDetailsPanel() {
665 return userDetailsPanel;
669 * Retrieve the dragController.
671 * @return the dragController
673 public PickupDragController getDragController() {
674 return dragController;
677 public String getToken(){
681 public String getWebDAVPassword() {
682 return webDAVPassword;
685 public void removeGlassPanel(){
686 glassPanel.removeFromParent();
690 * Retrieve the currentUserResource.
692 * @return the currentUserResource
694 public UserResource getCurrentUserResource() {
695 return currentUserResource;
699 * Modify the currentUserResource.
701 * @param newUser the new currentUserResource
703 public void setCurrentUserResource(UserResource newUser) {
704 currentUserResource = newUser;
707 public static native void preventIESelection() /*-{
708 $doc.body.onselectstart = function () { return false; };
711 public static native void enableIESelection() /*-{
712 if ($doc.body.onselectstart != null)
713 $doc.body.onselectstart = null;
717 * @return the absolute path of the API root URL
719 public String getApiPath() {
720 Configuration conf = (Configuration) GWT.create(Configuration.class);
721 return GWT.getModuleBaseURL() + conf.apiPath();
724 public void refreshWebDAVPassword() {
725 Configuration conf = (Configuration) GWT.create(Configuration.class);
726 String domain = Window.Location.getHostName();
727 String path = Window.Location.getPath();
728 String cookie = conf.webdavCookie();
729 webDAVPassword = Cookies.getCookie(cookie);
730 Cookies.setCookie(cookie, "", null, domain, path, false);