Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPropertiesDialog.java @ a92f9b30

History | View | Annotate | Download (18.7 kB)

1 a57faaf0 Christos Stathis
/*
2 63366925 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 b651e67f Christos Stathis
import com.google.gwt.core.client.Scheduler;
38 447407c8 Christos Stathis
import com.google.gwt.event.dom.client.KeyDownEvent;
39 d6be39da Christos Stathis
import com.google.gwt.user.client.Command;
40 d6be39da Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
41 447407c8 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
42 b651e67f Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
43 5ebc9312 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostRequest;
44 b651e67f Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
45 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
46 a57faaf0 Christos Stathis
47 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
48 a57faaf0 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
49 903f3402 Christos Stathis
import com.google.gwt.event.dom.client.ChangeEvent;
50 903f3402 Christos Stathis
import com.google.gwt.event.dom.client.ChangeHandler;
51 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
52 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
53 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
54 f1b24b82 Christos Stathis
import com.google.gwt.http.client.Response;
55 a2411896 Christos Stathis
import com.google.gwt.http.client.URL;
56 a57faaf0 Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
57 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
58 4420a247 Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
59 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Button;
60 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DecoratedTabPanel;
61 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
62 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
63 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
64 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
65 903f3402 Christos Stathis
import com.google.gwt.user.client.ui.Label;
66 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.TabPanel;
67 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.TextBox;
68 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
69 d6be39da Christos Stathis
import java.util.Iterator;
70 5ebc9312 Christos Stathis
import java.util.Map;
71 a57faaf0 Christos Stathis
72 a57faaf0 Christos Stathis
/**
73 a57faaf0 Christos Stathis
 * The 'Folder properties' dialog box implementation.
74 a57faaf0 Christos Stathis
 */
