Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / DeleteFolderDialog.java @ afd3a0ef

History | View | Annotate | Download (5.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 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.dom.client.NativeEvent;
29
import com.google.gwt.event.dom.client.ClickEvent;
30
import com.google.gwt.event.dom.client.ClickHandler;
31
import com.google.gwt.event.dom.client.KeyCodes;
32
import com.google.gwt.user.client.DeferredCommand;
33
import com.google.gwt.user.client.Event.NativePreviewEvent;
34
import com.google.gwt.user.client.ui.AbstractImagePrototype;
35
import com.google.gwt.user.client.ui.Button;
36
import com.google.gwt.user.client.ui.DialogBox;
37
import com.google.gwt.user.client.ui.HTML;
38
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
39
import com.google.gwt.user.client.ui.HorizontalPanel;
40
import com.google.gwt.user.client.ui.TreeItem;
41
import com.google.gwt.user.client.ui.VerticalPanel;
42

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

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

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

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

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

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

    
112
                DeleteCommand df = new DeleteCommand(folder.getFolderResource().getUri()){
113

    
114
                        @Override
115
                        public void onComplete() {
116
                                TreeItem curFolder = GSS.get().getFolders().getCurrent();
117
                                if(curFolder.getParentItem() != null){
118
                                        GSS.get().getFolders().select(curFolder.getParentItem());
119
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) curFolder.getParentItem());
120
                                }
121
                                GSS.get().showFileList(true);
122
                                GSS.get().getStatusPanel().updateStats();
123
                        }
124

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

    
142
                DeferredCommand.addCommand(df);
143
        }
144

    
145
        @Override
146
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
147
                super.onPreviewNativeEvent(preview);
148

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

    
164
}