Bump version to 0.16
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileVersionsDialog.java
1 /*\r
2  * Copyright 2011-2013 GRNET S.A. All rights reserved.\r
3  *\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
7  *\r
8  *   1. Redistributions of source code must retain the above\r
9  *      copyright notice, this list of conditions and the following\r
10  *      disclaimer.\r
11  *\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
16  *\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
29  *\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
34  */\r
35 package gr.grnet.pithos.web.client;\r
36 \r
37 import gr.grnet.pithos.web.client.foldertree.File;\r
38 import gr.grnet.pithos.web.client.foldertree.FileVersions;\r
39 import gr.grnet.pithos.web.client.foldertree.Version;\r
40 import gr.grnet.pithos.web.client.rest.GetRequest;\r
41 import gr.grnet.pithos.web.client.rest.RestException;\r
42 \r
43 import java.util.List;\r
44 \r
45 import com.google.gwt.core.client.GWT;\r
46 import com.google.gwt.core.client.Scheduler;\r
47 import com.google.gwt.event.dom.client.ClickEvent;\r
48 import com.google.gwt.event.dom.client.ClickHandler;\r
49 import com.google.gwt.http.client.Response;\r
50 import com.google.gwt.resources.client.ImageResource;\r
51 import com.google.gwt.user.client.Command;\r
52 import com.google.gwt.user.client.ui.Anchor;\r
53 import com.google.gwt.user.client.ui.Button;\r
54 import com.google.gwt.user.client.ui.FocusPanel;\r
55 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
56 import com.google.gwt.user.client.ui.VerticalPanel;\r
57 \r
58 /**\r
59  * The 'File properties' dialog box implementation.\r
60  *\r
61  */\r
62 public class FileVersionsDialog extends AbstractPropertiesDialog {\r
63 \r
64         /**\r
65          * An image bundle for this widgets images.\r
66          */\r
67         public interface Images extends MessagePanel.Images {\r
68 \r
69                 @Source("gr/grnet/pithos/resources/edit_user.png")\r
70                 ImageResource permUser();\r
71 \r
72                 @Source("gr/grnet/pithos/resources/groups22.png")\r
73                 ImageResource permGroup();\r
74 \r
75                 @Source("gr/grnet/pithos/resources/editdelete.png")\r
76                 ImageResource delete();\r
77 \r
78                 @Source("gr/grnet/pithos/resources/db_update.png")\r
79                 ImageResource restore();\r
80 \r
81                 @Source("gr/grnet/pithos/resources/folder_inbox.png")\r
82                 ImageResource download();\r
83         }\r
84 \r
85         final File file;\r
86 \r
87     Images images = GWT.create(Images.class);\r
88 \r
89         /**\r
90          * The widget's constructor.\r
91          */\r
92         public FileVersionsDialog(Pithos _app, File _file) {\r
93         super(_app);\r
94         file = _file;\r
95 \r
96                 Anchor close = new Anchor("close");\r
97                 close.addStyleName("close");\r
98                 close.addClickHandler(new ClickHandler() {\r
99                         \r
100                         @Override\r
101                         public void onClick(ClickEvent event) {\r
102                                 hide();\r
103                         }\r
104                 });\r
105                 // Set the dialog's caption.\r
106                 setText("File versions");\r
107                 setGlassEnabled(true);\r
108                 setStyleName("pithos-DialogBox");\r
109 \r
110                 // Outer contains inner and buttons.\r
111                 final VerticalPanel outer = new VerticalPanel();\r
112                 outer.add(close);\r
113                 final FocusPanel focusPanel = new FocusPanel(outer);\r
114                 // Inner contains generalPanel and permPanel.\r
115                 inner = new VerticalPanel();\r
116                 inner.addStyleName("inner");\r
117 \r
118                 outer.add(inner);\r
119 \r
120                 // Create the 'OK' button, along with a listener that hides the dialog\r
121                 // when the button is clicked.\r
122                 final Button ok = new Button("OK", new ClickHandler() {\r
123                         @Override\r
124                         public void onClick(ClickEvent event) {\r
125                                 accept();\r
126                                 closeDialog();\r
127                         }\r
128                 });\r
129                 ok.addStyleName("button");\r
130 \r
131         outer.add(ok);\r
132         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);\r
133 \r
134         focusPanel.setFocus(true);\r
135         setWidget(outer);\r
136         }\r
137 \r
138         void doCenter() {\r
139                 super.center();\r
140         }\r
141         \r
142         @Override\r
143         public void center() {\r
144                 fetchVersions();\r
145         }\r
146 \r
147     protected void fetchVersions() {\r
148         String path = file.getUri() + "?format=json&version=list";\r
149         GetRequest<FileVersions> getVersions = new GetRequest<FileVersions>(FileVersions.class, Pithos.getStorageAPIURL(), file.getOwnerID(), path) {\r
150 \r
151                         @Override\r
152                         public void onSuccess(FileVersions _result) {\r
153                         inner.add(createVersionPanel(_result.getVersions()));\r
154                                 doCenter();\r
155                         }\r
156 \r
157                         @Override\r
158                         public void onError(Throwable t) {\r
159                                 GWT.log("", t);\r
160                                 app.setError(t);\r
161                 if (t instanceof RestException) {\r
162                     app.displayError("Unable to fetch versions: " + ((RestException) t).getHttpStatusText());\r
163                 }\r
164                 else\r
165                     app.displayError("System error unable to fetch versions: "+t.getMessage());\r
166                         }\r
167 \r
168                         @Override\r
169                         protected void onUnauthorized(Response response) {\r
170                                 app.sessionExpired();\r
171                         }\r
172                 };\r
173                 getVersions.setHeader("X-Auth-Token", app.getUserToken());\r
174                 Scheduler.get().scheduleDeferred(getVersions);\r
175         }\r
176 \r
177     VerticalPanel createVersionPanel(List<Version> versions) {\r
178         VerticalPanel versionPanel = new VerticalPanel();\r
179         VersionsList verList = new VersionsList(app, this, images, file, versions);\r
180         versionPanel.add(verList);\r
181         return versionPanel;\r
182     }\r
183 \r
184         /**\r
185          * Accepts any change and updates the file\r
186          * @return \r
187          *\r
188          */\r
189         @Override\r
190         protected boolean accept() {\r
191         app.updateFolder(file.getParent(), true, new Command() {\r
192                         \r
193                         @Override\r
194                         public void execute() {\r
195                                 if (file.isSharedOrPublished())\r
196                                         app.updateMySharedRoot();\r
197                         }\r
198                 }, true);\r
199         \r
200         return true;\r
201         }\r
202 }\r