75 a57faaf0 Christos Stathis
public class FolderPropertiesDialog extends DialogBox {
76 a57faaf0 Christos Stathis
77 7811b9d1 Christos Stathis
    protected Pithos app;
78 b651e67f Christos Stathis
79 a57faaf0 Christos Stathis
        /**
80 a57faaf0 Christos Stathis
         * The widget that holds the folderName of the folder.
81 a57faaf0 Christos Stathis
         */
82 903f3402 Christos Stathis
        TextBox folderName = new TextBox();
83 a57faaf0 Christos Stathis
84 a57faaf0 Christos Stathis
        /**
85 a57faaf0 Christos Stathis
         * A flag that denotes whether the dialog will be used to create or modify a
86 a57faaf0 Christos Stathis
         * folder.
87 a57faaf0 Christos Stathis
         */
88 a57faaf0 Christos Stathis
        private final boolean create;
89 a57faaf0 Christos Stathis
90 7811b9d1 Christos Stathis
        protected PermissionsList permList;
91 ecf95c9e Christos Stathis
92 447407c8 Christos Stathis
        final Folder folder;
93 a57faaf0 Christos Stathis
94 a57faaf0 Christos Stathis
        final TabPanel inner;
95 a57faaf0 Christos Stathis
96 a57faaf0 Christos Stathis
        /**
97 a57faaf0 Christos Stathis
         * The widget's constructor.
98 a57faaf0 Christos Stathis
         */
99 ecf95c9e Christos Stathis
        public FolderPropertiesDialog(final Pithos app, boolean _create,  Folder selected) {
100 b651e67f Christos Stathis
        this.app = app;
101 4420a247 Christos Stathis
                Anchor close = new Anchor();
102 4420a247 Christos Stathis
                close.addStyleName("close");
103 4420a247 Christos Stathis
                close.addClickHandler(new ClickHandler() {
104 4420a247 Christos Stathis
                        
105 4420a247 Christos Stathis
                        @Override
106 4420a247 Christos Stathis
                        public void onClick(ClickEvent event) {
107 4420a247 Christos Stathis
                                hide();
108 4420a247 Christos Stathis
                        }
109 4420a247 Christos Stathis
                });
110 4420a247 Christos Stathis
111 a57faaf0 Christos Stathis
                setAnimationEnabled(true);
112 4420a247 Christos Stathis
                setGlassEnabled(true);
113 4420a247 Christos Stathis
                setStyleName("pithos-DialogBox");
114 a57faaf0 Christos Stathis
115 a57faaf0 Christos Stathis
                // Enable IE selection for the dialog (must disable it upon closing it)
116 7811b9d1 Christos Stathis
                Pithos.enableIESelection();
117 a57faaf0 Christos Stathis
118 a57faaf0 Christos Stathis
                create = _create;
119 a57faaf0 Christos Stathis
                
120 447407c8 Christos Stathis
                folder = selected;
121 a57faaf0 Christos Stathis
122 a57faaf0 Christos Stathis
                // Use this opportunity to set the dialog's caption.
123 a57faaf0 Christos Stathis
                if (create)
124 a57faaf0 Christos Stathis
                        setText("Create folder");
125 a57faaf0 Christos Stathis
                else
126 a57faaf0 Christos Stathis
                        setText("Folder properties");
127 a57faaf0 Christos Stathis
128 a57faaf0 Christos Stathis
                // Outer contains inner and buttons
129 a57faaf0 Christos Stathis
                VerticalPanel outer = new VerticalPanel();
130 4420a247 Christos Stathis
                outer.add(close);
131 a57faaf0 Christos Stathis
                // Inner contains generalPanel and permPanel
132 a57faaf0 Christos Stathis
                inner = new DecoratedTabPanel();
133 a57faaf0 Christos Stathis
                inner.setAnimationEnabled(true);
134 4420a247 Christos Stathis
                inner.addStyleName("inner");
135 4420a247 Christos Stathis
                inner.getDeckPanel().addStyleName("pithos-TabPanelBottom");
136 ecf95c9e Christos Stathis
137 a57faaf0 Christos Stathis
                VerticalPanel generalPanel = new VerticalPanel();
138 ecf95c9e Christos Stathis
        FlexTable generalTable = new FlexTable();
139 ecf95c9e Christos Stathis
        generalTable.setText(0, 0, "Name");
140 ecf95c9e Christos Stathis
        generalTable.setText(1, 0, "Parent");
141 ecf95c9e Christos Stathis
        generalTable.setText(2, 0, "Creator");
142 ecf95c9e Christos Stathis
        generalTable.setText(3, 0, "Last modified");
143 903f3402 Christos Stathis
144 903f3402 Christos Stathis
                folderName.setText(create ? "" : folder.getName());
145 4fe76337 Christos Stathis
        folderName.setReadOnly(folder.isContainer() && !create);
146 ecf95c9e Christos Stathis
        generalTable.setWidget(0, 1, folderName);
147 ecf95c9e Christos Stathis
148 903f3402 Christos Stathis
        final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
149 903f3402 Christos Stathis
                folderNameNote.setVisible(false);
150 903f3402 Christos Stathis
                folderNameNote.setStylePrimaryName("gss-readForAllNote");
151 903f3402 Christos Stathis
                generalTable.setWidget(0, 2, folderNameNote);
152 903f3402 Christos Stathis
                
153 ecf95c9e Christos Stathis
        if (create)
154 ecf95c9e Christos Stathis
            generalTable.setText(1, 1, folder.getName());
155 ecf95c9e Christos Stathis
        else
156 ecf95c9e Christos Stathis
            generalTable.setText(1, 1, folder.getPrefix());
157 ecf95c9e Christos Stathis
        generalTable.setText(2, 1, "");
158 ecf95c9e Christos Stathis
        DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
159 ecf95c9e Christos Stathis
        if(folder.getLastModified() != null)
160 ecf95c9e Christos Stathis
            generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
161 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
162 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
163 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
164 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
165 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
166 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
167 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
168 ecf95c9e Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
169 ecf95c9e Christos Stathis
        generalTable.setCellSpacing(4);
170 ecf95c9e Christos Stathis
        generalPanel.add(generalTable);
171 ecf95c9e Christos Stathis
        inner.add(generalPanel, "General");
172 ecf95c9e Christos Stathis
173 ecf95c9e Christos Stathis
        VerticalPanel permPanel = new VerticalPanel();
174 ecf95c9e Christos Stathis
        FilePropertiesDialog.Images images = GWT.create(FilePropertiesDialog.Images.class);
175 ecf95c9e Christos Stathis
        boolean permsReadonly = folder.getInheritedPermissionsFrom() != null || folder.existChildrenPermissions();
176 ecf95c9e Christos Stathis
        permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner(), permsReadonly);
177 ecf95c9e Christos Stathis
        permPanel.add(permList);
178 ecf95c9e Christos Stathis
179 ecf95c9e Christos Stathis
        if (!permsReadonly) {
180 ecf95c9e Christos Stathis
            HorizontalPanel permButtons = new HorizontalPanel();
181 ecf95c9e Christos Stathis
            Button add = new Button("Add Group", new ClickHandler() {
182 ecf95c9e Christos Stathis
                @Override
183 7811b9d1 Christos Stathis
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
184 ecf95c9e Christos Stathis
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
185 ecf95c9e Christos Stathis
                    dlg.center();
186 ecf95c9e Christos Stathis
                }
187 ecf95c9e Christos Stathis
            });
188 969a4d94 Christos Stathis
            add.addStyleName("button");
189 ecf95c9e Christos Stathis
            permButtons.add(add);
190 ecf95c9e Christos Stathis
            permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
191 ecf95c9e Christos Stathis
192 ecf95c9e Christos Stathis
            Button addUser = new Button("Add User", new ClickHandler() {
193 ecf95c9e Christos Stathis
                @Override
194 7811b9d1 Christos Stathis
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
195 ecf95c9e Christos Stathis
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
196 ecf95c9e Christos Stathis
                    dlg.center();
197 ecf95c9e Christos Stathis
                }
198 ecf95c9e Christos Stathis
            });
