Added ids to folder context menu for trash folder after a wrong merge.
[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.dom.client.NativeEvent;\r
32 import com.google.gwt.event.dom.client.ClickEvent;\r
33 import com.google.gwt.event.dom.client.ClickHandler;\r
34 import com.google.gwt.event.dom.client.KeyCodes;\r
35 import com.google.gwt.user.client.DeferredCommand;\r
36 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
37 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
38 import com.google.gwt.user.client.ui.Button;\r
39 import com.google.gwt.user.client.ui.DialogBox;\r
40 import com.google.gwt.user.client.ui.HTML;\r
41 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
42 import com.google.gwt.user.client.ui.HorizontalPanel;\r
43 import com.google.gwt.user.client.ui.VerticalPanel;\r
44 \r
45 /**\r
46  * The 'delete file' dialog box.\r
47  */\r
48 public class DeleteFileDialog extends DialogBox {\r
49 \r
50         /**\r
51          * The widget's constructor.\r
52          *\r
53          * @param images the supplied images\r
54          */\r
55         public DeleteFileDialog(Images images) {\r
56                 // Set the dialog's caption.\r
57                 setText("Confirmation");\r
58                 setAnimationEnabled(true);\r
59                 Object selection = GSS.get().getCurrentSelection();\r
60                 // Create a VerticalPanel to contain the label and the buttons.\r
61                 VerticalPanel outer = new VerticalPanel();\r
62                 HorizontalPanel buttons = new HorizontalPanel();\r
63 \r
64                 HTML text;\r
65                 if (selection instanceof FileResource)\r
66                         text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + ((FileResource) selection).getName() + "'?</td></tr></table>");\r
67                 else\r
68                         text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete the selected files?</td></tr></table>");\r
69                 text.setStyleName("gss-warnMessage");\r
70                 outer.add(text);\r
71 \r
72                 // Create the 'Delete' button, along with a listener that hides the dialog\r
73                 // when the button is clicked and deletes the file.\r
74                 Button ok = new Button("Delete", new ClickHandler() {\r
75                         @Override\r
76                         public void onClick(ClickEvent event) {\r
77                                 deleteFile();\r
78                                 hide();\r
79                         }\r
80                 });\r
81                 ok.getElement().setId("confirmation.ok");\r
82                 buttons.add(ok);\r
83                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
84                 // Create the 'Cancel' button, along with a listener that hides the\r
85                 // dialog when the button is clicked.\r
86                 Button cancel = new Button("Cancel", new ClickHandler() {\r
87                         @Override\r
88                         public void onClick(ClickEvent event) {\r
89                                 hide();\r
90                         }\r
91                 });\r
92                 cancel.getElement().setId("confirmation.cancel");\r
93                 buttons.add(cancel);\r
94                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
95                 buttons.setSpacing(8);\r
96                 buttons.setStyleName("gss-warnMessage");\r
97                 outer.setStyleName("gss-warnMessage");\r
98                 outer.add(buttons);\r
99                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
100                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
101                 setWidget(outer);\r
102         }\r
103 \r
104         /**\r
105          * Generate an RPC request to delete a file.\r
106          *\r
107          * @param userId the ID of the current user\r
108          */\r
109         private void deleteFile() {\r
110                 Object selection = GSS.get().getCurrentSelection();\r
111                 if (selection == null) {\r
112                         GSS.get().displayError("No file was selected");\r
113                         return;\r
114                 }\r
115                 if (selection instanceof FileResource) {\r
116                         FileResource file = (FileResource) selection;\r
117 \r
118                         DeleteCommand df = new DeleteCommand(file.getUri()){\r
119 \r
120                                 @Override\r
121                                 public void onComplete() {\r
122                                         GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());\r
123                                         GSS.get().getStatusPanel().updateStats();\r
124                                 }\r
125 \r
126                                 @Override\r
127                                 public void onError(Throwable t) {\r
128                                         GWT.log("", t);\r
129                                         if(t instanceof RestException){\r
130                                                 int statusCode = ((RestException)t).getHttpStatusCode();\r
131                                                 if(statusCode == 405)\r
132                                                         GSS.get().displayError("You don't have the necessary permissions");\r
133                                                 else if(statusCode == 404)\r
134                                                         GSS.get().displayError("File not found");\r
135                                                 else\r
136                                                         GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());\r
137                                         }\r
138                                         else\r
139                                                 GSS.get().displayError("System error unable to delete file: "+t.getMessage());\r
140                                 }\r
141                         };\r
142 \r
143                         DeferredCommand.addCommand(df);\r
144                 }\r
145                 else if(selection instanceof List){\r
146                         List<FileResource> files = (List<FileResource>) selection;\r
147                         List<String> fileIds = new ArrayList<String>();\r
148                         for(FileResource f : files)\r
149                                 fileIds.add(f.getUri());\r
150 \r
151                         MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){\r
152 \r
153                                 @Override\r
154                                 public void onComplete() {\r
155                                         GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());\r
156                                 }\r
157 \r
158                                 @Override\r
159                                 public void onError(Throwable t) {\r
160                                         GWT.log("", t);\r
161                                         GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());\r
162                                 }\r
163 \r
164                                 @Override\r
165                                 public void onError(String path, Throwable t) {\r
166                                         GWT.log("", t);\r
167                                         if(t instanceof RestException){\r
168                                                 int statusCode = ((RestException)t).getHttpStatusCode();\r
169                                                 if(statusCode == 405)\r
170                                                         GSS.get().displayError("You don't have the necessary permissions");\r
171                                                 else if(statusCode == 404)\r
172                                                         GSS.get().displayError("File not found");\r
173                                                 else\r
174                                                         GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());\r
175                                         }\r
176                                         else\r
177                                                 GSS.get().displayError("System error unable to delete file:"+t.getMessage());\r
178 \r
179                                 }\r
180                         };\r
181 \r
182                         DeferredCommand.addCommand(ed);\r
183                 }\r
184         }\r
185 \r
186         @Override\r
187         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
188                 super.onPreviewNativeEvent(preview);\r
189 \r
190                 NativeEvent evt = preview.getNativeEvent();\r
191                 if (evt.getType().equals("keydown"))\r
192                         // Use the popup's key preview hooks to close the dialog when either\r
193                         // enter or escape is pressed.\r
194                         switch (evt.getKeyCode()) {\r
195                                 case KeyCodes.KEY_ENTER:\r
196                                         hide();\r
197                                         deleteFile();\r
198                                         break;\r
199                                 case KeyCodes.KEY_ESCAPE:\r
200                                         hide();\r
201                                         break;\r
202                         }\r
203         }\r
204 \r
205 \r
206 \r
207 }\r