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