fix sharing icon and popup for my shared items and others
[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                 buttons.add(ok);\r
82                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
83                 // Create the 'Cancel' button, along with a listener that hides the\r
84                 // dialog when the button is clicked.\r
85                 Button cancel = new Button("Cancel", new ClickHandler() {\r
86                         @Override\r
87                         public void onClick(ClickEvent event) {\r
88                                 hide();\r
89                         }\r
90                 });\r
91                 buttons.add(cancel);\r
92                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
93                 buttons.setSpacing(8);\r
94                 buttons.setStyleName("gss-warnMessage");\r
95                 outer.setStyleName("gss-warnMessage");\r
96                 outer.add(buttons);\r
97                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
98                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
99                 setWidget(outer);\r
100         }\r
101 \r
102         /**\r
103          * Generate an RPC request to delete a file.\r
104          *\r
105          * @param userId the ID of the current user\r
106          */\r
107         private void deleteFile() {\r
108                 Object selection = GSS.get().getCurrentSelection();\r
109                 if (selection == null) {\r
110                         GSS.get().displayError("No file was selected");\r
111                         return;\r
112                 }\r
113                 if (selection instanceof FileResource) {\r
114                         FileResource file = (FileResource) selection;\r
115 \r
116                         DeleteCommand df = new DeleteCommand(file.getUri()){\r
117 \r
118                                 @Override\r
119                                 public void onComplete() {\r
120                                         GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);\r
121                                         GSS.get().getStatusPanel().updateStats();\r
122                                 }\r
123 \r
124                                 @Override\r
125                                 public void onError(Throwable t) {\r
126                                         GWT.log("", t);\r
127                                         if(t instanceof RestException){\r
128                                                 int statusCode = ((RestException)t).getHttpStatusCode();\r
129                                                 if(statusCode == 405)\r
130                                                         GSS.get().displayError("You don't have the necessary permissions");\r
131                                                 else if(statusCode == 404)\r
132                                                         GSS.get().displayError("File not found");\r
133                                                 else\r
134                                                         GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());\r
135                                         }\r
136                                         else\r
137                                                 GSS.get().displayError("System error unable to delete file: "+t.getMessage());\r
138                                 }\r
139                         };\r
140 \r
141                         DeferredCommand.addCommand(df);\r
142                 }\r
143                 else if(selection instanceof List){\r
144                         List<FileResource> files = (List<FileResource>) selection;\r
145                         List<String> fileIds = new ArrayList<String>();\r
146                         for(FileResource f : files)\r
147                                 fileIds.add(f.getUri());\r
148 \r
149                         MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){\r
150 \r
151                                 @Override\r
152                                 public void onComplete() {\r
153                                         GSS.get().showFileList(true);\r
154                                 }\r
155 \r
156                                 @Override\r
157                                 public void onError(Throwable t) {\r
158                                         GWT.log("", t);\r
159                                         GSS.get().showFileList(true);\r
160                                 }\r
161 \r
162                                 @Override\r
163                                 public void onError(String path, Throwable t) {\r
164                                         GWT.log("", t);\r
165                                         if(t instanceof RestException){\r
166                                                 int statusCode = ((RestException)t).getHttpStatusCode();\r
167                                                 if(statusCode == 405)\r
168                                                         GSS.get().displayError("You don't have the necessary permissions");\r
169                                                 else if(statusCode == 404)\r
170                                                         GSS.get().displayError("File not found");\r
171                                                 else\r
172                                                         GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());\r
173                                         }\r
174                                         else\r
175                                                 GSS.get().displayError("System error unable to delete file:"+t.getMessage());\r
176 \r
177                                 }\r
178                         };\r
179 \r
180                         DeferredCommand.addCommand(ed);\r
181                 }\r
182         }\r
183 \r
184         @Override\r
185         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
186                 super.onPreviewNativeEvent(preview);\r
187 \r
188                 NativeEvent evt = preview.getNativeEvent();\r
189                 if (evt.getType().equals("keydown"))\r
190                         // Use the popup's key preview hooks to close the dialog when either\r
191                         // enter or escape is pressed.\r
192                         switch (evt.getKeyCode()) {\r
193                                 case KeyCodes.KEY_ENTER:\r
194                                         hide();\r
195                                         deleteFile();\r
196                                         break;\r
197                                 case KeyCodes.KEY_ESCAPE:\r
198                                         hide();\r
199                                         break;\r
200                         }\r
201         }\r
202 \r
203 \r
204 \r
205 }\r