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