root / src / gr / ebs / gss / client / DeleteFileDialog.java @ 4cef6f04
History | View | Annotate | Download (6.8 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.client; |
20 |
|
21 |
import gr.ebs.gss.client.MessagePanel.Images; |
22 |
import gr.ebs.gss.client.rest.DeleteCommand; |
23 |
import gr.ebs.gss.client.rest.MultipleDeleteCommand; |
24 |
import gr.ebs.gss.client.rest.RestException; |
25 |
import gr.ebs.gss.client.rest.resource.FileResource; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
|
30 |
import com.google.gwt.core.client.GWT; |
31 |
import com.google.gwt.dom.client.NativeEvent; |
32 |
import com.google.gwt.event.dom.client.ClickEvent; |
33 |
import com.google.gwt.event.dom.client.ClickHandler; |
34 |
import com.google.gwt.event.dom.client.KeyCodes; |
35 |
import com.google.gwt.user.client.DeferredCommand; |
36 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
37 |
import com.google.gwt.user.client.ui.AbstractImagePrototype; |
38 |
import com.google.gwt.user.client.ui.Button; |
39 |
import com.google.gwt.user.client.ui.DialogBox; |
40 |
import com.google.gwt.user.client.ui.HTML; |
41 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
42 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
43 |
import com.google.gwt.user.client.ui.VerticalPanel; |
44 |
|
45 |
/**
|
46 |
* The 'delete file' dialog box.
|
47 |
*/
|
48 |
public class DeleteFileDialog extends DialogBox { |
49 |
|
50 |
/**
|
51 |
* The widget's constructor.
|
52 |
*
|
53 |
* @param images the supplied images
|
54 |
*/
|
55 |
public DeleteFileDialog(Images images) {
|
56 |
// Set the dialog's caption.
|
57 |
setText("Confirmation");
|
58 |
setAnimationEnabled(true);
|
59 |
Object selection = GSS.get().getCurrentSelection();
|
60 |
// Create a VerticalPanel to contain the label and the buttons.
|
61 |
VerticalPanel outer = new VerticalPanel();
|
62 |
HorizontalPanel buttons = new HorizontalPanel();
|
63 |
|
64 |
HTML text;
|
65 |
if (selection instanceof FileResource) |
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>"); |
67 |
else
|
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>"); |
69 |
text.setStyleName("gss-warnMessage");
|
70 |
outer.add(text); |
71 |
|
72 |
// Create the 'Delete' button, along with a listener that hides the dialog
|
73 |
// when the button is clicked and deletes the file.
|
74 |
Button ok = new Button("Delete", new ClickHandler() { |
75 |
@Override
|
76 |
public void onClick(ClickEvent event) { |
77 |
deleteFile(); |
78 |
hide(); |
79 |
} |
80 |
}); |
81 |
buttons.add(ok); |
82 |
buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER); |
83 |
// Create the 'Cancel' button, along with a listener that hides the
|
84 |
// dialog when the button is clicked.
|
85 |
Button cancel = new Button("Cancel", new ClickHandler() { |
86 |
@Override
|
87 |
public void onClick(ClickEvent event) { |
88 |
hide(); |
89 |
} |
90 |
}); |
91 |
buttons.add(cancel); |
92 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
93 |
buttons.setSpacing(8);
|
94 |
buttons.setStyleName("gss-warnMessage");
|
95 |
outer.setStyleName("gss-warnMessage");
|
96 |
outer.add(buttons); |
97 |
outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER); |
98 |
outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
99 |
setWidget(outer); |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Generate an RPC request to delete a file.
|
104 |
*
|
105 |
* @param userId the ID of the current user
|
106 |
*/
|
107 |
private void deleteFile() { |
108 |
Object selection = GSS.get().getCurrentSelection();
|
109 |
if (selection == null) { |
110 |
GSS.get().displayError("No file was selected");
|
111 |
return;
|
112 |
} |
113 |
if (selection instanceof FileResource) { |
114 |
FileResource file = (FileResource) selection; |
115 |
|
116 |
DeleteCommand df = new DeleteCommand(file.getUri()){
|
117 |
|
118 |
@Override
|
119 |
public void onComplete() { |
120 |
GSS.get().getFileList().updateFileCache(true, true /*clear selection*/); |
121 |
GSS.get().getStatusPanel().updateStats(); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void onError(Throwable t) { |
126 |
GWT.log("", t);
|
127 |
if(t instanceof RestException){ |
128 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
129 |
if(statusCode == 405) |
130 |
GSS.get().displayError("You don't have the necessary permissions");
|
131 |
else if(statusCode == 404) |
132 |
GSS.get().displayError("File not found");
|
133 |
else
|
134 |
GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
|
135 |
} |
136 |
else
|
137 |
GSS.get().displayError("System error unable to delete file: "+t.getMessage());
|
138 |
} |
139 |
}; |
140 |
|
141 |
DeferredCommand.addCommand(df); |
142 |
} |
143 |
else if(selection instanceof List){ |
144 |
List<FileResource> files = (List<FileResource>) selection; |
145 |
List<String> fileIds = new ArrayList<String>(); |
146 |
for(FileResource f : files)
|
147 |
fileIds.add(f.getUri()); |
148 |
|
149 |
MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){ |
150 |
|
151 |
@Override
|
152 |
public void onComplete() { |
153 |
GSS.get().showFileList(true);
|
154 |
} |
155 |
|
156 |
@Override
|
157 |
public void onError(Throwable t) { |
158 |
GWT.log("", t);
|
159 |
GSS.get().showFileList(true);
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void onError(String path, Throwable t) { |
164 |
GWT.log("", t);
|
165 |
if(t instanceof RestException){ |
166 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
167 |
if(statusCode == 405) |
168 |
GSS.get().displayError("You don't have the necessary permissions");
|
169 |
else if(statusCode == 404) |
170 |
GSS.get().displayError("File not found");
|
171 |
else
|
172 |
GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
|
173 |
} |
174 |
else
|
175 |
GSS.get().displayError("System error unable to delete file:"+t.getMessage());
|
176 |
|
177 |
} |
178 |
}; |
179 |
|
180 |
DeferredCommand.addCommand(ed); |
181 |
} |
182 |
} |
183 |
|
184 |
@Override
|
185 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
186 |
super.onPreviewNativeEvent(preview);
|
187 |
|
188 |
NativeEvent evt = preview.getNativeEvent(); |
189 |
if (evt.getType().equals("keydown")) |
190 |
// Use the popup's key preview hooks to close the dialog when either
|
191 |
// enter or escape is pressed.
|
192 |
switch (evt.getKeyCode()) {
|
193 |
case KeyCodes.KEY_ENTER:
|
194 |
hide(); |
195 |
deleteFile(); |
196 |
break;
|
197 |
case KeyCodes.KEY_ESCAPE:
|
198 |
hide(); |
199 |
break;
|
200 |
} |
201 |
} |
202 |
|
203 |
|
204 |
|
205 |
} |