2 * Copyright (c) 2011 Greek Research and Technology Network
\r
4 package gr.grnet.pithos.web.client;
\r
6 import gr.grnet.pithos.web.client.MessagePanel.Images;
\r
7 import gr.grnet.pithos.web.client.rest.DeleteCommand;
\r
8 import gr.grnet.pithos.web.client.rest.MultipleDeleteCommand;
\r
9 import gr.grnet.pithos.web.client.rest.RestException;
\r
10 import gr.grnet.pithos.web.client.rest.resource.FileResource;
\r
12 import java.util.ArrayList;
\r
13 import java.util.List;
\r
15 import com.google.gwt.core.client.GWT;
\r
16 import com.google.gwt.dom.client.NativeEvent;
\r
17 import com.google.gwt.event.dom.client.ClickEvent;
\r
18 import com.google.gwt.event.dom.client.ClickHandler;
\r
19 import com.google.gwt.event.dom.client.KeyCodes;
\r
20 import com.google.gwt.user.client.DeferredCommand;
\r
21 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
22 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
23 import com.google.gwt.user.client.ui.Button;
\r
24 import com.google.gwt.user.client.ui.DialogBox;
\r
25 import com.google.gwt.user.client.ui.HTML;
\r
26 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
27 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
28 import com.google.gwt.user.client.ui.VerticalPanel;
\r
31 * The 'delete file' dialog box.
\r
33 public class DeleteFileDialog extends DialogBox {
\r
36 * The widget's constructor.
\r
38 * @param images the supplied images
\r
40 public DeleteFileDialog(Images images) {
\r
41 // Set the dialog's caption.
\r
42 setText("Confirmation");
\r
43 setAnimationEnabled(true);
\r
44 Object selection = GSS.get().getCurrentSelection();
\r
45 // Create a VerticalPanel to contain the label and the buttons.
\r
46 VerticalPanel outer = new VerticalPanel();
\r
47 HorizontalPanel buttons = new HorizontalPanel();
\r
50 if (selection instanceof FileResource)
\r
51 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
53 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
54 text.setStyleName("pithos-warnMessage");
\r
57 // Create the 'Delete' button, along with a listener that hides the dialog
\r
58 // when the button is clicked and deletes the file.
\r
59 Button ok = new Button("Delete", new ClickHandler() {
\r
61 public void onClick(ClickEvent event) {
\r
66 ok.getElement().setId("confirmation.ok");
\r
68 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
69 // Create the 'Cancel' button, along with a listener that hides the
\r
70 // dialog when the button is clicked.
\r
71 Button cancel = new Button("Cancel", new ClickHandler() {
\r
73 public void onClick(ClickEvent event) {
\r
77 cancel.getElement().setId("confirmation.cancel");
\r
78 buttons.add(cancel);
\r
79 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
80 buttons.setSpacing(8);
\r
81 buttons.setStyleName("pithos-warnMessage");
\r
82 outer.setStyleName("pithos-warnMessage");
\r
84 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
\r
85 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
90 * Generate an RPC request to delete a file.
\r
92 * @param userId the ID of the current user
\r
94 private void deleteFile() {
\r
95 Object selection = GSS.get().getCurrentSelection();
\r
96 if (selection == null) {
\r
97 GSS.get().displayError("No file was selected");
\r
100 if (selection instanceof FileResource) {
\r
101 FileResource file = (FileResource) selection;
\r
103 DeleteCommand df = new DeleteCommand(file.getUri()){
\r
106 public void onComplete() {
\r
107 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
\r
108 GSS.get().getStatusPanel().updateStats();
\r
112 public void onError(Throwable t) {
\r
114 if(t instanceof RestException){
\r
115 int statusCode = ((RestException)t).getHttpStatusCode();
\r
116 if(statusCode == 405)
\r
117 GSS.get().displayError("You don't have the necessary permissions");
\r
118 else if(statusCode == 404)
\r
119 GSS.get().displayError("File not found");
\r
121 GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
\r
124 GSS.get().displayError("System error unable to delete file: "+t.getMessage());
\r
128 DeferredCommand.addCommand(df);
\r
130 else if(selection instanceof List){
\r
131 List<FileResource> files = (List<FileResource>) selection;
\r
132 List<String> fileIds = new ArrayList<String>();
\r
133 for(FileResource f : files)
\r
134 fileIds.add(f.getUri());
\r
136 MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){
\r
139 public void onComplete() {
\r
140 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
\r
144 public void onError(Throwable t) {
\r
146 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
\r
150 public void onError(String path, Throwable t) {
\r
152 if(t instanceof RestException){
\r
153 int statusCode = ((RestException)t).getHttpStatusCode();
\r
154 if(statusCode == 405)
\r
155 GSS.get().displayError("You don't have the necessary permissions");
\r
156 else if(statusCode == 404)
\r
157 GSS.get().displayError("File not found");
\r
159 GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
\r
162 GSS.get().displayError("System error unable to delete file:"+t.getMessage());
\r
167 DeferredCommand.addCommand(ed);
\r
172 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
173 super.onPreviewNativeEvent(preview);
\r
175 NativeEvent evt = preview.getNativeEvent();
\r
176 if (evt.getType().equals("keydown"))
\r
177 // Use the popup's key preview hooks to close the dialog when either
\r
178 // enter or escape is pressed.
\r
179 switch (evt.getKeyCode()) {
\r
180 case KeyCodes.KEY_ENTER:
\r
184 case KeyCodes.KEY_ESCAPE:
\r