Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7 kB)

1
/*
2
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.MessagePanel.Images;
38
import gr.grnet.pithos.web.client.foldertree.File;
39
import gr.grnet.pithos.web.client.foldertree.Folder;
40
import gr.grnet.pithos.web.client.rest.DeleteRequest;
41
import gr.grnet.pithos.web.client.rest.RestException;
42

    
43
import java.util.Iterator;
44
import java.util.List;
45

    
46
import com.google.gwt.core.client.GWT;
47
import com.google.gwt.core.client.Scheduler;
48
import com.google.gwt.dom.client.NativeEvent;
49
import com.google.gwt.event.dom.client.ClickEvent;
50
import com.google.gwt.event.dom.client.ClickHandler;
51
import com.google.gwt.event.dom.client.KeyCodes;
52
import com.google.gwt.http.client.Response;
53
import com.google.gwt.http.client.URL;
54
import com.google.gwt.user.client.Command;
55
import com.google.gwt.user.client.Event.NativePreviewEvent;
56
import com.google.gwt.user.client.ui.AbstractImagePrototype;
57
import com.google.gwt.user.client.ui.Anchor;
58
import com.google.gwt.user.client.ui.Button;
59
import com.google.gwt.user.client.ui.DialogBox;
60
import com.google.gwt.user.client.ui.HTML;
61
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
62
import com.google.gwt.user.client.ui.VerticalPanel;
63

    
64
/**
65
 * The 'delete file' dialog box.
66
 */
67
public class DeleteFileDialog extends DialogBox {
68

    
69
    private List<File> files;
70

    
71
    protected Pithos app;
72

    
73
        /**
74
         * The widget's constructor.
75
         *
76
         * @param images the supplied images
77
         */
78
        public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {
79
        app = _app;
80
        files = _files;
81
                Anchor close = new Anchor("close");
82
                close.addStyleName("close");
83
                close.addClickHandler(new ClickHandler() {
84
                        
85
                        @Override
86
                        public void onClick(ClickEvent event) {
87
                                hide();
88
                        }
89
                });
90
                // Set the dialog's caption.
91
                setText("Confirmation");
92
                setGlassEnabled(true);
93
                setStyleName("pithos-DialogBox");
94
                // Create a VerticalPanel to contain the label and the buttons.
95
                VerticalPanel outer = new VerticalPanel();
96
                outer.add(close);
97
                
98
                VerticalPanel inner = new VerticalPanel();
99
                inner.addStyleName("inner");
100

    
101
                HTML text;
102
                if (files.size() == 1)
103
                        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
                else
105
                        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
                text.setStyleName("pithos-warnMessage");
107
                inner.add(text);
108

    
109
                // Create the 'Delete' button, along with a listener that hides the dialog
110
                // when the button is clicked and deletes the file.
111
                Button ok = new Button("Delete", new ClickHandler() {
112
                        @Override
113
                        public void onClick(ClickEvent event) {
114
                                deleteFiles();
115
                                hide();
116
                        }
117
                });
118
                ok.addStyleName("button");
119
                inner.add(ok);
120
                inner.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
121
                outer.add(inner);
122
                outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
123
                setWidget(outer);
124
        }
125

    
126
        /**
127
         * Generate an RPC request to delete a file.
128
         */
129
        protected void deleteFiles() {
130
        Iterator<File> iter = files.iterator();
131
        deleteFile(iter);
132
    }
133

    
134
        protected void deleteFile(final Iterator<File> iter) {
135
        if (iter.hasNext()) {
136
            File f = iter.next();
137
            String path = f.getUri();
138
            DeleteRequest deleteFile = new DeleteRequest(Pithos.getStorageAPIURL(), f.getOwnerID(), URL.encode(path)) {
139
                @Override
140
                public void onSuccess(Resource result) {
141
                    deleteFile(iter);
142
                }
143

    
144
                @Override
145
                public void onError(Throwable t) {
146
                    GWT.log("", t);
147
                                        app.setError(t);
148
                    if (t instanceof RestException) {
149
                        app.displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());
150
                    }
151
                    else
152
                        app.displayError("System error unable to delete file: "+t.getMessage());
153
                    deleteFile(iter);
154
                }
155

    
156
                                @Override
157
                                protected void onUnauthorized(Response response) {
158
                                        app.sessionExpired();
159
                                }
160
            };
161
            deleteFile.setHeader("X-Auth-Token", app.getUserToken());
162
            Scheduler.get().scheduleDeferred(deleteFile);
163
        }
164
        else {
165
                Folder f = files.get(0).getParent();
166
                if (app.isMySharedSelected())
167
                        app.updateSharedFolder(f, true, new Command() {
168
                                        
169
                                        @Override
170
                                        public void execute() {
171
                                                app.updateStatistics();
172
                                        }
173
                                });
174
                else
175
                    app.updateFolder(files.get(0).getParent(), true, new Command() {
176
                                        
177
                                        @Override
178
                                        public void execute() {
179
                                                app.updateStatistics();
180
                                        }
181
                                }, true);
182
        }
183
    }
184

    
185
        @Override
186
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
187
                super.onPreviewNativeEvent(preview);
188

    
189
                NativeEvent evt = preview.getNativeEvent();
190
                if (evt.getType().equals("keydown"))
191
                        // Use the popup's key preview hooks to close the dialog when either
192
                        // enter or escape is pressed.
193
                        switch (evt.getKeyCode()) {
194
                                case KeyCodes.KEY_ENTER:
195
                                        hide();
196
                                        deleteFiles();
197
                                        break;
198
                                case KeyCodes.KEY_ESCAPE:
199
                                        hide();
200
                                        break;
201
                        }
202
        }
203

    
204

    
205

    
206
}