Add Cut, Move to trash and Delete options to Other's shared context menu.
[pithos] / src / gr / ebs / gss / client / DeleteFileDialog.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.MessagePanel.Images;\r
22 import gr.ebs.gss.client.rest.DeleteCommand;\r
23 import gr.ebs.gss.client.rest.MultipleDeleteCommand;\r
24 import gr.ebs.gss.client.rest.RestException;\r
25 import gr.ebs.gss.client.rest.resource.FileResource;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \r
30 import com.google.gwt.core.client.GWT;\r
31 import com.google.gwt.user.client.DeferredCommand;\r
32 import com.google.gwt.user.client.ui.Button;\r
33 import com.google.gwt.user.client.ui.ClickListener;\r
34 import com.google.gwt.user.client.ui.DialogBox;\r
35 import com.google.gwt.user.client.ui.HTML;\r
36 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
37 import com.google.gwt.user.client.ui.HorizontalPanel;\r
38 import com.google.gwt.user.client.ui.KeyboardListener;\r
39 import com.google.gwt.user.client.ui.VerticalPanel;\r
40 import com.google.gwt.user.client.ui.Widget;\r
41 \r
42 /**\r
43  * The 'delete file' dialog box.\r
44  */\r
45 public class DeleteFileDialog extends DialogBox {\r
46 \r
47         /**\r
48          * The widget's constructor.\r
49          *\r
50          * @param images the supplied images\r
51          */\r
52         public DeleteFileDialog(Images images) {\r
53                 // Set the dialog's caption.\r
54                 setText("Confirmation");\r
55                 setAnimationEnabled(true);\r
56                 Object selection = GSS.get().getCurrentSelection();\r
57                 // Create a VerticalPanel to contain the label and the buttons.\r
58                 VerticalPanel outer = new VerticalPanel();\r
59                 HorizontalPanel buttons = new HorizontalPanel();\r
60 \r
61                 HTML text;\r
62                 if (selection instanceof FileResource)\r
63                         text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + ((FileResource) selection).getName() + "'?</td></tr></table>");\r
64                 else\r
65                         text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete the selected files?</td></tr></table>");\r
66                 text.setStyleName("gss-warnMessage");\r
67                 outer.add(text);\r
68 \r
69                 // Create the 'Delete' button, along with a listener that hides the dialog\r
70                 // when the button is clicked and deletes the file.\r
71                 Button ok = new Button("Delete", new ClickListener() {\r
72 \r
73                         public void onClick(Widget sender) {\r
74                                 deleteFile();\r
75                                 hide();\r
76                         }\r
77                 });\r
78                 buttons.add(ok);\r
79                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
80                 // Create the 'Cancel' button, along with a listener that hides the\r
81                 // dialog when the button is clicked.\r
82                 Button cancel = new Button("Cancel", new ClickListener() {\r
83 \r
84                         public void onClick(Widget sender) {\r
85                                 hide();\r
86                         }\r
87                 });\r
88                 buttons.add(cancel);\r
89                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
90                 buttons.setSpacing(8);\r
91                 buttons.setStyleName("gss-warnMessage");\r
92                 outer.setStyleName("gss-warnMessage");\r
93                 outer.add(buttons);\r
94                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
95                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
96                 setWidget(outer);\r
97         }\r
98 \r
99         /**\r
100          * Generate an RPC request to delete a file.\r
101          *\r
102          * @param userId the ID of the current user\r
103          */\r
104         private void deleteFile() {\r
105                 Object selection = GSS.get().getCurrentSelection();\r
106                 if (selection == null) {\r
107                         GSS.get().displayError("No file was selected");\r
108                         return;\r
109                 }\r
110                 if (selection instanceof FileResource) {\r
111                         FileResource file = (FileResource) selection;\r
112 \r
113                         DeleteCommand df = new DeleteCommand(file.getUri()){\r
114 \r
115                                 @Override\r
116                                 public void onComplete() {\r
117                                         GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);\r
118                                         GSS.get().getStatusPanel().updateStats();\r
119                                 }\r
120 \r
121                                 @Override\r
122                                 public void onError(Throwable t) {\r
123                                         GWT.log("", t);\r
124                                         if(t instanceof RestException){\r
125                                                 int statusCode = ((RestException)t).getHttpStatusCode();\r
126                                                 if(statusCode == 405)\r
127                                                         GSS.get().displayError("You don't have the necessary permissions");\r
128                                                 else if(statusCode == 404)\r
129                                                         GSS.get().displayError("File not found");\r
130                                                 else\r
131                                                         GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());\r
132                                         }\r
133                                         else\r
134                                                 GSS.get().displayError("System error unable to delete file: "+t.getMessage());\r
135                                 }\r
136                         };\r
137 \r
138                         DeferredCommand.addCommand(df);\r
139                 }\r
140                 else if(selection instanceof List){\r
141                         List<FileResource> files = (List<FileResource>) selection;\r
142                         List<String> fileIds = new ArrayList<String>();\r
143                         for(FileResource f : files)\r
144                                 fileIds.add(f.getUri());\r
145 \r
146                         MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){\r
147 \r
148                                 @Override\r
149                                 public void onComplete() {\r
150                                         GSS.get().showFileList(true);\r
151                                 }\r
152 \r
153                                 @Override\r
154                                 public void onError(Throwable t) {\r
155                                         GWT.log("", t);\r
156                                         GSS.get().showFileList(true);\r
157                                 }\r
158 \r
159                                 @Override\r
160                                 public void onError(String path, Throwable t) {\r
161                                         GWT.log("", t);\r
162                                         if(t instanceof RestException){\r
163                                                 int statusCode = ((RestException)t).getHttpStatusCode();\r
164                                                 if(statusCode == 405)\r
165                                                         GSS.get().displayError("You don't have the necessary permissions");\r
166                                                 else if(statusCode == 404)\r
167                                                         GSS.get().displayError("File not found");\r
168                                                 else\r
169                                                         GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());\r
170                                         }\r
171                                         else\r
172                                                 GSS.get().displayError("System error unable to delete file:"+t.getMessage());\r
173 \r
174                                 }\r
175                         };\r
176 \r
177                         DeferredCommand.addCommand(ed);\r
178                 }\r
179         }\r
180 \r
181         @Override\r
182         public boolean onKeyDownPreview(final char key, final int modifiers) {\r
183                 // Use the popup's key preview hooks to close the dialog when either\r
184                 // enter or escape is pressed.\r
185                 switch (key) {\r
186                         case KeyboardListener.KEY_ENTER:\r
187                                 hide();\r
188                                 deleteFile();\r
189                                 break;\r
190                         case KeyboardListener.KEY_ESCAPE:\r
191                                 hide();\r
192                                 break;\r
193                 }\r
194 \r
195                 return true;\r
196         }\r
197 \r
198 }\r