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.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.Window;
50 import com.google.gwt.user.client.ui.AbsolutePanel;
51 import com.google.gwt.user.client.ui.AbstractImagePrototype;
52 import com.google.gwt.user.client.ui.DecoratedTabPanel;
53 import com.google.gwt.user.client.ui.DockPanel;
54 import com.google.gwt.user.client.ui.HTML;
55 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
56 import com.google.gwt.user.client.ui.HasVerticalAlignment;
57 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
58 import com.google.gwt.user.client.ui.Label;
59 import com.google.gwt.user.client.ui.RootPanel;
60 import com.google.gwt.user.client.ui.TabPanel;
61 import com.google.gwt.user.client.ui.TreeItem;
62 import com.google.gwt.user.client.ui.VerticalPanel;
63 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);
83 private GlassPanel glassPanel = new GlassPanel();
86 * An aggregate image bundle that pulls together all the images for this
87 * application into a single bundle.
89 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 {
91 @Source("gr/ebs/gss/resources/document.png")
92 ImageResource folders();
94 @Source("gr/ebs/gss/resources/edit_group_22.png")
95 ImageResource groups();
97 @Source("gr/ebs/gss/resources/search.png")
98 ImageResource search();
102 * The single GSS instance.
104 private static GSS singleton;
107 * Gets the singleton GSS instance.
109 * @return the GSS object
111 public static GSS get() {
112 if (GSS.singleton == null)
113 GSS.singleton = new GSS();
114 return GSS.singleton;
118 * The Application Clipboard implementation;
120 private Clipboard clipboard = new Clipboard();
122 private UserResource currentUserResource;
125 * The top panel that contains the menu bar.
127 private TopPanel topPanel;
130 * The panel that contains the various system messages.
132 private MessagePanel messagePanel = new MessagePanel(GSS.images);
135 * The bottom panel that contains the status bar.
137 private StatusPanel statusPanel = new StatusPanel(GSS.images);
140 * The top right panel that displays the logged in user details
142 private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
145 * The file list widget.
147 private FileList fileList;
150 * The group list widget.
152 private Groups groups = new Groups(images);
155 * The search result widget.
157 private SearchResults searchResults;
160 * A widget that displays a message indicating that communication with the
161 * server is underway.
163 private LoadingIndicator loading;
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 * The WebDAV password of the current user
204 private String webDAVPassword;
206 private PickupDragController dragController;
208 public void onModuleLoad() {
209 // Initialize the singleton before calling the constructors of the
210 // various widgets that might call GSS.get().
212 RootPanel.get().add(glassPanel, 0, 0);
213 parseUserCredentials();
214 dragController = new PickupDragController(RootPanel.get(), false) {
217 public void previewDragStart() throws VetoDragException {
218 super.previewDragStart();
219 if (context.selectedWidgets.isEmpty())
220 throw new VetoDragException();
222 if(context.draggable != null)
223 if(context.draggable instanceof DnDFocusPanel){
224 DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
225 //prevent drag and drop for trashed files and for unselected tree items
226 if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
227 throw new VetoDragException();
228 else if(toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
229 throw new VetoDragException();
230 else if(toDrop.getItem() != null && !toDrop.getItem().isDraggable())
231 throw new VetoDragException();
234 else if(context.draggable instanceof DnDSimpleFocusPanel){
235 DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
236 //prevent drag and drop for trashed files and for unselected tree items
237 if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
238 throw new VetoDragException();
243 protected Widget newDragProxy(DragContext aContext) {
244 AbsolutePanel container = new AbsolutePanel();
245 DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
246 for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
248 Widget widget = (Widget) iterator.next();
249 if(widget instanceof DnDFocusPanel){
250 DnDFocusPanel book = (DnDFocusPanel) widget;
251 html = book.cloneHTML();
253 else if(widget instanceof DnDSimpleFocusPanel){
254 DnDSimpleFocusPanel book = (DnDSimpleFocusPanel) widget;
255 html = book.cloneHTML();
258 container.add(new Label("Drag ME"));
265 dragController.setBehaviorDragProxy(true);
266 dragController.setBehaviorMultipleSelection(false);
267 topPanel = new TopPanel(GSS.images);
268 topPanel.setWidth("100%");
270 messagePanel.setWidth("100%");
271 messagePanel.setVisible(false);
273 search = new Search(images);
274 searchStatus.add(search, DockPanel.WEST);
275 searchStatus.add(userDetailsPanel, DockPanel.EAST);
276 searchStatus.setCellHorizontalAlignment(userDetailsPanel, HasHorizontalAlignment.ALIGN_RIGHT);
277 searchStatus.setCellVerticalAlignment(search, HasVerticalAlignment.ALIGN_MIDDLE);
278 searchStatus.setCellVerticalAlignment(userDetailsPanel, HasVerticalAlignment.ALIGN_MIDDLE);
279 searchStatus.setWidth("100%");
281 fileList = new FileList(images);
283 searchResults = new SearchResults(images);
285 // Inner contains the various lists.
286 inner.setAnimationEnabled(true);
287 inner.getTabBar().addStyleName("gss-MainTabBar");
288 inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
289 inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
291 inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
292 inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
293 inner.setWidth("100%");
296 inner.addSelectionHandler(new SelectionHandler<Integer>() {
299 public void onSelection(SelectionEvent<Integer> event) {
300 int tabIndex= event.getSelectedItem();
303 fileList.clearSelectedRows();
304 fileList.updateCurrentlyShowingStats();
307 groups.updateCurrentlyShowingStats();
310 searchResults.clearSelectedRows();
311 searchResults.updateCurrentlyShowingStats();
317 // Add the left and right panels to the split panel.
318 splitPanel.setLeftWidget(folders);
319 splitPanel.setRightWidget(inner);
320 splitPanel.setSplitPosition("25%");
321 splitPanel.setSize("100%", "100%");
322 splitPanel.addStyleName("gss-splitPanel");
324 // Create a dock panel that will contain the menu bar at the top,
325 // the shortcuts to the left, the status bar at the bottom and the
326 // right panel taking the rest.
327 VerticalPanel outer = new VerticalPanel();
329 outer.add(searchStatus);
330 outer.add(messagePanel);
331 outer.add(splitPanel);
332 outer.add(statusPanel);
333 outer.setWidth("100%");
334 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
338 loading = new LoadingIndicator();
339 // Hook the window resize event, so that we can adjust the UI.
340 Window.addResizeHandler(this);
341 // Clear out the window's built-in margin, because we want to take
342 // advantage of the entire client area.
343 Window.setMargin("0px");
344 // Finally, add the outer panel to the RootPanel, so that it will be
346 RootPanel.get().add(outer);
347 // Call the window resized handler to get the initial sizes setup. Doing
348 // this in a deferred command causes it to occur after all widgets'
349 // sizes have been computed by the browser.
350 DeferredCommand.addCommand(new Command() {
351 public void execute() {
352 onWindowResized(Window.getClientHeight());
358 * Fetches the User object for the specified username.
360 * @param username the username of the user
362 private void fetchUser(final String username) {
363 String path = getApiPath() + username + "/";
364 GetCommand<UserResource> getUserCommand = new GetCommand<UserResource>(UserResource.class, username, path, null){
367 public void onComplete() {
368 currentUserResource = getResult();
369 final String announcement = currentUserResource.getAnnouncement();
370 if (announcement != null)
371 DeferredCommand.addCommand(new Command() {
372 public void execute() {
373 displayInformation(announcement);
379 public void onError(Throwable t) {
380 GWT.log("Fetching user error", t);
381 if(t instanceof RestException)
382 GSS.get().displayError("No user found:"+((RestException)t).getHttpStatusText());
384 GSS.get().displayError("System error fetching user data:"+t.getMessage());
388 DeferredCommand.addCommand(getUserCommand);
392 * Parse and store the user credentials to the appropriate fields.
394 private void parseUserCredentials() {
395 Configuration conf = (Configuration) GWT.create(Configuration.class);
396 String cookie = conf.authCookie();
397 String auth = Cookies.getCookie(cookie);
398 String domain = Window.Location.getHostName();
399 String path = Window.Location.getPath();
400 Cookies.setCookie(cookie, "", null, domain, path, false);
403 // Redundant, but silences warnings about possible auth NPE, below.
406 int sepIndex = auth.indexOf(conf.cookieSeparator());
409 token = auth.substring(sepIndex + 1, auth.length());
410 final String username = auth.substring(0, sepIndex);
411 if (username == null)
414 refreshWebDAVPassword();
416 DeferredCommand.addCommand(new Command() {
417 public void execute() {
424 * Redirect the user to the login page for authentication.
426 protected void authenticateUser() {
427 Configuration conf = (Configuration) GWT.create(Configuration.class);
428 Window.Location.assign(conf.loginUrl() + "?next=" + GWT.getModuleBaseURL());
432 * Redirect the user to the logout page.
435 Configuration conf = (Configuration) GWT.create(Configuration.class);
436 Window.Location.assign(conf.logoutUrl());
440 * Creates an HTML fragment that places an image & caption together, for use
443 * @param imageProto an image prototype for an image
444 * @param caption the group caption
445 * @return the header HTML fragment
447 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
448 String captionHTML = "<table class='caption' cellpadding='0' " +
449 "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() +
450 "</td><td class='rcaption'><b style='white-space:nowrap'> " +
451 caption + "</b></td></tr></table>";
456 private void onWindowResized(int height) {
457 // Adjust the split panel to take up the available room in the window.
458 int newHeight = height - splitPanel.getAbsoluteTop() - 44;
461 splitPanel.setHeight("" + newHeight);
465 public void onResize(ResizeEvent event) {
466 int height = event.getHeight();
467 onWindowResized(height);
470 public boolean isFileListShowing(){
471 int tab = inner.getTabBar().getSelectedTab();
472 if(tab == 0) return true;
476 public boolean isSearchResultsShowing(){
477 int tab = inner.getTabBar().getSelectedTab();
478 if(tab == 2) return true;
483 * Make the user list visible.
485 public void showUserList() {
490 * Make the file list visible.
492 public void showFileList() {
493 fileList.updateFileCache(false, true /*clear selection*/);
498 * 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.
522 * @param query the search query string
524 public void showSearchResults(String query) {
525 searchResults.updateFileCache(query);
526 searchResults.updateCurrentlyShowingStats();
531 * Display the 'loading' indicator.
533 public void showLoadingIndicator() {
538 * Hide the 'loading' indicator.
540 public void hideLoadingIndicator() {
545 * A native JavaScript method to reach out to the browser's window and
546 * invoke its resizeTo() method.
548 * @param x the new width
549 * @param y the new height
551 public static native void resizeTo(int x, int y) /*-{
556 * A helper method that returns true if the user's list is currently visible
557 * and false if it is hidden.
559 * @return true if the user list is visible
561 public boolean isUserListVisible() {
562 return inner.getTabBar().getSelectedTab() == 1;
566 * Display an error message.
568 * @param msg the message to display
570 public void displayError(String msg) {
571 messagePanel.displayError(msg);
575 * Display a warning message.
577 * @param msg the message to display
579 public void displayWarning(String msg) {
580 messagePanel.displayWarning(msg);
584 * Display an informational message.
586 * @param msg the message to display
588 public void displayInformation(String msg) {
589 messagePanel.displayInformation(msg);
593 * Retrieve the folders.
595 * @return the folders
597 public Folders getFolders() {
602 * Retrieve the search.
611 * Retrieve the currentSelection.
613 * @return the currentSelection
615 public Object getCurrentSelection() {
616 return currentSelection;
620 * Modify the currentSelection.
622 * @param newCurrentSelection the currentSelection to set
624 public void setCurrentSelection(Object newCurrentSelection) {
625 currentSelection = newCurrentSelection;
629 * Retrieve the groups.
633 public Groups getGroups() {
638 * Retrieve the fileList.
640 * @return the fileList
642 public FileList getFileList() {
646 public SearchResults getSearchResults(){
647 return searchResults;
651 * Retrieve the topPanel.
653 * @return the topPanel
655 TopPanel getTopPanel() {
660 * Retrieve the clipboard.
662 * @return the clipboard
664 public Clipboard getClipboard() {
669 public StatusPanel getStatusPanel(){
675 * Retrieve the userDetailsPanel.
677 * @return the userDetailsPanel
679 public UserDetailsPanel getUserDetailsPanel() {
680 return userDetailsPanel;
684 * Retrieve the dragController.
686 * @return the dragController
688 public PickupDragController getDragController() {
689 return dragController;
692 public String getToken(){
696 public String getWebDAVPassword() {
697 return webDAVPassword;
700 public void removeGlassPanel(){
701 glassPanel.removeFromParent();
705 * Retrieve the currentUserResource.
707 * @return the currentUserResource
709 public UserResource getCurrentUserResource() {
710 return currentUserResource;
714 * Modify the currentUserResource.
716 * @param newUser the new currentUserResource
718 public void setCurrentUserResource(UserResource newUser) {
719 currentUserResource = newUser;
722 public static native void preventIESelection() /*-{
723 $doc.body.onselectstart = function () { return false; };
726 public static native void enableIESelection() /*-{
727 if ($doc.body.onselectstart != null)
728 $doc.body.onselectstart = null;
732 * @return the absolute path of the API root URL
734 public String getApiPath() {
735 Configuration conf = (Configuration) GWT.create(Configuration.class);
736 return GWT.getModuleBaseURL() + conf.apiPath();
739 public void refreshWebDAVPassword() {
740 Configuration conf = (Configuration) GWT.create(Configuration.class);
741 String domain = Window.Location.getHostName();
742 String path = Window.Location.getPath();
743 String cookie = conf.webdavCookie();
744 webDAVPassword = Cookies.getCookie(cookie);
745 Cookies.setCookie(cookie, "", null, domain, path, false);