Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / DeleteFileDialog.java @ d4374639

History | View | Annotate | Download (6.9 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import com.google.gwt.core.client.Scheduler;
38
import gr.grnet.pithos.web.client.MessagePanel.Images;
39
import gr.grnet.pithos.web.client.foldertree.File;
40
import gr.grnet.pithos.web.client.foldertree.Resource;
41
import gr.grnet.pithos.web.client.rest.DeleteCommand;
42
import gr.grnet.pithos.web.client.rest.DeleteRequest;
43
import gr.grnet.pithos.web.client.rest.MultipleDeleteCommand;
44
import gr.grnet.pithos.web.client.rest.RestException;
45
import gr.grnet.pithos.web.client.rest.resource.FileResource;
46

    
47
import java.util.ArrayList;
48
import java.util.Iterator;
49
import java.util.List;
50

    
51
import com.google.gwt.core.client.GWT;
52
import com.google.gwt.dom.client.NativeEvent;
53
import com.google.gwt.event.dom.client.ClickEvent;
54
import com.google.gwt.event.dom.client.ClickHandler;
55
import com.google.gwt.event.dom.client.KeyCodes;
56
import com.google.gwt.user.client.DeferredCommand;
57
import com.google.gwt.user.client.Event.NativePreviewEvent;
58
import com.google.gwt.user.client.ui.AbstractImagePrototype;
59
import com.google.gwt.user.client.ui.Button;
60
import com.google.gwt.user.client.ui.DialogBox;
61
import com.google.gwt.user.client.ui.HTML;
62
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63
import com.google.gwt.user.client.ui.HorizontalPanel;
64
import com.google.gwt.user.client.ui.VerticalPanel;
65

    
66
/**
67
 * The 'delete file' dialog box.
68
 */
69
public class DeleteFileDialog extends DialogBox {
70

    
71
    private List<File> files;
72
        /**
73
         * The widget's constructor.
74
         *
75
         * @param images the supplied images
76
         */
77
        public DeleteFileDialog(Images images, List<File> _files) {
78
        files = _files;
79
                // Set the dialog's caption.
80
                setText("Confirmation");
81
                setAnimationEnabled(true);
82
                // Create a VerticalPanel to contain the label and the buttons.
83
                VerticalPanel outer = new VerticalPanel();
84
                HorizontalPanel buttons = new HorizontalPanel();
85

    
86
                HTML text;
87
                if (files.size() == 1)
88
                        text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + files.get(0).getName() + "'?</td></tr></table>");
89
                else
90
                        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>");
91
                text.setStyleName("pithos-warnMessage");
92
                outer.add(text);
93

    
94
                // Create the 'Delete' button, along with a listener that hides the dialog
95
                // when the button is clicked and deletes the file.
96
                Button ok = new Button("Delete", new ClickHandler() {
97
                        @Override
98
                        public void onClick(ClickEvent event) {
99
                                deleteFiles();
100
                                hide();
101
                        }
102
                });
103
                ok.getElement().setId("confirmation.ok");
104
                buttons.add(ok);
105
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
106
                // Create the 'Cancel' button, along with a listener that hides the
107
                // dialog when the button is clicked.
108
                Button cancel = new Button("Cancel", new ClickHandler() {
109
                        @Override
110
                        public void onClick(ClickEvent event) {
111
                                hide();
112
                        }
113
                });
114
                cancel.getElement().setId("confirmation.cancel");
115
                buttons.add(cancel);
116
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
117
                buttons.setSpacing(8);
118
                buttons.setStyleName("pithos-warnMessage");
119
                outer.setStyleName("pithos-warnMessage");
120
                outer.add(buttons);
121
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
122
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
123
                setWidget(outer);
124
        }
125

    
126
        /**
127
         * Generate an RPC request to delete a file.
128
         */
129
        private void deleteFiles() {
130
        Iterator<File> iter = files.iterator();
131
        deleteFile(iter);
132
    }
133

    
134
    private void deleteFile(final Iterator<File> iter) {
135
        if (iter.hasNext()) {
136
            File f = iter.next();
137
            String path = GSS.get().getApiPath() + GSS.get().getUsername() + f.getUri();
138
            DeleteRequest deleteFile = new DeleteRequest(path) {
139
                @Override
140
                public void onSuccess(Resource result) {
141
                    deleteFile(iter);
142
                }
143

    
144
                @Override
145
                public void onError(Throwable t) {
146
                    GWT.log("", t);
147
                    if (t instanceof RestException) {
148
                        GSS.get().displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());
149
                    }
150
                    else
151
                        GSS.get().displayError("System error unable to delete file: "+t.getMessage());
152
                }
153
            };
154
            deleteFile.setHeader("X-Auth-Token", GSS.get().getToken());
155
            Scheduler.get().scheduleDeferred(deleteFile);
156
        }
157
        else {
158
            GSS.get().updateFolder(files.get(0).getParent());
159
        }
160
    }
161

    
162
        @Override
163
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
164
                super.onPreviewNativeEvent(preview);
165

    
166
                NativeEvent evt = preview.getNativeEvent();
167
                if (evt.getType().equals("keydown"))
168
                        // Use the popup's key preview hooks to close the dialog when either
169
                        // enter or escape is pressed.
170
                        switch (evt.getKeyCode()) {
171
                                case KeyCodes.KEY_ENTER:
172
                                        hide();
173
                                        deleteFiles();
174
                                        break;
175
                                case KeyCodes.KEY_ESCAPE:
176
                                        hide();
177
                                        break;
178
                        }
179
        }
180

    
181

    
182

    
183
}