Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FilePropertiesDialog.java @ 9326b841

History | View | Annotate | Download (13.2 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 gr.grnet.pithos.web.client.foldertree.File;
38
import gr.grnet.pithos.web.client.foldertree.Resource;
39
import gr.grnet.pithos.web.client.rest.PostRequest;
40
import gr.grnet.pithos.web.client.rest.PutRequest;
41

    
42
import java.util.HashMap;
43
import java.util.Map;
44

    
45
import com.google.gwt.core.client.GWT;
46
import com.google.gwt.core.client.Scheduler;
47
import com.google.gwt.event.dom.client.ClickEvent;
48
import com.google.gwt.event.dom.client.ClickHandler;
49
import com.google.gwt.http.client.Response;
50
import com.google.gwt.http.client.URL;
51
import com.google.gwt.i18n.client.DateTimeFormat;
52
import com.google.gwt.user.client.Command;
53
import com.google.gwt.user.client.ui.Anchor;
54
import com.google.gwt.user.client.ui.Button;
55
import com.google.gwt.user.client.ui.DecoratedTabPanel;
56
import com.google.gwt.user.client.ui.FlexTable;
57
import com.google.gwt.user.client.ui.FocusPanel;
58
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
59
import com.google.gwt.user.client.ui.HorizontalPanel;
60
import com.google.gwt.user.client.ui.Image;
61
import com.google.gwt.user.client.ui.Label;
62
import com.google.gwt.user.client.ui.TextBox;
63
import com.google.gwt.user.client.ui.VerticalPanel;
64

    
65
/**
66
 * The 'File properties' dialog box implementation.
67
 *
68
 */
69
public class FilePropertiesDialog extends AbstractPropertiesDialog {
70

    
71
        /**
72
         * The widget that holds the name of the file.
73
         */
74
        private TextBox name = new TextBox();
75

    
76
        final File file;
77

    
78
    FlexTable metaTable;
79
        /**
80
         * The widget's constructor.
81
         */
82
        public FilePropertiesDialog(Pithos _app, File _file) {
83
        super(_app);
84
        file = _file;
85

    
86
                Anchor close = new Anchor();
87
                close.addStyleName("close");
88
                close.addClickHandler(new ClickHandler() {
89
                        
90
                        @Override
91
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
92
                                hide();
93
                        }
94
                });
95
                // Set the dialog's caption.
96
                setText("File properties");
97
                setAnimationEnabled(true);
98
                setGlassEnabled(true);
99
                setStyleName("pithos-DialogBox");
100

    
101
                // Outer contains inner and buttons.
102
                final VerticalPanel outer = new VerticalPanel();
103
                outer.add(close);
104
                // Inner contains generalPanel and permPanel.
105
                inner = new VerticalPanel();
106
                inner.addStyleName("inner");
107
//                inner.getDeckPanel().addStyleName("pithos-TabPanelBottom");
108

    
109

    
110
        inner.add(createGeneralPanel());
111

    
112
        outer.add(inner);
113

    
114
                // Create the 'OK' button, along with a listener that hides the dialog
115
                // when the button is clicked.
116
                final Button ok = new Button("OK", new ClickHandler() {
117
                        @Override
118
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
119
                                accept();
120
                                closeDialog();
121
                        }
122
                });
123
                ok.addStyleName("button");
124

    
125
        outer.add(ok);
126
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
127

    
128
        setWidget(outer);
129
        }
130

    
131
        private VerticalPanel createGeneralPanel() {
132
        final VerticalPanel generalPanel = new VerticalPanel();
133
        final FlexTable generalTable = new FlexTable();
134
        generalTable.setText(0, 0, "Name");
135
        generalTable.setText(1, 0, "Folder");
136
        generalTable.setText(2, 0, "Owner");
137
        generalTable.setText(3, 0, "Last modified");
138

    
139
        name.setWidth("100%");
140
        name.setText(file.getName());
141
        generalTable.setWidget(0, 1, name);
142
        if(file.getParent() != null)
143
            generalTable.setText(1, 1, file.getParent().getName());
144
        else
145
            generalTable.setText(1, 1, "-");
146
        generalTable.setText(2, 1, file.getOwner());
147

    
148
        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
149
        generalTable.setText(3, 1, file.getLastModified() != null ? formatter.format(file.getLastModified()) : "");
150

    
151
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
152
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
153
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
154
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
155
        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
156
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
157
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
158
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
159
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
160
        generalTable.setCellSpacing(4);
161

    
162
        generalPanel.add(generalTable);
163

    
164
        HorizontalPanel metaTitlePanel = new HorizontalPanel();
165
        metaTitlePanel.setSpacing(5);
166
        Label meta = new Label("Meta data");
167
        meta.addStyleName("pithos-metaTitle");
168
        metaTitlePanel.add(meta);
169
        
170
                Image plus = new Image("images/plus.png");
171
                plus.addStyleName("pithos-addMetaImg");
172
                metaTitlePanel.add(plus);
173
                
174
                generalPanel.add(metaTitlePanel);
175
                
176
                metaTable = new FlexTable();
177
                metaTable.setCellSpacing(0);
178
                metaTable.setHTML(0, 0, "Name");
179
                metaTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
180
                metaTable.setText(0, 1, "Value");
181
                metaTable.getFlexCellFormatter().setStyleName(0, 1, "props-labels");
182
                int rows = 1;
183
                for (String metaKey : file.getMeta().keySet()) {
184
                        addFormLine(metaTable, rows++, metaKey, file.getMeta().get(metaKey));
185
                }
186
                if (rows == 1) //If no meta add an empty line
187
                        addFormLine(metaTable, rows++, "", "");
188
                
189
                plus.addClickHandler(new ClickHandler() {
190
                        
191
                        @Override
192
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
193
                                addFormLine(metaTable, metaTable.getRowCount(), "", "");
194
                        }
195
                });
