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.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;
62 * Entry point classes define <code>onModuleLoad()</code>.
64 public class GSS implements EntryPoint, WindowResizeListener {
67 * A constant that denotes the completion of an IncrementalCommand.
69 public static final boolean DONE = false;
71 public static final int VISIBLE_FILE_COUNT = 100;
74 * Instantiate an application-level image bundle. This object will provide
75 * programmatic access to all the images needed by widgets.
77 private static Images images = (Images) GWT.create(Images.class);
78 private GlassPanel glassPanel = new GlassPanel();
81 * An aggregate image bundle that pulls together all the images for this
82 * application into a single bundle.
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 {
86 @Resource("gr/ebs/gss/resources/document.png")
87 AbstractImagePrototype folders();
89 @Resource("gr/ebs/gss/resources/edit_group_22.png")
90 AbstractImagePrototype groups();
92 @Resource("gr/ebs/gss/resources/search.png")
93 AbstractImagePrototype search();
97 * The single GSS instance.
99 private static GSS singleton;
102 * Gets the singleton GSS instance.
104 * @return the GSS object
106 public static GSS get() {
107 if (GSS.singleton == null)
108 GSS.singleton = new GSS();
109 return GSS.singleton;
113 * The Application Clipboard implementation;
115 private Clipboard clipboard = new Clipboard();
117 private UserResource currentUserResource;
120 * The top panel that contains the menu bar.
122 private TopPanel topPanel;
125 * The panel that contains the various system messages.
127 private MessagePanel messagePanel = new MessagePanel(GSS.images);
130 * The bottom panel that contains the status bar.
132 private StatusPanel statusPanel = new StatusPanel(GSS.images);
135 * The top right panel that displays the logged in user details
137 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
140 * The file list widget.
142 private FileList fileList;
145 * The group list widget.
147 private Groups groups = new Groups(images);
150 * The search result widget.
152 private SearchResults searchResults;
155 * A widget that displays a message indicating that communication with the
156 * server is underway.
158 private LoadingIndicator loading;
161 * The tab panel that occupies the right side of the screen.
163 private TabPanel inner = new TabPanel();
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 DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
219 //prevent drag and drop for trashed files and for unselected tree items
220 if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
221 throw new VetoDragException();
222 else if(toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
223 throw new VetoDragException();
224 else if(toDrop.getItem() != null && !toDrop.getItem().isDraggable())
225 throw new VetoDragException();
231 protected Widget newDragProxy(DragContext aContext) {
232 AbsolutePanel container = new AbsolutePanel();
233 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
234 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
235 Widget widget = (Widget) iterator.next();
236 DnDFocusPanel book = (DnDFocusPanel) widget;
237 HTML html = book.cloneHTML();
239 container.add(new Label("Drag ME"));
246 dragController.setBehaviorDragProxy(true);
247 dragController.setBehaviorMultipleSelection(false);
248 topPanel = new TopPanel(GSS.images);
249 topPanel.setWidth("100%");
251 messagePanel.setWidth("100%");
252 messagePanel.setVisible(false);
254 search = new Search(images);
255 searchStatus.add(search, DockPanel.WEST);
256 searchStatus.add(userDetailsPanel, DockPanel.EAST);
257 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
258 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
259 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
260 searchStatus.setWidth("100%");
262 fileList = new FileList(images);
264 searchResults = new SearchResults(images);
266 // Inner contains the various lists.
267 inner.getTabBar().setStyleName("gss-TabBar");
268 inner.setStyleName("gss-TabPanel");
269 inner.add(fileList, createHeaderHTML(images.folders(), "Files"), true);
271 inner.add(groups, createHeaderHTML(images.groups(), "Groups"), true);
272 inner.add(searchResults, createHeaderHTML(images.search(), "Search Results"), true);
273 inner.setWidth("100%");
276 inner.addTabListener(new TabListener() {
277 public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
280 fileList.updateCurrentlyShowingStats();
283 groups.updateCurrentlyShowingStats();
286 searchResults.updateCurrentlyShowingStats();
291 public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
296 // Add the left and right panels to the split panel.
297 splitPanel.setLeftWidget(folders);
298 splitPanel.setRightWidget(inner);
299 splitPanel.setSplitPosition("25%");
300 splitPanel.setSize("100%", "100%");
302 // Create a dock panel that will contain the menu bar at the top,
303 // the shortcuts to the left, the status bar at the bottom and the
304 // right panel taking the rest.
305 VerticalPanel outer = new VerticalPanel();
307 outer.add(searchStatus);
308 outer.add(messagePanel);
309 outer.add(splitPanel);
310 outer.add(statusPanel);
311 outer.setWidth("100%");
312 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
316 loading = new LoadingIndicator();
318 // Hook the window resize event, so that we can adjust the UI.
319 Window.addWindowResizeListener(this);
321 // Clear out the window's built-in margin, because we want to take
322 // advantage of the entire client area.
323 Window.setMargin("0px");
325 // Finally, add the outer panel to the RootPanel, so that it will be
327 RootPanel.get().add(outer);
329 // Call the window resized handler to get the initial sizes setup. Doing
330 // this in a deferred command causes it to occur after all widgets'
331 // sizes have been computed by the browser.
332 DeferredCommand.addCommand(new Command() {
333 public void execute() {
334 onWindowResized(Window.getClientWidth(), Window.getClientHeight());
340 * Fetches the User object for the specified username.
342 * @param username the username of the user
344 private void fetchUser(final String username) {
345 String path = getApiPath() + username + "/";
346 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path){
349 public void onComplete() {
350 currentUserResource = getResult();
351 final String announcement = currentUserResource.getAnnouncement();
352 if (announcement != null)
353 DeferredCommand.addCommand(new Command() {
354 public void execute() {
355 displayInformation(announcement);
361 public void onError(Throwable t) {
362 GWT.log("Fetching user error", t);
363 if(t instanceof RestException)
364 GSS.get().displayError("No user found:"+((RestException)t).getHttpStatusText());
366 GSS.get().displayError("System error fetching user data:"+t.getMessage());
370 DeferredCommand.addCommand(getUserCommand);
374 * Parse and store the user credentials to the appropriate fields.
376 private void parseUserCredentials() {
377 Configuration conf = (Configuration) GWT.create(Configuration.class);
378 String cookie = conf.authCookie();
379 String auth = Cookies.getCookie(cookie);
380 String domain = Window.Location.getHostName();
381 String path = Window.Location.getPath();
382 Cookies.setCookie(cookie, "", null, domain, path, false);
385 // Redundant, but silences warnings about possible auth NPE, below.
388 int sepIndex = auth.indexOf(conf.cookieSeparator());
391 token = auth.substring(sepIndex + 1, auth.length());
392 final String username = auth.substring(0, sepIndex);
393 if (username == null)
396 refreshWebDAVPassword();
398 DeferredCommand.addCommand(new Command() {
399 public void execute() {
406 * Redirect the user to the login page for authentication.
408 protected void authenticateUser() {
409 Configuration conf = (Configuration) GWT.create(Configuration.class);
410 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
414 * Redirect the user to the logout page.
417 Configuration conf = (Configuration) GWT.create(Configuration.class);
418 Window.Location.assign(conf.logoutUrl());
422 * Creates an HTML fragment that places an image & caption together, for use
425 * @param imageProto an image prototype for an image
426 * @param caption the group caption
427 * @return the header HTML fragment
429 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
430 String captionHTML = "<table class='caption' cellpadding='0' " +
431 "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() +
432 "</td><td class='rcaption'><b style='white-space:nowrap'> " +
433 caption + "</b></td></tr></table>";
440 * @see com.google.gwt.user.client.WindowResizeListener#onWindowResized(int,int)
442 public void onWindowResized(int width, int height) {
443 // Adjust the split panel to take up the available room in the window.
444 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
447 splitPanel.setHeight("" + newHeight);
450 public boolean isFileListShowing(){
451 int tab = inner.getTabBar().getSelectedTab();
452 if(tab == 0) return true;
456 public boolean isSearchResultsShowing(){
457 int tab = inner.getTabBar().getSelectedTab();
458 if(tab == 2) return true;
463 * Make the user list visible.
465 public void showUserList() {
470 * Make the file list visible.
472 public void showFileList() {
473 fileList.updateFileCache(false, true /*clear selection*/);
478 * Make the file list visible.
481 public void showFileList(boolean update) {
482 TreeItem currentFolder = getFolders().getCurrent();
483 if (currentFolder != null) {
484 List<FileResource> files = null;
485 Object cachedObject = currentFolder.getUserObject();
486 if (cachedObject instanceof FolderResource) {
487 FolderResource folder = (FolderResource) cachedObject;
488 files = folder.getFiles();
489 } else if (cachedObject instanceof TrashResource) {
490 TrashResource folder = (TrashResource) cachedObject;
491 files = folder.getFiles();
494 getFileList().setFiles(files);
496 fileList.updateFileCache(update, true /*clear selection*/);
501 * Make the search results visible.
502 * @param query the search query string
504 public void showSearchResults(String query) {
505 searchResults.updateFileCache(query);
506 searchResults.updateCurrentlyShowingStats();
511 * Display the 'loading' indicator.
513 public void showLoadingIndicator() {
518 * Hide the 'loading' indicator.
520 public void hideLoadingIndicator() {
525 * A native JavaScript method to reach out to the browser's window and
526 * invoke its resizeTo() method.
528 * @param x the new width
529 * @param y the new height
531 public static native void resizeTo(int x, int y) /*-{
536 * A helper method that returns true if the user's list is currently visible
537 * and false if it is hidden.
539 * @return true if the user list is visible
541 public boolean isUserListVisible() {
542 return inner.getTabBar().getSelectedTab() == 1;
546 * Display an error message.
548 * @param msg the message to display
550 public void displayError(String msg) {
551 messagePanel.displayError(msg);
555 * Display a warning message.
557 * @param msg the message to display
559 public void displayWarning(String msg) {
560 messagePanel.displayWarning(msg);
564 * Display an informational message.
566 * @param msg the message to display
568 public void displayInformation(String msg) {
569 messagePanel.displayInformation(msg);
573 * Retrieve the folders.
575 * @return the folders
577 public Folders getFolders() {
582 * Retrieve the search.
591 * Retrieve the currentSelection.
593 * @return the currentSelection
595 public Object getCurrentSelection() {
596 return currentSelection;
600 * Modify the currentSelection.
602 * @param newCurrentSelection the currentSelection to set
604 public void setCurrentSelection(Object newCurrentSelection) {
605 currentSelection = newCurrentSelection;
609 * Retrieve the groups.
613 public Groups getGroups() {
618 * Retrieve the fileList.
620 * @return the fileList
622 public FileList getFileList() {
626 public SearchResults getSearchResults(){
627 return searchResults;
631 * Retrieve the topPanel.
633 * @return the topPanel
635 TopPanel getTopPanel() {
640 * Retrieve the clipboard.
642 * @return the clipboard
644 public Clipboard getClipboard() {
649 public StatusPanel getStatusPanel(){
655 * Retrieve the userDetailsPanel.
657 * @return the userDetailsPanel
659 public UserDetailsPanel getUserDetailsPanel() {
660 return userDetailsPanel;
664 * Retrieve the dragController.
666 * @return the dragController
668 public PickupDragController getDragController() {
669 return dragController;
672 public String getToken(){
676 public String getWebDAVPassword() {
677 return webDAVPassword;
680 public void removeGlassPanel(){
681 glassPanel.removeFromParent();
685 * Retrieve the currentUserResource.
687 * @return the currentUserResource
689 public UserResource getCurrentUserResource() {
690 return currentUserResource;
694 * Modify the currentUserResource.
696 * @param newUser the new currentUserResource
698 public void setCurrentUserResource(UserResource newUser) {
699 currentUserResource = newUser;
702 public static native void preventIESelection() /*-{
703 $doc.body.onselectstart = function () { return false; };
706 public static native void enableIESelection() /*-{
707 if ($doc.body.onselectstart != null)
708 $doc.body.onselectstart = null;
712 * @return the absolute path of the API root URL
714 public String getApiPath() {
715 Configuration conf = (Configuration) GWT.create(Configuration.class);
716 return GWT.getModuleBaseURL() + conf.apiPath();
719 public void refreshWebDAVPassword() {
720 Configuration conf = (Configuration) GWT.create(Configuration.class);
721 String domain = Window.Location.getHostName();
722 String path = Window.Location.getPath();
723 String cookie = conf.webdavCookie();
724 webDAVPassword = Cookies.getCookie(cookie);
725 Cookies.setCookie(cookie, "", null, domain, path, false);