2 * Copyright 2007, 2008, 2009, 2010 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.dnd.DnDSimpleFocusPanel;
24 import gr.ebs.gss.client.rest.GetCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.TrashResource;
29 import gr.ebs.gss.client.rest.resource.UserResource;
31 import java.util.Iterator;
32 import java.util.List;
34 import com.allen_sauer.gwt.dnd.client.DragContext;
35 import com.allen_sauer.gwt.dnd.client.PickupDragController;
36 import com.allen_sauer.gwt.dnd.client.VetoDragException;
37 import com.google.gwt.core.client.EntryPoint;
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.event.logical.shared.ResizeEvent;
40 import com.google.gwt.event.logical.shared.ResizeHandler;
41 import com.google.gwt.event.logical.shared.SelectionEvent;
42 import com.google.gwt.event.logical.shared.SelectionHandler;
43 import com.google.gwt.resources.client.ClientBundle;
44 import com.google.gwt.resources.client.ImageResource;
45 import com.google.gwt.user.client.Command;
46 import com.google.gwt.user.client.Cookies;
47 import com.google.gwt.user.client.DOM;
48 import com.google.gwt.user.client.DeferredCommand;
49 import com.google.gwt.user.client.History;
50 import com.google.gwt.user.client.Window;
51 import com.google.gwt.user.client.ui.AbsolutePanel;
52 import com.google.gwt.user.client.ui.AbstractImagePrototype;
53 import com.google.gwt.user.client.ui.DecoratedTabPanel;
54 import com.google.gwt.user.client.ui.DockPanel;
55 import com.google.gwt.user.client.ui.HTML;
56 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57 import com.google.gwt.user.client.ui.HasVerticalAlignment;
58 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
59 import com.google.gwt.user.client.ui.Label;
60 import com.google.gwt.user.client.ui.RootPanel;
61 import com.google.gwt.user.client.ui.TabPanel;
62 import com.google.gwt.user.client.ui.TreeItem;
63 import com.google.gwt.user.client.ui.VerticalPanel;
64 import com.google.gwt.user.client.ui.Widget;
67 * Entry point classes define <code>onModuleLoad()</code>.
69 public class GSS implements EntryPoint, ResizeHandler {
72 * A constant that denotes the completion of an IncrementalCommand.
74 public static final boolean DONE = false;
76 public static final int VISIBLE_FILE_COUNT = 100;
79 * Instantiate an application-level image bundle. This object will provide
80 * programmatic access to all the images needed by widgets.
82 private static Images images = (Images) GWT.create(Images.class);
84 private GlassPanel glassPanel = new GlassPanel();
87 * An aggregate image bundle that pulls together all the images for this
88 * application into a single bundle.
90 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 {
92 @Source("gr/ebs/gss/resources/document.png")
93 ImageResource folders();
95 @Source("gr/ebs/gss/resources/edit_group_22.png")
96 ImageResource groups();
98 @Source("gr/ebs/gss/resources/search.png")
99 ImageResource search();
103 * The single GSS instance.
105 private static GSS singleton;
108 * Gets the singleton GSS instance.
110 * @return the GSS object
112 public static GSS get() {
113 if (GSS.singleton == null)
114 GSS.singleton = new GSS();
115 return GSS.singleton;
119 * The Application Clipboard implementation;
121 private Clipboard clipboard = new Clipboard();
123 private UserResource currentUserResource;
126 * The top panel that contains the menu bar.
128 private TopPanel topPanel;
131 * The panel that contains the various system messages.
133 private MessagePanel messagePanel = new MessagePanel(GSS.images);
136 * The bottom panel that contains the status bar.
138 private StatusPanel statusPanel = new StatusPanel(GSS.images);
141 * The top right panel that displays the logged in user details
143 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
146 * The file list widget.
148 private FileList fileList;
151 * The group list widget.
153 private Groups groups = new Groups(images);
156 * The search result widget.
158 private SearchResults searchResults;
161 * The tab panel that occupies the right side of the screen.
163 private TabPanel inner = new DecoratedTabPanel();
166 * The split panel that will contain the left and right panels.
168 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
171 * The horizontal panel that will contain the search and status panels.
173 private DockPanel searchStatus = new DockPanel();
178 private Search search;
181 * The widget that displays the tree of folders.
183 private Folders folders = new Folders(images);
186 * The currently selected item in the application, for use by the Edit menu
187 * commands. Potential types are Folder, File, User and Group.
189 private Object currentSelection;
192 * The authentication token of the current user.
194 private String token;
197 * The WebDAV password of the current user
199 private String webDAVPassword;
201 private PickupDragController dragController;
203 public void onModuleLoad() {
204 // Initialize the singleton before calling the constructors of the
205 // various widgets that might call GSS.get().
207 RootPanel.get().add(glassPanel, 0, 0);
208 parseUserCredentials();
209 dragController = new PickupDragController(RootPanel.get(), false) {
212 public void previewDragStart() throws VetoDragException {
213 super.previewDragStart();
214 if (context.selectedWidgets.isEmpty())
215 throw new VetoDragException();
217 if (context.draggable != null)
218 if (context.draggable instanceof DnDFocusPanel) {
219 DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
220 // prevent drag and drop for trashed files and for
221 // unselected tree items
222 if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
223 throw new VetoDragException();
224 else if (toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
225 throw new VetoDragException();
226 else if (toDrop.getItem() != null && !toDrop.getItem().isDraggable())
227 throw new VetoDragException();
229 } else if (context.draggable instanceof DnDSimpleFocusPanel) {
230 DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
231 // prevent drag and drop for trashed files and for
232 // unselected tree items
233 if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
234 throw new VetoDragException();
239 protected Widget newDragProxy(DragContext aContext) {
240 AbsolutePanel container = new AbsolutePanel();
241 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
242 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
244 Widget widget = (Widget) iterator.next();
245 if (widget instanceof DnDFocusPanel) {
246 DnDFocusPanel book = (DnDFocusPanel) widget;
247 html = book.cloneHTML();
248 } else if (widget instanceof DnDSimpleFocusPanel) {
249 DnDSimpleFocusPanel book = (DnDSimpleFocusPanel) widget;
250 html = book.cloneHTML();
253 container.add(new Label("Drag ME"));
260 dragController.setBehaviorDragProxy(true);
261 dragController.setBehaviorMultipleSelection(false);
262 topPanel = new TopPanel(GSS.images);
263 topPanel.setWidth("100%");
265 messagePanel.setWidth("100%");
266 messagePanel.setVisible(false);
268 search = new Search(images);
269 searchStatus.add(search, DockPanel.WEST);
270 searchStatus.add(userDetailsPanel, DockPanel.EAST);
271 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
272 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
273 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
274 searchStatus.setWidth("100%");
276 fileList = new FileList(images);
278 searchResults = new SearchResults(images);
280 // Inner contains the various lists.
281 inner.setAnimationEnabled(true);
282 inner.getTabBar().addStyleName("gss-MainTabBar");
283 inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
284 inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
286 inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
287 inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
288 inner.setWidth("100%");
291 inner.addSelectionHandler(new SelectionHandler<Integer>() {
294 public void onSelection(SelectionEvent<Integer> event) {
295 int tabIndex = event.getSelectedItem();
298 fileList.clearSelectedRows();
299 fileList.updateCurrentlyShowingStats();
300 History.newItem("Files");
303 groups.updateCurrentlyShowingStats();
304 History.newItem("Groups");
307 searchResults.clearSelectedRows();
308 searchResults.updateCurrentlyShowingStats();
309 History.newItem("Search");
315 // Add the left and right panels to the split panel.
316 splitPanel.setLeftWidget(folders);
317 splitPanel.setRightWidget(inner);
318 splitPanel.setSplitPosition("25%");
319 splitPanel.setSize("100%", "100%");
320 splitPanel.addStyleName("gss-splitPanel");
322 // Create a dock panel that will contain the menu bar at the top,
323 // the shortcuts to the left, the status bar at the bottom and the
324 // right panel taking the rest.
325 VerticalPanel outer = new VerticalPanel();
327 outer.add(searchStatus);
328 outer.add(messagePanel);
329 outer.add(splitPanel);
330 outer.add(statusPanel);
331 outer.setWidth("100%");
332 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
336 // Hook the window resize event, so that we can adjust the UI.
337 Window.addResizeHandler(this);
338 // Clear out the window's built-in margin, because we want to take
339 // advantage of the entire client area.
340 Window.setMargin("0px");
341 // Finally, add the outer panel to the RootPanel, so that it will be
343 RootPanel.get().add(outer);
344 // Call the window resized handler to get the initial sizes setup. Doing
345 // this in a deferred command causes it to occur after all widgets'
346 // sizes have been computed by the browser.
347 DeferredCommand.addCommand(new Command() {
349 public void execute() {
350 onWindowResized(Window.getClientHeight());
356 * Fetches the User object for the specified username.
358 * @param username the username of the user
360 private void fetchUser(final String username) {
361 String path = getApiPath() + username + "/";
362 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path, null) {
365 public void onComplete() {
366 currentUserResource = getResult();
367 final String announcement = currentUserResource.getAnnouncement();
368 if (announcement != null)
369 DeferredCommand.addCommand(new Command() {
371 public void execute() {
372 displayInformation(announcement);
378 public void onError(Throwable t) {
379 GWT.log("Fetching user error", t);
380 if (t instanceof RestException)
381 GSS.get().displayError("No user found:" + ((RestException) t).getHttpStatusText());
383 GSS.get().displayError("System error fetching user data:" + t.getMessage());
387 DeferredCommand.addCommand(getUserCommand);
391 * Parse and store the user credentials to the appropriate fields.
393 private void parseUserCredentials() {
394 Configuration conf = (Configuration) GWT.create(Configuration.class);
395 String cookie = conf.authCookie();
396 String auth = Cookies.getCookie(cookie);
399 // Redundant, but silences warnings about possible auth NPE, below.
402 int sepIndex = auth.indexOf(conf.cookieSeparator());
405 token = auth.substring(sepIndex + 1, auth.length());
406 final String username = auth.substring(0, sepIndex);
407 if (username == null)
410 refreshWebDAVPassword();
412 DeferredCommand.addCommand(new Command() {
414 public void execute() {
421 * Redirect the user to the login page for authentication.
423 protected void authenticateUser() {
424 Configuration conf = (Configuration) GWT.create(Configuration.class);
425 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
429 * Clear the cookie and redirect the user to the logout page.
432 Configuration conf = (Configuration) GWT.create(Configuration.class);
433 String cookie = conf.authCookie();
434 String domain = Window.Location.getHostName();
435 String path = Window.Location.getPath();
436 Cookies.setCookie(cookie, "", null, domain, path, false);
437 Window.Location.assign(conf.logoutUrl());
441 * Creates an HTML fragment that places an image & caption together, for use
444 * @param imageProto an image prototype for an image
445 * @param caption the group caption
446 * @return the header HTML fragment
448 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
449 String captionHTML = "<table class='caption' cellpadding='0' " + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() + "</td><td class='rcaption'><b style='white-space:nowrap'> " + caption + "</b></td></tr></table>";
453 private void onWindowResized(int height) {
454 // Adjust the split panel to take up the available room in the window.
455 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
458 splitPanel.setHeight("" + newHeight);
462 public void onResize(ResizeEvent event) {
463 int height = event.getHeight();
464 onWindowResized(height);
467 public boolean isFileListShowing() {
468 int tab = inner.getTabBar().getSelectedTab();
474 public boolean isSearchResultsShowing() {
475 int tab = inner.getTabBar().getSelectedTab();
482 * Make the user list visible.
484 public void showUserList() {
489 * Make the file list visible.
491 public void showFileList() {
492 fileList.updateFileCache(false, true /*clear selection*/);
497 * Make the file list visible.
501 public void showFileList(boolean update) {
502 TreeItem currentFolder = getFolders().getCurrent();
503 if (currentFolder != null) {
504 List<FileResource> files = null;
505 Object cachedObject = currentFolder.getUserObject();
506 if (cachedObject instanceof FolderResource) {
507 FolderResource folder = (FolderResource) cachedObject;
508 files = folder.getFiles();
509 } else if (cachedObject instanceof TrashResource) {
510 TrashResource folder = (TrashResource) cachedObject;
511 files = folder.getFiles();
514 getFileList().setFiles(files);
516 fileList.updateFileCache(update, true /*clear selection*/);
521 * Make the search results visible.
523 * @param query the search query string
525 public void showSearchResults(String query) {
526 searchResults.updateFileCache(query);
527 searchResults.updateCurrentlyShowingStats();
532 * Display the 'loading' indicator.
534 public void showLoadingIndicator() {
535 topPanel.getLoading().setVisible(true);
539 * Hide the 'loading' indicator.
541 public void hideLoadingIndicator() {
542 topPanel.getLoading().setVisible(false);
546 * A native JavaScript method to reach out to the browser's window and
547 * invoke its resizeTo() method.
549 * @param x the new width
550 * @param y the new height
552 public static native void resizeTo(int x, int y) /*-{
557 * A helper method that returns true if the user's list is currently visible
558 * and false if it is hidden.
560 * @return true if the user list is visible
562 public boolean isUserListVisible() {
563 return inner.getTabBar().getSelectedTab() == 1;
567 * Display an error message.
569 * @param msg the message to display
571 public void displayError(String msg) {
572 messagePanel.displayError(msg);
576 * Display a warning message.
578 * @param msg the message to display
580 public void displayWarning(String msg) {
581 messagePanel.displayWarning(msg);
585 * Display an informational message.
587 * @param msg the message to display
589 public void displayInformation(String msg) {
590 messagePanel.displayInformation(msg);
594 * Retrieve the folders.
596 * @return the folders
598 public Folders getFolders() {
603 * Retrieve the search.
612 * Retrieve the currentSelection.
614 * @return the currentSelection
616 public Object getCurrentSelection() {
617 return currentSelection;
621 * Modify the currentSelection.
623 * @param newCurrentSelection the currentSelection to set
625 public void setCurrentSelection(Object newCurrentSelection) {
626 currentSelection = newCurrentSelection;
630 * Retrieve the groups.
634 public Groups getGroups() {
639 * Retrieve the fileList.
641 * @return the fileList
643 public FileList getFileList() {
647 public SearchResults getSearchResults() {
648 return searchResults;
652 * Retrieve the topPanel.
654 * @return the topPanel
656 TopPanel getTopPanel() {
661 * Retrieve the clipboard.
663 * @return the clipboard
665 public Clipboard getClipboard() {
669 public StatusPanel getStatusPanel() {
674 * Retrieve the userDetailsPanel.
676 * @return the userDetailsPanel
678 public UserDetailsPanel getUserDetailsPanel() {
679 return userDetailsPanel;
683 * Retrieve the dragController.
685 * @return the dragController
687 public PickupDragController getDragController() {
688 return dragController;
691 public String getToken() {
695 public String getWebDAVPassword() {
696 return webDAVPassword;
699 public void removeGlassPanel() {
700 glassPanel.removeFromParent();
704 * Retrieve the currentUserResource.
706 * @return the currentUserResource
708 public UserResource getCurrentUserResource() {
709 return currentUserResource;
713 * Modify the currentUserResource.
715 * @param newUser the new currentUserResource
717 public void setCurrentUserResource(UserResource newUser) {
718 currentUserResource = newUser;
721 public static native void preventIESelection() /*-{
722 $doc.body.onselectstart = function () { return false; };
725 public static native void enableIESelection() /*-{
726 if ($doc.body.onselectstart != null)
727 $doc.body.onselectstart = null;
731 * @return the absolute path of the API root URL
733 public String getApiPath() {
734 Configuration conf = (Configuration) GWT.create(Configuration.class);
735 return GWT.getModuleBaseURL() + conf.apiPath();
738 public void refreshWebDAVPassword() {
739 Configuration conf = (Configuration) GWT.create(Configuration.class);
740 String domain = Window.Location.getHostName();
741 String path = Window.Location.getPath();
742 String cookie = conf.webdavCookie();
743 webDAVPassword = Cookies.getCookie(cookie);
744 Cookies.setCookie(cookie, "", null, domain, path, false);