Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.7 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 gr.grnet.pithos.web.client.MessagePanel.Images;
38
import gr.grnet.pithos.web.client.foldertree.File;
39
import gr.grnet.pithos.web.client.foldertree.Resource;
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.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.Anchor;
57
import com.google.gwt.user.client.ui.Button;
58
import com.google.gwt.user.client.ui.DialogBox;
59
import com.google.gwt.user.client.ui.HTML;
60
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
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
                Anchor close = new Anchor();
81
                close.addStyleName("close");
82
                close.addClickHandler(new ClickHandler() {
83
                        
84
                        @Override
85
                        public void onClick(ClickEvent event) {
86
                                hide();
87
                        }
88
                });
89
                // Set the dialog's caption.
90
                setText("Confirmation");
91
                setAnimationEnabled(true);
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(app.getApiPath(), f.getOwner(), 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
                }
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
}