199 969a4d94 Christos Stathis
            addUser.addStyleName("button");
200 ecf95c9e Christos Stathis
            permButtons.add(addUser);
201 ecf95c9e Christos Stathis
            permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
202 ecf95c9e Christos Stathis
            permButtons.setSpacing(8);
203 ecf95c9e Christos Stathis
            permPanel.add(permButtons);
204 ecf95c9e Christos Stathis
        }
205 ecf95c9e Christos Stathis
206 ecf95c9e Christos Stathis
        if (!create)
207 ecf95c9e Christos Stathis
            inner.add(permPanel, "Sharing");
208 ecf95c9e Christos Stathis
        inner.selectTab(0);
209 ecf95c9e Christos Stathis
210 ecf95c9e Christos Stathis
        outer.add(inner);
211 ecf95c9e Christos Stathis
212 a57faaf0 Christos Stathis
                // Create the 'Create/Update' button, along with a listener that hides the dialog
213 a57faaf0 Christos Stathis
                // when the button is clicked and quits the application.
214 a57faaf0 Christos Stathis
                String okLabel;
215 a57faaf0 Christos Stathis
                if (create)
216 a57faaf0 Christos Stathis
                        okLabel = "Create";
217 a57faaf0 Christos Stathis
                else
218 a57faaf0 Christos Stathis
                        okLabel = "Update";
219 903f3402 Christos Stathis
                final Button ok = new Button(okLabel, new ClickHandler() {
220 a57faaf0 Christos Stathis
                        @Override
221 7811b9d1 Christos Stathis
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
222 447407c8 Christos Stathis
                                createOrUpdateFolder();
223 447407c8 Christos Stathis
                                closeDialog();
224 a57faaf0 Christos Stathis
                        }
225 a57faaf0 Christos Stathis
                });
226 4420a247 Christos Stathis
                ok.addStyleName("button");
227 4420a247 Christos Stathis
                outer.add(ok);
