Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / DeleteFileDialog.java @ bdda6b2f

History | View | Annotate | Download (7.9 kB)

1 a57faaf0 Christos Stathis
/*
2 63366925 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.MessagePanel.Images;
38 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.DeleteCommand;
39 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.MultipleDeleteCommand;
40 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
41 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FileResource;
42 a57faaf0 Christos Stathis
43 a57faaf0 Christos Stathis
import java.util.ArrayList;
44 a57faaf0 Christos Stathis
import java.util.List;
45 a57faaf0 Christos Stathis
46 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
47 a57faaf0 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
48 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
49 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
50 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
51 a57faaf0 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
52 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
53 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
54 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Button;
55 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
56 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
57 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
59 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
60 a57faaf0 Christos Stathis
61 a57faaf0 Christos Stathis
/**
62 a57faaf0 Christos Stathis
 * The 'delete file' dialog box.
63 a57faaf0 Christos Stathis
 */
64 a57faaf0 Christos Stathis
public class DeleteFileDialog extends DialogBox {
65 a57faaf0 Christos Stathis
66 a57faaf0 Christos Stathis
        /**
67 a57faaf0 Christos Stathis
         * The widget's constructor.
68 a57faaf0 Christos Stathis
         *
69 a57faaf0 Christos Stathis
         * @param images the supplied images
70 a57faaf0 Christos Stathis
         */
71 a57faaf0 Christos Stathis
        public DeleteFileDialog(Images images) {
72 a57faaf0 Christos Stathis
                // Set the dialog's caption.
73 a57faaf0 Christos Stathis
                setText("Confirmation");
74 a57faaf0 Christos Stathis
                setAnimationEnabled(true);
75 a57faaf0 Christos Stathis
                Object selection = GSS.get().getCurrentSelection();
76 a57faaf0 Christos Stathis
                // Create a VerticalPanel to contain the label and the buttons.
77 a57faaf0 Christos Stathis
                VerticalPanel outer = new VerticalPanel();
78 a57faaf0 Christos Stathis
                HorizontalPanel buttons = new HorizontalPanel();
79 a57faaf0 Christos Stathis
80 a57faaf0 Christos Stathis
                HTML text;
81 a57faaf0 Christos Stathis
                if (selection instanceof FileResource)
82 a57faaf0 Christos Stathis
                        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>");
83 a57faaf0 Christos Stathis
                else
84 a57faaf0 Christos Stathis
                        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>");
85 a57faaf0 Christos Stathis
                text.setStyleName("pithos-warnMessage");
86 a57faaf0 Christos Stathis
                outer.add(text);
87 a57faaf0 Christos Stathis
88 a57faaf0 Christos Stathis
                // Create the 'Delete' button, along with a listener that hides the dialog
89 a57faaf0 Christos Stathis
                // when the button is clicked and deletes the file.
90 a57faaf0 Christos Stathis
                Button ok = new Button("Delete", new ClickHandler() {
91 a57faaf0 Christos Stathis
                        @Override
92 a57faaf0 Christos Stathis
                        public void onClick(ClickEvent event) {
93 a57faaf0 Christos Stathis
                                deleteFile();
94 a57faaf0 Christos Stathis
                                hide();
95 a57faaf0 Christos Stathis
                        }
96 a57faaf0 Christos Stathis
                });
97 a57faaf0 Christos Stathis
                ok.getElement().setId("confirmation.ok");
98 a57faaf0 Christos Stathis
                buttons.add(ok);
99 a57faaf0 Christos Stathis
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
100 a57faaf0 Christos Stathis
                // Create the 'Cancel' button, along with a listener that hides the
101 a57faaf0 Christos Stathis
                // dialog when the button is clicked.
102 a57faaf0 Christos Stathis
                Button cancel = new Button("Cancel", new ClickHandler() {
103 a57faaf0 Christos Stathis
                        @Override
104 a57faaf0 Christos Stathis
                        public void onClick(ClickEvent event) {
105 a57faaf0 Christos Stathis
                                hide();
106 a57faaf0 Christos Stathis
                        }
107 a57faaf0 Christos Stathis
                });
108 a57faaf0 Christos Stathis
                cancel.getElement().setId("confirmation.cancel");
109 a57faaf0 Christos Stathis
                buttons.add(cancel);
110 a57faaf0 Christos Stathis
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
111 a57faaf0 Christos Stathis
                buttons.setSpacing(8);
112 a57faaf0 Christos Stathis
                buttons.setStyleName("pithos-warnMessage");
113 a57faaf0 Christos Stathis
                outer.setStyleName("pithos-warnMessage");
114 a57faaf0 Christos Stathis
                outer.add(buttons);
115 a57faaf0 Christos Stathis
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
116 a57faaf0 Christos Stathis
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
117 a57faaf0 Christos Stathis
                setWidget(outer);
118 a57faaf0 Christos Stathis
        }
119 a57faaf0 Christos Stathis
120 a57faaf0 Christos Stathis
        /**
121 a57faaf0 Christos Stathis
         * Generate an RPC request to delete a file.
122 a57faaf0 Christos Stathis
         *
123 a57faaf0 Christos Stathis
         * @param userId the ID of the current user
124 a57faaf0 Christos Stathis
         */
125 a57faaf0 Christos Stathis
        private void deleteFile() {
126 a57faaf0 Christos Stathis
                Object selection = GSS.get().getCurrentSelection();
127 a57faaf0 Christos Stathis
                if (selection == null) {
128 a57faaf0 Christos Stathis
                        GSS.get().displayError("No file was selected");
129 a57faaf0 Christos Stathis
                        return;
130 a57faaf0 Christos Stathis
                }
131 a57faaf0 Christos Stathis
                if (selection instanceof FileResource) {
132 a57faaf0 Christos Stathis
                        FileResource file = (FileResource) selection;
133 a57faaf0 Christos Stathis
134 a57faaf0 Christos Stathis
                        DeleteCommand df = new DeleteCommand(file.getUri()){
135 a57faaf0 Christos Stathis
136 a57faaf0 Christos Stathis
                                @Override
137 a57faaf0 Christos Stathis
                                public void onComplete() {
138 a57faaf0 Christos Stathis
                                        GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
139 a57faaf0 Christos Stathis
                                        GSS.get().getStatusPanel().updateStats();
140 a57faaf0 Christos Stathis
                                }
141 a57faaf0 Christos Stathis
142 a57faaf0 Christos Stathis
                                @Override
143 a57faaf0 Christos Stathis
                                public void onError(Throwable t) {
144 a57faaf0 Christos Stathis
                                        GWT.log("", t);
145 a57faaf0 Christos Stathis
                                        if(t instanceof RestException){
146 a57faaf0 Christos Stathis
                                                int statusCode = ((RestException)t).getHttpStatusCode();
147 a57faaf0 Christos Stathis
                                                if(statusCode == 405)
148 a57faaf0 Christos Stathis
                                                        GSS.get().displayError("You don't have the necessary permissions");
149 a57faaf0 Christos Stathis
                                                else if(statusCode == 404)
150 a57faaf0 Christos Stathis
                                                        GSS.get().displayError("File not found");
151 a57faaf0 Christos Stathis
                                                else
152 a57faaf0 Christos Stathis
                                                        GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
153 a57faaf0 Christos Stathis
                                        }
154 a57faaf0 Christos Stathis
                                        else
155 a57faaf0 Christos Stathis
                                                GSS.get().displayError("System error unable to delete file: "+t.getMessage());
156 a57faaf0 Christos Stathis
                                }
157 a57faaf0 Christos Stathis
                        };
158 a57faaf0 Christos Stathis
159 a57faaf0 Christos Stathis
                        DeferredCommand.addCommand(df);
160 a57faaf0 Christos Stathis
                }
161 a57faaf0 Christos Stathis
                else if(selection instanceof List){
162 a57faaf0 Christos Stathis
                        List<FileResource> files = (List<FileResource>) selection;
163 a57faaf0 Christos Stathis
                        List<String> fileIds = new ArrayList<String>();
164 a57faaf0 Christos Stathis
                        for(FileResource f : files)
165 a57faaf0 Christos Stathis
                                fileIds.add(f.getUri());
166 a57faaf0 Christos Stathis
167 a57faaf0 Christos Stathis
                        MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){
168 a57faaf0 Christos Stathis
169 a57faaf0 Christos Stathis
                                @Override
170 a57faaf0 Christos Stathis
                                public void onComplete() {
171 a57faaf0 Christos Stathis
                                        GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
172 a57faaf0 Christos Stathis
                                }
173 a57faaf0 Christos Stathis
174 a57faaf0 Christos Stathis
                                @Override
175 a57faaf0 Christos Stathis
                                public void onError(Throwable t) {
176 a57faaf0 Christos Stathis
                                        GWT.log("", t);
177 a57faaf0 Christos Stathis
                                        GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
178 a57faaf0 Christos Stathis
                                }
179 a57faaf0 Christos Stathis
180 a57faaf0 Christos Stathis
                                @Override
181 a57faaf0 Christos Stathis
                                public void onError(String path, Throwable t) {
182 a57faaf0 Christos Stathis
                                        GWT.log("", t);
183 a57faaf0 Christos Stathis
                                        if(t instanceof RestException){
184 a57faaf0 Christos Stathis
                                                int statusCode = ((RestException)t).getHttpStatusCode();
185 a57faaf0 Christos Stathis
                                                if(statusCode == 405)
186 a57faaf0 Christos Stathis
                                                        GSS.get().displayError("You don't have the necessary permissions");
187 a57faaf0 Christos Stathis
                                                else if(statusCode == 404)
188 a57faaf0 Christos Stathis
                                                        GSS.get().displayError("File not found");
189 a57faaf0 Christos Stathis
                                                else
190 a57faaf0 Christos Stathis
                                                        GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
191 a57faaf0 Christos Stathis
                                        }
192 a57faaf0 Christos Stathis
                                        else
193 a57faaf0 Christos Stathis
                                                GSS.get().displayError("System error unable to delete file:"+t.getMessage());
194 a57faaf0 Christos Stathis
195 a57faaf0 Christos Stathis
                                }
196 a57faaf0 Christos Stathis
                        };
197 a57faaf0 Christos Stathis
198 a57faaf0 Christos Stathis
                        DeferredCommand.addCommand(ed);
199 a57faaf0 Christos Stathis
                }
200 a57faaf0 Christos Stathis
        }
201 a57faaf0 Christos Stathis
202 a57faaf0 Christos Stathis
        @Override
203 a57faaf0 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
204 a57faaf0 Christos Stathis
                super.onPreviewNativeEvent(preview);
205 a57faaf0 Christos Stathis
206 a57faaf0 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
207 a57faaf0 Christos Stathis
                if (evt.getType().equals("keydown"))
208 a57faaf0 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
209 a57faaf0 Christos Stathis
                        // enter or escape is pressed.
210 a57faaf0 Christos Stathis
                        switch (evt.getKeyCode()) {
211 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ENTER:
212 a57faaf0 Christos Stathis
                                        hide();
213 a57faaf0 Christos Stathis
                                        deleteFile();
214 a57faaf0 Christos Stathis
                                        break;
215 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
216 a57faaf0 Christos Stathis
                                        hide();
217 a57faaf0 Christos Stathis
                                        break;
218 a57faaf0 Christos Stathis
                        }
219 a57faaf0 Christos Stathis
        }
220 a57faaf0 Christos Stathis
221 a57faaf0 Christos Stathis
222 a57faaf0 Christos Stathis
223 a57faaf0 Christos Stathis
}