Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / DeleteFileDialog.java @ 12190a0c

History | View | Annotate | Download (6.7 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.DeleteRequest;
42
import gr.grnet.pithos.web.client.rest.RestException;
43

    
44
import java.util.Iterator;
45
import java.util.List;
46

    
47
import com.google.gwt.core.client.GWT;
48
import com.google.gwt.dom.client.NativeEvent;
49
import com.google.gwt.event.dom.client.ClickEvent;
50
import com.google.gwt.event.dom.client.ClickHandler;
51
import com.google.gwt.event.dom.client.KeyCodes;
52
import com.google.gwt.user.client.Event.NativePreviewEvent;
53
import com.google.gwt.user.client.ui.AbstractImagePrototype;
54
import com.google.gwt.user.client.ui.Button;
55
import com.google.gwt.user.client.ui.DialogBox;
56
import com.google.gwt.user.client.ui.HTML;
57
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58
import com.google.gwt.user.client.ui.HorizontalPanel;
59
import com.google.gwt.user.client.ui.VerticalPanel;
60

    
61
/**
62
 * The 'delete file' dialog box.
63
 */
64
public class DeleteFileDialog extends DialogBox {
65

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

    
81
                HTML text;
82
                if (files.size() == 1)
83
                        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>");
84
                else
85
                        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>");
86
                text.setStyleName("pithos-warnMessage");
87
                outer.add(text);
88

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

    
121
        /**
122
         * Generate an RPC request to delete a file.
123
         */
124
        private void deleteFiles() {
125
        Iterator<File> iter = files.iterator();
126
        deleteFile(iter);
127
    }
128

    
129
    private void deleteFile(final Iterator<File> iter) {
130
        if (iter.hasNext()) {
131
            File f = iter.next();
132
            String path = f.getUri();
133
            DeleteRequest deleteFile = new DeleteRequest(Pithos.get().getApiPath(), Pithos.get().getUsername(), path) {
134
                @Override
135
                public void onSuccess(Resource result) {
136
                    deleteFile(iter);
137
                }
138

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

    
157
        @Override
158
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
159
                super.onPreviewNativeEvent(preview);
160

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

    
176

    
177

    
178
}