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.Folder;
\r
39 import gr.grnet.pithos.web.client.foldertree.Resource;
\r
40 import gr.grnet.pithos.web.client.rest.PutRequest;
\r
41 import gr.grnet.pithos.web.client.rest.RestException;
\r
43 import java.util.Iterator;
\r
45 import com.google.gwt.core.client.GWT;
\r
46 import com.google.gwt.core.client.Scheduler;
\r
47 import com.google.gwt.dom.client.NativeEvent;
\r
48 import com.google.gwt.event.dom.client.ChangeEvent;
\r
49 import com.google.gwt.event.dom.client.ChangeHandler;
\r
50 import com.google.gwt.event.dom.client.ClickEvent;
\r
51 import com.google.gwt.event.dom.client.ClickHandler;
\r
52 import com.google.gwt.event.dom.client.KeyCodes;
\r
53 import com.google.gwt.event.dom.client.KeyDownEvent;
\r
54 import com.google.gwt.http.client.Response;
\r
55 import com.google.gwt.i18n.client.DateTimeFormat;
\r
56 import com.google.gwt.user.client.Command;
\r
57 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
58 import com.google.gwt.user.client.ui.Anchor;
\r
59 import com.google.gwt.user.client.ui.Button;
\r
60 import com.google.gwt.user.client.ui.DialogBox;
\r
61 import com.google.gwt.user.client.ui.FlexTable;
\r
62 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
63 import com.google.gwt.user.client.ui.Label;
\r
64 import com.google.gwt.user.client.ui.TextBox;
\r
65 import com.google.gwt.user.client.ui.VerticalPanel;
\r
68 * The 'Folder properties' dialog box implementation.
\r
70 public class FolderPropertiesDialog extends DialogBox {
\r
72 protected Pithos app;
\r
75 * The widget that holds the folderName of the folder.
\r
77 TextBox folderName = new TextBox();
\r
80 * A flag that denotes whether the dialog will be used to create or modify a
\r
83 private final boolean create;
\r
85 final Folder folder;
\r
87 final VerticalPanel inner;
\r
90 * The widget's constructor.
\r
92 public FolderPropertiesDialog(final Pithos app, boolean _create, Folder selected) {
\r
94 Anchor close = new Anchor();
\r
95 close.addStyleName("close");
\r
96 close.addClickHandler(new ClickHandler() {
\r
99 public void onClick(ClickEvent event) {
\r
104 setAnimationEnabled(true);
\r
105 setGlassEnabled(true);
\r
106 setStyleName("pithos-DialogBox");
\r
108 // Enable IE selection for the dialog (must disable it upon closing it)
\r
109 Pithos.enableIESelection();
\r
115 // Use this opportunity to set the dialog's caption.
\r
117 setText("Create folder");
\r
119 setText("Folder properties");
\r
121 // Outer contains inner and buttons
\r
122 VerticalPanel outer = new VerticalPanel();
\r
124 // Inner contains generalPanel and permPanel
\r
125 inner = new VerticalPanel();
\r
126 inner.addStyleName("inner");
\r
128 VerticalPanel generalPanel = new VerticalPanel();
\r
129 FlexTable generalTable = new FlexTable();
\r
130 generalTable.setText(0, 0, "Name");
\r
131 generalTable.setText(1, 0, "Parent");
\r
132 generalTable.setText(2, 0, "Creator");
\r
133 generalTable.setText(3, 0, "Last modified");
\r
135 folderName.setText(create ? "" : folder.getName());
\r
136 folderName.setReadOnly(folder.isContainer() && !create);
\r
137 generalTable.setWidget(0, 1, folderName);
\r
139 final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
\r
140 folderNameNote.setVisible(false);
\r
141 folderNameNote.setStylePrimaryName("gss-readForAllNote");
\r
142 generalTable.setWidget(0, 2, folderNameNote);
\r
145 generalTable.setText(1, 1, folder.getName());
\r
147 generalTable.setText(1, 1, folder.getParent().getName());
\r
149 generalTable.setText(2, 1, app.getUsername());
\r
151 generalTable.setText(2, 1, folder.getOwner());
\r
152 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
153 if(folder.getLastModified() != null)
\r
154 generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
\r
155 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
156 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
157 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
158 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
159 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
160 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
161 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
162 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
163 generalTable.setCellSpacing(4);
\r
164 generalPanel.add(generalTable);
\r
165 inner.add(generalPanel);
\r
169 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
170 // when the button is clicked and quits the application.
\r
173 okLabel = "Create";
\r
175 okLabel = "Update";
\r
176 final Button ok = new Button(okLabel, new ClickHandler() {
\r
178 public void onClick(ClickEvent event) {
\r
179 createOrUpdateFolder();
\r
183 ok.addStyleName("button");
\r
185 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
\r
187 folderName.addChangeHandler(new ChangeHandler() {
\r
190 public void onChange(ChangeEvent event) {
\r
191 if(folderName.getText().contains("/")) {
\r
192 folderNameNote.setVisible(true);
\r
193 ok.setEnabled(false);
\r
196 folderNameNote.setVisible(false);
\r
197 ok.setEnabled(true);
\r
206 public void center() {
\r
208 folderName.setFocus(true);
\r
212 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
213 super.onPreviewNativeEvent(preview);
\r
215 NativeEvent evt = preview.getNativeEvent();
\r
216 if (evt.getType().equals(KeyDownEvent.getType().getName()))
\r
217 // Use the popup's key preview hooks to close the dialog when either
\r
218 // enter or escape is pressed.
\r
219 switch (evt.getKeyCode()) {
\r
220 case KeyCodes.KEY_ENTER:
\r
221 createOrUpdateFolder();
\r
224 case KeyCodes.KEY_ESCAPE:
\r
232 * Enables IE selection prevention and hides the dialog
\r
233 * (we disable the prevention on creation of the dialog)
\r
235 public void closeDialog() {
\r
236 Pithos.preventIESelection();
\r
241 * Generate an RPC request to create a new folder.
\r
243 private void createFolder() {
\r
244 String name = folderName.getText().trim();
\r
245 if (name.length() == 0)
\r
247 String path = folder.getUri() + "/" + name;
\r
248 PutRequest createFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path) {
\r
250 public void onSuccess(Resource result) {
\r
251 app.updateFolder(folder, true, new Command() {
\r
254 public void execute() {
\r
255 app.updateStatistics();
\r
261 public void onError(Throwable t) {
\r
264 if (t instanceof RestException) {
\r
265 app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
\r
268 app.displayError("System error creating folder:" + t.getMessage());
\r
272 protected void onUnauthorized(Response response) {
\r
273 app.sessionExpired();
\r
276 createFolder.setHeader("X-Auth-Token", app.getToken());
\r
277 createFolder.setHeader("Accept", "*/*");
\r
278 createFolder.setHeader("Content-Length", "0");
\r
279 createFolder.setHeader("Content-Type", "application/directory");
\r
280 Scheduler.get().scheduleDeferred(createFolder);
\r
284 * Upon closing the dialog by clicking OK or pressing ENTER this method does
\r
285 * the actual work of modifying folder properties or creating a new Folder
\r
286 * depending on the value of the create field
\r
288 protected void createOrUpdateFolder() {
\r
296 private void updateFolder() {
\r
297 final String newName = folderName.getText().trim();
\r
298 if (newName.length() == 0)
\r
300 if (!folder.isContainer() && !folder.getName().equals(newName)) {
\r
301 final String path = folder.getParent().getUri() + "/" + newName;
\r
302 PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getParent().getOwner(), path) {
\r
304 public void onSuccess(Resource result) {
\r
305 Iterator<File> iter = folder.getFiles().iterator();
\r
306 app.copyFiles(iter, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
\r
308 public void execute() {
\r
309 Iterator<Folder> iterf = folder.getSubfolders().iterator();
\r
310 app.copySubfolders(iterf, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
\r
312 public void execute() {
\r
313 app.deleteFolder(folder, new Command() {
\r
316 public void execute() {
\r
317 app.updateFolder(folder.getParent(), false, new Command() {
\r
320 public void execute() {
\r
321 app.updateMySharedRoot();
\r
333 public void onError(Throwable t) {
\r
336 if(t instanceof RestException){
\r
337 app.displayError("Unable to update folder: " + ((RestException) t).getHttpStatusText());
\r
340 app.displayError("System error modifying folder: " + t.getMessage());
\r
344 protected void onUnauthorized(Response response) {
\r
345 app.sessionExpired();
\r
348 newFolder.setHeader("X-Auth-Token", app.getToken());
\r
349 newFolder.setHeader("Content-Type", "application/folder");
\r
350 newFolder.setHeader("Accept", "*/*");
\r
351 newFolder.setHeader("Content-Length", "0");
\r
352 Scheduler.get().scheduleDeferred(newFolder);
\r
355 app.updateFolder(folder.getParent(), false, new Command() {
\r
358 public void execute() {
\r
359 app.updateMySharedRoot();
\r