Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.9 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.components.TristateCheckBox;
38 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
39 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
40 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FileResource;
41 ab1eb3f8 Christos Stathis
42 ab1eb3f8 Christos Stathis
import java.util.ArrayList;
43 ab1eb3f8 Christos Stathis
import java.util.Iterator;
44 ab1eb3f8 Christos Stathis
import java.util.List;
45 ab1eb3f8 Christos Stathis
46 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
47 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
48 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
49 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.FocusEvent;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.FocusHandler;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONArray;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONBoolean;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONObject;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONString;
55 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.Command;
56 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
57 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Button;
58 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.DecoratedTabPanel;
59 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.DisclosurePanel;
60 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
61 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FlowPanel;
62 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FocusPanel;
63 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
64 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
65 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Label;
66 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
67 ab1eb3f8 Christos Stathis
68 ab1eb3f8 Christos Stathis
/**
69 ab1eb3f8 Christos Stathis
 * The 'Multiple file properties' dialog box implementation.
70 ab1eb3f8 Christos Stathis
 *
71 ab1eb3f8 Christos Stathis
 */
72 ab1eb3f8 Christos Stathis
public class FilesPropertiesDialog extends AbstractPropertiesDialog {
73 ab1eb3f8 Christos Stathis
74 ab1eb3f8 Christos Stathis
        private final TristateCheckBox versionedCheck;
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
        private final List<FileResource> files;
77 ab1eb3f8 Christos Stathis
78 ab1eb3f8 Christos Stathis
        private Boolean initialVersioned;
79 ab1eb3f8 Christos Stathis
80 ab1eb3f8 Christos Stathis
81 ab1eb3f8 Christos Stathis
        /**
82 ab1eb3f8 Christos Stathis
         * The widget's constructor.
83 ab1eb3f8 Christos Stathis
         *
84 ab1eb3f8 Christos Stathis
         * @param _files
85 ab1eb3f8 Christos Stathis
         */
86 ab1eb3f8 Christos Stathis
        public FilesPropertiesDialog(final List<FileResource> _files) {
87 ab1eb3f8 Christos Stathis
                super();
88 ab1eb3f8 Christos Stathis
89 ab1eb3f8 Christos Stathis
                files = _files;
90 ab1eb3f8 Christos Stathis
                int versionedNum = 0;
91 ab1eb3f8 Christos Stathis
                for (FileResource fr : files)
92 ab1eb3f8 Christos Stathis
                        if (fr.isVersioned()) versionedNum++;
93 ab1eb3f8 Christos Stathis
                Boolean versioned = null;
94 ab1eb3f8 Christos Stathis
                if (versionedNum==0) versioned = false;
95 ab1eb3f8 Christos Stathis
                if (versionedNum==files.size()) versioned = true;
96 ab1eb3f8 Christos Stathis
                initialVersioned = versioned;
97 ab1eb3f8 Christos Stathis
                versionedCheck = new TristateCheckBox(versioned);
98 ab1eb3f8 Christos Stathis
99 ab1eb3f8 Christos Stathis
                // Set the dialog's caption.
100 ab1eb3f8 Christos Stathis
                setText("Files properties");
101 ab1eb3f8 Christos Stathis
102 ab1eb3f8 Christos Stathis
                // Outer contains inner and buttons.
103 ab1eb3f8 Christos Stathis
                final VerticalPanel outer = new VerticalPanel();
104 ab1eb3f8 Christos Stathis
                final FocusPanel focusPanel = new FocusPanel(outer);
105 ab1eb3f8 Christos Stathis
                // Inner contains generalPanel and permPanel.
106 ab1eb3f8 Christos Stathis
                inner = new DecoratedTabPanel();
107 ab1eb3f8 Christos Stathis
                inner.setAnimationEnabled(true);
108 ab1eb3f8 Christos Stathis
                final VerticalPanel generalPanel = new VerticalPanel();
109 ab1eb3f8 Christos Stathis
                final HorizontalPanel buttons = new HorizontalPanel();
110 ab1eb3f8 Christos Stathis
                final VerticalPanel verPanel = new VerticalPanel();
111 ab1eb3f8 Christos Stathis
                final HorizontalPanel vPanel = new HorizontalPanel();
112 ab1eb3f8 Christos Stathis
113 ab1eb3f8 Christos Stathis
                inner.add(generalPanel, "General");
114 ab1eb3f8 Christos Stathis
                inner.add(verPanel, "Versions");
115 ab1eb3f8 Christos Stathis
                inner.selectTab(0);
116 ab1eb3f8 Christos Stathis
117 ab1eb3f8 Christos Stathis
                final FlexTable generalTable = new FlexTable();
118 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 0, String.valueOf(files.size())+" files selected");
119 ab1eb3f8 Christos Stathis
                generalTable.setText(1, 0, "Folder");
120 ab1eb3f8 Christos Stathis
                generalTable.setText(2, 0, "Tags");
121 ab1eb3f8 Christos Stathis
                FileResource firstFile = files.get(0);
122 ab1eb3f8 Christos Stathis
                if(firstFile.getFolderName() != null)
123 ab1eb3f8 Christos Stathis
                        generalTable.setText(1, 1, firstFile.getFolderName());
124 ab1eb3f8 Christos Stathis
                else
125 ab1eb3f8 Christos Stathis
                        generalTable.setText(1, 1, "-");
126 ab1eb3f8 Christos Stathis
127 ab1eb3f8 Christos Stathis
                // Find if tags are identical
128 ab1eb3f8 Christos Stathis
                List<String> tagsList = files.get(0).getTags();
129 ab1eb3f8 Christos Stathis
                List<String> tagss;
130 ab1eb3f8 Christos Stathis
                for (int i=1; i<files.size(); i++) {
131 ab1eb3f8 Christos Stathis
                        tagss = files.get(i).getTags();
132 ab1eb3f8 Christos Stathis
                        if (tagsList.size() != tagss.size() || !tagsList.containsAll(tagss)) {
133 ab1eb3f8 Christos Stathis
                                tagsList = null;
134 ab1eb3f8 Christos Stathis
                                break;
135 ab1eb3f8 Christos Stathis
                        }
136 ab1eb3f8 Christos Stathis
                }
137 ab1eb3f8 Christos Stathis
                // Get the tags.
138 ab1eb3f8 Christos Stathis
                StringBuffer tagsBuffer = new StringBuffer();
139 ab1eb3f8 Christos Stathis
                if (tagsList==null)
140 ab1eb3f8 Christos Stathis
                        tagsBuffer.append(MULTIPLE_VALUES_TEXT);
141 ab1eb3f8 Christos Stathis
                else {
142 ab1eb3f8 Christos Stathis
                        Iterator i = tagsList.iterator();
143 ab1eb3f8 Christos Stathis
                        while (i.hasNext()) {
144 ab1eb3f8 Christos Stathis
                                String tag = (String) i.next();
145 ab1eb3f8 Christos Stathis
                                tagsBuffer.append(tag).append(", ");
146 ab1eb3f8 Christos Stathis
                        }
147 ab1eb3f8 Christos Stathis
                        if (tagsBuffer.length() > 1)
148 ab1eb3f8 Christos Stathis
                                tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
149 ab1eb3f8 Christos Stathis
                }
150 ab1eb3f8 Christos Stathis
                initialTagText = tagsBuffer.toString();
151 ab1eb3f8 Christos Stathis
                tags.setText(initialTagText);
152 ab1eb3f8 Christos Stathis
                tags.addFocusHandler(new FocusHandler() {
153 ab1eb3f8 Christos Stathis
                        @Override
154 ab1eb3f8 Christos Stathis
                        public void onFocus(FocusEvent event) {
155 ab1eb3f8 Christos Stathis
                                if (MULTIPLE_VALUES_TEXT.equals(tags.getText()))
156 ab1eb3f8 Christos Stathis
                                        tags.setText("");
157 ab1eb3f8 Christos Stathis
                        }
158 ab1eb3f8 Christos Stathis
                }
159 ab1eb3f8 Christos Stathis
                );
160 ab1eb3f8 Christos Stathis
161 ab1eb3f8 Christos Stathis
                generalTable.setWidget(2, 1, tags);
162 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
163 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setColSpan(0, 0, 2);
164 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
165 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
166 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
167 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
168 ab1eb3f8 Christos Stathis
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
169 ab1eb3f8 Christos Stathis
                generalTable.setCellSpacing(4);
170 ab1eb3f8 Christos Stathis
171 ab1eb3f8 Christos Stathis
                // Create the 'OK' button, along with a listener that hides the dialog
172 ab1eb3f8 Christos Stathis
                // when the button is clicked.
173 ab1eb3f8 Christos Stathis
                final Button ok = new Button("OK", new ClickHandler() {
174 ab1eb3f8 Christos Stathis
                        @Override
175 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
176 ab1eb3f8 Christos Stathis
                                accept();
177 ab1eb3f8 Christos Stathis
                                closeDialog();
178 ab1eb3f8 Christos Stathis
                        }
179 ab1eb3f8 Christos Stathis
                });
