Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / DeleteFileDialog.java @ 895035a2

History | View | Annotate | Download (6.4 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.MessagePanel.Images;
22
import gr.ebs.gss.client.rest.DeleteCommand;
23
import gr.ebs.gss.client.rest.MultipleDeleteCommand;
24
import gr.ebs.gss.client.rest.RestException;
25
import gr.ebs.gss.client.rest.resource.FileResource;
26

    
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import com.google.gwt.core.client.GWT;
31
import com.google.gwt.user.client.DeferredCommand;
32
import com.google.gwt.user.client.ui.Button;
33
import com.google.gwt.user.client.ui.ClickListener;
34
import com.google.gwt.user.client.ui.DialogBox;
35
import com.google.gwt.user.client.ui.HTML;
36
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
37
import com.google.gwt.user.client.ui.HorizontalPanel;
38
import com.google.gwt.user.client.ui.KeyboardListener;
39
import com.google.gwt.user.client.ui.VerticalPanel;
40
import com.google.gwt.user.client.ui.Widget;
41

    
42
/**
43
 * The 'delete file' dialog box.
44
 */
45
public class DeleteFileDialog extends DialogBox {
46

    
47
        /**
48
         * The widget's constructor.
49
         *
50
         * @param images the supplied images
51
         */
52
        public DeleteFileDialog(Images images) {
53
                // Set the dialog's caption.
54
                setText("Confirmation");
55
                setAnimationEnabled(true);
56
                Object selection = GSS.get().getCurrentSelection();
57
                // Create a VerticalPanel to contain the label and the buttons.
58
                VerticalPanel outer = new VerticalPanel();
59
                HorizontalPanel buttons = new HorizontalPanel();
60

    
61
                HTML text;
62
                if (selection instanceof FileResource)
63
                        text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + ((FileResource) selection).getName() + "'?</td></tr></table>");
64
                else
65
                        text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete the selected files?</td></tr></table>");
66
                text.setStyleName("gss-warnMessage");
67
                outer.add(text);
68

    
69
                // Create the 'Delete' button, along with a listener that hides the dialog
70
                // when the button is clicked and deletes the file.
71
                Button ok = new Button("Delete", new ClickListener() {
72

    
73
                        public void onClick(Widget sender) {
74
                                deleteFile();
75
                                hide();
76
                        }
77
                });
78
                buttons.add(ok);
79
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
80
                // Create the 'Cancel' button, along with a listener that hides the
81
                // dialog when the button is clicked.
82
                Button cancel = new Button("Cancel", new ClickListener() {
83

    
84
                        public void onClick(Widget sender) {
85
                                hide();
86
                        }
87
                });
88
                buttons.add(cancel);
89
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
90
                buttons.setSpacing(8);
91
                buttons.setStyleName("gss-warnMessage");
92
                outer.setStyleName("gss-warnMessage");
93
                outer.add(buttons);
94
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
95
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
96
                setWidget(outer);
97
        }
98

    
99
        /**
100
         * Generate an RPC request to delete a file.
101
         *
102
         * @param userId the ID of the current user
103
         */
104
        private void deleteFile() {
105
                Object selection = GSS.get().getCurrentSelection();
106
                if (selection == null) {
107
                        GSS.get().displayError("No file was selected");
108
                        return;
109
                }
110
                if (selection instanceof FileResource) {
111
                        FileResource file = (FileResource) selection;
112

    
113
                        DeleteCommand df = new DeleteCommand(file.getUri()){
114

    
115
                                @Override
116
                                public void onComplete() {
117
                                        GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
118
                                        GSS.get().getStatusPanel().updateStats();
119
                                }
120

    
121
                                @Override
122
                                public void onError(Throwable t) {
123
                                        GWT.log("", t);
124
                                        if(t instanceof RestException){
125
                                                int statusCode = ((RestException)t).getHttpStatusCode();
126
                                                if(statusCode == 405)
127
                                                        GSS.get().displayError("You don't have the necessary permissions");
128
                                                else if(statusCode == 404)
129
                                                        GSS.get().displayError("File not found");
130
                                                else
131
                                                        GSS.get().displayError("Unable to delete file: "+((RestException)t).getHttpStatusText());
132
                                        }
133
                                        else
134
                                                GSS.get().displayError("System error unable to delete file: "+t.getMessage());
135
                                }
136
                        };
137

    
138
                        DeferredCommand.addCommand(df);
139
                }
140
                else if(selection instanceof List){
141
                        List<FileResource> files = (List<FileResource>) selection;
142
                        List<String> fileIds = new ArrayList<String>();
143
                        for(FileResource f : files)
144
                                fileIds.add(f.getUri());
145

    
146
                        MultipleDeleteCommand ed = new MultipleDeleteCommand(fileIds.toArray(new String[0])){
147

    
148
                                @Override
149
                                public void onComplete() {
150
                                        GSS.get().showFileList(true);
151
                                }
152

    
153
                                @Override
154
                                public void onError(Throwable t) {
155
                                        GWT.log("", t);
156
                                        GSS.get().showFileList(true);
157
                                }
158

    
159
                                @Override
160
                                public void onError(String path, Throwable t) {
161
                                        GWT.log("", t);
162
                                        if(t instanceof RestException){
163
                                                int statusCode = ((RestException)t).getHttpStatusCode();
164
                                                if(statusCode == 405)
165
                                                        GSS.get().displayError("You don't have the necessary permissions");
166
                                                else if(statusCode == 404)
167
                                                        GSS.get().displayError("File not found");
168
                                                else
169
                                                        GSS.get().displayError("Unable to delete file:"+((RestException)t).getHttpStatusText());
170
                                        }
171
                                        else
172
                                                GSS.get().displayError("System error unable to delete file:"+t.getMessage());
173

    
174
                                }
175
                        };
176

    
177
                        DeferredCommand.addCommand(ed);
178
                }
179
        }
180

    
181
        @Override
182
        public boolean onKeyDownPreview(final char key, final int modifiers) {
183
                // Use the popup's key preview hooks to close the dialog when either
184
                // enter or escape is pressed.
185
                switch (key) {
186
                        case KeyboardListener.KEY_ENTER:
187
                                hide();
188
                                deleteFile();
189
                                break;
190
                        case KeyboardListener.KEY_ESCAPE:
191
                                hide();
192
                                break;
193
                }
194

    
195
                return true;
196
        }
197

    
198
}