228 4420a247 Christos Stathis
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
229 a57faaf0 Christos Stathis
230 903f3402 Christos Stathis
        folderName.addChangeHandler(new ChangeHandler() {
231 903f3402 Christos Stathis
                        
232 903f3402 Christos Stathis
                        @Override
233 903f3402 Christos Stathis
                        public void onChange(@SuppressWarnings("unused") ChangeEvent event) {
234 903f3402 Christos Stathis
                                if(folderName.getText().contains("/")) {
235 903f3402 Christos Stathis
                                        folderNameNote.setVisible(true);
236 903f3402 Christos Stathis
                                        ok.setEnabled(false);
237 903f3402 Christos Stathis
                                }
238 903f3402 Christos Stathis
                                else {
239 903f3402 Christos Stathis
                                        folderNameNote.setVisible(false);
240 903f3402 Christos Stathis
                                        ok.setEnabled(true);
241 903f3402 Christos Stathis
                                }
242 903f3402 Christos Stathis
                        }
243 903f3402 Christos Stathis
                });
244 903f3402 Christos Stathis
245 ecf95c9e Christos Stathis
        setWidget(outer);
246 a57faaf0 Christos Stathis
        }
247 a57faaf0 Christos Stathis
248 a57faaf0 Christos Stathis
        @Override
249 a57faaf0 Christos Stathis
        public void center() {
250 a57faaf0 Christos Stathis
                super.center();
251 a57faaf0 Christos Stathis
                folderName.setFocus(true);
252 a57faaf0 Christos Stathis
        }
253 a57faaf0 Christos Stathis
254 a57faaf0 Christos Stathis
        @Override
255 a57faaf0 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
256 a57faaf0 Christos Stathis
                super.onPreviewNativeEvent(preview);
257 a57faaf0 Christos Stathis
258 a57faaf0 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
259 447407c8 Christos Stathis
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
260 a57faaf0 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
261 a57faaf0 Christos Stathis
                        // enter or escape is pressed.
262 a57faaf0 Christos Stathis
                        switch (evt.getKeyCode()) {
263 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ENTER:
264 a57faaf0 Christos Stathis
                                        createOrUpdateFolder();
265 ecf95c9e Christos Stathis
                    closeDialog();
266 a57faaf0 Christos Stathis
                                        break;
267 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
268 a57faaf0 Christos Stathis
                                        closeDialog();
269 a57faaf0 Christos Stathis
                                        break;
270 a57faaf0 Christos Stathis
                        }
271 a57faaf0 Christos Stathis
        }
272 a57faaf0 Christos Stathis
273 a57faaf0 Christos Stathis
274 a57faaf0 Christos Stathis
        /**
275 a57faaf0 Christos Stathis
         * Enables IE selection prevention and hides the dialog
276 a57faaf0 Christos Stathis
         * (we disable the prevention on creation of the dialog)
277 a57faaf0 Christos Stathis
         */
278 a57faaf0 Christos Stathis
        public void closeDialog() {
279 7811b9d1 Christos Stathis
                Pithos.preventIESelection();
280 a57faaf0 Christos Stathis
                hide();
281 a57faaf0 Christos Stathis
        }
282 a57faaf0 Christos Stathis
283 a57faaf0 Christos Stathis
        /**
284 a57faaf0 Christos Stathis
         * Generate an RPC request to create a new folder.
285 a57faaf0 Christos Stathis
         */
