Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / VersionsList.java @ 3c534380

History | View | Annotate | Download (7 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 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 9326b841 Christos Stathis
import gr.grnet.pithos.web.client.FileVersionsDialog.Images;
38 611de529 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
39 611de529 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
40 611de529 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Version;
41 611de529 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostRequest;
42 611de529 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
43 611de529 Christos Stathis
44 611de529 Christos Stathis
import java.util.List;
45 611de529 Christos Stathis
46 611de529 Christos Stathis
import com.google.gwt.core.client.Scheduler;
47 611de529 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
48 611de529 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
49 2efd2a81 Christos Stathis
import com.google.gwt.event.dom.client.ScrollHandler;
50 2efd2a81 Christos Stathis
import com.google.gwt.event.shared.HandlerRegistration;
51 611de529 Christos Stathis
import com.google.gwt.http.client.Response;
52 a2411896 Christos Stathis
import com.google.gwt.http.client.URL;
53 611de529 Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
54 611de529 Christos Stathis
import com.google.gwt.user.client.Window;
55 611de529 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
56 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
57 2efd2a81 Christos Stathis
import com.google.gwt.user.client.ui.CustomScrollPanel;
58 611de529 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
59 611de529 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
60 611de529 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
61 2efd2a81 Christos Stathis
import com.google.gwt.user.client.ui.NativeVerticalScrollbar;
62 2efd2a81 Christos Stathis
import com.google.gwt.user.client.ui.ScrollPanel;
63 611de529 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
64 2efd2a81 Christos Stathis
import com.google.gwt.user.client.ui.VerticalScrollbar;
65 2efd2a81 Christos Stathis
import com.google.gwt.user.client.ui.Widget;
66 a57faaf0 Christos Stathis
67 a57faaf0 Christos Stathis
public class VersionsList extends Composite {
68 a57faaf0 Christos Stathis
69 611de529 Christos Stathis
    Pithos app;
70 611de529 Christos Stathis
71 611de529 Christos Stathis
    File file;
72 611de529 Christos Stathis
    
73 611de529 Christos Stathis
    private List<Version> versions = null;
74 611de529 Christos Stathis
    
75 611de529 Christos Stathis
    private Images images;
76 611de529 Christos Stathis
    
77 2efd2a81 Christos Stathis
    private CustomScrollPanel permPanel = new CustomScrollPanel();
78 611de529 Christos Stathis
    
79 611de529 Christos Stathis
    private FlexTable permTable = new FlexTable();
80 611de529 Christos Stathis
81 9326b841 Christos Stathis
    FileVersionsDialog container;
82 611de529 Christos Stathis
83 9326b841 Christos Stathis
        public VersionsList(Pithos _app, FileVersionsDialog aContainer, final Images theImages, File _file, List<Version> theVersions) {
84 611de529 Christos Stathis
        app = _app;
85 611de529 Christos Stathis
                images = theImages;
86 611de529 Christos Stathis
                container = aContainer;
87 611de529 Christos Stathis
                file = _file;
88 611de529 Christos Stathis
                versions = theVersions;
89 611de529 Christos Stathis
                permTable.setText(0, 0, "Version");
90 611de529 Christos Stathis
                permTable.setText(0, 1, "Date");
91 611de529 Christos Stathis
                permTable.setText(0, 2, "");
92 611de529 Christos Stathis
                permTable.setText(0, 3, "");
93 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
94 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
95 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
96 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
97 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
98 611de529 Christos Stathis
                permPanel.add(permTable);
99 2efd2a81 Christos Stathis
                permPanel.addStyleName("pithos-versionList");
100 2efd2a81 Christos Stathis
                permPanel.setSize("260px", "150px");
101 611de529 Christos Stathis
                permTable.addStyleName("pithos-permList");
102 611de529 Christos Stathis
                initWidget(permPanel);
103 611de529 Christos Stathis
                showVersionsTable();
104 611de529 Christos Stathis
        }
105 611de529 Christos Stathis
106 611de529 Christos Stathis
        public void showVersionsTable(){
107 611de529 Christos Stathis
                DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
108 611de529 Christos Stathis
                int i = 1;
109 611de529 Christos Stathis
                for (final Version v : versions) {
110 611de529 Christos Stathis
                        HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>" + AbstractImagePrototype.create(images.restore()).getHTML() + "</span><div>Restore this Version</div></a>");
111 611de529 Christos Stathis
                        restoreVersion.addClickHandler(new ClickHandler() {
112 611de529 Christos Stathis
                                @Override
113 ebead1b5 Christos Stathis
                                public void onClick(ClickEvent event) {
114 611de529 Christos Stathis
                                        restoreVersion(v.getVersion());
115 611de529 Christos Stathis
                                }
116 611de529 Christos Stathis
                        });
117 611de529 Christos Stathis
118 611de529 Christos Stathis
                        permTable.setHTML(i, 0, "<span>" + v.getVersion() + "</span>");
119 7228fda0 Christos Stathis
                        if (v.getDate() != null)
120 7228fda0 Christos Stathis
                                permTable.setHTML(i, 1, "<span>" + formatter.format(v.getDate()) + "</span>");
121 7228fda0 Christos Stathis
                        else
122 7228fda0 Christos Stathis
                                permTable.setHTML(i, 1, "<span></span>");
123 611de529 Christos Stathis
                        HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>" + AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
124 611de529 Christos Stathis
                        downloadHtml.addClickHandler(new ClickHandler() {
125 611de529 Christos Stathis
                                @Override
126 ebead1b5 Christos Stathis
                                public void onClick(ClickEvent event) {
127 983f9ee2 Christos Stathis
                                        String fileUrl = app.getApiPath() + file.getOwner() + file.getUri() + "?version=" + v.getVersion();
128 611de529 Christos Stathis
                                        Window.open(fileUrl, "_BLANK", "");
129 611de529 Christos Stathis
                                }
130 611de529 Christos Stathis
                        });
131 611de529 Christos Stathis
                        permTable.setWidget(i, 2, downloadHtml);
132 611de529 Christos Stathis
                        permTable.setWidget(i, 3, restoreVersion);
133 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
134 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
135 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
136 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
137 611de529 Christos Stathis
                        i++;
138 611de529 Christos Stathis
                }
139 611de529 Christos Stathis
        }
140 611de529 Christos Stathis
141 611de529 Christos Stathis
        void restoreVersion(int version) {
142 611de529 Christos Stathis
                String path = file.getUri() + "?update=";
143 611de529 Christos Stathis
                PostRequest restoreVersion = new PostRequest(app.getApiPath(), file.getOwner(), path) {
144 611de529 Christos Stathis
                        
145 611de529 Christos Stathis
                        @Override
146 ebead1b5 Christos Stathis
                        public void onSuccess(Resource result) {
147 611de529 Christos Stathis
                                container.hide();
148 611de529 Christos Stathis
                        }
149 611de529 Christos Stathis
                        
150 611de529 Christos Stathis
                        @Override
151 611de529 Christos Stathis
                        public void onError(Throwable t) {
152 3f8622d4 Christos Stathis
                                app.setError(t);
153 611de529 Christos Stathis
                if (t instanceof RestException) {
154 611de529 Christos Stathis
                        if (((RestException) t).getHttpStatusCode() == Response.SC_NO_CONTENT)
155 611de529 Christos Stathis
                                onSuccess(null);
156 611de529 Christos Stathis
                        else
157 611de529 Christos Stathis
                                app.displayError("Unable to restore version: " + ((RestException) t).getHttpStatusText());
158 611de529 Christos Stathis
                }
159 611de529 Christos Stathis
                else
160 611de529 Christos Stathis
                    app.displayError("System error unable to restore versions: "+t.getMessage());
161 611de529 Christos Stathis
                        }
162 9539e23d Christos Stathis
163 9539e23d Christos Stathis
                        @Override
164 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
165 9539e23d Christos Stathis
                                app.sessionExpired();
166 9539e23d Christos Stathis
                        }
167 611de529 Christos Stathis
                };
168 611de529 Christos Stathis
                restoreVersion.setHeader("X-Auth-Token", app.getToken());
169 a2411896 Christos Stathis
                restoreVersion.setHeader("X-Source-Object", URL.encodePathSegment(file.getUri()));
170 611de529 Christos Stathis
                restoreVersion.setHeader("X-Source-Version", String.valueOf(version));
171 611de529 Christos Stathis
                restoreVersion.setHeader("Content-Range", "bytes 0-/*");
172 611de529 Christos Stathis
                Scheduler.get().scheduleDeferred(restoreVersion);
173 611de529 Christos Stathis
        }
174 a57faaf0 Christos Stathis
}