Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.1 kB)

1
/*
2
 * Copyright 2011-2012 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.foldertree.Resource;
41
import gr.grnet.pithos.web.client.rest.DeleteRequest;
42
import gr.grnet.pithos.web.client.rest.RestException;
43

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

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

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

    
70
    private List<File> files;
71

    
72
    protected Pithos app;
73

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

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

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

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

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

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

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

    
187
        @Override
188
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
189
                super.onPreviewNativeEvent(preview);
190

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

    
206

    
207

    
208
}