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.FocusEvent;
\r
32 import com.google.gwt.event.dom.client.FocusHandler;
\r
33 import com.google.gwt.json.client.JSONArray;
\r
34 import com.google.gwt.json.client.JSONBoolean;
\r
35 import com.google.gwt.json.client.JSONObject;
\r
36 import com.google.gwt.json.client.JSONString;
\r
37 import com.google.gwt.user.client.Command;
\r
38 import com.google.gwt.user.client.DeferredCommand;
\r
39 import com.google.gwt.user.client.ui.Button;
\r
40 import com.google.gwt.user.client.ui.ClickListener;
\r
41 import com.google.gwt.user.client.ui.DisclosurePanel;
\r
42 import com.google.gwt.user.client.ui.FlexTable;
\r
43 import com.google.gwt.user.client.ui.FlowPanel;
\r
44 import com.google.gwt.user.client.ui.FocusPanel;
\r
45 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
46 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
47 import com.google.gwt.user.client.ui.Label;
\r
48 import com.google.gwt.user.client.ui.TabPanel;
\r
49 import com.google.gwt.user.client.ui.VerticalPanel;
\r
50 import com.google.gwt.user.client.ui.Widget;
\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 TabPanel();
\r
92 final VerticalPanel generalPanel = new VerticalPanel();
\r
93 final HorizontalPanel buttons = new HorizontalPanel();
\r
94 final VerticalPanel verPanel = new VerticalPanel();
\r
95 final HorizontalPanel vPanel = new HorizontalPanel();
\r
97 inner.add(generalPanel, "General");
\r
98 inner.add(verPanel, "Versions");
\r
101 final FlexTable generalTable = new FlexTable();
\r
102 generalTable.setText(0, 0, String.valueOf(files.size())+" files selected");
\r
103 generalTable.setText(1, 0, "Folder");
\r
104 generalTable.setText(2, 0, "Tags");
\r
105 FileResource firstFile = files.get(0);
\r
106 if(firstFile.getFolderName() != null)
\r
107 generalTable.setText(1, 1, firstFile.getFolderName());
\r
109 generalTable.setText(1, 1, "-");
\r
111 // Find if tags are identical
\r
112 List<String> tagsList = files.get(0).getTags();
\r
113 List<String> tagss;
\r
114 for (int i=1; i<files.size(); i++) {
\r
115 tagss = files.get(i).getTags();
\r
116 if (tagsList.size() != tagss.size() || !tagsList.containsAll(tagss)) {
\r
122 StringBuffer tagsBuffer = new StringBuffer();
\r
123 if (tagsList==null)
\r
124 tagsBuffer.append(MULTIPLE_VALUES_TEXT);
\r
126 Iterator i = tagsList.iterator();
\r
127 while (i.hasNext()) {
\r
128 String tag = (String) i.next();
\r
129 tagsBuffer.append(tag).append(", ");
\r
131 if (tagsBuffer.length() > 1)
\r
132 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
134 initialTagText = tagsBuffer.toString();
\r
135 tags.setText(initialTagText);
\r
136 tags.addFocusHandler(new FocusHandler() {
\r
138 public void onFocus(FocusEvent event) {
\r
139 if (MULTIPLE_VALUES_TEXT.equals(tags.getText()))
\r
145 generalTable.setWidget(2, 1, tags);
\r
146 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
147 generalTable.getFlexCellFormatter().setColSpan(0, 0, 2);
\r
148 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
149 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
150 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
151 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
152 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
153 generalTable.setCellSpacing(4);
\r
155 // Create the 'OK' button, along with a listener that hides the dialog
\r
156 // when the button is clicked.
\r
157 final Button ok = new Button("OK", new ClickListener() {
\r
159 public void onClick(Widget sender) {
\r
165 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
166 // Create the 'Cancel' button, along with a listener that hides the
\r
167 // dialog when the button is clicked.
\r
168 final Button cancel = new Button("Cancel", new ClickListener() {
\r
170 public void onClick(Widget sender) {
\r
174 buttons.add(cancel);
\r
175 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
176 buttons.setSpacing(8);
\r
177 buttons.addStyleName("gwt-TabPanelBottom");
\r
179 generalPanel.add(generalTable);
\r
181 // Asynchronously retrieve the tags defined by this user.
\r
182 DeferredCommand.addCommand(new Command() {
\r
184 public void execute() {
\r
189 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
190 allTagsContent = new FlowPanel();
\r
191 allTags.setContent(allTagsContent);
\r
192 generalPanel.add(allTags);
\r
193 generalPanel.setSpacing(4);
\r
196 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
197 vPanel.setSpacing(8);
\r
198 vPanel.addStyleName("gwt-TabPanelBottom");
\r
199 vPanel.add(new Label("Versioned"));
\r
201 vPanel.add(versionedCheck);
\r
202 verPanel.add(vPanel);
\r
204 outer.add(buttons);
\r
205 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
206 outer.addStyleName("gwt-TabPanelBottom");
\r
208 focusPanel.setFocus(true);
\r
214 * Accepts any change and updates the file
\r
218 protected void accept() {
\r
219 JSONObject json = new JSONObject();
\r
220 if ( versionedCheck.getState()!=null && !versionedCheck.getState().equals(initialVersioned) )
\r
221 json.put("versioned", JSONBoolean.getInstance(versionedCheck.getState()));
\r
223 JSONArray taga = new JSONArray();
\r
225 String tagText = tags.getText();
\r
226 if (!MULTIPLE_VALUES_TEXT.equals(tagText) && !initialTagText.equals(tagText)) {
\r
227 String[] tagset = tagText.split(",");
\r
228 for (String t : tagset) {
\r
229 JSONString to = new JSONString(t);
\r
233 json.put("tags", taga);
\r
235 String jsonString = json.toString();
\r
236 if(jsonString.equals("{}")){
\r
237 GWT.log("NO CHANGES", null);
\r
240 final List<String> fileIds = new ArrayList<String>();
\r
241 for(FileResource f : files)
\r
242 fileIds.add(f.getUri()+"?update=");
\r
243 MultiplePostCommand rt = new MultiplePostCommand(fileIds.toArray(new String[0]), jsonString, 200){
\r
246 public void onComplete() {
\r
247 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
251 public void onError(String p, Throwable t) {
\r
253 if(t instanceof RestException){
\r
254 int statusCode = ((RestException)t).getHttpStatusCode();
\r
255 if(statusCode == 405)
\r
256 GSS.get().displayError("You don't have the necessary permissions");
\r
257 else if(statusCode == 404)
\r
258 GSS.get().displayError("File does not exist");
\r
259 else if(statusCode == 409)
\r
260 GSS.get().displayError("A file with the same name already exists");
\r
261 else if(statusCode == 413)
\r
262 GSS.get().displayError("Your quota has been exceeded");
\r
264 GSS.get().displayError("Unable to modify file::"+((RestException)t).getHttpStatusText());
\r
267 GSS.get().displayError("System error modifying file:"+t.getMessage());
\r
270 DeferredCommand.addCommand(rt);
\r