Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FilesPropertiesDialog.java @ 58777026

History | View | Annotate | Download (9.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 gr.grnet.pithos.web.client.components.TristateCheckBox;
38
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
39
import gr.grnet.pithos.web.client.rest.RestException;
40
import gr.grnet.pithos.web.client.rest.resource.FileResource;
41

    
42
import java.util.ArrayList;
43
import java.util.Iterator;
44
import java.util.List;
45

    
46
import com.google.gwt.core.client.GWT;
47
import com.google.gwt.event.dom.client.ClickEvent;
48
import com.google.gwt.event.dom.client.ClickHandler;
49
import com.google.gwt.event.dom.client.FocusEvent;
50
import com.google.gwt.event.dom.client.FocusHandler;
51
import com.google.gwt.json.client.JSONArray;
52
import com.google.gwt.json.client.JSONBoolean;
53
import com.google.gwt.json.client.JSONObject;
54
import com.google.gwt.json.client.JSONString;
55
import com.google.gwt.user.client.Command;
56
import com.google.gwt.user.client.DeferredCommand;
57
import com.google.gwt.user.client.ui.Button;
58
import com.google.gwt.user.client.ui.DecoratedTabPanel;
59
import com.google.gwt.user.client.ui.DisclosurePanel;
60
import com.google.gwt.user.client.ui.FlexTable;
61
import com.google.gwt.user.client.ui.FlowPanel;
62
import com.google.gwt.user.client.ui.FocusPanel;
63
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
64
import com.google.gwt.user.client.ui.HorizontalPanel;
65
import com.google.gwt.user.client.ui.Label;
66
import com.google.gwt.user.client.ui.VerticalPanel;
67

    
68
/**
69
 * The 'Multiple file properties' dialog box implementation.
70
 *
71
 */
72
public class FilesPropertiesDialog extends AbstractPropertiesDialog {
73

    
74
        private final TristateCheckBox versionedCheck;
75

    
76
        private final List<FileResource> files;
77

    
78
        private Boolean initialVersioned;
79

    
80

    
81
        /**
82
         * The widget's constructor.
83
         *
84
         * @param _files
85
         */
86
        public FilesPropertiesDialog(final List<FileResource> _files) {
87
                super();
88

    
89
                files = _files;
90
                int versionedNum = 0;
91
                for (FileResource fr : files)
92
                        if (fr.isVersioned()) versionedNum++;
93
                Boolean versioned = null;
94
                if (versionedNum==0) versioned = false;
95
                if (versionedNum==files.size()) versioned = true;
96
                initialVersioned = versioned;
97
                versionedCheck = new TristateCheckBox(versioned);
98

    
99
                // Set the dialog's caption.
100
                setText("Files properties");
101

    
102
                // Outer contains inner and buttons.
103
                final VerticalPanel outer = new VerticalPanel();
104
                final FocusPanel focusPanel = new FocusPanel(outer);
105
                // Inner contains generalPanel and permPanel.
106
                inner = new DecoratedTabPanel();
107
                inner.setAnimationEnabled(true);
108
                final VerticalPanel generalPanel = new VerticalPanel();
109
                final HorizontalPanel buttons = new HorizontalPanel();
110
                final VerticalPanel verPanel = new VerticalPanel();
111
                final HorizontalPanel vPanel = new HorizontalPanel();
112

    
113
                inner.add(generalPanel, "General");
114
                inner.add(verPanel, "Versions");
115
                inner.selectTab(0);
116

    
117
                final FlexTable generalTable = new FlexTable();
118
                generalTable.setText(0, 0, String.valueOf(files.size())+" files selected");
119
                generalTable.setText(1, 0, "Folder");
120
                generalTable.setText(2, 0, "Tags");
121
                FileResource firstFile = files.get(0);
122
                if(firstFile.getFolderName() != null)
123
                        generalTable.setText(1, 1, firstFile.getFolderName());
124
                else
125
                        generalTable.setText(1, 1, "-");
126

    
127
                // Find if tags are identical
128
                List<String> tagsList = files.get(0).getTags();
129
                List<String> tagss;
130
                for (int i=1; i<files.size(); i++) {
131
                        tagss = files.get(i).getTags();
132
                        if (tagsList.size() != tagss.size() || !tagsList.containsAll(tagss)) {
133
                                tagsList = null;
134
                                break;
135
                        }
136
                }
137
                // Get the tags.
138
                StringBuffer tagsBuffer = new StringBuffer();
139
                if (tagsList==null)
140
                        tagsBuffer.append(MULTIPLE_VALUES_TEXT);
141
                else {
142
                        Iterator i = tagsList.iterator();
143
                        while (i.hasNext()) {
144
                                String tag = (String) i.next();
145
                                tagsBuffer.append(tag).append(", ");
146
                        }
147
                        if (tagsBuffer.length() > 1)
148
                                tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
149
                }
150
                initialTagText = tagsBuffer.toString();
151
                tags.setText(initialTagText);
152
                tags.addFocusHandler(new FocusHandler() {
153
                        @Override
154
                        public void onFocus(FocusEvent event) {
155
                                if (MULTIPLE_VALUES_TEXT.equals(tags.getText()))
156
                                        tags.setText("");
157
                        }
158
                }
159
                );
160

    
161
                generalTable.setWidget(2, 1, tags);
162
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
163
                generalTable.getFlexCellFormatter().setColSpan(0, 0, 2);
164
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
165
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
166
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
167
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
168
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
169
                generalTable.setCellSpacing(4);
170

    
171
                // Create the 'OK' button, along with a listener that hides the dialog
172
                // when the button is clicked.
173
                final Button ok = new Button("OK", new ClickHandler() {
174
                        @Override
175
                        public void onClick(ClickEvent event) {
176
                                accept();
177
                                closeDialog();
178
                        }
179
                });
180
                buttons.add(ok);
181
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
182
                // Create the 'Cancel' button, along with a listener that hides the
183
                // dialog when the button is clicked.
184
                final Button cancel = new Button("Cancel", new ClickHandler() {
185
                        @Override
186
                        public void onClick(ClickEvent event) {
187
                                closeDialog();
188
                        }
189
                });
190
                buttons.add(cancel);
191
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
192
                buttons.setSpacing(8);
193
                buttons.addStyleName("pithos-TabPanelBottom");
194

    
195
                generalPanel.add(generalTable);
196

    
197
                // Asynchronously retrieve the tags defined by this user.
198
                DeferredCommand.addCommand(new Command() {
199

    
200
                        @Override
201
                        public void execute() {
202
                                updateTags();
203
                        }
204
                });
205

    
206
                DisclosurePanel allTags = new DisclosurePanel("All tags");
207
                allTagsContent = new FlowPanel();
208
                allTags.setContent(allTagsContent);
209
                generalPanel.add(allTags);
210
                generalPanel.setSpacing(4);
211

    
212

    
213
                vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
214
                vPanel.setSpacing(8);
215
                vPanel.addStyleName("pithos-TabPanelBottom");
216
                vPanel.add(new Label("Versioned"));
217

    
218
                vPanel.add(versionedCheck);
219
                verPanel.add(vPanel);
220
                outer.add(inner);
221
                outer.add(buttons);
222
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
223
                outer.addStyleName("pithos-TabPanelBottom");
224

    
225
                focusPanel.setFocus(true);
226
                setWidget(outer);
227
        }
228

    
229

    
230
        /**
231
         * Accepts any change and updates the file
232
         *
233
         */
234
        @Override
235
        protected void accept() {
236
                JSONObject json = new JSONObject();
237
                if ( versionedCheck.getState()!=null && !versionedCheck.getState().equals(initialVersioned) )
238
                                json.put("versioned", JSONBoolean.getInstance(versionedCheck.getState()));
239

    
240
                JSONArray taga = new JSONArray();
241
                int i = 0;
242
                String tagText = tags.getText();
243
                if (!MULTIPLE_VALUES_TEXT.equals(tagText) && !initialTagText.equals(tagText)) {
244
                        String[] tagset = tagText.split(",");
245
                        for (String t : tagset) {
246
                                JSONString to = new JSONString(t);
247
                                taga.set(i, to);
248
                                i++;
249
                        }
250
                        json.put("tags", taga);
251
                }
252
                String jsonString = json.toString();
253
                if(jsonString.equals("{}")){
254
                        GWT.log("NO CHANGES", null);
255
                        return;
256
                }
257
                final List<String> fileIds = new ArrayList<String>();
258
                for(FileResource f : files)
259
                        fileIds.add(f.getUri()+"?update=");
260
                MultiplePostCommand rt = new MultiplePostCommand(fileIds.toArray(new String[0]), jsonString, 200){
261

    
262
                        @Override
263
                        public void onComplete() {
264
                                GSS.get().getTreeView().refreshCurrentNode(false);
265
                        }
266

    
267
                        @Override
268
                        public void onError(String p, Throwable t) {
269
                                GWT.log("", t);
270
                                if(t instanceof RestException){
271
                                        int statusCode = ((RestException)t).getHttpStatusCode();
272
                                        if(statusCode == 405)
273
                                                GSS.get().displayError("You don't have the necessary permissions");
274
                                        else if(statusCode == 404)
275
                                                GSS.get().displayError("File does not exist");
276
                                        else if(statusCode == 409)
277
                                                GSS.get().displayError("A file with the same name already exists");
278
                                        else if(statusCode == 413)
279
                                                GSS.get().displayError("Your quota has been exceeded");
280
                                        else
281
                                                GSS.get().displayError("Unable to modify file::"+((RestException)t).getHttpStatusText());
282
                                }
283
                                else
284
                                        GSS.get().displayError("System error modifying file:"+t.getMessage());
285
                        }
286
                };
287
                DeferredCommand.addCommand(rt);
288
        }
289

    
290

    
291
}