Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.7 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 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 64f42c0b Christos Stathis
import com.google.gwt.user.client.Command;
54 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
55 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
56 4420a247 Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
57 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Button;
58 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
59 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
60 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
61 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
62 a57faaf0 Christos Stathis
63 a57faaf0 Christos Stathis
/**
64 a57faaf0 Christos Stathis
 * The 'delete file' dialog box.
65 a57faaf0 Christos Stathis
 */
66 a57faaf0 Christos Stathis
public class DeleteFileDialog extends DialogBox {
67 a57faaf0 Christos Stathis
68 d4374639 Christos Stathis
    private List<File> files;
69 a7c43f26 Christos Stathis
70 7811b9d1 Christos Stathis
    protected Pithos app;
71 a7c43f26 Christos Stathis
72 a57faaf0 Christos Stathis
        /**
73 a57faaf0 Christos Stathis
         * The widget's constructor.
74 a57faaf0 Christos Stathis
         *
75 a57faaf0 Christos Stathis
         * @param images the supplied images
76 a57faaf0 Christos Stathis
         */
77 a7c43f26 Christos Stathis
        public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {
78 a7c43f26 Christos Stathis
        app = _app;
79 d4374639 Christos Stathis
        files = _files;
80 4420a247 Christos Stathis
                Anchor close = new Anchor();
81 4420a247 Christos Stathis
                close.addStyleName("close");
82 4420a247 Christos Stathis
                close.addClickHandler(new ClickHandler() {
83 4420a247 Christos Stathis
                        
84 4420a247 Christos Stathis
                        @Override
85 4420a247 Christos Stathis
                        public void onClick(ClickEvent event) {
86 4420a247 Christos Stathis
                                hide();
87 4420a247 Christos Stathis
                        }
88 4420a247 Christos Stathis
                });
89 a57faaf0 Christos Stathis
                // Set the dialog's caption.
90 a57faaf0 Christos Stathis
                setText("Confirmation");
91 a57faaf0 Christos Stathis
                setAnimationEnabled(true);
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 63ee965c Christos Stathis
            DeleteRequest deleteFile = new DeleteRequest(app.getApiPath(), f.getOwner(), 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 d4374639 Christos Stathis
                }
154 9539e23d Christos Stathis
155 9539e23d Christos Stathis
                                @Override
156 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
157 9539e23d Christos Stathis
                                        app.sessionExpired();
158 9539e23d Christos Stathis
                                }
159 d4374639 Christos Stathis
            };
160 a7c43f26 Christos Stathis
            deleteFile.setHeader("X-Auth-Token", app.getToken());
161 d4374639 Christos Stathis
            Scheduler.get().scheduleDeferred(deleteFile);
162 d4374639 Christos Stathis
        }
163 d4374639 Christos Stathis
        else {
164 64f42c0b Christos Stathis
            app.updateFolder(files.get(0).getParent(), true, new Command() {
165 64f42c0b Christos Stathis
                                
166 64f42c0b Christos Stathis
                                @Override
167 64f42c0b Christos Stathis
                                public void execute() {
168 64f42c0b Christos Stathis
                                        app.updateStatistics();
169 64f42c0b Christos Stathis
                                }
170 64f42c0b Christos Stathis
                        });
171 d4374639 Christos Stathis
        }
172 d4374639 Christos Stathis
    }
173 a57faaf0 Christos Stathis
174 a57faaf0 Christos Stathis
        @Override
175 a57faaf0 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
176 a57faaf0 Christos Stathis
                super.onPreviewNativeEvent(preview);
177 a57faaf0 Christos Stathis
178 a57faaf0 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
179 a57faaf0 Christos Stathis
                if (evt.getType().equals("keydown"))
180 a57faaf0 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
181 a57faaf0 Christos Stathis
                        // enter or escape is pressed.
182 a57faaf0 Christos Stathis
                        switch (evt.getKeyCode()) {
183 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ENTER:
184 a57faaf0 Christos Stathis
                                        hide();
185 d4374639 Christos Stathis
                                        deleteFiles();
186 a57faaf0 Christos Stathis
                                        break;
187 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
188 a57faaf0 Christos Stathis
                                        hide();
189 a57faaf0 Christos Stathis
                                        break;
190 a57faaf0 Christos Stathis
                        }
191 a57faaf0 Christos Stathis
        }
192 a57faaf0 Christos Stathis
193 a57faaf0 Christos Stathis
194 a57faaf0 Christos Stathis
195 a57faaf0 Christos Stathis
}