2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.commands;
21 import gr.ebs.gss.client.FileMenu;
22 import gr.ebs.gss.client.FilePropertiesDialog;
23 import gr.ebs.gss.client.FolderPropertiesDialog;
24 import gr.ebs.gss.client.GSS;
25 import gr.ebs.gss.client.FileMenu.Images;
26 import gr.ebs.gss.client.rest.GetCommand;
27 import gr.ebs.gss.client.rest.HeadCommand;
28 import gr.ebs.gss.client.rest.MultipleGetCommand;
29 import gr.ebs.gss.client.rest.MultipleHeadCommand;
30 import gr.ebs.gss.client.rest.RestException;
31 import gr.ebs.gss.client.rest.resource.FileResource;
32 import gr.ebs.gss.client.rest.resource.FolderResource;
33 import gr.ebs.gss.client.rest.resource.GroupResource;
34 import gr.ebs.gss.client.rest.resource.GroupsResource;
36 import java.util.ArrayList;
37 import java.util.List;
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.user.client.Command;
41 import com.google.gwt.user.client.DeferredCommand;
42 import com.google.gwt.user.client.IncrementalCommand;
43 import com.google.gwt.user.client.ui.PopupPanel;
46 * The command that displays the appropriate Properties dialog, according to the
47 * selected object in the application.
51 public class PropertiesCommand implements Command {
53 final FileMenu.Images newImages;
55 private PopupPanel containerPanel;
57 private List<GroupResource> groups = null;
59 private List<FileResource> versions = null;
61 private int tabToShow = 0;
64 * @param _containerPanel
65 * @param _newImages the images of all the possible delete dialogs
66 * @param _tab the tab to switch to
68 public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
69 containerPanel = _containerPanel;
70 newImages = _newImages;
74 public void execute() {
75 containerPanel.hide();
76 if (GSS.get().getCurrentSelection() instanceof FolderResource) {
77 GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((FolderResource) GSS.get().getCurrentSelection()).getUri()) {
80 public void onComplete() {
81 GSS.get().setCurrentSelection(getResult());
86 public void onError(Throwable t) {
91 DeferredCommand.addCommand(eg);
93 else if (GSS.get().getCurrentSelection() instanceof FileResource) {
94 final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
95 // Needed because firefox caches head requests.
96 HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random() ) {
99 public void onComplete() {
100 FileResource res = getResult();
102 GSS.get().setCurrentSelection(res);
107 public void onError(Throwable t) {
108 if(t instanceof RestException)
109 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
113 DeferredCommand.addCommand(eg);
117 private void initialize(){
120 DeferredCommand.addCommand(new IncrementalCommand() {
122 public boolean execute() {
123 boolean res = canContinue();
125 displayProperties(newImages);
135 private boolean canContinue() {
136 if (groups == null || versions == null)
142 * Display the appropriate Properties dialog, according to the selected
143 * object in the application.
145 * @param propImages the images of all the possible properties dialogs
147 void displayProperties(final Images propImages) {
148 if (GSS.get().getCurrentSelection() instanceof FolderResource) {
149 FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
150 dlg.selectTab(tabToShow);
152 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
153 FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions);
154 dlg.selectTab(tabToShow);
159 private void getGroups() {
160 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath()) {
163 public void onComplete() {
164 GroupsResource res = getResult();
165 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {})) {
168 public void onComplete() {
169 List<GroupResource> groupList = getResult();
174 public void onError(Throwable t) {
176 GSS.get().displayError("Unable to fetch groups");
177 groups = new ArrayList<GroupResource>();
181 public void onError(String p, Throwable throwable) {
182 GWT.log("Path:" + p, throwable);
185 DeferredCommand.addCommand(ga);
189 public void onError(Throwable t) {
191 GSS.get().displayError("Unable to fetch groups");
192 groups = new ArrayList<GroupResource>();
195 DeferredCommand.addCommand(gg);
198 private void getVersions() {
199 if (GSS.get().getCurrentSelection() instanceof FileResource) {
200 FileResource afile = (FileResource) GSS.get().getCurrentSelection();
201 GWT.log("File is versioned:" + afile.isVersioned(), null);
202 if (afile.isVersioned()) {
203 List<String> paths = new ArrayList<String>();
204 for (int i = 1; i <= afile.getVersion(); i++)
205 paths.add(afile.getUri() + "?version=" + i);
206 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {})) {
209 public void onComplete() {
210 versions = getResult();
214 public void onError(Throwable t) {
216 GSS.get().displayError("Unable to fetch versions");
217 versions = new ArrayList<FileResource>();
221 public void onError(String p, Throwable throwable) {
222 GWT.log("Path:" + p, throwable);
225 DeferredCommand.addCommand(gv);
227 versions = new ArrayList<FileResource>();
229 versions = new ArrayList<FileResource>();