Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.7 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 d4374639 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
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 4420a247 Christos Stathis
                Anchor close = new Anchor();
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 a57faaf0 Christos Stathis
                setAnimationEnabled(true);
93 4420a247 Christos Stathis
                setGlassEnabled(true);
94 4420a247 Christos Stathis
                setStyleName("pithos-DialogBox");
95 a57faaf0 Christos Stathis
                // Create a VerticalPanel to contain the label and the buttons.
96 a57faaf0 Christos Stathis
                VerticalPanel outer = new VerticalPanel();
97 4420a247 Christos Stathis
                outer.add(close);
98 4420a247 Christos Stathis
                
99 4420a247 Christos Stathis
                VerticalPanel inner = new VerticalPanel();
100 4420a247 Christos Stathis
                inner.addStyleName("inner");
101 a57faaf0 Christos Stathis
102 a57faaf0 Christos Stathis
                HTML text;
103 d4374639 Christos Stathis
                if (files.size() == 1)
104 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>");
105 a57faaf0 Christos Stathis
                else
106 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>");
107 a57faaf0 Christos Stathis
                text.setStyleName("pithos-warnMessage");
108 4420a247 Christos Stathis
                inner.add(text);
109 a57faaf0 Christos Stathis
110 a57faaf0 Christos Stathis
                // Create the 'Delete' button, along with a listener that hides the dialog
111 a57faaf0 Christos Stathis
                // when the button is clicked and deletes the file.
112 a57faaf0 Christos Stathis
                Button ok = new Button("Delete", new ClickHandler() {
113 a57faaf0 Christos Stathis
                        @Override
114 ebead1b5 Christos Stathis
                        public void onClick(ClickEvent event) {
115 d4374639 Christos Stathis
                                deleteFiles();
116 a57faaf0 Christos Stathis
                                hide();
117 a57faaf0 Christos Stathis
                        }
118 a57faaf0 Christos Stathis
                });
119 4420a247 Christos Stathis
                ok.addStyleName("button");
120 4420a247 Christos Stathis
                inner.add(ok);
121 4420a247 Christos Stathis
                inner.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
122 4420a247 Christos Stathis
                outer.add(inner);
123 4420a247 Christos Stathis
                outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
124 a57faaf0 Christos Stathis
                setWidget(outer);
125 a57faaf0 Christos Stathis
        }
126 a57faaf0 Christos Stathis
127 a57faaf0 Christos Stathis
        /**
128 a57faaf0 Christos Stathis
         * Generate an RPC request to delete a file.
129 a57faaf0 Christos Stathis
         */
130 7811b9d1 Christos Stathis
        protected void deleteFiles() {
131 d4374639 Christos Stathis
        Iterator<File> iter = files.iterator();
132 d4374639 Christos Stathis
        deleteFile(iter);
133 d4374639 Christos Stathis
    }
134 d4374639 Christos Stathis
135 7811b9d1 Christos Stathis
        protected void deleteFile(final Iterator<File> iter) {
136 d4374639 Christos Stathis
        if (iter.hasNext()) {
137 d4374639 Christos Stathis
            File f = iter.next();
138 b51c628b Christos Stathis
            String path = f.getUri();
139 5f91f72d Christos Stathis
            DeleteRequest deleteFile = new DeleteRequest(app.getApiPath(), f.getOwner(), URL.encode(path)) {
140 d4374639 Christos Stathis
                @Override
141 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
142 d4374639 Christos Stathis
                    deleteFile(iter);
143 d4374639 Christos Stathis
                }
144 d4374639 Christos Stathis
145 d4374639 Christos Stathis
                @Override
146 d4374639 Christos Stathis
                public void onError(Throwable t) {
147 d4374639 Christos Stathis
                    GWT.log("", t);
148 3f8622d4 Christos Stathis
                                        app.setError(t);
149 d4374639 Christos Stathis
                    if (t instanceof RestException) {
150 a7c43f26 Christos Stathis
                        app.displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());
151 d4374639 Christos Stathis
                    }
152 d4374639 Christos Stathis
                    else
153 a7c43f26 Christos Stathis
                        app.displayError("System error unable to delete file: "+t.getMessage());
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 a7c43f26 Christos Stathis
            deleteFile.setHeader("X-Auth-Token", app.getToken());
162 d4374639 Christos Stathis
            Scheduler.get().scheduleDeferred(deleteFile);
163 d4374639 Christos Stathis
        }
164 d4374639 Christos Stathis
        else {
165 64f42c0b Christos Stathis
            app.updateFolder(files.get(0).getParent(), true, new Command() {
166 64f42c0b Christos Stathis
                                
167 64f42c0b Christos Stathis
                                @Override
168 64f42c0b Christos Stathis
                                public void execute() {
169 64f42c0b Christos Stathis
                                        app.updateStatistics();
170 64f42c0b Christos Stathis
                                }
171 6acd4df3 Christos Stathis
                        }, true);
172 d4374639 Christos Stathis
        }
173 d4374639 Christos Stathis
    }
174 a57faaf0 Christos Stathis
175 a57faaf0 Christos Stathis
        @Override
176 a57faaf0 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
177 a57faaf0 Christos Stathis
                super.onPreviewNativeEvent(preview);
178 a57faaf0 Christos Stathis
179 a57faaf0 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
180 a57faaf0 Christos Stathis
                if (evt.getType().equals("keydown"))
181 a57faaf0 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
182 a57faaf0 Christos Stathis
                        // enter or escape is pressed.
183 a57faaf0 Christos Stathis
                        switch (evt.getKeyCode()) {
184 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ENTER:
185 a57faaf0 Christos Stathis
                                        hide();
186 d4374639 Christos Stathis
                                        deleteFiles();
187 a57faaf0 Christos Stathis
                                        break;
188 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
189 a57faaf0 Christos Stathis
                                        hide();
190 a57faaf0 Christos Stathis
                                        break;
191 a57faaf0 Christos Stathis
                        }
192 a57faaf0 Christos Stathis
        }
193 a57faaf0 Christos Stathis
194 a57faaf0 Christos Stathis
195 a57faaf0 Christos Stathis
196 a57faaf0 Christos Stathis
}