180 ab1eb3f8 Christos Stathis
                buttons.add(ok);
181 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
182 ab1eb3f8 Christos Stathis
                // Create the 'Cancel' button, along with a listener that hides the
183 ab1eb3f8 Christos Stathis
                // dialog when the button is clicked.
184 ab1eb3f8 Christos Stathis
                final Button cancel = new Button("Cancel", new ClickHandler() {
185 ab1eb3f8 Christos Stathis
                        @Override
186 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
187 ab1eb3f8 Christos Stathis
                                closeDialog();
188 ab1eb3f8 Christos Stathis
                        }
189 ab1eb3f8 Christos Stathis
                });
190 ab1eb3f8 Christos Stathis
                buttons.add(cancel);
191 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
192 ab1eb3f8 Christos Stathis
                buttons.setSpacing(8);
193 ab1eb3f8 Christos Stathis
                buttons.addStyleName("pithos-TabPanelBottom");
194 ab1eb3f8 Christos Stathis
195 ab1eb3f8 Christos Stathis
                generalPanel.add(generalTable);
196 ab1eb3f8 Christos Stathis
197 ab1eb3f8 Christos Stathis
                // Asynchronously retrieve the tags defined by this user.
198 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(new Command() {
199 ab1eb3f8 Christos Stathis
200 ab1eb3f8 Christos Stathis
                        @Override
201 ab1eb3f8 Christos Stathis
                        public void execute() {
202 ab1eb3f8 Christos Stathis
                                updateTags();
203 ab1eb3f8 Christos Stathis
                        }
204 ab1eb3f8 Christos Stathis
                });
