2 * Copyright 2011-2013 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 com.google.gwt.core.client.GWT;
\r
38 import com.google.gwt.core.client.Scheduler;
\r
39 import com.google.gwt.dom.client.NativeEvent;
\r
40 import com.google.gwt.event.dom.client.*;
\r
41 import com.google.gwt.http.client.Response;
\r
42 import com.google.gwt.i18n.client.DateTimeFormat;
\r
43 import com.google.gwt.user.client.Command;
\r
44 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
45 import com.google.gwt.user.client.ui.*;
\r
46 import gr.grnet.pithos.web.client.foldertree.Folder;
\r
47 import gr.grnet.pithos.web.client.rest.PutRequest;
\r
48 import gr.grnet.pithos.web.client.rest.RestException;
\r
51 * The 'Folder properties' dialog box implementation.
\r
53 public class FolderPropertiesDialog extends DialogBox {
\r
55 protected Pithos app;
\r
58 * The widget that holds the folderName of the folder.
\r
60 TextBox folderName = new TextBox();
\r
63 * A flag that denotes whether the dialog will be used to create or modify a
\r
66 private final boolean create;
\r
68 final Folder folder;
\r
70 final VerticalPanel inner;
\r
73 * The widget's constructor.
\r
75 public FolderPropertiesDialog(final Pithos app, boolean _create, Folder selected) {
\r
77 Anchor close = new Anchor("close");
\r
78 close.addStyleName("close");
\r
79 close.addClickHandler(new ClickHandler() {
\r
82 public void onClick(ClickEvent event) {
\r
87 setGlassEnabled(true);
\r
88 setStyleName("pithos-DialogBox");
\r
90 // Enable IE selection for the dialog (must disable it upon closing it)
\r
91 Pithos.enableIESelection();
\r
97 // Use this opportunity to set the dialog's caption.
\r
99 setText("Create folder");
\r
102 setText("Folder properties");
\r
105 // Outer contains inner and buttons
\r
106 VerticalPanel outer = new VerticalPanel();
\r
108 // Inner contains generalPanel and permPanel
\r
109 inner = new VerticalPanel();
\r
110 inner.addStyleName("inner");
\r
112 VerticalPanel generalPanel = new VerticalPanel();
\r
113 FlexTable generalTable = new FlexTable();
\r
114 generalTable.setText(0, 0, "Name");
\r
115 generalTable.setText(1, 0, "Parent");
\r
116 generalTable.setText(2, 0, "Creator");
\r
117 generalTable.setText(3, 0, "Last modified");
\r
119 folderName.setText(create ? "" : folder.getName());
\r
120 folderName.setReadOnly(folder.isContainer() && !create);
\r
121 generalTable.setWidget(0, 1, folderName);
\r
123 final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
\r
124 folderNameNote.setVisible(false);
\r
125 folderNameNote.setStylePrimaryName("gss-readForAllNote");
\r
126 generalTable.setWidget(0, 2, folderNameNote);
\r
129 generalTable.setText(1, 1, folder.getName());
\r
132 generalTable.setText(1, 1, folder.getParent().getName());
\r
135 generalTable.setText(2, 1, app.getCurrentUserDisplayNameOrID());
\r
138 final String ownerID = folder.getOwnerID();
\r
139 final String displayName = app.getDisplayNameForUserID(ownerID);
\r
140 final String ownerDisplayName;
\r
141 if(displayName == null) {
\r
142 // FIXME: Get the actual display name and do not use the id
\r
143 ownerDisplayName = ownerID;
\r
146 ownerDisplayName = displayName;
\r
148 generalTable.setText(2, 1, ownerDisplayName);
\r
150 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
151 if(folder.getLastModified() != null) {
\r
152 generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
\r
154 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
155 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
156 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
157 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
158 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
159 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
160 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
161 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
162 generalTable.setCellSpacing(4);
\r
163 generalPanel.add(generalTable);
\r
164 inner.add(generalPanel);
\r
168 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
169 // when the button is clicked and quits the application.
\r
172 okLabel = "Create";
\r
175 okLabel = "Update";
\r
177 final Button ok = new Button(okLabel, new ClickHandler() {
\r
179 public void onClick(ClickEvent event) {
\r
180 createOrUpdateFolder();
\r
184 ok.addStyleName("button");
\r
186 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
\r
188 folderName.addChangeHandler(new ChangeHandler() {
\r
191 public void onChange(ChangeEvent event) {
\r
192 if(folderName.getText().contains("/")) {
\r
193 folderNameNote.setVisible(true);
\r
194 ok.setEnabled(false);
\r
197 folderNameNote.setVisible(false);
\r
198 ok.setEnabled(true);
\r
207 public void center() {
\r
209 folderName.setFocus(true);
\r
213 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
214 super.onPreviewNativeEvent(preview);
\r
216 NativeEvent evt = preview.getNativeEvent();
\r
217 if(evt.getType().equals(KeyDownEvent.getType().getName()))
\r
218 // Use the popup's key preview hooks to close the dialog when either
\r
219 // enter or escape is pressed.
\r
221 switch(evt.getKeyCode()) {
\r
222 case KeyCodes.KEY_ENTER:
\r
223 createOrUpdateFolder();
\r
226 case KeyCodes.KEY_ESCAPE:
\r
235 * Enables IE selection prevention and hides the dialog
\r
236 * (we disable the prevention on creation of the dialog)
\r
238 public void closeDialog() {
\r
239 Pithos.preventIESelection();
\r
244 * Generate an RPC request to create a new folder.
\r
246 private void createFolder() {
\r
247 String name = folderName.getText().trim();
\r
248 if(name.length() == 0) {
\r
251 String path = folder.getUri() + "/" + name;
\r
252 PutRequest createFolder = new PutRequest(Pithos.getStorageAPIURL(), folder.getOwnerID(), path) {
\r
254 public void onSuccess(Resource result) {
\r
255 app.updateFolder(folder, true, new Command() {
\r
258 public void execute() {
\r
259 app.updateStatistics();
\r
265 public void onError(Throwable t) {
\r
268 if(t instanceof RestException) {
\r
269 app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
\r
272 app.displayError("System error creating folder:" + t.getMessage());
\r
277 protected void onUnauthorized(Response response) {
\r
278 app.sessionExpired();
\r
281 createFolder.setHeader("X-Auth-Token", app.getUserToken());
\r
282 createFolder.setHeader("Accept", "*/*");
\r
283 createFolder.setHeader("Content-Length", "0");
\r
284 createFolder.setHeader("Content-Type", "application/directory");
\r
285 Scheduler.get().scheduleDeferred(createFolder);
\r
289 * Upon closing the dialog by clicking OK or pressing ENTER this method does
\r
290 * the actual work of modifying folder properties or creating a new Folder
\r
291 * depending on the value of the create field
\r
293 protected void createOrUpdateFolder() {
\r
303 private void updateFolder() {
\r
304 final String newName = folderName.getText().trim();
\r
305 if(newName.length() == 0) {
\r
308 if(!folder.isContainer() && !folder.getName().equals(newName)) {
\r
309 final String path = folder.getParent().getUri() + "/" + newName;
\r
310 app.copyFolder(folder, folder.getOwnerID(), path, true, new Command() {
\r
313 public void execute() {
\r
314 app.updateFolder(folder.getParent(), false, new Command() {
\r
317 public void execute() {
\r
318 app.updateMySharedRoot();
\r
325 app.updateFolder(folder.getParent(), false, new Command() {
\r
328 public void execute() {
\r
329 app.updateMySharedRoot();
\r