Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / FileMenu.java @ ea517aad

History | View | Annotate | Download (6.5 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.commands.EmptyTrashCommand;
22
import gr.ebs.gss.client.commands.NewFolderCommand;
23
import gr.ebs.gss.client.commands.PropertiesCommand;
24
import gr.ebs.gss.client.commands.RefreshCommand;
25
import gr.ebs.gss.client.commands.UploadFileCommand;
26
import gr.ebs.gss.client.rest.RestCommand;
27
import gr.ebs.gss.client.rest.resource.FileResource;
28
import gr.ebs.gss.client.rest.resource.FolderResource;
29
import gr.ebs.gss.client.rest.resource.GroupUserResource;
30

    
31
import com.google.gwt.http.client.URL;
32
import com.google.gwt.user.client.Command;
33
import com.google.gwt.user.client.ui.AbstractImagePrototype;
34
import com.google.gwt.user.client.ui.ClickListener;
35
import com.google.gwt.user.client.ui.MenuBar;
36
import com.google.gwt.user.client.ui.PopupPanel;
37
import com.google.gwt.user.client.ui.TreeItem;
38
import com.google.gwt.user.client.ui.Widget;
39

    
40
/**
41
 * The 'File' menu implementation.
42
 */
43
public class FileMenu extends PopupPanel implements ClickListener {
44

    
45
        /**
46
         * The widget's images.
47
         */
48
        private final Images images;
49

    
50
        /**
51
         * An image bundle for this widgets images.
52
         */
53
        public interface Images extends FilePropertiesDialog.Images {
54

    
55
                @Resource("gr/ebs/gss/resources/folder_new.png")
56
                AbstractImagePrototype folderNew();
57

    
58
                @Resource("gr/ebs/gss/resources/folder_outbox.png")
59
                AbstractImagePrototype fileUpdate();
60

    
61
                @Resource("gr/ebs/gss/resources/view_text.png")
62
                AbstractImagePrototype viewText();
63

    
64
                @Resource("gr/ebs/gss/resources/folder_inbox.png")
65
                AbstractImagePrototype download();
66

    
67
                @Resource("gr/ebs/gss/resources/trashcan_empty.png")
68
                AbstractImagePrototype emptyTrash();
69

    
70
                @Resource("gr/ebs/gss/resources/internet.png")
71
                AbstractImagePrototype sharing();
72

    
73
                @Resource("gr/ebs/gss/resources/refresh.png")
74
                AbstractImagePrototype refresh();
75
}
76

    
77
        final MenuBar contextMenu = new MenuBar(true);
78

    
79
        /**
80
         * The widget's constructor.
81
         *
82
         * @param _images the image bundle passed on by the parent object
83
         */
84
        public FileMenu(final Images _images) {
85
                // The popup's constructor's argument is a boolean specifying that it
86
                // auto-close itself when the user clicks outside of it.
87
                super(true);
88
                setAnimationEnabled(true);
89
                images = _images;
90
                add(contextMenu);
91

    
92
        }
93

    
94
        public void onClick(final Widget sender) {
95
                final FileMenu menu = new FileMenu(images);
96
                final int left = sender.getAbsoluteLeft();
97
                final int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
98
                menu.setPopupPosition(left, top);
99

    
100
                menu.show();
101
        }
102

    
103
        /**
104
         * Do some validation before downloading a file.
105
         */
106
        void preDownloadCheck() {
107
                Object selection = GSS.get().getCurrentSelection();
108
                if (selection == null || !(selection instanceof FileResource)) {
109
                        GSS.get().displayError("You have to select a file first");
110
                        return;
111
                }
112
        }
113

    
114
        /**
115
         * Create a download link for the respective menu item, if the currently
116
         * selected object is a file.
117
         *
118
         * @param link a String array with two elements that is modified so that the
119
         *            first position contains the opening tag and the second one the
120
         *            closing tag
121
         */
122
        void createDownloadLink(String[] link) {
123
                GSS app = GSS.get();
124
                Object selection = app.getCurrentSelection();
125
                if (selection != null && selection instanceof FileResource) {
126
                        FileResource file = (FileResource) selection;
127
                        String dateString = RestCommand.getDate();
128
                        String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
129
                        String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
130
                        link[0] = "<a class='hidden-link' href='" + file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString) + "' target='_blank'>";
131
                        link[1] = "</a>";
132
                }
133
        }
134

    
135
        public MenuBar createMenu() {
136
                contextMenu.clearItems();
137
                contextMenu.setAutoOpen(false);
138
                final Command downloadCmd = new Command() {
139

    
140
                        public void execute() {
141
                                hide();
142
                                preDownloadCheck();
143
                        }
144
                };
145
                Folders folders = GSS.get().getFolders();
146
                TreeItem selectedItem = folders.getCurrent();
147
                boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
148
                boolean uploadVisible = GSS.get().getFolders().getCurrent().getUserObject() instanceof FolderResource;
149
                boolean propertiesNotVisible = selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource);
150
                contextMenu.addItem("<span>" + images.folderNew().getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));
151
                if (uploadVisible) contextMenu.addItem("<span>" + images.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));
152
                if (downloadVisible) {
153
                        String[] link = {"", ""};
154
                        createDownloadLink(link);
155
                        contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
156
                }
157
                contextMenu.addItem("<span>" + images.emptyTrash().getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
158
                contextMenu.addItem("<span>" + images.refresh().getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
159
                contextMenu.addItem("<span>" + images.sharing().getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1))
160
                                           .setVisible(!propertiesNotVisible);
161
                contextMenu.addItem("<span>" + images.viewText().getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0))
162
                                           .setVisible(!propertiesNotVisible);
163
                return contextMenu;
164
        }
165

    
166
}