2 * Copyright 2011 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
21 * Copyright 2008, 2009 Electronic Business Systems Ltd.
23 * This file is part of GSS.
25 * GSS is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * GSS is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
39 import gr.ebs.gss.client.CellTreeView;
40 import gr.ebs.gss.client.GSS;
41 import gr.ebs.gss.client.rest.MultiplePostCommand;
42 import gr.ebs.gss.client.rest.PostCommand;
43 import gr.ebs.gss.client.rest.RestException;
44 import gr.ebs.gss.client.rest.resource.FileResource;
45 import gr.ebs.gss.client.rest.resource.FolderResource;
46 import gr.ebs.gss.client.rest.resource.RestResource;
48 import java.util.ArrayList;
49 import java.util.List;
51 import com.google.gwt.core.client.GWT;
52 import com.google.gwt.http.client.URL;
53 import com.google.gwt.user.client.Command;
54 import com.google.gwt.user.client.DeferredCommand;
55 import com.google.gwt.user.client.ui.AbstractImagePrototype;
56 import com.google.gwt.user.client.ui.MenuBar;
57 import com.google.gwt.user.client.ui.PopupPanel;
58 import com.google.gwt.user.client.ui.TreeItem;
63 public class DnDFolderPopupMenu extends PopupPanel {
65 public DnDFolderPopupMenu(final CellTreeView.Images newImages, final FolderResource target, final Object toCopy) {
66 // The popup's constructor's argument is a boolean specifying that it
67 // auto-close itself when the user clicks outside of it.
69 setAnimationEnabled(true);
70 // A dummy command that we will execute from unimplemented leaves.
71 final Command cancelCmd = new Command() {
74 public void execute() {
79 final MenuBar contextMenu = new MenuBar(true);
80 final CellTreeView folders = GSS.get().getTreeView();
82 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + " Move</span>", true, new Command() {
85 public void execute() {
86 if (toCopy instanceof FolderResource){
87 /*List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
88 List<TreeItem> parents = new ArrayList();
89 for(TreeItem item : treeItems)
90 if(item.getParentItem() != null)
91 parents.add(item.getParentItem());*/
92 moveFolder(target, (FolderResource) toCopy);
94 else if(toCopy instanceof List)
95 moveFiles(target, (List<FileResource>) toCopy);
99 }).setVisible(target != null);
101 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + " Copy</span>", true, new Command() {
104 public void execute() {
105 if (toCopy instanceof FolderResource)
106 copyFolder(target, (FolderResource) toCopy);
107 else if(toCopy instanceof List)
108 copyFiles(target, (List<FileResource>) toCopy);
112 }).setVisible(target != null);
114 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + " Delete (Trash)</span>", true, new Command() {
117 public void execute() {
118 if (toCopy instanceof FolderResource){
119 /*final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
120 List<TreeItem> parents = new ArrayList();
121 for(TreeItem item : treeItems)
122 if(item.getParentItem() != null)
123 parents.add(item.getParentItem());
125 trashFolder((FolderResource) toCopy);
127 else if(toCopy instanceof List)
128 trashFiles((List<FileResource>) toCopy);
132 }).setVisible(target == null);
133 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + " Cancel</span>", true, cancelCmd);
139 private void copyFolder(final FolderResource target, FolderResource toCopy) {
140 String atarget = target.getUri();
141 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
142 atarget = atarget + toCopy.getName();
143 PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
146 public void onComplete() {
147 /*final TreeItem folder;
148 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
149 if (folderTemp == null)
150 folder = GSS.get().getFolders().getOtherSharedItem(target);
153 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
154 GSS.get().getStatusPanel().updateStats();*/
158 public void onError(Throwable t) {
160 if (t instanceof RestException) {
161 int statusCode = ((RestException) t).getHttpStatusCode();
162 if (statusCode == 405)
163 GSS.get().displayError("You don't have the necessary permissions");
165 else if (statusCode == 409)
166 GSS.get().displayError("A folder with the same name already exists");
167 else if (statusCode == 413)
168 GSS.get().displayError("Your quota has been exceeded");
170 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
172 GSS.get().displayError("System error copying folder:" + t.getMessage());
175 DeferredCommand.addCommand(cf);
178 private void moveFolder(final FolderResource target, final FolderResource toCopy) {
179 String atarget = target.getUri();
180 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
181 atarget = atarget + toCopy.getName();
183 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
186 public void onComplete() {
187 /*final TreeItem folder;
188 for(TreeItem i : items){
189 DnDTreeItem id = (DnDTreeItem)i;
190 if(id.getChild(toCopy) != null)
191 id.removeItem(id.getChild(toCopy));
193 GSS.get().getFolders().clearSelection();
194 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
195 if (folderTemp == null)
196 folder = GSS.get().getFolders().getOtherSharedItem(target);
199 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
200 GSS.get().showFileList(true);
201 GSS.get().getStatusPanel().updateStats();*/
205 public void onError(Throwable t) {
207 if (t instanceof RestException) {
208 int statusCode = ((RestException) t).getHttpStatusCode();
209 if (statusCode == 405)
210 GSS.get().displayError("You don't have the necessary permissions");
212 else if (statusCode == 409)
213 GSS.get().displayError("A folder with the same name already exists");
214 else if (statusCode == 413)
215 GSS.get().displayError("Your quota has been exceeded");
217 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
219 GSS.get().displayError("System error copying folder:" + t.getMessage());
222 DeferredCommand.addCommand(cf);
225 private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
226 List<String> fileIds = new ArrayList<String>();
227 String target = ftarget.getUri();
228 target = target.endsWith("/") ? target : target + '/';
229 for (FileResource fileResource : files) {
230 String fileTarget = target + URL.encodeComponent(fileResource.getName());
231 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
234 executeCopyOrMoveFiles(index, fileIds);
238 private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
239 List<String> fileIds = new ArrayList<String>();
240 String target = ftarget.getUri();
241 target = target.endsWith("/") ? target : target + '/';
242 for (FileResource fileResource : files) {
243 String fileTarget = target + URL.encodeComponent(fileResource.getName());
244 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
247 executeCopyOrMoveFiles(index, fileIds);
251 private void trashFolder(final FolderResource folder){
252 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
255 public void onComplete() {
256 /*for(TreeItem item : items)
257 GSS.get().getFolders().updateFolder((DnDTreeItem) item);
258 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
260 GSS.get().showFileList(true);
265 public void onError(Throwable t) {
267 if(t instanceof RestException){
268 int statusCode = ((RestException)t).getHttpStatusCode();
269 if(statusCode == 405)
270 GSS.get().displayError("You don't have the necessary permissions");
271 else if(statusCode == 404)
272 GSS.get().displayError("Folder does not exist");
274 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
277 GSS.get().displayError("System error trashing folder:"+t.getMessage());
280 DeferredCommand.addCommand(tot);
283 private void trashFiles(List<FileResource> files){
284 final List<String> fileIds = new ArrayList<String>();
285 for(FileResource f : files)
286 fileIds.add(f.getUri()+"?trash=");
287 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
290 public void onComplete() {
291 GSS.get().showFileList(true);
295 public void onError(String p, Throwable t) {
297 if(t instanceof RestException){
298 int statusCode = ((RestException)t).getHttpStatusCode();
299 if(statusCode == 405)
300 GSS.get().displayError("You don't have the necessary permissions");
301 else if(statusCode == 404)
302 GSS.get().displayError("File does not exist");
304 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
307 GSS.get().displayError("System error trashing file:"+t.getMessage());
310 DeferredCommand.addCommand(tot);
314 private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
315 if (index >= paths.size()) {
316 GSS.get().showFileList(true);
317 GSS.get().getStatusPanel().updateStats();
320 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
323 public void onComplete() {
324 executeCopyOrMoveFiles(index + 1, paths);
328 public void onError(Throwable t) {
330 if (t instanceof RestException) {
331 int statusCode = ((RestException) t).getHttpStatusCode();
332 if (statusCode == 405)
333 GSS.get().displayError("You don't have the necessary permissions");
334 else if (statusCode == 404)
335 GSS.get().displayError("File not found");
336 else if (statusCode == 409)
337 GSS.get().displayError("A file with the same name already exists");
338 else if (statusCode == 413)
339 GSS.get().displayError("Your quota has been exceeded");
341 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
343 GSS.get().displayError("System error copying file:" + t.getMessage());
347 DeferredCommand.addCommand(cf);