2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\r
6 * GSS is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * GSS is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
\r
19 package gr.ebs.gss.client;
\r
21 import gr.ebs.gss.client.components.TristateCheckBox;
\r
22 import gr.ebs.gss.client.rest.MultiplePostCommand;
\r
23 import gr.ebs.gss.client.rest.RestException;
\r
24 import gr.ebs.gss.client.rest.resource.FileResource;
\r
26 import java.util.ArrayList;
\r
27 import java.util.Iterator;
\r
28 import java.util.List;
\r
30 import com.google.gwt.core.client.GWT;
\r
31 import com.google.gwt.event.dom.client.ClickEvent;
\r
32 import com.google.gwt.event.dom.client.ClickHandler;
\r
33 import com.google.gwt.event.dom.client.FocusEvent;
\r
34 import com.google.gwt.event.dom.client.FocusHandler;
\r
35 import com.google.gwt.json.client.JSONArray;
\r
36 import com.google.gwt.json.client.JSONBoolean;
\r
37 import com.google.gwt.json.client.JSONObject;
\r
38 import com.google.gwt.json.client.JSONString;
\r
39 import com.google.gwt.user.client.Command;
\r
40 import com.google.gwt.user.client.DeferredCommand;
\r
41 import com.google.gwt.user.client.ui.Button;
\r
42 import com.google.gwt.user.client.ui.DecoratedTabPanel;
\r
43 import com.google.gwt.user.client.ui.DisclosurePanel;
\r
44 import com.google.gwt.user.client.ui.FlexTable;
\r
45 import com.google.gwt.user.client.ui.FlowPanel;
\r
46 import com.google.gwt.user.client.ui.FocusPanel;
\r
47 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
48 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
49 import com.google.gwt.user.client.ui.Label;
\r
50 import com.google.gwt.user.client.ui.VerticalPanel;
\r
53 * The 'Multiple file properties' dialog box implementation.
\r
57 public class FilesPropertiesDialog extends AbstractPropertiesDialog {
\r
59 private final TristateCheckBox versionedCheck;
\r
61 private final List<FileResource> files;
\r
63 private Boolean initialVersioned;
\r
67 * The widget's constructor.
\r
71 public FilesPropertiesDialog(final List<FileResource> _files) {
\r
75 int versionedNum = 0;
\r
76 for (FileResource fr : files)
\r
77 if (fr.isVersioned()) versionedNum++;
\r
78 Boolean versioned = null;
\r
79 if (versionedNum==0) versioned = false;
\r
80 if (versionedNum==files.size()) versioned = true;
\r
81 initialVersioned = versioned;
\r
82 versionedCheck = new TristateCheckBox(versioned);
\r
84 // Set the dialog's caption.
\r
85 setText("Files properties");
\r
87 // Outer contains inner and buttons.
\r
88 final VerticalPanel outer = new VerticalPanel();
\r
89 final FocusPanel focusPanel = new FocusPanel(outer);
\r
90 // Inner contains generalPanel and permPanel.
\r
91 inner = new DecoratedTabPanel();
\r
92 inner.setAnimationEnabled(true);
\r
93 final VerticalPanel generalPanel = new VerticalPanel();
\r
94 final HorizontalPanel buttons = new HorizontalPanel();
\r
95 final VerticalPanel verPanel = new VerticalPanel();
\r
96 final HorizontalPanel vPanel = new HorizontalPanel();
\r
98 inner.add(generalPanel, "General");
\r
99 inner.add(verPanel, "Versions");
\r
100 inner.selectTab(0);
\r
102 final FlexTable generalTable = new FlexTable();
\r
103 generalTable.setText(0, 0, String.valueOf(files.size())+" files selected");
\r
104 generalTable.setText(1, 0, "Folder");
\r
105 generalTable.setText(2, 0, "Tags");
\r
106 FileResource firstFile = files.get(0);
\r
107 if(firstFile.getFolderName() != null)
\r
108 generalTable.setText(1, 1, firstFile.getFolderName());
\r
110 generalTable.setText(1, 1, "-");
\r
112 // Find if tags are identical
\r
113 List<String> tagsList = files.get(0).getTags();
\r
114 List<String> tagss;
\r
115 for (int i=1; i<files.size(); i++) {
\r
116 tagss = files.get(i).getTags();
\r
117 if (tagsList.size() != tagss.size() || !tagsList.containsAll(tagss)) {
\r
123 StringBuffer tagsBuffer = new StringBuffer();
\r
124 if (tagsList==null)
\r
125 tagsBuffer.append(MULTIPLE_VALUES_TEXT);
\r
127 Iterator i = tagsList.iterator();
\r
128 while (i.hasNext()) {
\r
129 String tag = (String) i.next();
\r
130 tagsBuffer.append(tag).append(", ");
\r
132 if (tagsBuffer.length() > 1)
\r
133 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
135 initialTagText = tagsBuffer.toString();
\r
136 tags.setText(initialTagText);
\r
137 tags.addFocusHandler(new FocusHandler() {
\r
139 public void onFocus(FocusEvent event) {
\r
140 if (MULTIPLE_VALUES_TEXT.equals(tags.getText()))
\r
146 generalTable.setWidget(2, 1, tags);
\r
147 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
148 generalTable.getFlexCellFormatter().setColSpan(0, 0, 2);
\r
149 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
150 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
151 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
152 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
153 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
154 generalTable.setCellSpacing(4);
\r
156 // Create the 'OK' button, along with a listener that hides the dialog
\r
157 // when the button is clicked.
\r
158 final Button ok = new Button("OK", new ClickHandler() {
\r
160 public void onClick(ClickEvent event) {
\r
166 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
167 // Create the 'Cancel' button, along with a listener that hides the
\r
168 // dialog when the button is clicked.
\r
169 final Button cancel = new Button("Cancel", new ClickHandler() {
\r
171 public void onClick(ClickEvent event) {
\r
175 buttons.add(cancel);
\r
176 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
177 buttons.setSpacing(8);
\r
178 buttons.addStyleName("gss-TabPanelBottom");
\r
180 generalPanel.add(generalTable);
\r
182 // Asynchronously retrieve the tags defined by this user.
\r
183 DeferredCommand.addCommand(new Command() {
\r
186 public void execute() {
\r
191 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
192 allTagsContent = new FlowPanel();
\r
193 allTags.setContent(allTagsContent);
\r
194 generalPanel.add(allTags);
\r
195 generalPanel.setSpacing(4);
\r
198 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
199 vPanel.setSpacing(8);
\r
200 vPanel.addStyleName("gss-TabPanelBottom");
\r
201 vPanel.add(new Label("Versioned"));
\r
203 vPanel.add(versionedCheck);
\r
204 verPanel.add(vPanel);
\r
206 outer.add(buttons);
\r
207 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
208 outer.addStyleName("gss-TabPanelBottom");
\r
210 focusPanel.setFocus(true);
\r
216 * Accepts any change and updates the file
\r
220 protected void accept() {
\r
221 JSONObject json = new JSONObject();
\r
222 if ( versionedCheck.getState()!=null && !versionedCheck.getState().equals(initialVersioned) )
\r
223 json.put("versioned", JSONBoolean.getInstance(versionedCheck.getState()));
\r
225 JSONArray taga = new JSONArray();
\r
227 String tagText = tags.getText();
\r
228 if (!MULTIPLE_VALUES_TEXT.equals(tagText) && !initialTagText.equals(tagText)) {
\r
229 String[] tagset = tagText.split(",");
\r
230 for (String t : tagset) {
\r
231 JSONString to = new JSONString(t);
\r
235 json.put("tags", taga);
\r
237 String jsonString = json.toString();
\r
238 if(jsonString.equals("{}")){
\r
239 GWT.log("NO CHANGES", null);
\r
242 final List<String> fileIds = new ArrayList<String>();
\r
243 for(FileResource f : files)
\r
244 fileIds.add(f.getUri()+"?update=");
\r
245 MultiplePostCommand rt = new MultiplePostCommand(fileIds.toArray(new String[0]), jsonString, 200){
\r
248 public void onComplete() {
\r
249 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
253 public void onError(String p, Throwable t) {
\r
255 if(t instanceof RestException){
\r
256 int statusCode = ((RestException)t).getHttpStatusCode();
\r
257 if(statusCode == 405)
\r
258 GSS.get().displayError("You don't have the necessary permissions");
\r
259 else if(statusCode == 404)
\r
260 GSS.get().displayError("File does not exist");
\r
261 else if(statusCode == 409)
\r
262 GSS.get().displayError("A file with the same name already exists");
\r
263 else if(statusCode == 413)
\r
264 GSS.get().displayError("Your quota has been exceeded");
\r
266 GSS.get().displayError("Unable to modify file::"+((RestException)t).getHttpStatusText());
\r
269 GSS.get().displayError("System error modifying file:"+t.getMessage());
\r
272 DeferredCommand.addCommand(rt);
\r