286 a57faaf0 Christos Stathis
        private void createFolder() {
287 903f3402 Christos Stathis
                String name = folderName.getText().trim();
288 b51c628b Christos Stathis
        String path = folder.getUri() + "/" + name;
289 63ee965c Christos Stathis
        PutRequest createFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path) {
290 b651e67f Christos Stathis
            @Override
291 7811b9d1 Christos Stathis
            public void onSuccess(@SuppressWarnings("unused") Resource result) {
292 64f42c0b Christos Stathis
                app.updateFolder(folder, true, new Command() {
293 64f42c0b Christos Stathis
                                        
294 64f42c0b Christos Stathis
                                        @Override
295 64f42c0b Christos Stathis
                                        public void execute() {
296 64f42c0b Christos Stathis
                                                app.updateStatistics();
297 64f42c0b Christos Stathis
                                        }
298 64f42c0b Christos Stathis
                                });
299 b651e67f Christos Stathis
            }
300 b651e67f Christos Stathis
301 b651e67f Christos Stathis
            @Override
302 b651e67f Christos Stathis
            public void onError(Throwable t) {
303 b651e67f Christos Stathis
                                GWT.log("", t);
304 b651e67f Christos Stathis
                                if (t instanceof RestException) {
305 b651e67f Christos Stathis
                                        app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
306 b651e67f Christos Stathis
                                }
307 b651e67f Christos Stathis
                                else
308 b651e67f Christos Stathis
                                        app.displayError("System error creating folder:" + t.getMessage());
309 b651e67f Christos Stathis
            }
310 9539e23d Christos Stathis
311 9539e23d Christos Stathis
                        @Override
312 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
313 9539e23d Christos Stathis
                                app.sessionExpired();
314 9539e23d Christos Stathis
                        }
315 9539e23d Christos Stathis
       };
316 f1aabd89 Christos Stathis
        createFolder.setHeader("X-Auth-Token", app.getToken());
317 b651e67f Christos Stathis
        createFolder.setHeader("Accept", "*/*");
318 b651e67f Christos Stathis
        createFolder.setHeader("Content-Length", "0");
319 b3fd800b Christos Stathis
        createFolder.setHeader("Content-Type", "application/directory");
320 b651e67f Christos Stathis
        Scheduler.get().scheduleDeferred(createFolder);
321 a57faaf0 Christos Stathis
        }
322 a57faaf0 Christos Stathis
323 a57faaf0 Christos Stathis
        /**
324 a57faaf0 Christos Stathis
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
325 a57faaf0 Christos Stathis
         * the actual work of modifying folder properties or creating a new Folder
326 a57faaf0 Christos Stathis
         * depending on the value of the create field
327 a57faaf0 Christos Stathis
         */
328 7811b9d1 Christos Stathis
        protected void createOrUpdateFolder() {
329 a57faaf0 Christos Stathis
                if (create)
330 a57faaf0 Christos Stathis
                        createFolder();
331 a57faaf0 Christos Stathis
                else
332 a57faaf0 Christos Stathis
                        updateFolder();
333 a57faaf0 Christos Stathis
334 a57faaf0 Christos Stathis
        }
335 a57faaf0 Christos Stathis
336 a57faaf0 Christos Stathis
        private void updateFolder() {
337 5ebc9312 Christos Stathis
        final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
338 5ebc9312 Christos Stathis
        final String newName = folderName.getText().trim();
339 d6be39da Christos Stathis
        if (!folder.isContainer() && !folder.getName().equals(newName)) {
340 b51c628b Christos Stathis
            final String path = folder.getParent().getUri() + "/" + newName;
341 63ee965c Christos Stathis
            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getParent().getOwner(), path) {
342 d6be39da Christos Stathis
                @Override
343 7811b9d1 Christos Stathis
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
344 d6be39da Christos Stathis
                    Iterator<File> iter = folder.getFiles().iterator();
345 63ee965c Christos Stathis
                    app.copyFiles(iter, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
346 d6be39da Christos Stathis
                        @Override
347 d6be39da Christos Stathis
                        public void execute() {
348 d6be39da Christos Stathis
                            Iterator<Folder> iterf = folder.getSubfolders().iterator();
349 63ee965c Christos Stathis
                            app.copySubfolders(iterf, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
350 d6be39da Christos Stathis
                                @Override
351 d6be39da Christos Stathis
                                public void execute() {
352 d6be39da Christos Stathis
                                    app.deleteFolder(folder);
353 5ebc9312 Christos Stathis
                                    updateMetadata(path + "?update=", perms);
354 d6be39da Christos Stathis
                                }
355 d6be39da Christos Stathis
                            });
356 d6be39da Christos Stathis
                        }
357 d6be39da Christos Stathis
                    });
