2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
\r
4 * Redistribution and use in source and binary forms, with or
\r
5 * without modification, are permitted provided that the following
\r
6 * conditions are met:
\r
8 * 1. Redistributions of source code must retain the above
\r
9 * copyright notice, this list of conditions and the following
\r
12 * 2. Redistributions in binary form must reproduce the above
\r
13 * copyright notice, this list of conditions and the following
\r
14 * disclaimer in the documentation and/or other materials
\r
15 * provided with the distribution.
\r
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
\r
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
\r
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
\r
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
28 * POSSIBILITY OF SUCH DAMAGE.
\r
30 * The views and conclusions contained in the software and
\r
31 * documentation are those of the authors and should not be
\r
32 * interpreted as representing official policies, either expressed
\r
33 * or implied, of GRNET S.A.
\r
35 package gr.grnet.pithos.web.client;
\r
37 import gr.grnet.pithos.web.client.foldertree.File;
\r
38 import gr.grnet.pithos.web.client.foldertree.Resource;
\r
39 import gr.grnet.pithos.web.client.rest.PostRequest;
\r
40 import gr.grnet.pithos.web.client.rest.PutRequest;
\r
42 import java.util.HashMap;
\r
43 import java.util.Map;
\r
45 import com.google.gwt.core.client.GWT;
\r
46 import com.google.gwt.core.client.Scheduler;
\r
47 import com.google.gwt.event.dom.client.ClickEvent;
\r
48 import com.google.gwt.event.dom.client.ClickHandler;
\r
49 import com.google.gwt.http.client.Response;
\r
50 import com.google.gwt.http.client.URL;
\r
51 import com.google.gwt.i18n.client.DateTimeFormat;
\r
52 import com.google.gwt.user.client.Command;
\r
53 import com.google.gwt.user.client.ui.Anchor;
\r
54 import com.google.gwt.user.client.ui.Button;
\r
55 import com.google.gwt.user.client.ui.FlexTable;
\r
56 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
57 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
58 import com.google.gwt.user.client.ui.Image;
\r
59 import com.google.gwt.user.client.ui.Label;
\r
60 import com.google.gwt.user.client.ui.TextBox;
\r
61 import com.google.gwt.user.client.ui.VerticalPanel;
\r
64 * The 'File properties' dialog box implementation.
\r
67 public class FilePropertiesDialog extends AbstractPropertiesDialog {
\r
70 * The widget that holds the name of the file.
\r
72 private TextBox name = new TextBox();
\r
76 FlexTable metaTable;
\r
78 * The widget's constructor.
\r
80 public FilePropertiesDialog(Pithos _app, File _file) {
\r
84 Anchor close = new Anchor();
\r
85 close.addStyleName("close");
\r
86 close.addClickHandler(new ClickHandler() {
\r
89 public void onClick(ClickEvent event) {
\r
93 // Set the dialog's caption.
\r
94 setText("File properties");
\r
95 setAnimationEnabled(true);
\r
96 setGlassEnabled(true);
\r
97 setStyleName("pithos-DialogBox");
\r
99 // Outer contains inner and buttons.
\r
100 final VerticalPanel outer = new VerticalPanel();
\r
102 // Inner contains generalPanel and permPanel.
\r
103 inner = new VerticalPanel();
\r
104 inner.addStyleName("inner");
\r
106 inner.add(createGeneralPanel());
\r
110 // Create the 'OK' button, along with a listener that hides the dialog
\r
111 // when the button is clicked.
\r
112 final Button ok = new Button("OK", new ClickHandler() {
\r
114 public void onClick(ClickEvent event) {
\r
119 ok.addStyleName("button");
\r
122 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
\r
127 private VerticalPanel createGeneralPanel() {
\r
128 final VerticalPanel generalPanel = new VerticalPanel();
\r
129 final FlexTable generalTable = new FlexTable();
\r
130 generalTable.setText(0, 0, "Name");
\r
131 generalTable.setText(1, 0, "Folder");
\r
132 generalTable.setText(2, 0, "Owner");
\r
133 generalTable.setText(3, 0, "Last modified");
\r
135 name.setWidth("100%");
\r
136 name.setText(file.getName());
\r
137 generalTable.setWidget(0, 1, name);
\r
138 if(file.getParent() != null)
\r
139 generalTable.setText(1, 1, file.getParent().getName());
\r
141 generalTable.setText(1, 1, "-");
\r
142 generalTable.setText(2, 1, file.getOwner());
\r
144 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
145 generalTable.setText(3, 1, file.getLastModified() != null ? formatter.format(file.getLastModified()) : "");
\r
147 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
148 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
149 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
150 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
151 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
152 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
153 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
154 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
155 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
156 generalTable.setCellSpacing(4);
\r
158 generalPanel.add(generalTable);
\r
160 HorizontalPanel metaTitlePanel = new HorizontalPanel();
\r
161 metaTitlePanel.setSpacing(5);
\r
162 Label meta = new Label("Meta data");
\r
163 meta.addStyleName("pithos-metaTitle");
\r
164 metaTitlePanel.add(meta);
\r
166 Image plus = new Image("images/plus.png");
\r
167 plus.addStyleName("pithos-addMetaImg");
\r
168 metaTitlePanel.add(plus);
\r
170 generalPanel.add(metaTitlePanel);
\r
172 metaTable = new FlexTable();
\r
173 metaTable.setCellSpacing(0);
\r
174 metaTable.setHTML(0, 0, "Name");
\r
175 metaTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
176 metaTable.setText(0, 1, "Value");
\r
177 metaTable.getFlexCellFormatter().setStyleName(0, 1, "props-labels");
\r
179 for (String metaKey : file.getMeta().keySet()) {
\r
180 addFormLine(metaTable, rows++, metaKey, file.getMeta().get(metaKey));
\r
182 if (rows == 1) //If no meta add an empty line
\r
183 addFormLine(metaTable, rows++, "", "");
\r
185 plus.addClickHandler(new ClickHandler() {
\r
188 public void onClick(ClickEvent event) {
\r
189 addFormLine(metaTable, metaTable.getRowCount(), "", "");
\r
193 generalPanel.add(metaTable);
\r
194 generalPanel.setSpacing(4);
\r
195 return generalPanel;
\r
198 void addFormLine(final FlexTable table, int row, String _name, String _value) {
\r
199 TextBox nameBox = new TextBox();
\r
200 nameBox.setText(_name);
\r
201 nameBox.addStyleName("pithos-metaName");
\r
202 table.setWidget(row, 0, nameBox);
\r
203 table.getFlexCellFormatter().setStyleName(1, 0, "props-values");
\r
205 TextBox valueBox = new TextBox();
\r
206 valueBox.setText(_value);
\r
207 valueBox.addStyleName("pithos-metaValue");
\r
208 table.setWidget(row, 1, valueBox);
\r
209 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
211 Image delete = new Image("images/delete.png");
\r
212 delete.addStyleName("pithos-metaDeleteImg");
\r
213 delete.addClickHandler(new ClickHandler() {
\r
216 public void onClick(ClickEvent event) {
\r
217 int rowIndex = table.getCellForEvent(event).getRowIndex();
\r
218 table.removeRow(rowIndex);
\r
221 table.setWidget(row, 2, delete);
\r
225 * Accepts any change and updates the file
\r
229 protected void accept() {
\r
230 String newFilename = null;
\r
232 if (!name.getText().trim().equals(file.getName())) {
\r
233 newFilename = name.getText().trim();
\r
236 final Map<String, String> newMeta = new HashMap<String, String>();
\r
237 for (int row = 1; row < metaTable.getRowCount(); row++) {
\r
238 String key = ((TextBox) metaTable.getWidget(row, 0)).getText().trim();
\r
239 String value = ((TextBox) metaTable.getWidget(row, 1)).getText().trim();
\r
240 if (key.length() > 0 && value.length() > 0)
\r
241 newMeta.put(key, value);
\r
244 if (newFilename != null) {
\r
245 final String path = file.getParent().getUri() + "/" + newFilename;
\r
246 PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
\r
248 public void onSuccess(Resource result) {
\r
249 updateMetaData(app.getApiPath(), file.getOwner(), path + "?update=", newMeta);
\r
253 public void onError(Throwable t) {
\r
256 app.displayError("System error modifying file:" + t.getMessage());
\r
260 protected void onUnauthorized(Response response) {
\r
261 app.sessionExpired();
\r
264 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
265 updateFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
\r
266 updateFile.setHeader("Content-Type", file.getContentType());
\r
267 for (String key : file.getMeta().keySet())
\r
268 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
\r
269 if (file.isPublished())
\r
270 updateFile.setHeader("X-Object-Public", "true");
\r
271 String readPermHeader = "read=";
\r
272 String writePermHeader = "write=";
\r
273 for (String u : file.getPermissions().keySet()) {
\r
274 Boolean[] p = file.getPermissions().get(u);
\r
275 if (p[0] != null && p[0])
\r
276 readPermHeader += u + ",";
\r
277 if (p[1] != null && p[1])
\r
278 writePermHeader += u + ",";
\r
280 if (readPermHeader.endsWith("="))
\r
281 readPermHeader = "";
\r
282 else if (readPermHeader.endsWith(","))
\r
283 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
\r
284 if (writePermHeader.endsWith("="))
\r
285 writePermHeader = "";
\r
286 else if (writePermHeader.endsWith(","))
\r
287 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
\r
288 String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader;
\r
289 if (permHeader.length() == 0)
\r
292 permHeader = URL.encodePathSegment(permHeader);
\r
293 updateFile.setHeader("X-Object-Sharing", permHeader);
\r
295 Scheduler.get().scheduleDeferred(updateFile);
\r
298 updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newMeta);
\r
301 protected void updateMetaData(String api, String owner, String path, Map<String, String> newMeta) {
\r
302 if (newMeta != null) {
\r
303 PostRequest updateFile = new PostRequest(api, owner, path) {
\r
305 public void onSuccess(Resource result) {
\r
306 app.updateFolder(file.getParent(), true, new Command() {
\r
309 public void execute() {
\r
310 app.updateMySharedRoot();
\r
316 public void onError(Throwable t) {
\r
319 app.displayError("System error modifying file:" + t.getMessage());
\r
323 protected void onUnauthorized(Response response) {
\r
324 app.sessionExpired();
\r
327 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
329 for (String t : file.getMeta().keySet()) {
\r
330 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(t.trim()), "~");
\r
333 for (String key : newMeta.keySet())
\r
334 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
\r
336 Scheduler.get().scheduleDeferred(updateFile);
\r
339 app.updateFolder(file.getParent(), true, new Command() {
\r
342 public void execute() {
\r
343 if (file.isShared())
\r
344 app.updateMySharedRoot();
\r