Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.7 kB)

1
/*
2
 * Copyright 2007, 2008, 2009, 2010 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.GroupUserResource;
29

    
30
import java.util.List;
31

    
32
import com.google.gwt.event.dom.client.ClickEvent;
33
import com.google.gwt.event.dom.client.ClickHandler;
34
import com.google.gwt.http.client.URL;
35
import com.google.gwt.resources.client.ClientBundle;
36
import com.google.gwt.resources.client.ImageResource;
37
import com.google.gwt.user.client.Command;
38
import com.google.gwt.user.client.ui.AbstractImagePrototype;
39
import com.google.gwt.user.client.ui.MenuBar;
40
import com.google.gwt.user.client.ui.PopupPanel;
41
import com.google.gwt.user.client.ui.TreeItem;
42

    
43
/**
44
 * The 'File' menu implementation.
45
 */
46
public class FileMenu extends PopupPanel implements ClickHandler {
47

    
48
        /**
49
         * The widget's images.
50
         */
51
        private final Images images;
52

    
53
        /**
54
         * An image bundle for this widgets images.
55
         */
56
        public interface Images extends ClientBundle,FilePropertiesDialog.Images {
57

    
58
                @Source("gr/ebs/gss/resources/folder_new.png")
59
                ImageResource folderNew();
60

    
61
                @Source("gr/ebs/gss/resources/folder_outbox.png")
62
                ImageResource fileUpdate();
63

    
64
                @Source("gr/ebs/gss/resources/view_text.png")
65
                ImageResource viewText();
66

    
67
                @Source("gr/ebs/gss/resources/folder_inbox.png")
68
                ImageResource download();
69

    
70
                @Source("gr/ebs/gss/resources/trashcan_empty.png")
71
                ImageResource emptyTrash();
72

    
73
                @Source("gr/ebs/gss/resources/internet.png")
74
                ImageResource sharing();
75

    
76
                @Source("gr/ebs/gss/resources/refresh.png")
77
                ImageResource refresh();
78
}
79

    
80
        final MenuBar contextMenu = new MenuBar(true);
81

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

    
95
        }
96

    
97
        @Override
98
        public void onClick(ClickEvent event) {
99
                final FileMenu menu = new FileMenu(images);
100
                final int left = event.getRelativeElement().getAbsoluteLeft();
101
                final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
102
                menu.setPopupPosition(left, top);
103
                menu.show();
104

    
105
        }
106

    
107

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

    
119
        /**
120
         * Create a download link for the respective menu item, if the currently
121
         * selected object is a file.
122
         *
123
         * @param link a String array with two elements that is modified so that the
124
         *            first position contains the opening tag and the second one the
125
         *            closing tag
126
         * @param forceDownload If true, link will be such that browser should ask for filename
127
         *                                 and save location
128
         */
129
        void createDownloadLink(String[] link, boolean forceDownload) {
130
                String downloadURL = getDownloadURL();
131
                if (!downloadURL.isEmpty()) {
132
                        link[0] = "<a class='hidden-link' href='" + downloadURL
133
                                        + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
134
                        link[1] = "</a>";
135
                }
136
        }
137

    
138
        public String getDownloadURL() {
139
                GSS app = GSS.get();
140
                Object selection = app.getCurrentSelection();
141
                if (selection != null && selection instanceof FileResource) {
142
                        FileResource file = (FileResource) selection;
143
                        return getDownloadURL(file);
144
                }
145
                return "";
146
        }
147

    
148
        public String getDownloadURL(FileResource file) {
149
                GSS app = GSS.get();
150
                if (file != null) {
151
                        String dateString = RestCommand.getDate();
152
                        String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
153
                        String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
154
                        return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
155
                }
156
                return "";
157
        }
158

    
159
        public MenuBar createMenu() {
160
                contextMenu.clearItems();
161
                contextMenu.setAutoOpen(false);
162
                final Command downloadCmd = new Command() {
163

    
164
                        public void execute() {
165
                                hide();
166
                                preDownloadCheck();
167
                        }
168
                };
169
                Folders folders = GSS.get().getFolders();
170
                TreeItem selectedItem = folders.getCurrent();
171
                boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
172
                boolean propertiesVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource || GSS.get().getCurrentSelection() instanceof List));
173
                boolean newFolderVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
174
                boolean uploadVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
175
                if(newFolderVisible)
176
                        contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));
177
                if(uploadVisible)
178
                        contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
179
                if (downloadVisible) {
180
                        String[] link = {"", ""};
181
                        createDownloadLink(link, false);
182
                        contextMenu.addItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
183
                        createDownloadLink(link, true);
184
                        contextMenu.addItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Save As" + link[1] + "</span>", true, downloadCmd);
185
                }
186
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
187
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
188
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1))
189
                                           .setVisible(propertiesVisible);
190
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0))
191
                                           .setVisible(propertiesVisible);
192
                return contextMenu;
193
        }
194

    
195

    
196

    
197
}