Added ids to folder context menu for trash folder after a wrong merge.
[pithos] / src / gr / ebs / gss / client / GSS.java
index ea49b32..5674086 100644 (file)
 package gr.ebs.gss.client;
 
 import gr.ebs.gss.client.clipboard.Clipboard;
-import gr.ebs.gss.client.dnd.DnDFocusPanel;
-import gr.ebs.gss.client.dnd.DnDSimpleFocusPanel;
+import gr.ebs.gss.client.commands.GetUserCommand;
 import gr.ebs.gss.client.rest.GetCommand;
 import gr.ebs.gss.client.rest.RestException;
 import gr.ebs.gss.client.rest.resource.FileResource;
 import gr.ebs.gss.client.rest.resource.FolderResource;
+import gr.ebs.gss.client.rest.resource.OtherUserResource;
+import gr.ebs.gss.client.rest.resource.OthersResource;
+import gr.ebs.gss.client.rest.resource.RestResource;
+import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
 import gr.ebs.gss.client.rest.resource.TrashResource;
 import gr.ebs.gss.client.rest.resource.UserResource;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
-import com.allen_sauer.gwt.dnd.client.DragContext;
-import com.allen_sauer.gwt.dnd.client.PickupDragController;
-import com.allen_sauer.gwt.dnd.client.VetoDragException;
 import com.google.gwt.core.client.EntryPoint;
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.event.logical.shared.ResizeEvent;
@@ -44,29 +45,25 @@ import com.google.gwt.event.logical.shared.SelectionEvent;
 import com.google.gwt.event.logical.shared.SelectionHandler;
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
+import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.resources.client.ClientBundle;
 import com.google.gwt.resources.client.ImageResource;
 import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.Cookies;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.History;
 import com.google.gwt.user.client.Window;
-import com.google.gwt.user.client.ui.AbsolutePanel;
 import com.google.gwt.user.client.ui.AbstractImagePrototype;
 import com.google.gwt.user.client.ui.DecoratedTabPanel;
 import com.google.gwt.user.client.ui.DockPanel;
-import com.google.gwt.user.client.ui.HTML;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.HasVerticalAlignment;
 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
-import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.TabPanel;
-import com.google.gwt.user.client.ui.TreeItem;
 import com.google.gwt.user.client.ui.VerticalPanel;
-import com.google.gwt.user.client.ui.Widget;
-
 /**
  * Entry point classes define <code>onModuleLoad()</code>.
  */
