Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FileMenu.java @ fad48f0d

History | View | Annotate | Download (7.4 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.commands.EmptyTrashCommand;
38
import gr.grnet.pithos.web.client.commands.NewFolderCommand;
39
import gr.grnet.pithos.web.client.commands.PropertiesCommand;
40
import gr.grnet.pithos.web.client.commands.RefreshCommand;
41
import gr.grnet.pithos.web.client.commands.UploadFileCommand;
42
import gr.grnet.pithos.web.client.foldertree.File;
43
import gr.grnet.pithos.web.client.foldertree.Folder;
44
import gr.grnet.pithos.web.client.rest.resource.FileResource;
45

    
46
import java.util.List;
47

    
48
import com.google.gwt.event.dom.client.ClickEvent;
49
import com.google.gwt.event.dom.client.ClickHandler;
50
import com.google.gwt.resources.client.ClientBundle;
51
import com.google.gwt.resources.client.ImageResource;
52
import com.google.gwt.user.client.Command;
53
import com.google.gwt.user.client.ui.AbstractImagePrototype;
54
import com.google.gwt.user.client.ui.MenuBar;
55
import com.google.gwt.user.client.ui.MenuItem;
56

    
57
/**
58
 * The 'File' menu implementation.
59
 */
60
public class FileMenu extends MenuBar {
61

    
62
        /**
63
         * The widget's images.
64
         */
65
        private final Images images;
66

    
67
        /**
68
         * An image bundle for this widgets images.
69
         */
70
        public interface Images extends ClientBundle,FilePropertiesDialog.Images {
71

    
72
                @Source("gr/grnet/pithos/resources/folder_new.png")
73
                ImageResource folderNew();
74

    
75
                @Source("gr/grnet/pithos/resources/folder_outbox.png")
76
                ImageResource fileUpdate();
77

    
78
                @Source("gr/grnet/pithos/resources/view_text.png")
79
                ImageResource viewText();
80

    
81
                @Override
82
                @Source("gr/grnet/pithos/resources/folder_inbox.png")
83
                ImageResource download();
84

    
85
                @Source("gr/grnet/pithos/resources/trashcan_empty.png")
86
                ImageResource emptyTrash();
87

    
88
                @Source("gr/grnet/pithos/resources/internet.png")
89
                ImageResource sharing();
90

    
91
                @Source("gr/grnet/pithos/resources/refresh.png")
92
                ImageResource refresh();
93
}
94

    
95
        final MenuBar contextMenu = new MenuBar(true);
96

    
97
        /**
98
         * The widget's constructor.
99
         *
100
         * @param _images the image bundle passed on by the parent object
101
         */
102
        public FileMenu(GSS _app, final Images _images) {
103
                setAnimationEnabled(true);
104
                images = _images;
105

    
106
        final Command downloadCmd = new Command() {
107

    
108
            @Override
109
            public void execute() {
110
                preDownloadCheck();
111
            }
112
        };
113

    
114
        Folder selectedFolder = _app.getFolderTreeView().getSelection();
115
        List<File> selectedFiles = _app.getFileList().getSelectedFiles();
116
        if (selectedFolder != null) {
117
                    MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(null, selectedFolder, images));
118
                    addItem(newFolderItem);
119

    
120
//            MenuItem uploadItem = new MenuItem("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, null));
121
//            addItem(uploadItem);
122
        }
123
        if (selectedFiles.size() == 1) {
124
//            String[] link = {"", ""};
125
//            createDownloadLink(link, false);
126
//
127
//            MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
128
//            addItem(downloadItem);
129
        }
130

    
131
//        MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
132
//        emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
133
//        contextMenu.addItem(emptyTrashItem);
134

    
135
//        MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
136
//        refreshItem.getElement().setId("topMenu.file.refresh");
137
//        contextMenu.addItem(refreshItem);
138

    
139
//        MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1));
140
//        sharingItem.getElement().setId("topMenu.file.sharing");
141
//        contextMenu.addItem(sharingItem)
142
//                       .setVisible(propertiesVisible);
143
//
144
//        MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0));
145
//        propertiesItem.getElement().setId("topMenu.file.properties");
146
//        contextMenu.addItem(propertiesItem)
147
//                       .setVisible(propertiesVisible);
148
        }
149

    
150
        /**
151
         * Do some validation before downloading a file.
152
         */
153
        void preDownloadCheck() {
154
                Object selection = GSS.get().getCurrentSelection();
155
                if (selection == null || !(selection instanceof FileResource)) {
156
                        GSS.get().displayError("You have to select a file first");
157
                        return;
158
                }
159
        }
160

    
161
        /**
162
         * Create a download link for the respective menu item, if the currently
163
         * selected object is a file.
164
         *
165
         * @param link a String array with two elements that is modified so that the
166
         *            first position contains the opening tag and the second one the
167
         *            closing tag
168
         * @param forceDownload If true, link will be such that browser should ask for filename
169
         *                                 and save location
170
         */
171
        void createDownloadLink(String[] link, boolean forceDownload) {
172
                String downloadURL = getDownloadURL();
173
                if (!downloadURL.isEmpty()) {
174
                        link[0] = "<a class='hidden-link' href='" + downloadURL
175
                                        + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
176
                        link[1] = "</a>";
177
                }
178
        }
179

    
180
        public String getDownloadURL() {
181
                GSS app = GSS.get();
182
                Object selection = app.getCurrentSelection();
183
                if (selection != null && selection instanceof FileResource) {
184
                        FileResource file = (FileResource) selection;
185
                        return getDownloadURL(file);
186
                }
187
                return "";
188
        }
189

    
190
        public String getDownloadURL(FileResource file) {
191
                GSS app = GSS.get();
192
                if (file != null) {
193
                        return file.getUri();
194
                }
195
                return "";
196
        }
197

    
198
}