358 d6be39da Christos Stathis
                }
359 d6be39da Christos Stathis
360 d6be39da Christos Stathis
                @Override
361 d6be39da Christos Stathis
                public void onError(Throwable t) {
362 d6be39da Christos Stathis
                    GWT.log("", t);
363 d6be39da Christos Stathis
                    if(t instanceof RestException){
364 d6be39da Christos Stathis
                        app.displayError("Unable to update folder: " + ((RestException) t).getHttpStatusText());
365 d6be39da Christos Stathis
                    }
366 d6be39da Christos Stathis
                    else
367 d6be39da Christos Stathis
                        app.displayError("System error modifying folder: " + t.getMessage());
368 d6be39da Christos Stathis
                }
369 9539e23d Christos Stathis
370 9539e23d Christos Stathis
                                @Override
371 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
372 9539e23d Christos Stathis
                                        app.sessionExpired();
373 9539e23d Christos Stathis
                                }
374 d6be39da Christos Stathis
            };
375 d6be39da Christos Stathis
            newFolder.setHeader("X-Auth-Token", app.getToken());
376 d6be39da Christos Stathis
            newFolder.setHeader("Content-Type", "application/folder");
377 d6be39da Christos Stathis
            newFolder.setHeader("Accept", "*/*");
378 d6be39da Christos Stathis
            newFolder.setHeader("Content-Length", "0");
379 d6be39da Christos Stathis
            Scheduler.get().scheduleDeferred(newFolder);
380 d6be39da Christos Stathis
        }
381 5ebc9312 Christos Stathis
        else
382 5ebc9312 Christos Stathis
            updateMetadata(folder.getUri() + "?update=", perms);
383 a57faaf0 Christos Stathis
        }
384 a57faaf0 Christos Stathis
385 f1b24b82 Christos Stathis
        protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {
386 5ebc9312 Christos Stathis
        if (newPermissions != null) {
387 5ebc9312 Christos Stathis
            PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {
388 5ebc9312 Christos Stathis
                @Override
389 7811b9d1 Christos Stathis
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
390 16e8478a Christos Stathis
                    app.updateFolder(folder.getParent(), false, null);
391 5ebc9312 Christos Stathis
                }
392 5ebc9312 Christos Stathis
393 5ebc9312 Christos Stathis
                @Override
394 5ebc9312 Christos Stathis
                public void onError(Throwable t) {
395 5ebc9312 Christos Stathis
                    GWT.log("", t);
396 f1b24b82 Christos Stathis
                    if (t instanceof RestException) {
397 f1b24b82 Christos Stathis
                            if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder
398 f1b24b82 Christos Stathis
                            final String path1 = folder.getUri();
399 63ee965c Christos Stathis
                            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {
400 f1b24b82 Christos Stathis
                                @Override
401 f1b24b82 Christos Stathis
                                public void onSuccess(@SuppressWarnings("unused") Resource result) {
402 f1b24b82 Christos Stathis
                                        updateMetadata(path, newPermissions);
403 f1b24b82 Christos Stathis
                                }
404 f1b24b82 Christos Stathis
405 f1b24b82 Christos Stathis
                                @Override
406 abd8738f Christos Stathis
                                public void onError(Throwable _t) {
407 abd8738f Christos Stathis
                                    GWT.log("", _t);
408 abd8738f Christos Stathis
                                    if(_t instanceof RestException){
409 abd8738f Christos Stathis
                                        app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
410 f1b24b82 Christos Stathis
                                    }
411 f1b24b82 Christos Stathis
                                    else
412 abd8738f Christos Stathis
                                        app.displayError("System error modifying folder: " + _t.getMessage());
413 f1b24b82 Christos Stathis
                                }
414 9539e23d Christos Stathis
415 9539e23d Christos Stathis
                                                @Override
416 9539e23d Christos Stathis
                                                protected void onUnauthorized(Response response) {
417 9539e23d Christos Stathis
                                                        app.sessionExpired();
418 9539e23d Christos Stathis
                                                }
419 f1b24b82 Christos Stathis
                            };
420 f1b24b82 Christos Stathis
                            newFolder.setHeader("X-Auth-Token", app.getToken());
421 f1b24b82 Christos Stathis
                            newFolder.setHeader("Content-Type", "application/folder");
422 f1b24b82 Christos Stathis
                            newFolder.setHeader("Accept", "*/*");
423 f1b24b82 Christos Stathis
                            newFolder.setHeader("Content-Length", "0");
424 f1b24b82 Christos Stathis
                            Scheduler.get().scheduleDeferred(newFolder);
425 f1b24b82 Christos Stathis
                            }
426 bac098d0 Christos Stathis
                            else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {
427 bac098d0 Christos Stathis
                                    app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
428 bac098d0 Christos Stathis
                            }
429 bac098d0 Christos Stathis
                            else
430 bac098d0 Christos Stathis
                                    app.displayError("Wrror modifying folder: " + t.getMessage());
431 f1b24b82 Christos Stathis
                    }
432 f1b24b82 Christos Stathis
                    else
433 f1b24b82 Christos Stathis
                            app.displayError("System error modifying folder: " + t.getMessage());
434 5ebc9312 Christos Stathis
                }
435 9539e23d Christos Stathis
436 9539e23d Christos Stathis
                                @Override
437 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
438 9539e23d Christos Stathis
                                        app.sessionExpired();
439 9539e23d Christos Stathis
                                }
