Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / FileMenu.java @ 555e8e59

History | View | Annotate | Download (6.1 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.UploadFileCommand;
25
import gr.ebs.gss.client.rest.AbstractRestCommand;
26
import gr.ebs.gss.client.rest.resource.FileResource;
27
import gr.ebs.gss.client.rest.resource.GroupUserResource;
28

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

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

    
43
        /**
44
         * The widget's images.
45
         */
46
        private final Images images;
47

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

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

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

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

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

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

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

    
72
        final MenuBar contextMenu = new MenuBar(true);
73

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

    
87
        }
88

    
89
        public void onClick(final Widget sender) {
90
                final FileMenu menu = new FileMenu(images);
91
                final int left = sender.getAbsoluteLeft();
92
                final int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
93
                menu.setPopupPosition(left, top);
94

    
95
                menu.show();
96
        }
97

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

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

    
129
        public MenuBar createMenu() {
130
                contextMenu.clearItems();
131
                contextMenu.setAutoOpen(false);
132
                final Command downloadCmd = new Command() {
133

    
134
                        public void execute() {
135
                                hide();
136
                                preDownloadCheck();
137
                        }
138
                };
139
                Folders folders = GSS.get().getFolders();
140
                TreeItem selectedItem = folders.getCurrent();
141
                boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
142
                boolean propertiesNotVisible = selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource);
143
                contextMenu.addItem("<span>" + images.folderNew().getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));
144
                contextMenu.addItem("<span>" + images.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));
145
                if (downloadVisible) {
146
                        String[] link = {"", ""};
147
                        createDownloadLink(link);
148
                        contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
149
                }
150
                contextMenu.addItem("<span>" + images.emptyTrash().getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
151

    
152
                contextMenu.addItem("<span>" + images.sharing().getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1))
153
                                           .setVisible(!propertiesNotVisible);
154
                contextMenu.addItem("<span>" + images.viewText().getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0))
155
                                           .setVisible(!propertiesNotVisible);
156
                return contextMenu;
157
        }
158

    
159
}