Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FileVersionsDialog.java @ 666f16ac

History | View | Annotate | Download (6.8 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.foldertree.File;
38
import gr.grnet.pithos.web.client.foldertree.FileVersions;
39
import gr.grnet.pithos.web.client.foldertree.Resource;
40
import gr.grnet.pithos.web.client.foldertree.Version;
41
import gr.grnet.pithos.web.client.rest.GetRequest;
42
import gr.grnet.pithos.web.client.rest.PostRequest;
43
import gr.grnet.pithos.web.client.rest.PutRequest;
44
import gr.grnet.pithos.web.client.rest.RestException;
45

    
46
import java.util.HashMap;
47
import java.util.List;
48
import java.util.Map;
49

    
50
import com.google.gwt.core.client.GWT;
51
import com.google.gwt.core.client.Scheduler;
52
import com.google.gwt.event.dom.client.ClickEvent;
53
import com.google.gwt.event.dom.client.ClickHandler;
54
import com.google.gwt.http.client.Response;
55
import com.google.gwt.http.client.URL;
56
import com.google.gwt.resources.client.ImageResource;
57
import com.google.gwt.user.client.Command;
58
import com.google.gwt.user.client.ui.Anchor;
59
import com.google.gwt.user.client.ui.Button;
60
import com.google.gwt.user.client.ui.CheckBox;
61
import com.google.gwt.user.client.ui.DecoratedTabPanel;
62
import com.google.gwt.user.client.ui.FocusPanel;
63
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
64
import com.google.gwt.user.client.ui.TextBox;
65
import com.google.gwt.user.client.ui.VerticalPanel;
66

    
67
/**
68
 * The 'File properties' dialog box implementation.
69
 *
70
 */
71
public class FileVersionsDialog extends AbstractPropertiesDialog {
72

    
73
        protected PermissionsList permList;
74

    
75
        protected CheckBox readForAll;
76

    
77
        /**
78
         * An image bundle for this widgets images.
79
         */
80
        public interface Images extends MessagePanel.Images {
81

    
82
                @Source("gr/grnet/pithos/resources/edit_user.png")
83
                ImageResource permUser();
84

    
85
                @Source("gr/grnet/pithos/resources/groups22.png")
86
                ImageResource permGroup();
87

    
88
                @Source("gr/grnet/pithos/resources/editdelete.png")
89
                ImageResource delete();
90

    
91
                @Source("gr/grnet/pithos/resources/db_update.png")
92
                ImageResource restore();
93

    
94
                @Source("gr/grnet/pithos/resources/folder_inbox.png")
95
                ImageResource download();
96
        }
97

    
98
        final File file;
99

    
100
    Images images = GWT.create(Images.class);
101

    
102
        /**
103
         * The widget's constructor.
104
         */
105
        public FileVersionsDialog(Pithos _app, File _file) {
106
        super(_app);
107
        file = _file;
108

    
109
                Anchor close = new Anchor();
110
                close.addStyleName("close");
111
                close.addClickHandler(new ClickHandler() {
112
                        
113
                        @Override
114
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
115
                                hide();
116
                        }
117
                });
118
                // Set the dialog's caption.
119
                setText("File versions");
120
                setAnimationEnabled(true);
121
                setGlassEnabled(true);
122
                setStyleName("pithos-DialogBox");
123

    
124
                // Outer contains inner and buttons.
125
                final VerticalPanel outer = new VerticalPanel();
126
                outer.add(close);
127
                final FocusPanel focusPanel = new FocusPanel(outer);
128
                // Inner contains generalPanel and permPanel.
129
                inner = new VerticalPanel();
130
                inner.addStyleName("inner");
131

    
132
                fetchVersions();
133
                        
134
        outer.add(inner);
135

    
136
                // Create the 'OK' button, along with a listener that hides the dialog
137
                // when the button is clicked.
138
                final Button ok = new Button("OK", new ClickHandler() {
139
                        @Override
140
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
141
                                accept();
142
                                closeDialog();
143
                        }
144
                });
145
                ok.addStyleName("button");
146

    
147
        outer.add(ok);
148
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
149

    
150
        focusPanel.setFocus(true);
151
        setWidget(outer);
152
        }
153

    
154
    protected void fetchVersions() {
155
            String path = file.getUri() + "?format=json&version=list";
156
            GetRequest<FileVersions> getVersions = new GetRequest<FileVersions>(FileVersions.class, app.getApiPath(), file.getOwner(), path) {
157

    
158
                        @Override
159
                        public void onSuccess(FileVersions _result) {
160
                        inner.add(createVersionPanel(_result.getVersions()));
161
                        }
162

    
163
                        @Override
164
                        public void onError(Throwable t) {
165
                                GWT.log("", t);
166
                                app.setError(t);
167
                if (t instanceof RestException) {
168
                    app.displayError("Unable to fetch versions: " + ((RestException) t).getHttpStatusText());
169
                }
170
                else
171
                    app.displayError("System error unable to fetch versions: "+t.getMessage());
172
                        }
173

    
174
                        @Override
175
                        protected void onUnauthorized(@SuppressWarnings("unused") Response response) {
176
                                app.sessionExpired();
177
                        }
178
                };
179
                getVersions.setHeader("X-Auth-Token", app.getToken());
180
                Scheduler.get().scheduleDeferred(getVersions);
181
        }
182

    
183
    VerticalPanel createVersionPanel(List<Version> versions) {
184
        VerticalPanel versionPanel = new VerticalPanel();
185
        VersionsList verList = new VersionsList(app, this, images, file, versions);
186
        versionPanel.add(verList);
187
        return versionPanel;
188
    }
189

    
190
        /**
191
         * Accepts any change and updates the file
192
         *
193
         */
194
        @Override
195
        protected void accept() {
196
                updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=");
197
        }
198

    
199
        protected void updateMetaData(String api, String owner, String path) {
200
        app.updateFolder(file.getParent(), true, new Command() {
201
                        
202
                        @Override
203
                        public void execute() {
204
                                if (file.isShared())
205
                                        app.updateMySharedRoot();
206
                        }
207
                });
208
    }
209
}