440 5ebc9312 Christos Stathis
            };
441 5ebc9312 Christos Stathis
            updateFolder.setHeader("X-Auth-Token", app.getToken());
442 7811b9d1 Christos Stathis
            String readPermHeader = "read=";
443 7811b9d1 Christos Stathis
            String writePermHeader = "write=";
444 7811b9d1 Christos Stathis
            for (String u : newPermissions.keySet()) {
445 7811b9d1 Christos Stathis
                Boolean[] p = newPermissions.get(u);
446 7811b9d1 Christos Stathis
                if (p[0] != null && p[0])
447 7811b9d1 Christos Stathis
                    readPermHeader += u + ",";
448 7811b9d1 Christos Stathis
                if (p[1] != null && p[1])
449 7811b9d1 Christos Stathis
                    writePermHeader += u + ",";
450 5ebc9312 Christos Stathis
            }
451 7811b9d1 Christos Stathis
            if (readPermHeader.endsWith("="))
452 7811b9d1 Christos Stathis
                readPermHeader = "";
453 7811b9d1 Christos Stathis
            else if (readPermHeader.endsWith(","))
454 7811b9d1 Christos Stathis
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
455 7811b9d1 Christos Stathis
            if (writePermHeader.endsWith("="))
456 7811b9d1 Christos Stathis
                writePermHeader = "";
457 7811b9d1 Christos Stathis
            else if (writePermHeader.endsWith(","))
458 7811b9d1 Christos Stathis
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
459 7811b9d1 Christos Stathis
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
460 7811b9d1 Christos Stathis
            if (permHeader.length() == 0)
461 a2411896 Christos Stathis
                permHeader = "~";
462 a2411896 Christos Stathis
            else
463 a2411896 Christos Stathis
                    permHeader = URL.encodePathSegment(permHeader);
464 7811b9d1 Christos Stathis
            updateFolder.setHeader("X-Object-Sharing", permHeader);
465 5ebc9312 Christos Stathis
            Scheduler.get().scheduleDeferred(updateFolder);
466 5ebc9312 Christos Stathis
        }
467 5ebc9312 Christos Stathis
        else
468 16e8478a Christos Stathis
            app.updateFolder(folder.getParent(), false, null);
469 5ebc9312 Christos Stathis
    }
470 5ebc9312 Christos Stathis
471 a57faaf0 Christos Stathis
        public void selectTab(int _tab) {
472 a57faaf0 Christos Stathis
                inner.selectTab(_tab);
473 a57faaf0 Christos Stathis
        }
474 a57faaf0 Christos Stathis
}