Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.1 kB)

1
/*
2
 * Copyright 2011 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 com.google.gwt.core.client.Scheduler;
38
import gr.grnet.pithos.web.client.MessagePanel.Images;
39
import gr.grnet.pithos.web.client.foldertree.File;
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.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.user.client.Command;
54
import com.google.gwt.user.client.Event.NativePreviewEvent;
55
import com.google.gwt.user.client.ui.AbstractImagePrototype;
56
import com.google.gwt.user.client.ui.Button;
57
import com.google.gwt.user.client.ui.DialogBox;
58
import com.google.gwt.user.client.ui.HTML;
59
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
60
import com.google.gwt.user.client.ui.HorizontalPanel;
61
import com.google.gwt.user.client.ui.VerticalPanel;
62

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

    
68
    private List<File> files;
69

    
70
    protected Pithos app;
71

    
72
        /**
73
         * The widget's constructor.
74
         *
75
         * @param images the supplied images
76
         */
77
        public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {
78
        app = _app;
79
        files = _files;
80
                // Set the dialog's caption.
81
                setText("Confirmation");
82
                setAnimationEnabled(true);
83
                // Create a VerticalPanel to contain the label and the buttons.
84
                VerticalPanel outer = new VerticalPanel();
85
                HorizontalPanel buttons = new HorizontalPanel();
86

    
87
                HTML text;
88
                if (files.size() == 1)
89
                        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>");
90
                else
91
                        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>");
92
                text.setStyleName("pithos-warnMessage");
93
                outer.add(text);
94

    
95
                // Create the 'Delete' button, along with a listener that hides the dialog
96
                // when the button is clicked and deletes the file.
97
                Button ok = new Button("Delete", new ClickHandler() {
98
                        @Override
99
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
100
                                deleteFiles();
101
                                hide();
102
                        }
103
                });
104
                ok.getElement().setId("confirmation.ok");
105
                buttons.add(ok);
106
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
107
                // Create the 'Cancel' button, along with a listener that hides the
108
                // dialog when the button is clicked.
109
                Button cancel = new Button("Cancel", new ClickHandler() {
110
                        @Override
111
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
112
                                hide();
113
                        }
114
                });
115
                cancel.getElement().setId("confirmation.cancel");
116
                buttons.add(cancel);
117
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
118
                buttons.setSpacing(8);
119
                buttons.setStyleName("pithos-warnMessage");
120
                outer.setStyleName("pithos-warnMessage");
121
                outer.add(buttons);
122
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
123
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
124
                setWidget(outer);
125
        }
126

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

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

    
145
                @Override
146
                public void onError(Throwable t) {
147
                    GWT.log("", 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
                }
154

    
155
                                @Override
156
                                protected void onUnauthorized(Response response) {
157
                                        app.sessionExpired();
158
                                }
159
            };
160
            deleteFile.setHeader("X-Auth-Token", app.getToken());
161
            Scheduler.get().scheduleDeferred(deleteFile);
162
        }
163
        else {
164
            app.updateFolder(files.get(0).getParent(), true, new Command() {
165
                                
166
                                @Override
167
                                public void execute() {
168
                                        app.updateStatistics();
169
                                }
170
                        });
171
        }
172
    }
173

    
174
        @Override
175
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
176
                super.onPreviewNativeEvent(preview);
177

    
178
                NativeEvent evt = preview.getNativeEvent();
179
                if (evt.getType().equals("keydown"))
180
                        // Use the popup's key preview hooks to close the dialog when either
181
                        // enter or escape is pressed.
182
                        switch (evt.getKeyCode()) {
183
                                case KeyCodes.KEY_ENTER:
184
                                        hide();
185
                                        deleteFiles();
186
                                        break;
187
                                case KeyCodes.KEY_ESCAPE:
188
                                        hide();
189
                                        break;
190
                        }
191
        }
192

    
193

    
194

    
195
}