Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / DeleteFolderDialog.java @ 1206:292dec4eae08

History | View | Annotate | Download (6 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 org.gss_project.gss.web.client;
20

    
21
import org.gss_project.gss.web.client.MessagePanel.Images;
22
import org.gss_project.gss.web.client.rest.DeleteCommand;
23
import org.gss_project.gss.web.client.rest.RestException;
24
import org.gss_project.gss.web.client.rest.resource.FolderResource;
25
import org.gss_project.gss.web.client.rest.resource.RestResource;
26
import org.gss_project.gss.web.client.rest.resource.RestResourceWrapper;
27
import org.gss_project.gss.web.client.rest.resource.TrashFolderResource;
28

    
29
import com.google.gwt.core.client.GWT;
30
import com.google.gwt.dom.client.NativeEvent;
31
import com.google.gwt.event.dom.client.ClickEvent;
32
import com.google.gwt.event.dom.client.ClickHandler;
33
import com.google.gwt.event.dom.client.KeyCodes;
34
import com.google.gwt.user.client.DeferredCommand;
35
import com.google.gwt.user.client.Event.NativePreviewEvent;
36
import com.google.gwt.user.client.ui.AbstractImagePrototype;
37
import com.google.gwt.user.client.ui.Button;
38
import com.google.gwt.user.client.ui.DialogBox;
39
import com.google.gwt.user.client.ui.HTML;
40
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
41
import com.google.gwt.user.client.ui.HorizontalPanel;
42
import com.google.gwt.user.client.ui.VerticalPanel;
43

    
44
/**
45
 * The 'delete folder' dialog box.
46
 */
47
public class DeleteFolderDialog extends DialogBox {
48

    
49
        /**
50
         * The widget's constructor.
51
         * @param images the supplied images
52
         */
53
        public DeleteFolderDialog(Images images) {
54
                // Set the dialog's caption.
55
                setText("Confirmation");
56
                setAnimationEnabled(true);
57
                FolderResource folder = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
58
                // Create a VerticalPanel to contain the HTML label and the buttons.
59
                VerticalPanel outer = new VerticalPanel();
60
                HorizontalPanel buttons = new HorizontalPanel();
61

    
62
                HTML text = new HTML("<table><tr><td rowspan='2'>" + AbstractImagePrototype.create(images.warn()).getHTML() +
63
                                        "</td><td>" + "Are you sure you want to <b>permanently</b> delete folder '" + folder.getName() +
64
                                        "'?</td></tr></table>");
65
                text.setStyleName("gss-warnMessage");
66
                outer.add(text);
67

    
68
                // Create the 'Delete' button, along with a listener that hides the dialog
69
                // when the button is clicked and deletes the folder.
70
                Button ok = new Button("Delete", new ClickHandler() {
71
                        @Override
72
                        public void onClick(ClickEvent event) {
73
                                deleteFolder();
74
                                hide();
75
                        }
76
                });
77
                ok.getElement().setId("confirmation.ok");
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 ClickHandler() {
83
                        @Override
84
                        public void onClick(ClickEvent event) {
85
                                hide();
86
                        }
87
                });
88
                cancel.getElement().setId("confirmation.cancel");
89
                buttons.add(cancel);
90
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
91
                buttons.setSpacing(8);
92
                buttons.setStyleName("gss-warnMessage");
93
                outer.setStyleName("gss-warnMessage");
94
                outer.add(buttons);
95
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
96
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
97
                setWidget(outer);
98
        }
99

    
100
        /**
101
         * Generate an RPC request to delete a folder.
102
         *
103
         * @param userId the ID of the current user
104
         */
105
        private void deleteFolder() {
106
                RestResource folder = GSS.get().getTreeView().getSelection();
107
                if (folder == null) {
108
                        GSS.get().displayError("No folder was selected");
109
                        return;
110
                }
111
                if(!(folder instanceof RestResourceWrapper))
112
                        return;
113

    
114
                DeleteCommand df = new DeleteCommand(folder.getUri()){
115

    
116
                        @Override
117
                        public void onComplete() {
118
                                FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
119
                                if((RestResourceWrapper) GSS.get().getTreeView().getSelection() instanceof TrashFolderResource)
120
                                        GSS.get().getTreeView().updateTrashNode();
121
                                else
122
                                        GSS.get().getTreeView().updateNodeChildrenForRemove(fres.getParentURI());
123
                                GSS.get().getTreeView().clearSelection();
124
                                GSS.get().showFileList(true);
125
                                
126
                                GSS.get().getStatusPanel().updateStats();
127
                        }
128

    
129
                        @Override
130
                        public void onError(Throwable t) {
131
                                GWT.log("", t);
132
                                if(t instanceof RestException){
133
                                        int statusCode = ((RestException)t).getHttpStatusCode();
134
                                        if(statusCode == 405)
135
                                                GSS.get().displayError("You don't have the necessary permissions");
136
                                        else if(statusCode == 404)
137
                                                GSS.get().displayError("Folder not found");
138
                                        else
139
                                                GSS.get().displayError("Unable to delete folder: "+((RestException)t).getHttpStatusText());
140
                                }
141
                                else
142
                                        GSS.get().displayError("System error unable to delete folder: "+t.getMessage());
143
                        }
144
                };
145

    
146
                DeferredCommand.addCommand(df);
147
        }
148

    
149
        @Override
150
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
151
                super.onPreviewNativeEvent(preview);
152

    
153
                NativeEvent evt = preview.getNativeEvent();
154
                if (evt.getType().equals("keydown"))
155
                        // Use the popup's key preview hooks to close the dialog when either
156
                        // enter or escape is pressed.
157
                        switch (evt.getKeyCode()) {
158
                                case KeyCodes.KEY_ENTER:
159
                                        hide();
160
                                        deleteFolder();
161
                                        break;
162
                                case KeyCodes.KEY_ESCAPE:
163
                                        hide();
164
                                        break;
165
                        }
166
        }
167

    
168
}