205 ab1eb3f8 Christos Stathis
206 ab1eb3f8 Christos Stathis
                DisclosurePanel allTags = new DisclosurePanel("All tags");
207 ab1eb3f8 Christos Stathis
                allTagsContent = new FlowPanel();
208 ab1eb3f8 Christos Stathis
                allTags.setContent(allTagsContent);
209 ab1eb3f8 Christos Stathis
                generalPanel.add(allTags);
210 ab1eb3f8 Christos Stathis
                generalPanel.setSpacing(4);
211 ab1eb3f8 Christos Stathis
212 ab1eb3f8 Christos Stathis
213 ab1eb3f8 Christos Stathis
                vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
214 ab1eb3f8 Christos Stathis
                vPanel.setSpacing(8);
215 ab1eb3f8 Christos Stathis
                vPanel.addStyleName("pithos-TabPanelBottom");
216 ab1eb3f8 Christos Stathis
                vPanel.add(new Label("Versioned"));
217 ab1eb3f8 Christos Stathis
218 ab1eb3f8 Christos Stathis
                vPanel.add(versionedCheck);
219 ab1eb3f8 Christos Stathis
                verPanel.add(vPanel);
220 ab1eb3f8 Christos Stathis
                outer.add(inner);
221 ab1eb3f8 Christos Stathis
                outer.add(buttons);
222 ab1eb3f8 Christos Stathis
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
223 ab1eb3f8 Christos Stathis
                outer.addStyleName("pithos-TabPanelBottom");
224 ab1eb3f8 Christos Stathis
225 ab1eb3f8 Christos Stathis
                focusPanel.setFocus(true);
226 ab1eb3f8 Christos Stathis
                setWidget(outer);
227 ab1eb3f8 Christos Stathis
        }
