X-Git-Url: https://code.grnet.gr/git/pithos/blobdiff_plain/afd3a0ef323903ab769574e0871fc55e87df609f..75665dbfe4d30298ec2ca29f93f0a490b449c83b:/src/gr/ebs/gss/client/FileMenu.java diff --git a/src/gr/ebs/gss/client/FileMenu.java b/src/gr/ebs/gss/client/FileMenu.java index c486447..1ed1f0d 100644 --- a/src/gr/ebs/gss/client/FileMenu.java +++ b/src/gr/ebs/gss/client/FileMenu.java @@ -1,193 +1,223 @@ -/* - * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd. - * - * This file is part of GSS. - * - * GSS is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GSS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GSS. If not, see . - */ -package gr.ebs.gss.client; - -import gr.ebs.gss.client.commands.EmptyTrashCommand; -import gr.ebs.gss.client.commands.NewFolderCommand; -import gr.ebs.gss.client.commands.PropertiesCommand; -import gr.ebs.gss.client.commands.RefreshCommand; -import gr.ebs.gss.client.commands.UploadFileCommand; -import gr.ebs.gss.client.rest.RestCommand; -import gr.ebs.gss.client.rest.resource.FileResource; -import gr.ebs.gss.client.rest.resource.GroupUserResource; - -import java.util.List; - -import com.google.gwt.event.dom.client.ClickEvent; -import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.http.client.URL; -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.ui.AbstractImagePrototype; -import com.google.gwt.user.client.ui.MenuBar; -import com.google.gwt.user.client.ui.PopupPanel; -import com.google.gwt.user.client.ui.TreeItem; - -/** - * The 'File' menu implementation. - */ -public class FileMenu extends PopupPanel implements ClickHandler { - - /** - * The widget's images. - */ - private final Images images; - - /** - * An image bundle for this widgets images. - */ - public interface Images extends ClientBundle,FilePropertiesDialog.Images { - - @Source("gr/ebs/gss/resources/folder_new.png") - ImageResource folderNew(); - - @Source("gr/ebs/gss/resources/folder_outbox.png") - ImageResource fileUpdate(); - - @Source("gr/ebs/gss/resources/view_text.png") - ImageResource viewText(); - - @Source("gr/ebs/gss/resources/folder_inbox.png") - ImageResource download(); - - @Source("gr/ebs/gss/resources/trashcan_empty.png") - ImageResource emptyTrash(); - - @Source("gr/ebs/gss/resources/internet.png") - ImageResource sharing(); - - @Source("gr/ebs/gss/resources/refresh.png") - ImageResource refresh(); -} - - final MenuBar contextMenu = new MenuBar(true); - - /** - * The widget's constructor. - * - * @param _images the image bundle passed on by the parent object - */ - public FileMenu(final Images _images) { - // The popup's constructor's argument is a boolean specifying that it - // auto-close itself when the user clicks outside of it. - super(true); - setAnimationEnabled(true); - images = _images; - add(contextMenu); - - } - - @Override - public void onClick(ClickEvent event) { - final FileMenu menu = new FileMenu(images); - final int left = event.getRelativeElement().getAbsoluteLeft(); - final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight(); - menu.setPopupPosition(left, top); - menu.show(); - - } - - - /** - * Do some validation before downloading a file. - */ - void preDownloadCheck() { - Object selection = GSS.get().getCurrentSelection(); - if (selection == null || !(selection instanceof FileResource)) { - GSS.get().displayError("You have to select a file first"); - return; - } - } - - /** - * Create a download link for the respective menu item, if the currently - * selected object is a file. - * - * @param link a String array with two elements that is modified so that the - * first position contains the opening tag and the second one the - * closing tag - * @param forceDownload If true, link will be such that browser should ask for filename - * and save location - */ - void createDownloadLink(String[] link, boolean forceDownload) { - String downloadURL = getDownloadURL(); - if (!downloadURL.isEmpty()) { - link[0] = ""; - link[1] = ""; - } - } - - public String getDownloadURL() { - GSS app = GSS.get(); - Object selection = app.getCurrentSelection(); - if (selection != null && selection instanceof FileResource) { - FileResource file = (FileResource) selection; - return getDownloadURL(file); - } - return ""; - } - - public String getDownloadURL(FileResource file) { - GSS app = GSS.get(); - if (file != null) { - String dateString = RestCommand.getDate(); - String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length()); - String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken())); - return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString); - } - return ""; - } - - public MenuBar createMenu() { - contextMenu.clearItems(); - contextMenu.setAutoOpen(false); - final Command downloadCmd = new Command() { - - public void execute() { - hide(); - preDownloadCheck(); - } - }; - Folders folders = GSS.get().getFolders(); - TreeItem selectedItem = folders.getCurrent(); - boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource; - boolean propertiesVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource || GSS.get().getCurrentSelection() instanceof List)); - contextMenu.addItem("" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder", true, new NewFolderCommand(this, images)); - contextMenu.addItem("" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload", true, new UploadFileCommand(this)); - if (downloadVisible) { - String[] link = {"", ""}; - createDownloadLink(link, false); - contextMenu.addItem("" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "", true, downloadCmd); - createDownloadLink(link, true); - contextMenu.addItem("" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "", true, downloadCmd); - } - contextMenu.addItem("" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash", true, new EmptyTrashCommand(this)); - contextMenu.addItem("" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh", true, new RefreshCommand(this, images)); - contextMenu.addItem("" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing", true, new PropertiesCommand(this, images, 1)) - .setVisible(propertiesVisible); - contextMenu.addItem("" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties", true, new PropertiesCommand(this, images, 0)) - .setVisible(propertiesVisible); - return contextMenu; - } - - - -} +/* + * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd. + * + * This file is part of GSS. + * + * GSS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GSS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GSS. If not, see . + */ +package gr.ebs.gss.client; + +import gr.ebs.gss.client.commands.EmptyTrashCommand; +import gr.ebs.gss.client.commands.NewFolderCommand; +import gr.ebs.gss.client.commands.PropertiesCommand; +import gr.ebs.gss.client.commands.RefreshCommand; +import gr.ebs.gss.client.commands.UploadFileCommand; +import gr.ebs.gss.client.rest.RestCommand; +import gr.ebs.gss.client.rest.resource.FileResource; +import gr.ebs.gss.client.rest.resource.GroupUserResource; +import gr.ebs.gss.client.rest.resource.RestResource; + +import java.util.List; + +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.http.client.URL; +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.ui.AbstractImagePrototype; +import com.google.gwt.user.client.ui.MenuBar; +import com.google.gwt.user.client.ui.MenuItem; +import com.google.gwt.user.client.ui.PopupPanel; +import com.google.gwt.user.client.ui.TreeItem; + +/** + * The 'File' menu implementation. + */ +public class FileMenu extends PopupPanel implements ClickHandler { + + /** + * The widget's images. + */ + private final Images images; + + /** + * An image bundle for this widgets images. + */ + public interface Images extends ClientBundle,FilePropertiesDialog.Images { + + @Source("gr/ebs/gss/resources/folder_new.png") + ImageResource folderNew(); + + @Source("gr/ebs/gss/resources/folder_outbox.png") + ImageResource fileUpdate(); + + @Source("gr/ebs/gss/resources/view_text.png") + ImageResource viewText(); + + @Override + @Source("gr/ebs/gss/resources/folder_inbox.png") + ImageResource download(); + + @Source("gr/ebs/gss/resources/trashcan_empty.png") + ImageResource emptyTrash(); + + @Source("gr/ebs/gss/resources/internet.png") + ImageResource sharing(); + + @Source("gr/ebs/gss/resources/refresh.png") + ImageResource refresh(); +} + + final MenuBar contextMenu = new MenuBar(true); + + /** + * The widget's constructor. + * + * @param _images the image bundle passed on by the parent object + */ + public FileMenu(final Images _images) { + // The popup's constructor's argument is a boolean specifying that it + // auto-close itself when the user clicks outside of it. + super(true); + setAnimationEnabled(true); + images = _images; + add(contextMenu); + + } + + @Override + public void onClick(ClickEvent event) { + final FileMenu menu = new FileMenu(images); + final int left = event.getRelativeElement().getAbsoluteLeft(); + final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight(); + menu.setPopupPosition(left, top); + menu.show(); + + } + + + /** + * Do some validation before downloading a file. + */ + void preDownloadCheck() { + Object selection = GSS.get().getCurrentSelection(); + if (selection == null || !(selection instanceof FileResource)) { + GSS.get().displayError("You have to select a file first"); + return; + } + } + + /** + * Create a download link for the respective menu item, if the currently + * selected object is a file. + * + * @param link a String array with two elements that is modified so that the + * first position contains the opening tag and the second one the + * closing tag + * @param forceDownload If true, link will be such that browser should ask for filename + * and save location + */ + void createDownloadLink(String[] link, boolean forceDownload) { + String downloadURL = getDownloadURL(); + if (!downloadURL.isEmpty()) { + link[0] = ""; + link[1] = ""; + } + } + + public String getDownloadURL() { + GSS app = GSS.get(); + Object selection = app.getCurrentSelection(); + if (selection != null && selection instanceof FileResource) { + FileResource file = (FileResource) selection; + return getDownloadURL(file); + } + return ""; + } + + public String getDownloadURL(FileResource file) { + GSS app = GSS.get(); + if (file != null) { + String dateString = RestCommand.getDate(); + String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length()); + String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken())); + return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString); + } + return ""; + } + + public MenuBar createMenu() { + contextMenu.clearItems(); + contextMenu.setAutoOpen(false); + final Command downloadCmd = new Command() { + + @Override + public void execute() { + hide(); + preDownloadCheck(); + } + }; + //TODO: CELLTREE + RestResource selectedItem = GSS.get().getTreeView().getSelection(); + boolean downloadVisible = true;//GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource; + boolean propertiesVisible = true;//!(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource || GSS.get().getCurrentSelection() instanceof List)); + boolean newFolderVisible = true;//!(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem))); + boolean uploadVisible = true;//!(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem)|| folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem))); + if(newFolderVisible){ + MenuItem newFolderItem = new MenuItem("" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder", true, new NewFolderCommand(this, images)); + newFolderItem.getElement().setId("topMenu.file.newFolder"); + contextMenu.addItem(newFolderItem); + } + if(uploadVisible){ + MenuItem uploadItem = new MenuItem("" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload", true, new UploadFileCommand(this)); + uploadItem.getElement().setId("topMenu.file.upload"); + contextMenu.addItem(uploadItem); + } + if (downloadVisible) { + String[] link = {"", ""}; + createDownloadLink(link, false); + + MenuItem downloadItem = new MenuItem("" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "", true, downloadCmd); + contextMenu.addItem(downloadItem); + downloadItem.getElement().setId("topMenu.file.download"); + + createDownloadLink(link, true); + + MenuItem saveAsItem = new MenuItem("" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "", true, downloadCmd); + saveAsItem.getElement().setId("topMenu.file.saveAs"); + contextMenu.addItem(saveAsItem); + } + MenuItem emptyTrashItem = new MenuItem("" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash", true, new EmptyTrashCommand(this)); + emptyTrashItem.getElement().setId("topMenu.file.emptyTrash"); + contextMenu.addItem(emptyTrashItem); + + MenuItem refreshItem = new MenuItem("" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh", true, new RefreshCommand(this, images)); + refreshItem.getElement().setId("topMenu.file.refresh"); + contextMenu.addItem(refreshItem); + + MenuItem sharingItem = new MenuItem("" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing", true, new PropertiesCommand(this, images, 1)); + sharingItem.getElement().setId("topMenu.file.sharing"); + contextMenu.addItem(sharingItem) + .setVisible(propertiesVisible); + + MenuItem propertiesItem = new MenuItem("" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties", true, new PropertiesCommand(this, images, 0)); + propertiesItem.getElement().setId("topMenu.file.properties"); + contextMenu.addItem(propertiesItem) + .setVisible(propertiesVisible); + return contextMenu; + } + +}