]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OpenFile.java
409b96dedca031f97b5de91b4c54e8a654a1d24d
[lgpl/argeo-commons.git] / OpenFile.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.specific;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.eclipse.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.rap.rwt.RWT;
24 import org.eclipse.rap.rwt.client.service.UrlLauncher;
25
26 /**
27 * RWT specific command handler to open a file retrieved from the server. It
28 * forwards the request to the correct service after encoding file name and path
29 * in the request URI.
30 *
31 * <p>
32 * The parameter "URI" is used to determine the correct file service, the path
33 * and the file name. An optional file name can be precized to present a
34 * different file name as the one used to retrieve it to the end user.
35 * </p>
36 *
37 * <p>
38 * Various instances of this handler with different command ID might coexist in
39 * order to provide context specific download service.
40 * </p>
41 *
42 * <p>
43 * The instance specific service is called by its ID and must have been
44 * externally created
45 * </p>
46 */
47 public class OpenFile extends AbstractHandler {
48 private final static Log log = LogFactory.getLog(OpenFile.class);
49
50 /* DEPENDENCY INJECTION */
51 private String openFileServiceId;
52
53 public final static String PARAM_FILE_NAME = OpenFileService.PARAM_FILE_NAME;
54 public final static String PARAM_FILE_URI = OpenFileService.PARAM_FILE_URI; // "param.fileURI";
55
56 public Object execute(ExecutionEvent event) throws ExecutionException {
57 String fileName = event.getParameter(PARAM_FILE_NAME);
58 String fileUri = event.getParameter(PARAM_FILE_URI);
59
60 // Sanity check
61 if (fileUri == null || "".equals(fileUri.trim())
62 || openFileServiceId == null
63 || "".equals(openFileServiceId.trim()))
64 return null;
65
66 StringBuilder url = new StringBuilder();
67 url.append(RWT.getServiceManager().getServiceHandlerUrl(
68 openFileServiceId));
69
70 url.append("&").append(PARAM_FILE_NAME).append("=");
71 url.append(fileName);
72 url.append("&").append(PARAM_FILE_URI).append("=");
73 url.append(fileUri);
74
75 String downloadUrl = url.toString();
76 if (log.isTraceEnabled())
77 log.debug("URL : " + downloadUrl);
78
79 UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
80 launcher.openURL(downloadUrl);
81 return null;
82 }
83
84 /* DEPENDENCY INJECTION */
85 public void setOpenFileServiceId(String openFileServiceId) {
86 this.openFileServiceId = openFileServiceId;
87 }
88 }