196

    
197
                generalPanel.add(metaTable);
198
        generalPanel.setSpacing(4);
199
        return generalPanel;
200
    }
201

    
202
        void addFormLine(final FlexTable table, int row, String _name, String _value) {
203
                TextBox nameBox = new TextBox();
204
                nameBox.setText(_name);
205
                nameBox.addStyleName("pithos-metaName");
206
                table.setWidget(row, 0, nameBox);
207
                table.getFlexCellFormatter().setStyleName(1, 0, "props-values");
208

    
209
                TextBox valueBox = new TextBox();
210
                valueBox.setText(_value);
211
                valueBox.addStyleName("pithos-metaValue");
212
                table.setWidget(row, 1, valueBox);
213
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
214
                
215
                Image delete = new Image("images/delete.png");
216
                delete.addStyleName("pithos-metaDeleteImg");
217
                delete.addClickHandler(new ClickHandler() {
218
                        
219
                        @Override
220
                        public void onClick(ClickEvent event) {
221
                                int rowIndex = table.getCellForEvent(event).getRowIndex();
222
                                table.removeRow(rowIndex);
223
                        }
224
                });
225
                table.setWidget(row, 2, delete);
226
        }
227

    
228
        /**
229
         * Accepts any change and updates the file
230
         *
231
         */
232
        @Override
233
        protected void accept() {
234
                String newFilename = null;
235

    
236
                if (!name.getText().trim().equals(file.getName())) {
237
                        newFilename = name.getText().trim();
238
                }
239

    
240
        final Map<String, String> newMeta = new HashMap<String, String>();
241
        for (int row = 1; row < metaTable.getRowCount(); row++) {
242
                String key = ((TextBox) metaTable.getWidget(row, 0)).getText().trim();
243
                String value = ((TextBox) metaTable.getWidget(row, 1)).getText().trim();
244
                if (key.length() > 0 && value.length() > 0)
245
                        newMeta.put(key, value);
246
        }
247

    
248
        if (newFilename != null) {
249
            final String path = file.getParent().getUri() + "/" + newFilename;
250
            PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
251
                @Override
252
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
253
                    updateMetaData(app.getApiPath(), file.getOwner(), path + "?update=", newMeta);
254
                }
255

    
256
                @Override
257
                public void onError(Throwable t) {
258
                    GWT.log("", t);
259
                                        app.setError(t);
260
                    app.displayError("System error modifying file:" + t.getMessage());
261
                }
262

    
263
                                @Override
264
                                protected void onUnauthorized(@SuppressWarnings("unused") Response response) {
265
                                        app.sessionExpired();
266
                                }
267
            };
268
            updateFile.setHeader("X-Auth-Token", app.getToken());
269
            updateFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
270
            updateFile.setHeader("Content-Type", file.getContentType());
271
            for (String key : file.getMeta().keySet())
272
                updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
273
            if (file.isPublished())
274
                updateFile.setHeader("X-Object-Public", "true");
275
            String readPermHeader = "read=";
276
            String writePermHeader = "write=";
277
            for (String u : file.getPermissions().keySet()) {
278
                Boolean[] p = file.getPermissions().get(u);
279
                if (p[0] != null && p[0])
280
                    readPermHeader += u + ",";
281
                if (p[1] != null && p[1])
282
                    writePermHeader += u + ",";
283
            }
284
            if (readPermHeader.endsWith("="))
285
                readPermHeader = "";
286
            else if (readPermHeader.endsWith(","))
287
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
288
            if (writePermHeader.endsWith("="))
289
                writePermHeader = "";
290
            else if (writePermHeader.endsWith(","))
291
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
292
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
293
            if (permHeader.length() == 0)
294
                permHeader="~";
295
            else
296
                    permHeader = URL.encodePathSegment(permHeader);
297
            updateFile.setHeader("X-Object-Sharing", permHeader);
298

    
299
            Scheduler.get().scheduleDeferred(updateFile);
300
        }
301
        else
302
            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newMeta);
303
        }
304

    
305
        protected void updateMetaData(String api, String owner, String path, Map<String, String> newMeta) {
306
        if (newMeta != null) {
307
            PostRequest updateFile = new PostRequest(api, owner, path) {
308
                @Override
309
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
310
                    app.updateFolder(file.getParent(), true, new Command() {
311
                                                
312
                                                @Override
313
                                                public void execute() {
314
                                                        app.updateMySharedRoot();
315
                                                }
316
                                        });
317
                }
318

    
319
                @Override
320
                public void onError(Throwable t) {
321
                    GWT.log("", t);
322
                                        app.setError(t);
323
                    app.displayError("System error modifying file:" + t.getMessage());
324
                }
325

    
326
                                @Override
327
                                protected void onUnauthorized(@SuppressWarnings("unused") Response response) {
328
                                        app.sessionExpired();
329
                                }
330
            };
331
            updateFile.setHeader("X-Auth-Token", app.getToken());
332
            
333
            for (String t : file.getMeta().keySet()) {
334
                        updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(t.trim()), "~");
335
            }
336
            
337
            for (String key : newMeta.keySet())
338
                updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
339
            
340
            Scheduler.get().scheduleDeferred(updateFile);
341
        }
342
        else
343
            app.updateFolder(file.getParent(), true, new Command() {
344
                                
345
                                @Override
346
                                public void execute() {
347
                                        if (file.isShared())
348
                                                app.updateMySharedRoot();
349
                                }
350
                        });
351
    }
352
}