2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\r
6 * GSS is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * GSS is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
\r
19 package gr.ebs.gss.client;
\r
21 import gr.ebs.gss.client.MessagePanel.Images;
\r
22 import gr.ebs.gss.client.rest.DeleteCommand;
\r
23 import gr.ebs.gss.client.rest.MultipleDeleteCommand;
\r
24 import gr.ebs.gss.client.rest.RestException;
\r
25 import gr.ebs.gss.client.rest.resource.FileResource;
\r
27 import java.util.ArrayList;
\r
28 import java.util.List;
\r
30 import com.google.gwt.core.client.GWT;
\r
31 import com.google.gwt.dom.client.NativeEvent;
\r
32 import com.google.gwt.event.dom.client.ClickEvent;
\r
33 import com.google.gwt.event.dom.client.ClickHandler;
\r
34 import com.google.gwt.event.dom.client.KeyCodes;
\r
35 import com.google.gwt.user.client.DeferredCommand;
\r
36 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
37 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
38 import com.google.gwt.user.client.ui.Button;
\r
39 import com.google.gwt.user.client.ui.DialogBox;
\r
40 import com.google.gwt.user.client.ui.HTML;
\r
41 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
42 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
43 import com.google.gwt.user.client.ui.VerticalPanel;
\r
46 * The 'delete file' dialog box.
\r
48 public class DeleteFileDialog extends DialogBox {
\r
51 * The widget's constructor.
\r
53 * @param images the supplied images
\r
55 public DeleteFileDialog(Images images) {
\r
56 // Set the dialog's caption.
\r
57 setText("Confirmation");
\r
58 setAnimationEnabled(true);
\r
59 Object selection = GSS.get().getCurrentSelection();
\r
60 // Create a VerticalPanel to contain the label and the buttons.
\r
61 VerticalPanel outer = new VerticalPanel();
\r
62 HorizontalPanel buttons = new HorizontalPanel();
\r
65 if (selection instanceof FileResource)
\r
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>");
\r
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>");
\r
69 text.setStyleName("gss-warnMessage");
\r
72 // Create the 'Delete' button, along with a listener that hides the dialog
\r
73 // when the button is clicked and deletes the file.
\r
74 Button ok = new Button("Delete", new ClickHandler() {
\r
76 public void onClick(ClickEvent event) {
\r
82 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
83 // Create the 'Cancel' button, along with a listener that hides the
\r
84 // dialog when the button is clicked.
\r
85 Button cancel = new Button("Cancel", new ClickHandler() {
\r
87 public void onClick(ClickEvent event) {
\r
91 buttons.add(cancel);
\r
92 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
93 buttons.setSpacing(8);
\r
94 buttons.setStyleName("gss-warnMessage");
\r
95 outer.setStyleName("gss-warnMessage");
\r
97 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
\r
98 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
103 * Generate an RPC request to delete a file.
\r
105 * @param userId the ID of the current user
\r
107 private void deleteFile() {
\r
108 Object selection = GSS.get().getCurrentSelection();
\r
109 if (selection == null) {
\r
110 GSS.get().displayError("No file was selected");
\r
113 if (selection instanceof FileResource) {
\r
114 FileResource file = (FileResource) selection;
\r
116 DeleteCommand df = new DeleteCommand(file.getUri()){
\r
119 public void onComplete() {
\r
120 GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
\r
121 GSS.get().getStatusPanel().updateStats();
\r
125 public void onError(Throwable t) {
\r
127 if(t instanceof RestException){
\r
128 int statusCode = ((RestException)t).getHttpStatusCode();
\r
129 if(statusCode == 405)
\r
130 GSS.get().displayError("You don't have the necessary permissions");
\r
131 else if(statusCode == 404)
\r
132 GSS.get().displayError("File not found");
\r
134 GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
\r
137 GSS.get().displayError("System error unable to delete file: "+t.getMessage());
\r
141 DeferredCommand.addCommand(df);
\r
143 else if(selection instanceof List){
\r
144 List<FileResource> files = (List<FileResource>) selection;
\r
145 List<String> fileIds = new ArrayList<String>();
\r
146 for(FileResource f : files)
\r
147 fileIds.add(f.getUri());
\r
149 MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){
\r
152 public void onComplete() {
\r
153 GSS.get().showFileList(true);
\r
157 public void onError(Throwable t) {
\r
159 GSS.get().showFileList(true);
\r
163 public void onError(String path, Throwable t) {
\r
165 if(t instanceof RestException){
\r
166 int statusCode = ((RestException)t).getHttpStatusCode();
\r
167 if(statusCode == 405)
\r
168 GSS.get().displayError("You don't have the necessary permissions");
\r
169 else if(statusCode == 404)
\r
170 GSS.get().displayError("File not found");
\r
172 GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
\r
175 GSS.get().displayError("System error unable to delete file:"+t.getMessage());
\r
180 DeferredCommand.addCommand(ed);
\r
185 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
186 super.onPreviewNativeEvent(preview);
\r
188 NativeEvent evt = preview.getNativeEvent();
\r
189 if (evt.getType().equals("keydown"))
\r
190 // Use the popup's key preview hooks to close the dialog when either
\r
191 // enter or escape is pressed.
\r
192 switch (evt.getKeyCode()) {
\r
193 case KeyCodes.KEY_ENTER:
\r
197 case KeyCodes.KEY_ESCAPE:
\r