Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.1 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 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 d4374639 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
39 3586ad5e Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
40 d4374639 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
41 d4374639 Christos Stathis
import gr.grnet.pithos.web.client.rest.DeleteRequest;
42 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
43 a57faaf0 Christos Stathis
44 d4374639 Christos Stathis
import java.util.Iterator;
45 a57faaf0 Christos Stathis
import java.util.List;
46 a57faaf0 Christos Stathis
47 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
48 ebead1b5 Christos Stathis
import com.google.gwt.core.client.Scheduler;
49 a57faaf0 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
50 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
51 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
52 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
53 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
54 5f91f72d Christos Stathis
import com.google.gwt.http.client.URL;
55 64f42c0b Christos Stathis
import com.google.gwt.user.client.Command;
56 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
57 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
58 4420a247 Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
59 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Button;
60 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
61 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
62 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
64 a57faaf0 Christos Stathis
65 a57faaf0 Christos Stathis
/**
66 a57faaf0 Christos Stathis
 * The 'delete file' dialog box.
67 a57faaf0 Christos Stathis
 */
68 a57faaf0 Christos Stathis
public class DeleteFileDialog extends DialogBox {
69 a57faaf0 Christos Stathis
70 d4374639 Christos Stathis
    private List<File> files;
71 a7c43f26 Christos Stathis
72 7811b9d1 Christos Stathis
    protected Pithos app;
73 a7c43f26 Christos Stathis
74 a57faaf0 Christos Stathis
        /**
75 a57faaf0 Christos Stathis
         * The widget's constructor.
76 a57faaf0 Christos Stathis
         *
77 a57faaf0 Christos Stathis
         * @param images the supplied images
78 a57faaf0 Christos Stathis
         */
79 a7c43f26 Christos Stathis
        public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {
80 a7c43f26 Christos Stathis
        app = _app;
81 d4374639 Christos Stathis
        files = _files;
82 948a9b34 Christos Stathis
                Anchor close = new Anchor("close");
83 4420a247 Christos Stathis
                close.addStyleName("close");
84 4420a247 Christos Stathis
                close.addClickHandler(new ClickHandler() {
85 4420a247 Christos Stathis
                        
86 4420a247 Christos Stathis
                        @Override
87 4420a247 Christos Stathis
                        public void onClick(ClickEvent event) {
88 4420a247 Christos Stathis
                                hide();
89 4420a247 Christos Stathis
                        }
90 4420a247 Christos Stathis
                });
91 a57faaf0 Christos Stathis
                // Set the dialog's caption.
92 a57faaf0 Christos Stathis
                setText("Confirmation");
93 a57faaf0 Christos Stathis
                setAnimationEnabled(true);
94 4420a247 Christos Stathis
                setGlassEnabled(true);
95 4420a247 Christos Stathis
                setStyleName("pithos-DialogBox");
96 a57faaf0 Christos Stathis
                // Create a VerticalPanel to contain the label and the buttons.
97 a57faaf0 Christos Stathis
                VerticalPanel outer = new VerticalPanel();
98 4420a247 Christos Stathis
                outer.add(close);
99 4420a247 Christos Stathis
                
100 4420a247 Christos Stathis
                VerticalPanel inner = new VerticalPanel();
101 4420a247 Christos Stathis
                inner.addStyleName("inner");
102 a57faaf0 Christos Stathis
103 a57faaf0 Christos Stathis
                HTML text;
104 d4374639 Christos Stathis
                if (files.size() == 1)
105 d4374639 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 '" + files.get(0).getName() + "'?</td></tr></table>");
106 a57faaf0 Christos Stathis
                else
107 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>");
108 a57faaf0 Christos Stathis
                text.setStyleName("pithos-warnMessage");
109 4420a247 Christos Stathis
                inner.add(text);
110 a57faaf0 Christos Stathis
111 a57faaf0 Christos Stathis
                // Create the 'Delete' button, along with a listener that hides the dialog
112 a57faaf0 Christos Stathis
                // when the button is clicked and deletes the file.
113 a57faaf0 Christos Stathis
                Button ok = new Button("Delete", new ClickHandler() {
114 a57faaf0 Christos Stathis
                        @Override
115 ebead1b5 Christos Stathis
                        public void onClick(ClickEvent event) {
116 d4374639 Christos Stathis
                                deleteFiles();
117 a57faaf0 Christos Stathis
                                hide();
118 a57faaf0 Christos Stathis
                        }
119 a57faaf0 Christos Stathis
                });
120 4420a247 Christos Stathis
                ok.addStyleName("button");
121 4420a247 Christos Stathis
                inner.add(ok);
122 4420a247 Christos Stathis
                inner.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
123 4420a247 Christos Stathis
                outer.add(inner);
124 4420a247 Christos Stathis
                outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
125 a57faaf0 Christos Stathis
                setWidget(outer);
126 a57faaf0 Christos Stathis
        }
127 a57faaf0 Christos Stathis
128 a57faaf0 Christos Stathis
        /**
129 a57faaf0 Christos Stathis
         * Generate an RPC request to delete a file.
130 a57faaf0 Christos Stathis
         */
131 7811b9d1 Christos Stathis
        protected void deleteFiles() {
132 d4374639 Christos Stathis
        Iterator<File> iter = files.iterator();
133 d4374639 Christos Stathis
        deleteFile(iter);
134 d4374639 Christos Stathis
    }
135 d4374639 Christos Stathis
136 7811b9d1 Christos Stathis
        protected void deleteFile(final Iterator<File> iter) {
137 d4374639 Christos Stathis
        if (iter.hasNext()) {
138 d4374639 Christos Stathis
            File f = iter.next();
139 b51c628b Christos Stathis
            String path = f.getUri();
140 5f91f72d Christos Stathis
            DeleteRequest deleteFile = new DeleteRequest(app.getApiPath(), f.getOwner(), URL.encode(path)) {
141 d4374639 Christos Stathis
                @Override
142 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
143 d4374639 Christos Stathis
                    deleteFile(iter);
144 d4374639 Christos Stathis
                }
145 d4374639 Christos Stathis
146 d4374639 Christos Stathis
                @Override
147 d4374639 Christos Stathis
                public void onError(Throwable t) {
148 d4374639 Christos Stathis
                    GWT.log("", t);
149 3f8622d4 Christos Stathis
                                        app.setError(t);
150 d4374639 Christos Stathis
                    if (t instanceof RestException) {
151 a7c43f26 Christos Stathis
                        app.displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());
152 d4374639 Christos Stathis
                    }
153 d4374639 Christos Stathis
                    else
154 a7c43f26 Christos Stathis
                        app.displayError("System error unable to delete file: "+t.getMessage());
155 b73a903e Christos Stathis
                    deleteFile(iter);
156 d4374639 Christos Stathis
                }
157 9539e23d Christos Stathis
158 9539e23d Christos Stathis
                                @Override
159 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
160 9539e23d Christos Stathis
                                        app.sessionExpired();
161 9539e23d Christos Stathis
                                }
162 d4374639 Christos Stathis
            };
