]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/OpenFile.java
new proposal to address the generic "open file" issue using a cleaner strategy.
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.rcp / src / main / java / org / argeo / eclipse / ui / specific / 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 java.awt.Desktop;
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.argeo.ArgeoException;
23 import org.eclipse.core.commands.AbstractHandler;
24 import org.eclipse.core.commands.ExecutionEvent;
25 import org.eclipse.core.commands.ExecutionException;
26
27 /**
28 * RCP specific command handler to open a file.
29 *
30 * The parameter "URI" is used to determine the correct method to open it.
31 *
32 * Various instances of this handler with different command ID might coexist in
33 * order to provide context specific open file service.
34 *
35 */
36 public class OpenFile extends AbstractHandler {
37 // private final static Log log = LogFactory.getLog(OpenFile.class);
38
39 public final static String PARAM_FILE_NAME = "param.fileName";
40 public final static String PARAM_FILE_URI = "param.fileURI";
41
42 private final static String FILE_SCHEME = "file";
43
44 public Object execute(ExecutionEvent event) throws ExecutionException {
45 @SuppressWarnings("unused")
46 @Deprecated
47 String fileName = event.getParameter(PARAM_FILE_NAME);
48 String fileUri = event.getParameter(PARAM_FILE_URI);
49
50 // sanity check
51 if (fileUri == null || "".equals(fileUri.trim()))
52 return null;
53
54 Desktop desktop = null;
55 if (Desktop.isDesktopSupported()) {
56 desktop = Desktop.getDesktop();
57 }
58
59 File file = getFileFromUri(fileUri);
60 if (file != null)
61 try {
62 desktop.open(file);
63 } catch (IOException e) {
64 throw new ArgeoException("Unable to open file with URI: "
65 + fileUri, e);
66 }
67
68 return null;
69 }
70
71 protected File getFileFromUri(String uri) {
72 if (uri.startsWith(FILE_SCHEME)) {
73 String path = uri.substring((FILE_SCHEME + "://").length());
74 return new File(path);
75 }
76 return null;
77 }
78 }