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.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
36 import com.allen_sauer.gwt.dnd.client.DragContext;
37 import com.allen_sauer.gwt.dnd.client.PickupDragController;
38 import com.allen_sauer.gwt.dnd.client.VetoDragException;
39 import com.google.gwt.core.client.EntryPoint;
40 import com.google.gwt.core.client.GWT;
41 import com.google.gwt.event.logical.shared.ResizeEvent;
42 import com.google.gwt.event.logical.shared.ResizeHandler;
43 import com.google.gwt.event.logical.shared.SelectionEvent;
44 import com.google.gwt.event.logical.shared.SelectionHandler;
45 import com.google.gwt.event.logical.shared.ValueChangeEvent;
46 import com.google.gwt.event.logical.shared.ValueChangeHandler;
47 import com.google.gwt.http.client.URL;
48 import com.google.gwt.resources.client.ClientBundle;
49 import com.google.gwt.resources.client.ImageResource;
50 import com.google.gwt.user.client.Command;
51 import com.google.gwt.user.client.Cookies;
52 import com.google.gwt.user.client.DOM;
53 import com.google.gwt.user.client.DeferredCommand;
54 import com.google.gwt.user.client.History;
55 import com.google.gwt.user.client.Window;
56 import com.google.gwt.user.client.ui.AbsolutePanel;
57 import com.google.gwt.user.client.ui.AbstractImagePrototype;
58 import com.google.gwt.user.client.ui.DecoratedTabPanel;
59 import com.google.gwt.user.client.ui.DockPanel;
60 import com.google.gwt.user.client.ui.HTML;
61 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
62 import com.google.gwt.user.client.ui.HasVerticalAlignment;
63 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
64 import com.google.gwt.user.client.ui.Label;
65 import com.google.gwt.user.client.ui.RootPanel;
66 import com.google.gwt.user.client.ui.TabPanel;
67 import com.google.gwt.user.client.ui.TreeItem;
68 import com.google.gwt.user.client.ui.VerticalPanel;
69 import com.google.gwt.user.client.ui.Widget;
72 * Entry point classes define <code>onModuleLoad()</code>.
74 public class GSS implements EntryPoint, ResizeHandler {
77 * A constant that denotes the completion of an IncrementalCommand.
79 public static final boolean DONE = false;
81 public static final int VISIBLE_FILE_COUNT = 100;
84 * Instantiate an application-level image bundle. This object will provide
85 * programmatic access to all the images needed by widgets.
87 private static Images images = (Images) GWT.create(Images.class);
89 private GlassPanel glassPanel = new GlassPanel();
92 * An aggregate image bundle that pulls together all the images for this
93 * application into a single bundle.
95 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 {
97 @Source("gr/ebs/gss/resources/document.png")
98 ImageResource folders();
100 @Source("gr/ebs/gss/resources/edit_group_22.png")
101 ImageResource groups();
103 @Source("gr/ebs/gss/resources/search.png")
104 ImageResource search();
108 * The single GSS instance.
110 private static GSS singleton;
113 * Gets the singleton GSS instance.
115 * @return the GSS object
117 public static GSS get() {
118 if (GSS.singleton == null)
119 GSS.singleton = new GSS();
120 return GSS.singleton;
124 * The Application Clipboard implementation;
126 private Clipboard clipboard = new Clipboard();
128 private UserResource currentUserResource;
131 * The top panel that contains the menu bar.
133 private TopPanel topPanel;
136 * The panel that contains the various system messages.
138 private MessagePanel messagePanel = new MessagePanel(GSS.images);
141 * The bottom panel that contains the status bar.
143 private StatusPanel statusPanel = new StatusPanel(GSS.images);
146 * The top right panel that displays the logged in user details
148 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
151 * The file list widget.
153 private FileList fileList;
156 * The group list widget.
158 private Groups groups = new Groups(images);
161 * The search result widget.
163 private SearchResults searchResults;
166 * The tab panel that occupies the right side of the screen.
168 private TabPanel inner = new DecoratedTabPanel();
171 * The split panel that will contain the left and right panels.
173 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
176 * The horizontal panel that will contain the search and status panels.
178 private DockPanel searchStatus = new DockPanel();
183 private Search search;
186 * The widget that displays the tree of folders.
188 private Folders folders = new Folders(images);
191 * The currently selected item in the application, for use by the Edit menu
192 * commands. Potential types are Folder, File, User and Group.
194 private Object currentSelection;
197 * The authentication token of the current user.
199 private String token;
202 * A map that stores user's selection in order browser's
203 * history functionality to be implemented.
205 private Map<String, Object> map = new HashMap<String, Object>();
208 * The WebDAV password of the current user
210 private String webDAVPassword;
212 private PickupDragController dragController;
214 public void onModuleLoad() {
215 // Initialize the singleton before calling the constructors of the
216 // various widgets that might call GSS.get().
218 RootPanel.get().add(glassPanel, 0, 0);
219 parseUserCredentials();
220 dragController = new PickupDragController(RootPanel.get(), false) {
223 public void previewDragStart() throws VetoDragException {
224 super.previewDragStart();
225 if (context.selectedWidgets.isEmpty())
226 throw new VetoDragException();
228 if (context.draggable != null)
229 if (context.draggable instanceof DnDFocusPanel) {
230 DnDFocusPanel toDrop = (DnDFocusPanel) 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();
235 else if (toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
236 throw new VetoDragException();
237 else if (toDrop.getItem() != null && !toDrop.getItem().isDraggable())
238 throw new VetoDragException();
240 } else if (context.draggable instanceof DnDSimpleFocusPanel) {
241 DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
242 // prevent drag and drop for trashed files and for
243 // unselected tree items
244 if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
245 throw new VetoDragException();
250 protected Widget newDragProxy(DragContext aContext) {
251 AbsolutePanel container = new AbsolutePanel();
252 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
253 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
255 Widget widget = (Widget) iterator.next();
256 if (widget instanceof DnDFocusPanel) {
257 DnDFocusPanel book = (DnDFocusPanel) widget;
258 html = book.cloneHTML();
259 } else if (widget instanceof DnDSimpleFocusPanel) {
260 DnDSimpleFocusPanel book = (DnDSimpleFocusPanel) widget;
261 html = book.cloneHTML();
264 container.add(new Label("Drag ME"));
271 dragController.setBehaviorDragProxy(true);
272 dragController.setBehaviorMultipleSelection(false);
273 topPanel = new TopPanel(GSS.images);
274 topPanel.setWidth("100%");
276 messagePanel.setWidth("100%");
277 messagePanel.setVisible(false);
279 search = new Search(images);
280 searchStatus.add(search, DockPanel.WEST);
281 searchStatus.add(userDetailsPanel, DockPanel.EAST);
282 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
283 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
284 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
285 searchStatus.setWidth("100%");
287 fileList = new FileList(images);
289 searchResults = new SearchResults(images);
291 // Inner contains the various lists.
292 inner.setAnimationEnabled(true);
293 inner.getTabBar().addStyleName("gss-MainTabBar");
294 inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
295 inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
297 inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
298 inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
299 inner.setWidth("100%");
302 inner.addSelectionHandler(new SelectionHandler<Integer>() {
305 public void onSelection(SelectionEvent<Integer> event) {
306 int tabIndex = event.getSelectedItem();
307 TreeItem treeItem = GSS.get().getFolders().getCurrent();
308 PopupTree tree = GSS.get().getFolders().getPopupTree();
311 fileList.clearSelectedRows();
312 fileList.updateCurrentlyShowingStats();
315 * When the Groups tab is selected a new
318 groups.updateCurrentlyShowingStats();
319 // Add a new pair key - object in the History map.
320 setHistory("Groups", treeItem);
321 // Add a new browser history entry.
322 History.newItem("Groups");
325 searchResults.clearSelectedRows();
326 searchResults.updateCurrentlyShowingStats();
327 setHistory("Search", treeItem);
328 History.newItem("Search");
333 // If the application starts with no history token, redirect to a new "Files" state
334 String initToken = History.getToken();
335 if(initToken.length() == 0)
336 History.newItem("Files");
338 // Add history listener
339 History.addValueChangeHandler(new ValueChangeHandler<String>() {
340 public void onValueChange(ValueChangeEvent<String> event) {
341 String historyToken = event.getValue();
342 TreeItem treeItem = GSS.get().getFolders().getCurrent();
344 if(historyToken.equals("Search"))
346 else if(historyToken.equals("Groups"))
348 else if(historyToken.equals("Files")){
349 setHistory("Files", treeItem);
350 History.newItem("Files");
354 PopupTree tree = GSS.get().getFolders().getPopupTree();
355 String newHistoryToken = URL.decode(historyToken);
356 System.out.println(map.keySet());
357 SelectionEvent.fire(tree, (TreeItem) getHistoryItem(newHistoryToken));
359 } catch (IndexOutOfBoundsException e) {
366 // Add the left and right panels to the split panel.
367 splitPanel.setLeftWidget(folders);
368 splitPanel.setRightWidget(inner);
369 splitPanel.setSplitPosition("25%");
370 splitPanel.setSize("100%", "100%");
371 splitPanel.addStyleName("gss-splitPanel");
373 // Create a dock panel that will contain the menu bar at the top,
374 // the shortcuts to the left, the status bar at the bottom and the
375 // right panel taking the rest.
376 VerticalPanel outer = new VerticalPanel();
378 outer.add(searchStatus);
379 outer.add(messagePanel);
380 outer.add(splitPanel);
381 outer.add(statusPanel);
382 outer.setWidth("100%");
383 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
387 // Hook the window resize event, so that we can adjust the UI.
388 Window.addResizeHandler(this);
389 // Clear out the window's built-in margin, because we want to take
390 // advantage of the entire client area.
391 Window.setMargin("0px");
392 // Finally, add the outer panel to the RootPanel, so that it will be
394 RootPanel.get().add(outer);
395 // Call the window resized handler to get the initial sizes setup. Doing
396 // this in a deferred command causes it to occur after all widgets'
397 // sizes have been computed by the browser.
398 DeferredCommand.addCommand(new Command() {
400 public void execute() {
401 onWindowResized(Window.getClientHeight());
407 * Fetches the User object for the specified username.
409 * @param username the username of the user
411 private void fetchUser(final String username) {
412 String path = getApiPath() + username + "/";
413 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path, null) {
416 public void onComplete() {
417 currentUserResource = getResult();
418 final String announcement = currentUserResource.getAnnouncement();
419 if (announcement != null)
420 DeferredCommand.addCommand(new Command() {
422 public void execute() {
423 displayInformation(announcement);
429 public void onError(Throwable t) {
430 GWT.log("Fetching user error", t);
431 if (t instanceof RestException)
432 GSS.get().displayError("No user found:" + ((RestException) t).getHttpStatusText());
434 GSS.get().displayError("System error fetching user data:" + t.getMessage());
438 DeferredCommand.addCommand(getUserCommand);
442 * Parse and store the user credentials to the appropriate fields.
444 private void parseUserCredentials() {
445 Configuration conf = (Configuration) GWT.create(Configuration.class);
446 String cookie = conf.authCookie();
447 String auth = Cookies.getCookie(cookie);
448 String domain = Window.Location.getHostName();
449 String path = Window.Location.getPath();
450 Cookies.setCookie(cookie, "", null, domain, path, false);
453 // Redundant, but silences warnings about possible auth NPE, below.
456 int sepIndex = auth.indexOf(conf.cookieSeparator());
459 token = auth.substring(sepIndex + 1, auth.length());
460 final String username = auth.substring(0, sepIndex);
461 if (username == null)
464 refreshWebDAVPassword();
466 DeferredCommand.addCommand(new Command() {
468 public void execute() {
475 * Redirect the user to the login page for authentication.
477 protected void authenticateUser() {
478 Configuration conf = (Configuration) GWT.create(Configuration.class);
479 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
483 * Redirect the user to the logout page.
486 Configuration conf = (Configuration) GWT.create(Configuration.class);
487 Window.Location.assign(conf.logoutUrl());
491 * Creates an HTML fragment that places an image & caption together, for use
494 * @param imageProto an image prototype for an image
495 * @param caption the group caption
496 * @return the header HTML fragment
498 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
499 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>";
503 private void onWindowResized(int height) {
504 // Adjust the split panel to take up the available room in the window.
505 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
508 splitPanel.setHeight("" + newHeight);
512 public void onResize(ResizeEvent event) {
513 int height = event.getHeight();
514 onWindowResized(height);
517 public boolean isFileListShowing() {
518 int tab = inner.getTabBar().getSelectedTab();
524 public boolean isSearchResultsShowing() {
525 int tab = inner.getTabBar().getSelectedTab();
532 * Make the user list visible.
534 public void showUserList() {
539 * Make the file list visible.
541 public void showFileList() {
542 fileList.updateFileCache(false, true /*clear selection*/);
547 * Make the file list visible.
551 public void showFileList(boolean update) {
552 TreeItem currentFolder = getFolders().getCurrent();
553 if (currentFolder != null) {
554 List<FileResource> files = null;
555 Object cachedObject = currentFolder.getUserObject();
556 if (cachedObject instanceof FolderResource) {
557 FolderResource folder = (FolderResource) cachedObject;
558 files = folder.getFiles();
559 } else if (cachedObject instanceof TrashResource) {
560 TrashResource folder = (TrashResource) cachedObject;
561 files = folder.getFiles();
564 getFileList().setFiles(files);
566 fileList.updateFileCache(update, true /*clear selection*/);
571 * Make the search results visible.
573 * @param query the search query string
575 public void showSearchResults(String query) {
576 searchResults.updateFileCache(query);
577 searchResults.updateCurrentlyShowingStats();
582 * Display the 'loading' indicator.
584 public void showLoadingIndicator() {
585 topPanel.getLoading().setVisible(true);
589 * Hide the 'loading' indicator.
591 public void hideLoadingIndicator() {
592 topPanel.getLoading().setVisible(false);
596 * A native JavaScript method to reach out to the browser's window and
597 * invoke its resizeTo() method.
599 * @param x the new width
600 * @param y the new height
602 public static native void resizeTo(int x, int y) /*-{
607 * A helper method that returns true if the user's list is currently visible
608 * and false if it is hidden.
610 * @return true if the user list is visible
612 public boolean isUserListVisible() {
613 return inner.getTabBar().getSelectedTab() == 1;
617 * Display an error message.
619 * @param msg the message to display
621 public void displayError(String msg) {
622 messagePanel.displayError(msg);
626 * Display a warning message.
628 * @param msg the message to display
630 public void displayWarning(String msg) {
631 messagePanel.displayWarning(msg);
635 * Display an informational message.
637 * @param msg the message to display
639 public void displayInformation(String msg) {
640 messagePanel.displayInformation(msg);
644 * Retrieve the folders.
646 * @return the folders
648 public Folders getFolders() {
653 * Retrieve the search.
662 * Retrieve the currentSelection.
664 * @return the currentSelection
666 public Object getCurrentSelection() {
667 return currentSelection;
671 * Modify the currentSelection.
673 * @param newCurrentSelection the currentSelection to set
675 public void setCurrentSelection(Object newCurrentSelection) {
676 currentSelection = newCurrentSelection;
680 * Retrieve the groups.
684 public Groups getGroups() {
689 * Retrieve the fileList.
691 * @return the fileList
693 public FileList getFileList() {
697 public SearchResults getSearchResults() {
698 return searchResults;
702 * Retrieve the topPanel.
704 * @return the topPanel
706 TopPanel getTopPanel() {
711 * Retrieve the clipboard.
713 * @return the clipboard
715 public Clipboard getClipboard() {
719 public StatusPanel getStatusPanel() {
724 * Retrieve the userDetailsPanel.
726 * @return the userDetailsPanel
728 public UserDetailsPanel getUserDetailsPanel() {
729 return userDetailsPanel;
733 * Retrieve the dragController.
735 * @return the dragController
737 public PickupDragController getDragController() {
738 return dragController;
741 public String getToken() {
745 public String getWebDAVPassword() {
746 return webDAVPassword;
749 public void removeGlassPanel() {
750 glassPanel.removeFromParent();
754 * Retrieve the currentUserResource.
756 * @return the currentUserResource
758 public UserResource getCurrentUserResource() {
759 return currentUserResource;
763 * Modify the currentUserResource.
765 * @param newUser the new currentUserResource
767 public void setCurrentUserResource(UserResource newUser) {
768 currentUserResource = newUser;
771 public static native void preventIESelection() /*-{
772 $doc.body.onselectstart = function () { return false; };
775 public static native void enableIESelection() /*-{
776 if ($doc.body.onselectstart != null)
777 $doc.body.onselectstart = null;
781 * @return the absolute path of the API root URL
783 public String getApiPath() {
784 Configuration conf = (Configuration) GWT.create(Configuration.class);
785 return GWT.getModuleBaseURL() + conf.apiPath();
788 public void refreshWebDAVPassword() {
789 Configuration conf = (Configuration) GWT.create(Configuration.class);
790 String domain = Window.Location.getHostName();
791 String path = Window.Location.getPath();
792 String cookie = conf.webdavCookie();
793 webDAVPassword = Cookies.getCookie(cookie);
794 Cookies.setCookie(cookie, "", null, domain, path, false);
799 * @return Object of the corresponding key in the History map
801 public Object getHistoryItem(String key){
805 * Set a pair of key - object in the History (using a map)
810 public void setHistory(String key, Object obj){