]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFile.java
Add dependency to new project CMS UI RAP.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.rap / src / org / argeo / eclipse / ui / specific / OpenFile.java
1 package org.argeo.eclipse.ui.specific;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.eclipse.ui.EclipseUiUtils;
6 import org.argeo.eclipse.ui.util.SingleSourcingConstants;
7 import org.eclipse.core.commands.AbstractHandler;
8 import org.eclipse.core.commands.ExecutionEvent;
9 import org.eclipse.rap.rwt.RWT;
10 import org.eclipse.rap.rwt.client.service.UrlLauncher;
11
12 /**
13 * RWT specific object to open a file retrieved from the server. It forwards the
14 * request to the correct service after encoding file name and path in the
15 * request URI.
16 *
17 * <p>
18 * The parameter "URI" is used to determine the correct file service, the path
19 * and the file name. An optional file name can be added to present the end user
20 * with a different file name as the one used to retrieve it.
21 * </p>
22 *
23 *
24 * <p>
25 * The instance specific service is called by its ID and must have been
26 * externally created
27 * </p>
28 */
29 public class OpenFile extends AbstractHandler {
30 private final static Log log = LogFactory.getLog(OpenFile.class);
31
32 public final static String ID = SingleSourcingConstants.OPEN_FILE_CMD_ID;
33 public final static String PARAM_FILE_NAME = SingleSourcingConstants.PARAM_FILE_NAME;
34 public final static String PARAM_FILE_URI = SingleSourcingConstants.PARAM_FILE_URI;;
35
36 /* DEPENDENCY INJECTION */
37 private String openFileServiceId;
38
39 public Object execute(ExecutionEvent event) {
40 String fileName = event.getParameter(PARAM_FILE_NAME);
41 String fileUri = event.getParameter(PARAM_FILE_URI);
42 // Sanity check
43 if (fileUri == null || "".equals(fileUri.trim()) || openFileServiceId == null
44 || "".equals(openFileServiceId.trim()))
45 return null;
46
47 org.argeo.eclipse.ui.specific.OpenFile openFileClient = new org.argeo.eclipse.ui.specific.OpenFile();
48 openFileClient.execute(openFileServiceId, fileUri, fileName);
49 return null;
50 }
51
52 public Object execute(String openFileServiceId, String fileUri, String fileName) {
53 StringBuilder url = new StringBuilder();
54 url.append(RWT.getServiceManager().getServiceHandlerUrl(openFileServiceId));
55
56 if (EclipseUiUtils.notEmpty(fileName))
57 url.append("&").append(SingleSourcingConstants.PARAM_FILE_NAME).append("=").append(fileName);
58 url.append("&").append(SingleSourcingConstants.PARAM_FILE_URI).append("=").append(fileUri);
59
60 String downloadUrl = url.toString();
61 if (log.isTraceEnabled())
62 log.trace("Calling OpenFileService with ID: " + openFileServiceId + " , with download URL: " + downloadUrl);
63
64 UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
65 launcher.openURL(downloadUrl);
66 return null;
67 }
68
69 /* DEPENDENCY INJECTION */
70 public void setOpenFileServiceId(String openFileServiceId) {
71 this.openFileServiceId = openFileServiceId;
72 }
73 }