moved towards gwt version 2.0 - updated dnd to version 3 -removed all deprecations
[pithos] / src / gr / ebs / gss / client / VersionsList.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 import gr.ebs.gss.client.rest.DeleteCommand;
23 import gr.ebs.gss.client.rest.PostCommand;
24 import gr.ebs.gss.client.rest.RestCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.List;
32
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.event.dom.client.ClickEvent;
35 import com.google.gwt.event.dom.client.ClickHandler;
36 import com.google.gwt.http.client.URL;
37 import com.google.gwt.i18n.client.DateTimeFormat;
38 import com.google.gwt.user.client.DeferredCommand;
39 import com.google.gwt.user.client.Window;
40 import com.google.gwt.user.client.ui.AbstractImagePrototype;
41 import com.google.gwt.user.client.ui.Composite;
42 import com.google.gwt.user.client.ui.FlexTable;
43 import com.google.gwt.user.client.ui.HTML;
44 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
45 import com.google.gwt.user.client.ui.VerticalPanel;
46
47 /**
48  * @author kman
49  */
50 public class VersionsList extends Composite {
51
52         int selectedRow = -1;
53
54         int permissionCount = -1;
55
56         List<FileResource> versions = null;
57
58         final Images images;
59
60         final VerticalPanel permPanel = new VerticalPanel();
61
62         final FlexTable permTable = new FlexTable();
63
64         FileResource toRemove = null;
65
66         FilePropertiesDialog container;
67
68         public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
69                 images = theImages;
70                 container = aContainer;
71                 versions = theVersions;
72                 Collections.sort(theVersions, new Comparator<FileResource>(){
73
74                         public int compare(FileResource o1, FileResource o2) {
75                                 return o1.getVersion().compareTo(o2.getVersion());
76                         }
77
78                 });
79                 permTable.setText(0, 0, "Version");
80                 permTable.setText(0, 1, "Created");
81                 permTable.setText(0, 2, "Modified");
82                 permTable.setText(0, 3, "Size");
83                 permTable.setText(0, 4, "");
84                 permTable.setText(0, 5, "");
85                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
86                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
87                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
88                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
89                 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
90                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
91                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
92                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
93                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
94                 permPanel.add(permTable);
95                 permPanel.addStyleName("gwt-TabPanelBottom");
96                 permTable.addStyleName("gss-permList");
97                 initWidget(permPanel);
98                 updateTable();
99         }
100
101         public void updateTable() {
102                 int i = 1;
103                 if (toRemove != null) {
104                         versions.remove(toRemove);
105                         toRemove = null;
106                 }
107                 for (final FileResource dto : versions) {
108                         HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
109                         restoreVersion.addClickHandler( new ClickHandler() {
110                                 @Override
111                                 public void onClick(ClickEvent event) {
112                                         restoreVersion(dto);
113                                 }
114                         });
115
116                         permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
117                         permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + "</span>");
118                         permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + "</span>");
119                         permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
120                         HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
121                         downloadHtml.addClickHandler(new ClickHandler() {
122                                 @Override
123                                 public void onClick(ClickEvent event) {
124                                         GSS app = GSS.get();
125                                         String dateString = RestCommand.getDate();
126                                         String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
127                                         String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
128                                         String fileUrl = dto.getUri() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
129                                         Window.open(fileUrl, "_BLANK", "");
130
131                                 }
132                         });
133                         GWT.log("images:"+AbstractImagePrototype.create(images.download()).getHTML(), null);
134                         permTable.setWidget(i, 4, downloadHtml);
135                         permTable.setWidget(i, 5, restoreVersion);
136                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
137                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
138                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
139                         permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
140                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
141                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
142                         i++;
143                 }
144                 for (; i < permTable.getRowCount(); i++)
145                         permTable.removeRow(i);
146         }
147
148
149         void removeVersion(final FileResource version) {
150                 DeleteCommand df = new DeleteCommand(version.getUri()){
151
152                         @Override
153                         public void onComplete() {
154                                 toRemove = version;
155                                 updateTable();
156                                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
157                         }
158
159                         @Override
160                         public void onError(Throwable t) {
161                                 GWT.log("", t);
162                                 if(t instanceof RestException){
163                                         int statusCode = ((RestException)t).getHttpStatusCode();
164                                         if(statusCode == 405)
165                                                 GSS.get().displayError("You don't have the necessary permissions");
166                                         else if(statusCode == 404)
167                                                 GSS.get().displayError("Versions does not exist");
168                                         else
169                                                 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
170                                 }
171                                 else
172                                         GSS.get().displayError("System error removing version:"+t.getMessage());
173                         }
174                 };
175                 DeferredCommand.addCommand(df);
176
177         }
178
179         void restoreVersion(final FileResource version) {
180                 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
181                 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
182
183
184                         @Override
185                         public void onComplete() {
186                                 container.hide();
187                 GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
188                         }
189
190                         @Override
191                         public void onError(Throwable t) {
192                                 GWT.log("", t);
193                                 if(t instanceof RestException)
194                                         GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
195                                 else
196                                         GSS.get().displayError("System error restoring version:"+t.getMessage());
197                         }
198
199                 };
200                 DeferredCommand.addCommand(ep);
201         }
202
203         private String formatDate(Date date){
204                 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
205                 return format.format(date);
206         }
207
208 }