Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.1 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.dnd.DnDTreeItem;
23
import gr.ebs.gss.client.rest.DeleteCommand;
24
import gr.ebs.gss.client.rest.RestException;
25
import gr.ebs.gss.client.rest.resource.FolderResource;
26

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

    
40
/**
41
 * The 'delete folder' dialog box.
42
 */
43
public class DeleteFolderDialog extends DialogBox {
44

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

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

    
64
                // Create the 'Delete' button, along with a listener that hides the dialog
65
                // when the button is clicked and deletes the folder.
66
                Button ok = new Button("Delete", new ClickListener() {
67

    
68
                        public void onClick(Widget sender) {
69
                                deleteFolder();
70
                                hide();
71
                        }
72
                });
73
                buttons.add(ok);
74
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
75
                // Create the 'Cancel' button, along with a listener that hides the
76
                // dialog when the button is clicked.
77
                Button cancel = new Button("Cancel", new ClickListener() {
78

    
79
                        public void onClick(Widget sender) {
80
                                hide();
81
                        }
82
                });
83
                buttons.add(cancel);
84
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
85
                buttons.setSpacing(8);
86
                buttons.setStyleName("gss-warnMessage");
87
                outer.setStyleName("gss-warnMessage");
88
                outer.add(buttons);
89
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
90
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
91
                setWidget(outer);
92
        }
93

    
94
        /**
95
         * Generate an RPC request to delete a folder.
96
         *
97
         * @param userId the ID of the current user
98
         */
99
        private void deleteFolder() {
100

    
101
                DnDTreeItem folder = (DnDTreeItem) GSS.get().getFolders().getCurrent();
102
                if (folder == null) {
103
                        GSS.get().displayError("No folder was selected");
104
                        return;
105
                }
106
                if(folder.getFolderResource() == null)
107
                        return;
108

    
109
                DeleteCommand df = new DeleteCommand(folder.getFolderResource().getUri()){
110

    
111
                        @Override
112
                        public void onComplete() {
113
                                TreeItem curFolder = GSS.get().getFolders().getCurrent();
114
                                GSS.get().getFolders().updateFolder((DnDTreeItem) curFolder.getParentItem());
115
                        }
116

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

    
134
                DeferredCommand.addCommand(df);
135
        }
136

    
137
        @Override
138
        public boolean onKeyDownPreview(final char key, final int modifiers) {
139
                // Use the popup's key preview hooks to close the dialog when either
140
                // enter or escape is pressed.
141
                switch (key) {
142
                        case KeyboardListener.KEY_ENTER:
143
                                hide();
144
                                deleteFolder();
145
                                break;
146
                        case KeyboardListener.KEY_ESCAPE:
147
                                hide();
148
                                break;
149
                }
150
                return true;
151
        }
152

    
153
}