228 ab1eb3f8 Christos Stathis
229 ab1eb3f8 Christos Stathis
230 ab1eb3f8 Christos Stathis
        /**
231 ab1eb3f8 Christos Stathis
         * Accepts any change and updates the file
232 ab1eb3f8 Christos Stathis
         *
233 ab1eb3f8 Christos Stathis
         */
234 ab1eb3f8 Christos Stathis
        @Override
235 ab1eb3f8 Christos Stathis
        protected void accept() {
236 ab1eb3f8 Christos Stathis
                JSONObject json = new JSONObject();
237 ab1eb3f8 Christos Stathis
                if ( versionedCheck.getState()!=null && !versionedCheck.getState().equals(initialVersioned) )
238 ab1eb3f8 Christos Stathis
                                json.put("versioned", JSONBoolean.getInstance(versionedCheck.getState()));
239 ab1eb3f8 Christos Stathis
240 ab1eb3f8 Christos Stathis
                JSONArray taga = new JSONArray();
241 ab1eb3f8 Christos Stathis
                int i = 0;
242 ab1eb3f8 Christos Stathis
                String tagText = tags.getText();
243 ab1eb3f8 Christos Stathis
                if (!MULTIPLE_VALUES_TEXT.equals(tagText) && !initialTagText.equals(tagText)) {
244 ab1eb3f8 Christos Stathis
                        String[] tagset = tagText.split(",");
245 ab1eb3f8 Christos Stathis
                        for (String t : tagset) {
246 ab1eb3f8 Christos Stathis
                                JSONString to = new JSONString(t);
247 ab1eb3f8 Christos Stathis
                                taga.set(i, to);
248 ab1eb3f8 Christos Stathis
                                i++;
249 ab1eb3f8 Christos Stathis
                        }
250 ab1eb3f8 Christos Stathis
                        json.put("tags", taga);
251 ab1eb3f8 Christos Stathis
                }
252 ab1eb3f8 Christos Stathis
                String jsonString = json.toString();
253 ab1eb3f8 Christos Stathis
                if(jsonString.equals("{}")){
254 ab1eb3f8 Christos Stathis
                        GWT.log("NO CHANGES", null);
255 ab1eb3f8 Christos Stathis
                        return;
256 ab1eb3f8 Christos Stathis
                }
257 ab1eb3f8 Christos Stathis
                final List<String> fileIds = new ArrayList<String>();
258 ab1eb3f8 Christos Stathis
                for(FileResource f : files)
259 ab1eb3f8 Christos Stathis
                        fileIds.add(f.getUri()+"?update=");
260 ab1eb3f8 Christos Stathis
                MultiplePostCommand rt = new MultiplePostCommand(fileIds.toArray(new String[0]), jsonString, 200){
261 ab1eb3f8 Christos Stathis
262 ab1eb3f8 Christos Stathis
                        @Override
263 ab1eb3f8 Christos Stathis
                        public void onComplete() {
264 ab1eb3f8 Christos Stathis
                                GSS.get().getTreeView().refreshCurrentNode(false);
265 ab1eb3f8 Christos Stathis
                        }
266 ab1eb3f8 Christos Stathis
267 ab1eb3f8 Christos Stathis
                        @Override
268 ab1eb3f8 Christos Stathis
                        public void onError(String p, Throwable t) {
269 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
270 ab1eb3f8 Christos Stathis
                                if(t instanceof RestException){
271 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException)t).getHttpStatusCode();
272 ab1eb3f8 Christos Stathis
                                        if(statusCode == 405)
273 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
274 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 404)
275 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("File does not exist");
276 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 409)
277 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("A file with the same name already exists");
278 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 413)
279 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Your quota has been exceeded");
280 ab1eb3f8 Christos Stathis
                                        else
281 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to modify file::"+((RestException)t).getHttpStatusText());
282 ab1eb3f8 Christos Stathis
                                }
283 ab1eb3f8 Christos Stathis
                                else
284 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error modifying file:"+t.getMessage());
285 ab1eb3f8 Christos Stathis
                        }
286 ab1eb3f8 Christos Stathis
                };
287 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(rt);
288 ab1eb3f8 Christos Stathis
        }
289 ab1eb3f8 Christos Stathis
290 ab1eb3f8 Christos Stathis
291 ab1eb3f8 Christos Stathis
}