Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.9 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.rest.DeleteCommand;
39
import gr.grnet.pithos.web.client.rest.MultipleDeleteCommand;
40
import gr.grnet.pithos.web.client.rest.RestException;
41
import gr.grnet.pithos.web.client.rest.resource.FileResource;
42

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

    
46
import com.google.gwt.core.client.GWT;
47
import com.google.gwt.dom.client.NativeEvent;
48
import com.google.gwt.event.dom.client.ClickEvent;
49
import com.google.gwt.event.dom.client.ClickHandler;
50
import com.google.gwt.event.dom.client.KeyCodes;
51
import com.google.gwt.user.client.DeferredCommand;
52
import com.google.gwt.user.client.Event.NativePreviewEvent;
53
import com.google.gwt.user.client.ui.AbstractImagePrototype;
54
import com.google.gwt.user.client.ui.Button;
55
import com.google.gwt.user.client.ui.DialogBox;
56
import com.google.gwt.user.client.ui.HTML;
57
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58
import com.google.gwt.user.client.ui.HorizontalPanel;
59
import com.google.gwt.user.client.ui.VerticalPanel;
60

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

    
66
        /**
67
         * The widget's constructor.
68
         *
69
         * @param images the supplied images
70
         */
71
        public DeleteFileDialog(Images images) {
72
                // Set the dialog's caption.
73
                setText("Confirmation");
74
                setAnimationEnabled(true);
75
                Object selection = GSS.get().getCurrentSelection();
76
                // Create a VerticalPanel to contain the label and the buttons.
77
                VerticalPanel outer = new VerticalPanel();
78
                HorizontalPanel buttons = new HorizontalPanel();
79

    
80
                HTML text;
81
                if (selection instanceof FileResource)
82
                        text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + ((FileResource) selection).getName() + "'?</td></tr></table>");
83
                else
84
                        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>");
85
                text.setStyleName("pithos-warnMessage");
86
                outer.add(text);
87

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

    
120
        /**
121
         * Generate an RPC request to delete a file.
122
         *
123
         * @param userId the ID of the current user
124
         */
125
        private void deleteFile() {
126
                Object selection = GSS.get().getCurrentSelection();
127
                if (selection == null) {
128
                        GSS.get().displayError("No file was selected");
129
                        return;
130
                }
131
                if (selection instanceof FileResource) {
132
                        FileResource file = (FileResource) selection;
133

    
134
                        DeleteCommand df = new DeleteCommand(file.getUri()){
135

    
136
                                @Override
137
                                public void onComplete() {
138
                                        GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
139
                                        GSS.get().getStatusPanel().updateStats();
140
                                }
141

    
142
                                @Override
143
                                public void onError(Throwable t) {
144
                                        GWT.log("", t);
145
                                        if(t instanceof RestException){
146
                                                int statusCode = ((RestException)t).getHttpStatusCode();
147
                                                if(statusCode == 405)
148
                                                        GSS.get().displayError("You don't have the necessary permissions");
149
                                                else if(statusCode == 404)
150
                                                        GSS.get().displayError("File not found");
151
                                                else
152
                                                        GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
153
                                        }
154
                                        else
155
                                                GSS.get().displayError("System error unable to delete file: "+t.getMessage());
156
                                }
157
                        };
158

    
159
                        DeferredCommand.addCommand(df);
160
                }
161
                else if(selection instanceof List){
162
                        List<FileResource> files = (List<FileResource>) selection;
163
                        List<String> fileIds = new ArrayList<String>();
164
                        for(FileResource f : files)
165
                                fileIds.add(f.getUri());
166

    
167
                        MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){
168

    
169
                                @Override
170
                                public void onComplete() {
171
                                        GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
172
                                }
173

    
174
                                @Override
175
                                public void onError(Throwable t) {
176
                                        GWT.log("", t);
177
                                        GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
178
                                }
179

    
180
                                @Override
181
                                public void onError(String path, Throwable t) {
182
                                        GWT.log("", t);
183
                                        if(t instanceof RestException){
184
                                                int statusCode = ((RestException)t).getHttpStatusCode();
185
                                                if(statusCode == 405)
186
                                                        GSS.get().displayError("You don't have the necessary permissions");
187
                                                else if(statusCode == 404)
188
                                                        GSS.get().displayError("File not found");
189
                                                else
190
                                                        GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
191
                                        }
192
                                        else
193
                                                GSS.get().displayError("System error unable to delete file:"+t.getMessage());
194

    
195
                                }
196
                        };
197

    
198
                        DeferredCommand.addCommand(ed);
199
                }
200
        }
201

    
202
        @Override
203
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
204
                super.onPreviewNativeEvent(preview);
205

    
206
                NativeEvent evt = preview.getNativeEvent();
207
                if (evt.getType().equals("keydown"))
208
                        // Use the popup's key preview hooks to close the dialog when either
209
                        // enter or escape is pressed.
210
                        switch (evt.getKeyCode()) {
211
                                case KeyCodes.KEY_ENTER:
212
                                        hide();
213
                                        deleteFile();
214
                                        break;
215
                                case KeyCodes.KEY_ESCAPE:
216
                                        hide();
217
                                        break;
218
                        }
219
        }
220

    
221

    
222

    
223
}