View Javadoc
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.argeo.eclipse.ui.EclipseUiUtils;
21  import org.argeo.eclipse.ui.util.SingleSourcingConstants;
22  import org.eclipse.core.commands.AbstractHandler;
23  import org.eclipse.core.commands.ExecutionEvent;
24  import org.eclipse.rap.rwt.RWT;
25  import org.eclipse.rap.rwt.client.service.UrlLauncher;
26  
27  /**
28   * RWT specific object to open a file retrieved from the server. It forwards the
29   * request to the correct service after encoding file name and path in the
30   * request URI.
31   * 
32   * <p>
33   * The parameter "URI" is used to determine the correct file service, the path
34   * and the file name. An optional file name can be added to present the end user
35   * with a different file name as the one used to retrieve it.
36   * </p>
37   * 
38   * 
39   * <p>
40   * The instance specific service is called by its ID and must have been
41   * externally created
42   * </p>
43   */
44  public class OpenFile extends AbstractHandler {
45  	private final static Log log = LogFactory.getLog(OpenFile.class);
46  
47  	public final static String ID = SingleSourcingConstants.OPEN_FILE_CMD_ID;
48  	public final static String PARAM_FILE_NAME = SingleSourcingConstants.PARAM_FILE_NAME;
49  	public final static String PARAM_FILE_URI = SingleSourcingConstants.PARAM_FILE_URI;;
50  	
51  	/* DEPENDENCY INJECTION */
52  	private String openFileServiceId;
53  
54  	public Object execute(ExecutionEvent event) {
55  		String fileName = event.getParameter(PARAM_FILE_NAME);
56  		String fileUri = event.getParameter(PARAM_FILE_URI);
57  		// Sanity check
58  		if (fileUri == null || "".equals(fileUri.trim()) || openFileServiceId == null
59  				|| "".equals(openFileServiceId.trim()))
60  			return null;
61  
62  		org.argeo.eclipse.ui.specific.OpenFile openFileClient = new org.argeo.eclipse.ui.specific.OpenFile();
63  		openFileClient.execute(openFileServiceId, fileUri, fileName);
64  		return null;
65  	}
66  
67  	public Object execute(String openFileServiceId, String fileUri, String fileName) {
68  		StringBuilder url = new StringBuilder();
69  		url.append(RWT.getServiceManager().getServiceHandlerUrl(openFileServiceId));
70  
71  		if (EclipseUiUtils.notEmpty(fileName))
72  			url.append("&").append(SingleSourcingConstants.PARAM_FILE_NAME).append("=").append(fileName);
73  		url.append("&").append(SingleSourcingConstants.PARAM_FILE_URI).append("=").append(fileUri);
74  
75  		String downloadUrl = url.toString();
76  		if (log.isTraceEnabled())
77  			log.trace("Calling OpenFileService with ID: " + openFileServiceId + " , with download 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  }