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