3b97710001e3737509ec44213f5f673666527aef
[pithos-web-client] / src / gr / grnet / pithos / web / client / DeleteFileDialog.java
1 /*\r
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
3  *\r
4  * Redistribution and use in source and binary forms, with or\r
5  * without modification, are permitted provided that the following\r
6  * conditions are met:\r
7  *\r
8  *   1. Redistributions of source code must retain the above\r
9  *      copyright notice, this list of conditions and the following\r
10  *      disclaimer.\r
11  *\r
12  *   2. Redistributions in binary form must reproduce the above\r
13  *      copyright notice, this list of conditions and the following\r
14  *      disclaimer in the documentation and/or other materials\r
15  *      provided with the distribution.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28  * POSSIBILITY OF SUCH DAMAGE.\r
29  *\r
30  * The views and conclusions contained in the software and\r
31  * documentation are those of the authors and should not be\r
32  * interpreted as representing official policies, either expressed\r
33  * or implied, of GRNET S.A.\r
34  */\r
35 package gr.grnet.pithos.web.client;\r
36 \r
37 import gr.grnet.pithos.web.client.MessagePanel.Images;\r
38 import gr.grnet.pithos.web.client.foldertree.File;\r
39 import gr.grnet.pithos.web.client.foldertree.Resource;\r
40 import gr.grnet.pithos.web.client.rest.DeleteRequest;\r
41 import gr.grnet.pithos.web.client.rest.RestException;\r
42 \r
43 import java.util.Iterator;\r
44 import java.util.List;\r
45 \r
46 import com.google.gwt.core.client.GWT;\r
47 import com.google.gwt.core.client.Scheduler;\r
48 import com.google.gwt.dom.client.NativeEvent;\r
49 import com.google.gwt.event.dom.client.ClickEvent;\r
50 import com.google.gwt.event.dom.client.ClickHandler;\r
51 import com.google.gwt.event.dom.client.KeyCodes;\r
52 import com.google.gwt.http.client.Response;\r
53 import com.google.gwt.http.client.URL;\r
54 import com.google.gwt.user.client.Command;\r
55 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
56 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
57 import com.google.gwt.user.client.ui.Anchor;\r
58 import com.google.gwt.user.client.ui.Button;\r
59 import com.google.gwt.user.client.ui.DialogBox;\r
60 import com.google.gwt.user.client.ui.HTML;\r
61 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
62 import com.google.gwt.user.client.ui.VerticalPanel;\r
63 \r
64 /**\r
65  * The 'delete file' dialog box.\r
66  */\r
67 public class DeleteFileDialog extends DialogBox {\r
68 \r
69     private List<File> files;\r
70 \r
71     protected Pithos app;\r
72 \r
73         /**\r
74          * The widget's constructor.\r
75          *\r
76          * @param images the supplied images\r
77          */\r
78         public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {\r
79         app = _app;\r
80         files = _files;\r
81                 Anchor close = new Anchor();\r
82                 close.addStyleName("close");\r
83                 close.addClickHandler(new ClickHandler() {\r
84                         \r
85                         @Override\r
86                         public void onClick(ClickEvent event) {\r
87                                 hide();\r
88                         }\r
89                 });\r
90                 // Set the dialog's caption.\r
91                 setText("Confirmation");\r
92                 setAnimationEnabled(true);\r
93                 setGlassEnabled(true);\r
94                 setStyleName("pithos-DialogBox");\r
95                 // Create a VerticalPanel to contain the label and the buttons.\r
96                 VerticalPanel outer = new VerticalPanel();\r
97                 outer.add(close);\r
98                 \r
99                 VerticalPanel inner = new VerticalPanel();\r
100                 inner.addStyleName("inner");\r
101 \r
102                 HTML text;\r
103                 if (files.size() == 1)\r
104                         text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + files.get(0).getName() + "'?</td></tr></table>");\r
105                 else\r
106                         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
107                 text.setStyleName("pithos-warnMessage");\r
108                 inner.add(text);\r
109 \r
110                 // Create the 'Delete' button, along with a listener that hides the dialog\r
111                 // when the button is clicked and deletes the file.\r
112                 Button ok = new Button("Delete", new ClickHandler() {\r
113                         @Override\r
114                         public void onClick(ClickEvent event) {\r
115                                 deleteFiles();\r
116                                 hide();\r
117                         }\r
118                 });\r
119                 ok.addStyleName("button");\r
120                 inner.add(ok);\r
121                 inner.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
122                 outer.add(inner);\r
123                 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);\r
124                 setWidget(outer);\r
125         }\r
126 \r
127         /**\r
128          * Generate an RPC request to delete a file.\r
129          */\r
130         protected void deleteFiles() {\r
131         Iterator<File> iter = files.iterator();\r
132         deleteFile(iter);\r
133     }\r
134 \r
135         protected void deleteFile(final Iterator<File> iter) {\r
136         if (iter.hasNext()) {\r
137             File f = iter.next();\r
138             String path = f.getUri();\r
139             DeleteRequest deleteFile = new DeleteRequest(app.getApiPath(), f.getOwner(), URL.encode(path)) {\r
140                 @Override\r
141                 public void onSuccess(Resource result) {\r
142                     deleteFile(iter);\r
143                 }\r
144 \r
145                 @Override\r
146                 public void onError(Throwable t) {\r
147                     GWT.log("", t);\r
148                                         app.setError(t);\r
149                     if (t instanceof RestException) {\r
150                         app.displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());\r
151                     }\r
152                     else\r
153                         app.displayError("System error unable to delete file: "+t.getMessage());\r
154                 }\r
155 \r
156                                 @Override\r
157                                 protected void onUnauthorized(Response response) {\r
158                                         app.sessionExpired();\r
159                                 }\r
160             };\r
161             deleteFile.setHeader("X-Auth-Token", app.getToken());\r
162             Scheduler.get().scheduleDeferred(deleteFile);\r
163         }\r
164         else {\r
165             app.updateFolder(files.get(0).getParent(), true, new Command() {\r
166                                 \r
167                                 @Override\r
168                                 public void execute() {\r
169                                         app.updateStatistics();\r
170                                 }\r
171                         }, true);\r
172         }\r
173     }\r
174 \r
175         @Override\r
176         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
177                 super.onPreviewNativeEvent(preview);\r
178 \r
179                 NativeEvent evt = preview.getNativeEvent();\r
180                 if (evt.getType().equals("keydown"))\r
181                         // Use the popup's key preview hooks to close the dialog when either\r
182                         // enter or escape is pressed.\r
183                         switch (evt.getKeyCode()) {\r
184                                 case KeyCodes.KEY_ENTER:\r
185                                         hide();\r
186                                         deleteFiles();\r
187                                         break;\r
188                                 case KeyCodes.KEY_ESCAPE:\r
189                                         hide();\r
190                                         break;\r
191                         }\r
192         }\r
193 \r
194 \r
195 \r
196 }\r