Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / DeleteFileDialog.java @ 4cef6f04

History | View | Annotate | Download (6.8 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.client;
20 14ad7326 pastith
21 14ad7326 pastith
import gr.ebs.gss.client.MessagePanel.Images;
22 895035a2 pastith
import gr.ebs.gss.client.rest.DeleteCommand;
23 895035a2 pastith
import gr.ebs.gss.client.rest.MultipleDeleteCommand;
24 a52ea5e4 pastith
import gr.ebs.gss.client.rest.RestException;
25 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FileResource;
26 14ad7326 pastith
27 14ad7326 pastith
import java.util.ArrayList;
28 14ad7326 pastith
import java.util.List;
29 14ad7326 pastith
30 14ad7326 pastith
import com.google.gwt.core.client.GWT;
31 afd3a0ef Giannis Koutsoubos
import com.google.gwt.dom.client.NativeEvent;
32 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ClickEvent;
33 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ClickHandler;
34 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.KeyCodes;
35 a52ea5e4 pastith
import com.google.gwt.user.client.DeferredCommand;
36 afd3a0ef Giannis Koutsoubos
import com.google.gwt.user.client.Event.NativePreviewEvent;
37 afd3a0ef Giannis Koutsoubos
import com.google.gwt.user.client.ui.AbstractImagePrototype;
38 14ad7326 pastith
import com.google.gwt.user.client.ui.Button;
39 14ad7326 pastith
import com.google.gwt.user.client.ui.DialogBox;
40 14ad7326 pastith
import com.google.gwt.user.client.ui.HTML;
41 14ad7326 pastith
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
42 14ad7326 pastith
import com.google.gwt.user.client.ui.HorizontalPanel;
43 14ad7326 pastith
import com.google.gwt.user.client.ui.VerticalPanel;
44 14ad7326 pastith
45 14ad7326 pastith
/**
46 14ad7326 pastith
 * The 'delete file' dialog box.
47 14ad7326 pastith
 */
48 14ad7326 pastith
public class DeleteFileDialog extends DialogBox {
49 14ad7326 pastith
50 14ad7326 pastith
        /**
51 14ad7326 pastith
         * The widget's constructor.
52 14ad7326 pastith
         *
53 14ad7326 pastith
         * @param images the supplied images
54 14ad7326 pastith
         */
55 d2c63e7e pastith
        public DeleteFileDialog(Images images) {
56 d2c63e7e pastith
                // Set the dialog's caption.
57 d2c63e7e pastith
                setText("Confirmation");
58 14ad7326 pastith
                setAnimationEnabled(true);
59 14ad7326 pastith
                Object selection = GSS.get().getCurrentSelection();
60 d2c63e7e pastith
                // Create a VerticalPanel to contain the label and the buttons.
61 d2c63e7e pastith
                VerticalPanel outer = new VerticalPanel();
62 d2c63e7e pastith
                HorizontalPanel buttons = new HorizontalPanel();
63 d2c63e7e pastith
64 d2c63e7e pastith
                HTML text;
65 9f712d7c koutsoub
                if (selection instanceof FileResource)
66 afd3a0ef Giannis Koutsoubos
                        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>");
67 14ad7326 pastith
                else
68 afd3a0ef Giannis Koutsoubos
                        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>");
69 14ad7326 pastith
                text.setStyleName("gss-warnMessage");
70 14ad7326 pastith
                outer.add(text);
71 14ad7326 pastith
72 d2c63e7e pastith
                // Create the 'Delete' button, along with a listener that hides the dialog
73 d2c63e7e pastith
                // when the button is clicked and deletes the file.
74 afd3a0ef Giannis Koutsoubos
                Button ok = new Button("Delete", new ClickHandler() {
75 afd3a0ef Giannis Koutsoubos
                        @Override
76 afd3a0ef Giannis Koutsoubos
                        public void onClick(ClickEvent event) {
77 a52ea5e4 pastith
                                deleteFile();
78 14ad7326 pastith
                                hide();
79 14ad7326 pastith
                        }
80 14ad7326 pastith
                });
81 14ad7326 pastith
                buttons.add(ok);
82 14ad7326 pastith
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
83 14ad7326 pastith
                // Create the 'Cancel' button, along with a listener that hides the
84 d2c63e7e pastith
                // dialog when the button is clicked.
85 afd3a0ef Giannis Koutsoubos
                Button cancel = new Button("Cancel", new ClickHandler() {
86 afd3a0ef Giannis Koutsoubos
                        @Override
87 afd3a0ef Giannis Koutsoubos
                        public void onClick(ClickEvent event) {
88 14ad7326 pastith
                                hide();
89 14ad7326 pastith
                        }
90 14ad7326 pastith
                });
91 14ad7326 pastith
                buttons.add(cancel);
92 14ad7326 pastith
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
93 14ad7326 pastith
                buttons.setSpacing(8);
94 14ad7326 pastith
                buttons.setStyleName("gss-warnMessage");
95 14ad7326 pastith
                outer.setStyleName("gss-warnMessage");
96 14ad7326 pastith
                outer.add(buttons);
97 14ad7326 pastith
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
98 14ad7326 pastith
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
99 14ad7326 pastith
                setWidget(outer);
100 14ad7326 pastith
        }
101 14ad7326 pastith
102 14ad7326 pastith
        /**
103 14ad7326 pastith
         * Generate an RPC request to delete a file.
104 14ad7326 pastith
         *
105 14ad7326 pastith
         * @param userId the ID of the current user
106 14ad7326 pastith
         */
107 a52ea5e4 pastith
        private void deleteFile() {
108 d2c63e7e pastith
                Object selection = GSS.get().getCurrentSelection();
109 14ad7326 pastith
                if (selection == null) {
110 d2c63e7e pastith
                        GSS.get().displayError("No file was selected");
111 14ad7326 pastith
                        return;
112 14ad7326 pastith
                }
113 a52ea5e4 pastith
                if (selection instanceof FileResource) {
114 a52ea5e4 pastith
                        FileResource file = (FileResource) selection;
115 d2c63e7e pastith
116 895035a2 pastith
                        DeleteCommand df = new DeleteCommand(file.getUri()){
117 a52ea5e4 pastith
118 d2c63e7e pastith
                                @Override
119 a52ea5e4 pastith
                                public void onComplete() {
120 18a508ee fstamatelopoulos
                                        GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
121 14ad7326 pastith
                                        GSS.get().getStatusPanel().updateStats();
122 14ad7326 pastith
                                }
123 14ad7326 pastith
124 d2c63e7e pastith
                                @Override
125 a52ea5e4 pastith
                                public void onError(Throwable t) {
126 a52ea5e4 pastith
                                        GWT.log("", t);
127 a52ea5e4 pastith
                                        if(t instanceof RestException){
128 a52ea5e4 pastith
                                                int statusCode = ((RestException)t).getHttpStatusCode();
129 a52ea5e4 pastith
                                                if(statusCode == 405)
130 a52ea5e4 pastith
                                                        GSS.get().displayError("You don't have the necessary permissions");
131 a52ea5e4 pastith
                                                else if(statusCode == 404)
132 a52ea5e4 pastith
                                                        GSS.get().displayError("File not found");
133 a52ea5e4 pastith
                                                else
134 d2c63e7e pastith
                                                        GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
135 a52ea5e4 pastith
                                        }
136 14ad7326 pastith
                                        else
137 d2c63e7e pastith
                                                GSS.get().displayError("System error unable to delete file: "+t.getMessage());
138 14ad7326 pastith
                                }
139 a52ea5e4 pastith
                        };
140 a52ea5e4 pastith
141 d2c63e7e pastith
                        DeferredCommand.addCommand(df);
142 14ad7326 pastith
                }
143 14ad7326 pastith
                else if(selection instanceof List){
144 a52ea5e4 pastith
                        List<FileResource> files = (List<FileResource>) selection;
145 d2c63e7e pastith
                        List<String> fileIds = new ArrayList<String>();
146 a52ea5e4 pastith
                        for(FileResource f : files)
147 555e8e59 pastith
                                fileIds.add(f.getUri());
148 d2c63e7e pastith
149 895035a2 pastith
                        MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){
150 a52ea5e4 pastith
151 d2c63e7e pastith
                                @Override
152 a52ea5e4 pastith
                                public void onComplete() {
153 a52ea5e4 pastith
                                        GSS.get().showFileList(true);
154 14ad7326 pastith
                                }
155 14ad7326 pastith
156 a52ea5e4 pastith
                                @Override
157 a52ea5e4 pastith
                                public void onError(Throwable t) {
158 a52ea5e4 pastith
                                        GWT.log("", t);
159 a52ea5e4 pastith
                                        GSS.get().showFileList(true);
160 a52ea5e4 pastith
                                }
161 a52ea5e4 pastith
162 a52ea5e4 pastith
                                @Override
163 a52ea5e4 pastith
                                public void onError(String path, Throwable t) {
164 a52ea5e4 pastith
                                        GWT.log("", t);
165 a52ea5e4 pastith
                                        if(t instanceof RestException){
166 a52ea5e4 pastith
                                                int statusCode = ((RestException)t).getHttpStatusCode();
167 a52ea5e4 pastith
                                                if(statusCode == 405)
168 a52ea5e4 pastith
                                                        GSS.get().displayError("You don't have the necessary permissions");
169 a52ea5e4 pastith
                                                else if(statusCode == 404)
170 a52ea5e4 pastith
                                                        GSS.get().displayError("File not found");
171 a52ea5e4 pastith
                                                else
172 e08c358f koutsoub
                                                        GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
173 a52ea5e4 pastith
                                        }
174 14ad7326 pastith
                                        else
175 a52ea5e4 pastith
                                                GSS.get().displayError("System error unable to delete file:"+t.getMessage());
176 a52ea5e4 pastith
177 14ad7326 pastith
                                }
178 a52ea5e4 pastith
                        };
179 d2c63e7e pastith
180 a52ea5e4 pastith
                        DeferredCommand.addCommand(ed);
181 14ad7326 pastith
                }
182 14ad7326 pastith
        }
183 14ad7326 pastith
184 d2c63e7e pastith
        @Override
185 afd3a0ef Giannis Koutsoubos
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
186 afd3a0ef Giannis Koutsoubos
                super.onPreviewNativeEvent(preview);
187 afd3a0ef Giannis Koutsoubos
188 afd3a0ef Giannis Koutsoubos
                NativeEvent evt = preview.getNativeEvent();
189 afd3a0ef Giannis Koutsoubos
                if (evt.getType().equals("keydown"))
190 afd3a0ef Giannis Koutsoubos
                        // Use the popup's key preview hooks to close the dialog when either
191 afd3a0ef Giannis Koutsoubos
                        // enter or escape is pressed.
192 afd3a0ef Giannis Koutsoubos
                        switch (evt.getKeyCode()) {
193 afd3a0ef Giannis Koutsoubos
                                case KeyCodes.KEY_ENTER:
194 afd3a0ef Giannis Koutsoubos
                                        hide();
195 afd3a0ef Giannis Koutsoubos
                                        deleteFile();
196 afd3a0ef Giannis Koutsoubos
                                        break;
197 afd3a0ef Giannis Koutsoubos
                                case KeyCodes.KEY_ESCAPE:
198 afd3a0ef Giannis Koutsoubos
                                        hide();
199 afd3a0ef Giannis Koutsoubos
                                        break;
200 afd3a0ef Giannis Koutsoubos
                        }
201 14ad7326 pastith
        }
202 14ad7326 pastith
203 afd3a0ef Giannis Koutsoubos
204 afd3a0ef Giannis Koutsoubos
205 14ad7326 pastith
}