@@ -91,7 +88,7 @@ public class GSS implements EntryPoint, ResizeHandler {
         * An aggregate image bundle that pulls together all the images for this
         * application into a single bundle.
         */
-       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 {
+       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, CellTreeView.Images {
 
                @Source("gr/ebs/gss/resources/document.png")
                ImageResource folders();
@@ -164,7 +161,16 @@ public class GSS implements EntryPoint, ResizeHandler {
        /**
         * The tab panel that occupies the right side of the screen.
         */
-       private TabPanel inner = new DecoratedTabPanel();
+       private TabPanel inner = new DecoratedTabPanel(){
+               
+               public void onBrowserEvent(com.google.gwt.user.client.Event event) {
+                       if (DOM.eventGetType(event) == Event.ONCONTEXTMENU){
+                               if(isFileListShowing()){
+                                       getFileList().showContextMenu(event);
+                               }
+                       }
+               };
+       };
 
        /**
         * The split panel that will contain the left and right panels.
@@ -184,8 +190,8 @@ public class GSS implements EntryPoint, ResizeHandler {
        /**
         * The widget that displays the tree of folders.
         */
-       private Folders folders = new Folders(images);
-
+       
+       private CellTreeView treeView = new CellTreeView(images);
        /**
         * The currently selected item in the application, for use by the Edit menu
         * commands. Potential types are Folder, File, User and Group.
@@ -198,77 +204,22 @@ public class GSS implements EntryPoint, ResizeHandler {
        private String token;
 
        /**
-        * A map that stores a set of String URI and the corresponding object
-        * in order history functionality to be implemented.
-        */
-       private Map<String, Object> map = new HashMap<String, Object>();
-
-       /**
         * The WebDAV password of the current user
         */
        private String webDAVPassword;
 
-       private PickupDragController dragController;
+       
+
+       public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
 
+       @Override
        public void onModuleLoad() {
                // Initialize the singleton before calling the constructors of the
                // various widgets that might call GSS.get().
                singleton = this;
                RootPanel.get().add(glassPanel, 0, 0);
                parseUserCredentials();
-               dragController = new PickupDragController(RootPanel.get(), false) {
-
-                       @Override
-                       public void previewDragStart() throws VetoDragException {
-                               super.previewDragStart();
-                               if (context.selectedWidgets.isEmpty())
-                                       throw new VetoDragException();
-
-                               if (context.draggable != null)
-                                       if (context.draggable instanceof DnDFocusPanel) {
-                                               DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
-                                               // prevent drag and drop for trashed files and for
-                                               // unselected tree items
-                                               if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
-                                                       throw new VetoDragException();
-                                               else if (toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
-                                                       throw new VetoDragException();
-                                               else if (toDrop.getItem() != null && !toDrop.getItem().isDraggable())
-                                                       throw new VetoDragException();
-
-                                       } else if (context.draggable instanceof DnDSimpleFocusPanel) {
-                                               DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
-                                               // prevent drag and drop for trashed files and for
-                                               // unselected tree items
-                                               if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
-                                                       throw new VetoDragException();
-                                       }
-                       }
-
-                       @Override
-                       protected Widget newDragProxy(DragContext aContext) {
-                               AbsolutePanel container = new AbsolutePanel();
-                               DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
-                               for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
-                                       HTML html = null;
-                                       Widget widget = (Widget) iterator.next();
-                                       if (widget instanceof DnDFocusPanel) {
-                                               DnDFocusPanel book = (DnDFocusPanel) widget;
-                                               html = book.cloneHTML();
-                                       } else if (widget instanceof DnDSimpleFocusPanel) {
-                                               DnDSimpleFocusPanel book = (DnDSimpleFocusPanel) widget;
-                                               html = book.cloneHTML();
-                                       }
-                                       if (html == null)
-                                               container.add(new Label("Drag ME"));
-                                       else
-                                               container.add(html);
-                               }
-                               return container;
-                       }
-               };
-               dragController.setBehaviorDragProxy(true);
-               dragController.setBehaviorMultipleSelection(false);
+               
                topPanel = new TopPanel(GSS.images);
                topPanel.setWidth("100%");
 
@@ -288,13 +239,15 @@ public class GSS implements EntryPoint, ResizeHandler {
                searchResults = new SearchResults(images);
 
                // Inner contains the various lists.
+               inner.sinkEvents(Event.ONCONTEXTMENU);
                inner.setAnimationEnabled(true);
                inner.getTabBar().addStyleName("gss-MainTabBar");
                inner.getDeckPanel().addStyleName("gss-MainTabPanelBottom");
                inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
-
+               
                inner.add(groups, createHeaderHTML(AbstractImagePrototype.create(images.groups()), "Groups"), true);
                inner.add(searchResults, createHeaderHTML(AbstractImagePrototype.create(images.search()), "Search Results"), true);
+               //inner.add(new CellTreeView(images), createHeaderHTML(AbstractImagePrototype.create(images.search()), "Cell tree sample"), true);
                inner.setWidth("100%");
                inner.selectTab(0);
 
@@ -303,7 +256,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                        @Override
                        public void onSelection(SelectionEvent<Integer> event) {
                                int tabIndex = event.getSelectedItem();
-                               TreeItem treeItem = GSS.get().getFolders().getCurrent();
+//                             TreeItem treeItem = GSS.get().getFolders().getCurrent();
                                switch (tabIndex) {
                                        case 0:
 //                                             Files tab selected
@@ -313,13 +266,13 @@ public class GSS implements EntryPoint, ResizeHandler {
                                        case 1:
 //                                             Groups tab selected
                                                groups.updateCurrentlyShowingStats();
-                                       updateHistory("Groups", treeItem);
+                                       updateHistory("Groups");
                                                break;
                                        case 2:
 //                                             Search tab selected
                                                searchResults.clearSelectedRows();
                                                searchResults.updateCurrentlyShowingStats();
-                                       updateHistory("Search", treeItem);
+                                       updateHistory("Search");
                                                break;
                                }
                        }
@@ -328,42 +281,39 @@ public class GSS implements EntryPoint, ResizeHandler {
                String initToken = History.getToken();
                if(initToken.length() == 0)
                        History.newItem("Files");
-
-
 //                Add history listener to handle any history events
-                  History.addValueChangeHandler(new ValueChangeHandler<String>() {
-                             public void onValueChange(ValueChangeEvent<String> event) {
-                               String historyToken = event.getValue();
-                               try {
-                                       if(historyToken.equals("Search"))
-                                               inner.selectTab(2);
-                                       else if(historyToken.equals("Groups"))
-                                               inner.selectTab(1);
-                                       else if(historyToken.equals("Files")|| historyToken.length()==0)
-                                               inner.selectTab(0);
-                                               else {
-                                                       GWT.log("****Inside addValueChangeHandler****");
-                                                       GWT.log("historyToken = '" +historyToken+ "'");
-                                                       PopupTree popupTree = GSS.get().getFolders().getPopupTree();
-                                                       TreeItem treeObj = GSS.get().getFolders().getPopupTree().getTreeItem(historyToken);
-                                                       GWT.log("treeObj.getUserObject().toString() = '" +treeObj.getUserObject().toString()+ "'");
-                                                       SelectionEvent.fire(popupTree, treeObj);
-
-//                                                     SelectionEvent.fire(GSS.get().getFolders().getPopupTree(), (TreeItem) getHistoryItem(historyToken));
-                                               }
-                                               } catch (IndexOutOfBoundsException e) {
-                                               inner.selectTab(0);
-                                               }
-                                       }
-                             });
+               History.addValueChangeHandler(new ValueChangeHandler<String>() {
+                       @Override
+                       public void onValueChange(ValueChangeEvent<String> event) {
+                               String tokenInput = event.getValue();
+                               String historyToken = handleSpecialFolderNames(tokenInput);
+                               try {
+                                       if(historyToken.equals("Search"))
+                                               inner.selectTab(2);
+                                       else if(historyToken.equals("Groups"))
+                                               inner.selectTab(1);
+                                       else if(historyToken.equals("Files")|| historyToken.length()==0)
+                                               inner.selectTab(0);
+                                       else {
+                                               /*TODO: CELLTREE
+                                               PopupTree popupTree = GSS.get().getFolders().getPopupTree();
+                                               TreeItem treeObj = GSS.get().getFolders().getPopupTree().getTreeItem(historyToken);
+                                               SelectionEvent.fire(popupTree, treeObj);
+                                               */
+                                       }
+                               } catch (IndexOutOfBoundsException e) {
+                                       inner.selectTab(0);
+                               }
+                       }
+               });
 
                // Add the left and right panels to the split panel.
-               splitPanel.setLeftWidget(folders);
+               splitPanel.setLeftWidget(treeView);
                splitPanel.setRightWidget(inner);
                splitPanel.setSplitPosition("25%");
                splitPanel.setSize("100%", "100%");
                splitPanel.addStyleName("gss-splitPanel");
-
+               
                // Create a dock panel that will contain the menu bar at the top,
                // the shortcuts to the left, the status bar at the bottom and the
                // right panel taking the rest.
@@ -391,6 +341,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                // sizes have been computed by the browser.
                DeferredCommand.addCommand(new Command() {
 
+                       @Override
                        public void execute() {
                                onWindowResized(Window.getClientHeight());
                        }
@@ -408,11 +359,13 @@ public class GSS implements EntryPoint, ResizeHandler {
 
                        @Override
                        public void onComplete() {
+                               
                                currentUserResource = getResult();
                                final String announcement = currentUserResource.getAnnouncement();
                                if (announcement != null)
                                        DeferredCommand.addCommand(new Command() {
 
+                                               @Override
                                                public void execute() {
                                                        displayInformation(announcement);
                                                }
@@ -456,6 +409,7 @@ public class GSS implements EntryPoint, ResizeHandler {
 
                DeferredCommand.addCommand(new Command() {
 
+                       @Override
                        public void execute() {
                                fetchUser(username);
                        }
@@ -501,6 +455,10 @@ public class GSS implements EntryPoint, ResizeHandler {
                if (newHeight < 1)
                        newHeight = 1;
                splitPanel.setHeight("" + newHeight);
+               inner.setHeight("" + newHeight);
+               /*if(isFileListShowing()){
+                       getFileList().setHeight("" + (newHeight-50));
+               }*/
        }
 
        @Override
@@ -544,7 +502,7 @@ public class GSS implements EntryPoint, ResizeHandler {
         * @param update
         */
        public void showFileList(boolean update) {
-               TreeItem currentFolder = getFolders().getCurrent();
+               /*TreeItem currentFolder = getFolders().getCurrent();
                if (currentFolder != null) {
                        List<FileResource> files = null;
                        Object cachedObject = currentFolder.getUserObject();
@@ -557,6 +515,60 @@ public class GSS implements EntryPoint, ResizeHandler {
                        }
                        if (files != null)
                                getFileList().setFiles(files);
+               }*/
+               RestResource currentFolder = getTreeView().getSelection();
+               GWT.log("SELECTED:"+currentFolder);
+               if(currentFolder!=null){
+                       GWT.log("SELECTED:"+currentFolder.getClass());
+                       List<FileResource> files = null;
+                       if (currentFolder instanceof RestResourceWrapper) {
+                               RestResourceWrapper folder = (RestResourceWrapper) currentFolder;
+                               files = folder.getResource().getFiles();
+                       } else if (currentFolder instanceof TrashResource) {
+                               TrashResource folder = (TrashResource) currentFolder;
+                               files = folder.getFiles();
+                       }
+                       else if(currentFolder instanceof OthersResource){
+                               files = new ArrayList<FileResource>();
+                       }
+                       else if(currentFolder instanceof OtherUserResource){
+                               files = ((OtherUserResource)currentFolder).getFiles();
+                       }
+                       if (files != null)
+                               getFileList().setFiles(files);
+               }
+               fileList.updateFileCache(update, true /*clear selection*/);
+               inner.selectTab(0);
+       }
+       
+       public void showFileList(RestResource r,boolean update) {
+               /*TreeItem currentFolder = getFolders().getCurrent();
+               if (currentFolder != null) {
+                       List<FileResource> files = null;
+                       Object cachedObject = currentFolder.getUserObject();
+                       if (cachedObject instanceof FolderResource) {
+                               FolderResource folder = (FolderResource) cachedObject;
+                               files = folder.getFiles();
+                       } else if (cachedObject instanceof TrashResource) {
+                               TrashResource folder = (TrashResource) cachedObject;
+                               files = folder.getFiles();
+                       }
+                       if (files != null)
+                               getFileList().setFiles(files);
+               }*/
+               RestResource currentFolder = r;
+               GWT.log("SELECTED:"+currentFolder);
+               if(currentFolder!=null){
+                       List<FileResource> files = null;
+                       if (currentFolder instanceof RestResourceWrapper) {
+                               RestResourceWrapper folder = (RestResourceWrapper) currentFolder;
+                               files = folder.getResource().getFiles();
+                       } else if (currentFolder instanceof TrashResource) {
+                               TrashResource folder = (TrashResource) currentFolder;
+                               files = folder.getFiles();
+                       }
+                       if (files != null)
+                               getFileList().setFiles(files);
                }
                fileList.updateFileCache(update, true /*clear selection*/);
                inner.selectTab(0);
@@ -639,10 +651,10 @@ public class GSS implements EntryPoint, ResizeHandler {
         * Retrieve the folders.
         *
         * @return the folders
-        */
+        
        public Folders getFolders() {
                return folders;
-       }
+       }*/
 
        /**
         * Retrieve the search.
@@ -724,14 +736,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                return userDetailsPanel;
        }
 
-       /**
-        * Retrieve the dragController.
-        *
-        * @return the dragController
-        */
-       public PickupDragController getDragController() {
-               return dragController;
-       }
+       
 
        public String getToken() {
                return token;
@@ -788,29 +793,101 @@ public class GSS implements EntryPoint, ResizeHandler {
                webDAVPassword = Cookies.getCookie(cookie);
                Cookies.setCookie(cookie, "", null, domain, path, false);
        }
+
        /**
-        * @param key
-        * @return Object of the corresponding String URI which is stored in the history map
+        * Convert server date to local time according to browser timezone
+        * and format it according to localized pattern.
+        * Time is always formatted to 24hr format.
+        * NB: This assumes that server runs in UTC timezone. Otherwise
+        * we would need to adjust for server time offset as well.
+        *
+        * @param date
+        * @return String
         */
-       public Object getHistoryItem(String key){
-               return map.get(key);
+       public static String formatLocalDateTime(Date date) {
+               Date convertedDate = new Date(date.getTime() - date.getTimezoneOffset());
+               final DateTimeFormat dateFormatter = DateTimeFormat.getShortDateFormat();
+               final DateTimeFormat timeFormatter = DateTimeFormat.getFormat("HH:mm");
+               String datePart = dateFormatter.format(convertedDate);
+               String timePart = timeFormatter.format(convertedDate);
+               return datePart + " " + timePart;
        }
-
+       
        /**
-        * Replaces any whitespace in the given string to "+"
-        * Sets a pair of key - object in the History (using a map)
-        * and adds a new browser history entry
+        * History support for folder navigation
+        * adds a new browser history entry
+        *
         * @param key
-        * @param obj
         */
-       public void updateHistory(String key, Object obj){
+       public void updateHistory(String key){
 //             Replace any whitespace of the initial string to "+"
-               String result = key.replaceAll("\\s","+");
-               GWT.log("~~~~Inside GSS.get().updateHistory()~~~~");
-               GWT.log("result = "+result+"'");
-//             Add a new pair key - object in the History map.
-               map.put(result, obj);
+//             String result = key.replaceAll("\\s","+");
 //             Add a new browser history entry.
-               History.newItem(result);
+//             History.newItem(result);
+               History.newItem(key);
+       }
+
+       /**
+        * This method examines the token input and add a "/" at the end in case it's omitted.
+        * This happens only in Files/trash/, Files/shared/, Files/others.
+        *
+        * @param tokenInput
+        * @return the formated token with a "/" at the end or the same tokenInput parameter
+        */
+
+       private String handleSpecialFolderNames(String tokenInput){
+               List<String> pathsToCheck = Arrays.asList("Files/trash", "Files/shared", "Files/others");
+               if(pathsToCheck.contains(tokenInput))
+                       return tokenInput + "/";
+               return tokenInput;
+
+       }
+
+       /**
+        * Reject illegal resource names, like '.' or '..' or slashes '/'.
+        */
+       static boolean isValidResourceName(String name) {
+               if (".".equals(name) || "..".equals(name) || name.contains("/"))
+                       return false;
+               return true;
+       }
+
+       public void putUserToMap(String _userName, String _userFullName){
+               userFullNameMap.put(_userName, _userFullName);
+       }
+
+       public String findUserFullName(String _userName){
+               return userFullNameMap.get(_userName);
+       }
+       public String getUserFullName(String _userName) {
+               
+        if (GSS.get().findUserFullName(_userName) == null)
+                //if there is no userFullName found then the map fills with the given _userName,
+                //so userFullName = _userName
+                GSS.get().putUserToMap(_userName, _userName);
+        else if(GSS.get().findUserFullName(_userName).indexOf('@') != -1){
+                //if the userFullName = _userName the GetUserCommand updates the userFullName in the map
+                GetUserCommand guc = new GetUserCommand(_userName);
+                guc.execute();
+        }
+        return GSS.get().findUserFullName(_userName);
+       }
+       /**
+        * Retrieve the treeView.
+        *
+        * @return the treeView
+        */
+       public CellTreeView getTreeView() {
+               return treeView;
+       }
+       
+       public void onResourceUpdate(RestResource resource){
+               if(resource instanceof RestResourceWrapper){
+                       if(getTreeView().getSelection()!=null&&getTreeView().getSelection().getUri().equals(resource.getUri()))
+                               showFileList(resource,true);
+               }
+               
        }
+       
+       
 }