2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd. This file is part
3 * of GSS. GSS is free software: you can redistribute it and/or modify it under
4 * the terms of the GNU General Public License as published by the Free Software
5 * Foundation, either version 3 of the License, or (at your option) any later
6 * version. GSS is distributed in the hope that it will be useful, but WITHOUT
7 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9 * details. You should have received a copy of the GNU General Public License
10 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
12 package gr.ebs.gss.client;
14 import gr.ebs.gss.client.clipboard.Clipboard;
15 import gr.ebs.gss.client.dnd.DnDFocusPanel;
16 import gr.ebs.gss.client.dnd.DnDSimpleFocusPanel;
17 import gr.ebs.gss.client.rest.GetCommand;
18 import gr.ebs.gss.client.rest.RestException;
19 import gr.ebs.gss.client.rest.resource.FileResource;
20 import gr.ebs.gss.client.rest.resource.FolderResource;
21 import gr.ebs.gss.client.rest.resource.TrashResource;
22 import gr.ebs.gss.client.rest.resource.UserResource;
24 import java.util.Iterator;
25 import java.util.List;
27 import com.allen_sauer.gwt.dnd.client.DragContext;
28 import com.allen_sauer.gwt.dnd.client.PickupDragController;
29 import com.allen_sauer.gwt.dnd.client.VetoDragException;
30 import com.google.gwt.core.client.EntryPoint;
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.event.logical.shared.ResizeEvent;
33 import com.google.gwt.event.logical.shared.ResizeHandler;
34 import com.google.gwt.event.logical.shared.SelectionEvent;
35 import com.google.gwt.event.logical.shared.SelectionHandler;
36 import com.google.gwt.resources.client.ClientBundle;
37 import com.google.gwt.resources.client.ImageResource;
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.History;
43 import com.google.gwt.user.client.Window;
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.DecoratedTabPanel;
47 import com.google.gwt.user.client.ui.DockPanel;
48 import com.google.gwt.user.client.ui.HTML;
49 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
50 import com.google.gwt.user.client.ui.HasVerticalAlignment;
51 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
52 import com.google.gwt.user.client.ui.Label;
53 import com.google.gwt.user.client.ui.RootPanel;
54 import com.google.gwt.user.client.ui.TabPanel;
55 import com.google.gwt.user.client.ui.TreeItem;
56 import com.google.gwt.user.client.ui.VerticalPanel;
57 import com.google.gwt.user.client.ui.Widget;
60 * Entry point classes define <code>onModuleLoad()</code>.
62 public class GSS implements EntryPoint, ResizeHandler {
65 * A constant that denotes the completion of an IncrementalCommand.
67 public static final boolean DONE = false;
69 public static final int VISIBLE_FILE_COUNT = 100;
72 * Instantiate an application-level image bundle. This object will provide
73 * programmatic access to all the images needed by widgets.
75 private static Images images = (Images) GWT.create(Images.class);
77 private GlassPanel glassPanel = new GlassPanel();
80 * An aggregate image bundle that pulls together all the images for this
81 * application into a single bundle.
83 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 {
85 @Source("gr/ebs/gss/resources/document.png")
86 ImageResource folders();
88 @Source("gr/ebs/gss/resources/edit_group_22.png")
89 ImageResource groups();
91 @Source("gr/ebs/gss/resources/search.png")
92 ImageResource search();
96 * The single GSS instance.
98 private static GSS singleton;
101 * Gets the singleton GSS instance.
103 * @return the GSS object
105 public static GSS get() {
106 if (GSS.singleton == null)
107 GSS.singleton = new GSS();
108 return GSS.singleton;
112 * The Application Clipboard implementation;
114 private Clipboard clipboard = new Clipboard();
116 private UserResource currentUserResource;
119 * The top panel that contains the menu bar.
121 private TopPanel topPanel;
124 * The panel that contains the various system messages.
126 private MessagePanel messagePanel = new MessagePanel(GSS.images);
129 * The bottom panel that contains the status bar.
131 private StatusPanel statusPanel = new StatusPanel(GSS.images);
134 * The top right panel that displays the logged in user details
136 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
139 * The file list widget.
141 private FileList fileList;
144 * The group list widget.
146 private Groups groups = new Groups(images);
149 * The search result widget.
151 private SearchResults searchResults;
154 * The tab panel that occupies the right side of the screen.
156 private TabPanel inner = new DecoratedTabPanel();
159 * The split panel that will contain the left and right panels.
161 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
164 * The horizontal panel that will contain the search and status panels.
166 private DockPanel searchStatus = new DockPanel();
171 private Search search;
174 * The widget that displays the tree of folders.
176 private Folders folders = new Folders(images);
179 * The currently selected item in the application, for use by the Edit menu
180 * commands. Potential types are Folder, File, User and Group.
182 private Object currentSelection;
185 * The authentication token of the current user.
187 private String token;
190 * The WebDAV password of the current user
192 private String webDAVPassword;
194 private PickupDragController dragController;
196 public void onModuleLoad() {
197 // Initialize the singleton before calling the constructors of the
198 // various widgets that might call GSS.get().
200 RootPanel.get().add(glassPanel, 0, 0);
201 parseUserCredentials();
202 dragController = new PickupDragController(RootPanel.get(), false) {
205 public void previewDragStart() throws VetoDragException {
206 super.previewDragStart();
207 if (context.selectedWidgets.isEmpty())
208 throw new VetoDragException();
210 if (context.draggable != null)
211 if (context.draggable instanceof DnDFocusPanel) {
212 DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
213 // prevent drag and drop for trashed files and for
214 // unselected tree items
215 if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
216 throw new VetoDragException();
217 else if (toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
218 throw new VetoDragException();
219 else if (toDrop.getItem() != null && !toDrop.getItem().isDraggable())
220 throw new VetoDragException();
222 } else if (context.draggable instanceof DnDSimpleFocusPanel) {
223 DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
224 // prevent drag and drop for trashed files and for
225 // unselected tree items
226 if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
227 throw new VetoDragException();
232 protected Widget newDragProxy(DragContext aContext) {
233 AbsolutePanel container = new AbsolutePanel();
234 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
235 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
237 Widget widget = (Widget) iterator.next();
238 if (widget instanceof DnDFocusPanel) {
239 DnDFocusPanel book = (DnDFocusPanel) widget;
240 html = book.cloneHTML();
241 } else if (widget instanceof DnDSimpleFocusPanel) {
242 DnDSimpleFocusPanel book = (DnDSimpleFocusPanel) widget;
243 html = book.cloneHTML();
246 container.add(new Label("Drag ME"));
253 dragController.setBehaviorDragProxy(true);
254 dragController.setBehaviorMultipleSelection(false);
255 topPanel = new TopPanel(GSS.images);
256 topPanel.setWidth("100%");
258 messagePanel.setWidth("100%");
259 messagePanel.setVisible(false);
261 search = new Search(images);
262 searchStatus.add(search, DockPanel.WEST);
263 searchStatus.add(userDetailsPanel, DockPanel.EAST);
264 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
265 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
266 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
267 searchStatus.setWidth("100%");
269 fileList = new FileList(images);
271 searchResults = new SearchResults(images);
273 // Inner contains the various lists.
274 inner.setAnimationEnabled(true);
275 inner.getTabBar().addStyleName("gss-MainTabBar");
276 inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
277 inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
279 inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
280 inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
281 inner.setWidth("100%");
284 inner.addSelectionHandler(new SelectionHandler<Integer>() {
287 public void onSelection(SelectionEvent<Integer> event) {
288 int tabIndex = event.getSelectedItem();
291 fileList.clearSelectedRows();
292 fileList.updateCurrentlyShowingStats();
293 History.newItem("Files");
296 groups.updateCurrentlyShowingStats();
297 History.newItem("Groups");
300 searchResults.clearSelectedRows();
301 searchResults.updateCurrentlyShowingStats();
302 History.newItem("Search");
307 // History.addValueChangeHandler(new ValueChangeHandler<String>() {
309 // public void onValueChange(ValueChangeEvent<String> event) {
310 // String historyToken = event.getValue();
312 // if (historyToken.equals("Files"))
313 // inner.selectTab(1);
314 // else if (historyToken.equals("Groups"))
315 // inner.selectTab(2);
316 // else if (historyToken.equals("Search Results"))
317 // inner.selectTab(3);
319 // inner.selectTab(0);
320 // } catch (IndexOutOfBoundsException e) {
321 // inner.selectTab(0);
326 // Add the left and right panels to the split panel.
327 splitPanel.setLeftWidget(folders);
328 splitPanel.setRightWidget(inner);
329 splitPanel.setSplitPosition("25%");
330 splitPanel.setSize("100%", "100%");
331 splitPanel.addStyleName("gss-splitPanel");
333 // Create a dock panel that will contain the menu bar at the top,
334 // the shortcuts to the left, the status bar at the bottom and the
335 // right panel taking the rest.
336 VerticalPanel outer = new VerticalPanel();
338 outer.add(searchStatus);
339 outer.add(messagePanel);
340 outer.add(splitPanel);
341 outer.add(statusPanel);
342 outer.setWidth("100%");
343 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
347 // Hook the window resize event, so that we can adjust the UI.
348 Window.addResizeHandler(this);
349 // Clear out the window's built-in margin, because we want to take
350 // advantage of the entire client area.
351 Window.setMargin("0px");
352 // Finally, add the outer panel to the RootPanel, so that it will be
354 RootPanel.get().add(outer);
355 // Call the window resized handler to get the initial sizes setup. Doing
356 // this in a deferred command causes it to occur after all widgets'
357 // sizes have been computed by the browser.
358 DeferredCommand.addCommand(new Command() {
360 public void execute() {
361 onWindowResized(Window.getClientHeight());
367 * Fetches the User object for the specified username.
369 * @param username the username of the user
371 private void fetchUser(final String username) {
372 String path = getApiPath() + username + "/";
373 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path, null) {
376 public void onComplete() {
377 currentUserResource = getResult();
378 final String announcement = currentUserResource.getAnnouncement();
379 if (announcement != null)
380 DeferredCommand.addCommand(new Command() {
382 public void execute() {
383 displayInformation(announcement);
389 public void onError(Throwable t) {
390 GWT.log("Fetching user error", t);
391 if (t instanceof RestException)
392 GSS.get().displayError("No user found:" + ((RestException) t).getHttpStatusText());
394 GSS.get().displayError("System error fetching user data:" + t.getMessage());
398 DeferredCommand.addCommand(getUserCommand);
402 * Parse and store the user credentials to the appropriate fields.
404 private void parseUserCredentials() {
405 Configuration conf = (Configuration) GWT.create(Configuration.class);
406 String cookie = conf.authCookie();
407 String auth = Cookies.getCookie(cookie);
408 String domain = Window.Location.getHostName();
409 String path = Window.Location.getPath();
410 Cookies.setCookie(cookie, "", null, domain, path, false);
413 // Redundant, but silences warnings about possible auth NPE, below.
416 int sepIndex = auth.indexOf(conf.cookieSeparator());
419 token = auth.substring(sepIndex + 1, auth.length());
420 final String username = auth.substring(0, sepIndex);
421 if (username == null)
424 refreshWebDAVPassword();
426 DeferredCommand.addCommand(new Command() {
428 public void execute() {
435 * Redirect the user to the login page for authentication.
437 protected void authenticateUser() {
438 Configuration conf = (Configuration) GWT.create(Configuration.class);
439 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
443 * Redirect the user to the logout page.
446 Configuration conf = (Configuration) GWT.create(Configuration.class);
447 Window.Location.assign(conf.logoutUrl());
451 * Creates an HTML fragment that places an image & caption together, for use
454 * @param imageProto an image prototype for an image
455 * @param caption the group caption
456 * @return the header HTML fragment
458 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
459 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>";
463 private void onWindowResized(int height) {
464 // Adjust the split panel to take up the available room in the window.
465 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
468 splitPanel.setHeight("" + newHeight);
472 public void onResize(ResizeEvent event) {
473 int height = event.getHeight();
474 onWindowResized(height);
477 public boolean isFileListShowing() {
478 int tab = inner.getTabBar().getSelectedTab();
484 public boolean isSearchResultsShowing() {
485 int tab = inner.getTabBar().getSelectedTab();
492 * Make the user list visible.
494 public void showUserList() {
499 * Make the file list visible.
501 public void showFileList() {
502 fileList.updateFileCache(false, true /*clear selection*/);
507 * Make the file list visible.
511 public void showFileList(boolean update) {
512 TreeItem currentFolder = getFolders().getCurrent();
513 if (currentFolder != null) {
514 List<FileResource> files = null;
515 Object cachedObject = currentFolder.getUserObject();
516 if (cachedObject instanceof FolderResource) {
517 FolderResource folder = (FolderResource) cachedObject;
518 files = folder.getFiles();
519 } else if (cachedObject instanceof TrashResource) {
520 TrashResource folder = (TrashResource) cachedObject;
521 files = folder.getFiles();
524 getFileList().setFiles(files);
526 fileList.updateFileCache(update, true /*clear selection*/);
531 * Make the search results visible.
533 * @param query the search query string
535 public void showSearchResults(String query) {
536 searchResults.updateFileCache(query);
537 searchResults.updateCurrentlyShowingStats();
542 * Display the 'loading' indicator.
544 public void showLoadingIndicator() {
545 topPanel.getLoading().setVisible(true);
549 * Hide the 'loading' indicator.
551 public void hideLoadingIndicator() {
552 topPanel.getLoading().setVisible(false);
556 * A native JavaScript method to reach out to the browser's window and
557 * invoke its resizeTo() method.
559 * @param x the new width
560 * @param y the new height
562 public static native void resizeTo(int x, int y) /*-{
567 * A helper method that returns true if the user's list is currently visible
568 * and false if it is hidden.
570 * @return true if the user list is visible
572 public boolean isUserListVisible() {
573 return inner.getTabBar().getSelectedTab() == 1;
577 * Display an error message.
579 * @param msg the message to display
581 public void displayError(String msg) {
582 messagePanel.displayError(msg);
586 * Display a warning message.
588 * @param msg the message to display
590 public void displayWarning(String msg) {
591 messagePanel.displayWarning(msg);
595 * Display an informational message.
597 * @param msg the message to display
599 public void displayInformation(String msg) {
600 messagePanel.displayInformation(msg);
604 * Retrieve the folders.
606 * @return the folders
608 public Folders getFolders() {
613 * Retrieve the search.
622 * Retrieve the currentSelection.
624 * @return the currentSelection
626 public Object getCurrentSelection() {
627 return currentSelection;
631 * Modify the currentSelection.
633 * @param newCurrentSelection the currentSelection to set
635 public void setCurrentSelection(Object newCurrentSelection) {
636 currentSelection = newCurrentSelection;
640 * Retrieve the groups.
644 public Groups getGroups() {
649 * Retrieve the fileList.
651 * @return the fileList
653 public FileList getFileList() {
657 public SearchResults getSearchResults() {
658 return searchResults;
662 * Retrieve the topPanel.
664 * @return the topPanel
666 TopPanel getTopPanel() {
671 * Retrieve the clipboard.
673 * @return the clipboard
675 public Clipboard getClipboard() {
679 public StatusPanel getStatusPanel() {
684 * Retrieve the userDetailsPanel.
686 * @return the userDetailsPanel
688 public UserDetailsPanel getUserDetailsPanel() {
689 return userDetailsPanel;
693 * Retrieve the dragController.
695 * @return the dragController
697 public PickupDragController getDragController() {
698 return dragController;
701 public String getToken() {
705 public String getWebDAVPassword() {
706 return webDAVPassword;
709 public void removeGlassPanel() {
710 glassPanel.removeFromParent();
714 * Retrieve the currentUserResource.
716 * @return the currentUserResource
718 public UserResource getCurrentUserResource() {
719 return currentUserResource;
723 * Modify the currentUserResource.
725 * @param newUser the new currentUserResource
727 public void setCurrentUserResource(UserResource newUser) {
728 currentUserResource = newUser;
731 public static native void preventIESelection() /*-{
732 $doc.body.onselectstart = function () { return false; };
735 public static native void enableIESelection() /*-{
736 if ($doc.body.onselectstart != null)
737 $doc.body.onselectstart = null;
741 * @return the absolute path of the API root URL
743 public String getApiPath() {
744 Configuration conf = (Configuration) GWT.create(Configuration.class);
745 return GWT.getModuleBaseURL() + conf.apiPath();
748 public void refreshWebDAVPassword() {
749 Configuration conf = (Configuration) GWT.create(Configuration.class);
750 String domain = Window.Location.getHostName();
751 String path = Window.Location.getPath();
752 String cookie = conf.webdavCookie();
753 webDAVPassword = Cookies.getCookie(cookie);
754 Cookies.setCookie(cookie, "", null, domain, path, false);