Merge branch 'master' of https://code.grnet.gr/git/pithos
[pithos] / web_client / src / gr / grnet / pithos / web / client / DeleteFileDialog.java
1 /*\r
2  * Copyright 2011 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 com.google.gwt.core.client.Scheduler;\r
38 import gr.grnet.pithos.web.client.MessagePanel.Images;\r
39 import gr.grnet.pithos.web.client.foldertree.File;\r
40 import gr.grnet.pithos.web.client.foldertree.Resource;\r
41 import gr.grnet.pithos.web.client.rest.DeleteRequest;\r
42 import gr.grnet.pithos.web.client.rest.RestException;\r
43 \r
44 import java.util.Iterator;\r
45 import java.util.List;\r
46 \r
47 import com.google.gwt.core.client.GWT;\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.user.client.Event.NativePreviewEvent;\r
53 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
54 import com.google.gwt.user.client.ui.Button;\r
55 import com.google.gwt.user.client.ui.DialogBox;\r
56 import com.google.gwt.user.client.ui.HTML;\r
57 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
58 import com.google.gwt.user.client.ui.HorizontalPanel;\r
59 import com.google.gwt.user.client.ui.VerticalPanel;\r
60 \r
61 /**\r
62  * The 'delete file' dialog box.\r
63  */\r
64 public class DeleteFileDialog extends DialogBox {\r
65 \r
66     private List<File> files;\r
67 \r
68     protected Pithos app;\r
69 \r
70         /**\r
71          * The widget's constructor.\r
72          *\r
73          * @param images the supplied images\r
74          */\r
75         public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {\r
76         app = _app;\r
77         files = _files;\r
78                 // Set the dialog's caption.\r
79                 setText("Confirmation");\r
80                 setAnimationEnabled(true);\r
81                 // Create a VerticalPanel to contain the label and the buttons.\r
82                 VerticalPanel outer = new VerticalPanel();\r
83                 HorizontalPanel buttons = new HorizontalPanel();\r
84 \r
85                 HTML text;\r
86                 if (files.size() == 1)\r
87                         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
88                 else\r
89                         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
90                 text.setStyleName("pithos-warnMessage");\r
91                 outer.add(text);\r
92 \r
93                 // Create the 'Delete' button, along with a listener that hides the dialog\r
94                 // when the button is clicked and deletes the file.\r
95                 Button ok = new Button("Delete", new ClickHandler() {\r
96                         @Override\r
97                         public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
98                                 deleteFiles();\r
99                                 hide();\r
100                         }\r
101                 });\r
102                 ok.getElement().setId("confirmation.ok");\r
103                 buttons.add(ok);\r
104                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
105                 // Create the 'Cancel' button, along with a listener that hides the\r
106                 // dialog when the button is clicked.\r
107                 Button cancel = new Button("Cancel", new ClickHandler() {\r
108                         @Override\r
109                         public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
110                                 hide();\r
111                         }\r
112                 });\r
113                 cancel.getElement().setId("confirmation.cancel");\r
114                 buttons.add(cancel);\r
115                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
116                 buttons.setSpacing(8);\r
117                 buttons.setStyleName("pithos-warnMessage");\r
118                 outer.setStyleName("pithos-warnMessage");\r
119                 outer.add(buttons);\r
120                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
121                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
122                 setWidget(outer);\r
123         }\r
124 \r
125         /**\r
126          * Generate an RPC request to delete a file.\r
127          */\r
128         protected void deleteFiles() {\r
129         Iterator<File> iter = files.iterator();\r
130         deleteFile(iter);\r
131     }\r
132 \r
133         protected void deleteFile(final Iterator<File> iter) {\r
134         if (iter.hasNext()) {\r
135             File f = iter.next();\r
136             String path = f.getUri();\r
137             DeleteRequest deleteFile = new DeleteRequest(app.getApiPath(), f.getOwner(), path) {\r
138                 @Override\r
139                 public void onSuccess(@SuppressWarnings("unused") Resource result) {\r
140                     deleteFile(iter);\r
141                 }\r
142 \r
143                 @Override\r
144                 public void onError(Throwable t) {\r
145                     GWT.log("", t);\r
146                     if (t instanceof RestException) {\r
147                         app.displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());\r
148                     }\r
149                     else\r
150                         app.displayError("System error unable to delete file: "+t.getMessage());\r
151                 }\r
152             };\r
153             deleteFile.setHeader("X-Auth-Token", app.getToken());\r
154             Scheduler.get().scheduleDeferred(deleteFile);\r
155         }\r
156         else {\r
157             app.updateFolder(files.get(0).getParent(), true, null);\r
158         }\r
159     }\r
160 \r
161         @Override\r
162         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
163                 super.onPreviewNativeEvent(preview);\r
164 \r
165                 NativeEvent evt = preview.getNativeEvent();\r
166                 if (evt.getType().equals("keydown"))\r
167                         // Use the popup's key preview hooks to close the dialog when either\r
168                         // enter or escape is pressed.\r
169                         switch (evt.getKeyCode()) {\r
170                                 case KeyCodes.KEY_ENTER:\r
171                                         hide();\r
172                                         deleteFiles();\r
173                                         break;\r
174                                 case KeyCodes.KEY_ESCAPE:\r
175                                         hide();\r
176                                         break;\r
177                         }\r
178         }\r
179 \r
180 \r
181 \r
182 }\r