Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7 kB)

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