Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.2 kB)

1
/*
2
 * Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client;
5

    
6
import gr.grnet.pithos.web.client.commands.EmptyTrashCommand;
7
import gr.grnet.pithos.web.client.commands.NewFolderCommand;
8
import gr.grnet.pithos.web.client.commands.PropertiesCommand;
9
import gr.grnet.pithos.web.client.commands.RefreshCommand;
10
import gr.grnet.pithos.web.client.commands.UploadFileCommand;
11
import gr.grnet.pithos.web.client.rest.RestCommand;
12
import gr.grnet.pithos.web.client.rest.resource.FileResource;
13
import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
14
import gr.grnet.pithos.web.client.rest.resource.OthersResource;
15
import gr.grnet.pithos.web.client.rest.resource.RestResource;
16
import gr.grnet.pithos.web.client.rest.resource.SharedResource;
17
import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
18
import gr.grnet.pithos.web.client.rest.resource.TrashResource;
19

    
20
import java.util.List;
21

    
22
import com.google.gwt.event.dom.client.ClickEvent;
23
import com.google.gwt.event.dom.client.ClickHandler;
24
import com.google.gwt.http.client.URL;
25
import com.google.gwt.resources.client.ClientBundle;
26
import com.google.gwt.resources.client.ImageResource;
27
import com.google.gwt.user.client.Command;
28
import com.google.gwt.user.client.ui.AbstractImagePrototype;
29
import com.google.gwt.user.client.ui.MenuBar;
30
import com.google.gwt.user.client.ui.MenuItem;
31
import com.google.gwt.user.client.ui.PopupPanel;
32

    
33
/**
34
 * The 'File' menu implementation.
35
 */
36
public class FileMenu extends PopupPanel implements ClickHandler {
37

    
38
        /**
39
         * The widget's images.
40
         */
41
        private final Images images;
42

    
43
        /**
44
         * An image bundle for this widgets images.
45
         */
46
        public interface Images extends ClientBundle,FilePropertiesDialog.Images {
47

    
48
                @Source("gr/grnet/pithos/resources/folder_new.png")
49
                ImageResource folderNew();
50

    
51
                @Source("gr/grnet/pithos/resources/folder_outbox.png")
52
                ImageResource fileUpdate();
53

    
54
                @Source("gr/grnet/pithos/resources/view_text.png")
55
                ImageResource viewText();
56

    
57
                @Override
58
                @Source("gr/grnet/pithos/resources/folder_inbox.png")
59
                ImageResource download();
60

    
61
                @Source("gr/grnet/pithos/resources/trashcan_empty.png")
62
                ImageResource emptyTrash();
63

    
64
                @Source("gr/grnet/pithos/resources/internet.png")
65
                ImageResource sharing();
66

    
67
                @Source("gr/grnet/pithos/resources/refresh.png")
68
                ImageResource refresh();
69
}
70

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

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

    
86
        }
87

    
88
        @Override
89
        public void onClick(ClickEvent event) {
90
                final FileMenu menu = new FileMenu(images);
91
                final int left = event.getRelativeElement().getAbsoluteLeft();
92
                final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
93
                menu.setPopupPosition(left, top);
94
                menu.show();
95

    
96
        }
97

    
98

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

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

    
129
        public String getDownloadURL() {
130
                GSS app = GSS.get();
131
                Object selection = app.getCurrentSelection();
132
                if (selection != null && selection instanceof FileResource) {
133
                        FileResource file = (FileResource) selection;
134
                        return getDownloadURL(file);
135
                }
136
                return "";
137
        }
138

    
139
        public String getDownloadURL(FileResource file) {
140
                GSS app = GSS.get();
141
                if (file != null) {
142
                        return file.getUri();
143
                }
144
                return "";
145
        }
146

    
147
        public MenuBar createMenu() {
148
                contextMenu.clearItems();
149
                contextMenu.setAutoOpen(false);
150
                final Command downloadCmd = new Command() {
151

    
152
                        @Override
153
                        public void execute() {
154
                                hide();
155
                                preDownloadCheck();
156
                        }
157
                };
158
        CellTreeView treeView = GSS.get().getTreeView();
159
        if (treeView == null)
160
            return contextMenu;
161
                RestResource selectedItem = treeView.getSelection();
162
                boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
163
                boolean propertiesVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource 
164
                                        //|| folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource 
165
                                        || GSS.get().getCurrentSelection() instanceof List));
166
                boolean newFolderVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
167
                boolean uploadVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
168
                if(newFolderVisible){
169
                        MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));
170
                        newFolderItem.getElement().setId("topMenu.file.newFolder");
171
                        contextMenu.addItem(newFolderItem);                        
172
                }
173
                if(uploadVisible){
174
                        MenuItem uploadItem = new MenuItem("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));                        
175
                        contextMenu.addItem(uploadItem);
176
                }
177
                if (downloadVisible) {
178
                        String[] link = {"", ""};
179
                        createDownloadLink(link, false);
180
                        
181
                        MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
182
                        contextMenu.addItem(downloadItem);
183
                        
184
                        createDownloadLink(link, true);
185
                        
186
                        MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Save As" + link[1] + "</span>", true, downloadCmd);                        
187
                        contextMenu.addItem(saveAsItem);
188
                }
189
                MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
190
                emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
191
                contextMenu.addItem(emptyTrashItem);
192
                
193
                MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
194
                refreshItem.getElement().setId("topMenu.file.refresh");
195
                contextMenu.addItem(refreshItem);
196
                
197
                MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1));
198
                sharingItem.getElement().setId("topMenu.file.sharing");
199
                contextMenu.addItem(sharingItem)
200
                                           .setVisible(propertiesVisible);
201
                
202
                MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0));
203
                propertiesItem.getElement().setId("topMenu.file.properties");
204
                contextMenu.addItem(propertiesItem)
205
                                           .setVisible(propertiesVisible);
206
                return contextMenu;
207
        }
208

    
209
}