163 a7c43f26 Christos Stathis
            deleteFile.setHeader("X-Auth-Token", app.getToken());
164 d4374639 Christos Stathis
            Scheduler.get().scheduleDeferred(deleteFile);
165 d4374639 Christos Stathis
        }
166 d4374639 Christos Stathis
        else {
167 3586ad5e Christos Stathis
                Folder f = files.get(0).getParent();
168 3586ad5e Christos Stathis
                if (app.isMySharedSelected())
169 3586ad5e Christos Stathis
                        app.updateSharedFolder(f, true, new Command() {
170 3586ad5e Christos Stathis
                                        
171 3586ad5e Christos Stathis
                                        @Override
172 3586ad5e Christos Stathis
                                        public void execute() {
173 3586ad5e Christos Stathis
                                                app.updateStatistics();
174 3586ad5e Christos Stathis
                                        }
175 3586ad5e Christos Stathis
                                });
176 3586ad5e Christos Stathis
                else
177 3586ad5e Christos Stathis
                    app.updateFolder(files.get(0).getParent(), true, new Command() {
178 3586ad5e Christos Stathis
                                        
179 3586ad5e Christos Stathis
                                        @Override
180 3586ad5e Christos Stathis
                                        public void execute() {
181 3586ad5e Christos Stathis
                                                app.updateStatistics();
182 3586ad5e Christos Stathis
                                        }
183 3586ad5e Christos Stathis
                                }, true);
184 d4374639 Christos Stathis
        }
185 d4374639 Christos Stathis
    }
186 a57faaf0 Christos Stathis
187 a57faaf0 Christos Stathis
        @Override
188 a57faaf0 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
189 a57faaf0 Christos Stathis
                super.onPreviewNativeEvent(preview);
190 a57faaf0 Christos Stathis
191 a57faaf0 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
192 a57faaf0 Christos Stathis
                if (evt.getType().equals("keydown"))
193 a57faaf0 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
194 a57faaf0 Christos Stathis
                        // enter or escape is pressed.
195 a57faaf0 Christos Stathis
                        switch (evt.getKeyCode()) {
196 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ENTER:
197 a57faaf0 Christos Stathis
                                        hide();
198 d4374639 Christos Stathis
                                        deleteFiles();
199 a57faaf0 Christos Stathis
                                        break;
200 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
201 a57faaf0 Christos Stathis
                                        hide();
202 a57faaf0 Christos Stathis
                                        break;
203 a57faaf0 Christos Stathis
                        }
204 a57faaf0 Christos Stathis
        }
205 a57faaf0 Christos Stathis
206 a57faaf0 Christos Stathis
207 a57faaf0 Christos Stathis
208 a57faaf0 Christos Stathis
}