KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > ExternalEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.misc;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.program.Program;
25 import org.eclipse.ui.internal.WorkbenchMessages;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27 import org.eclipse.ui.internal.registry.EditorDescriptor;
28 import org.osgi.framework.Bundle;
29
30 public class ExternalEditor {
31     private IPath filePath;
32
33     private EditorDescriptor descriptor;
34
35     /**
36      * Create an external editor.
37      */

38     public ExternalEditor(IPath newFilePath, EditorDescriptor editorDescriptor) {
39         this.filePath = newFilePath;
40         this.descriptor = editorDescriptor;
41     }
42
43     /**
44      * open the editor. If the descriptor has a program then use it - otherwise build its
45      * info from the descriptor.
46      * @exception Throws a CoreException if the external editor could not be opened.
47      */

48     public void open() throws CoreException {
49
50         Program program = this.descriptor.getProgram();
51         if (program == null) {
52             openWithUserDefinedProgram();
53         } else {
54             String JavaDoc path = ""; //$NON-NLS-1$
55
if (filePath != null) {
56                 path = filePath.toOSString();
57                 if (program.execute(path)) {
58                     return;
59                 }
60             }
61             throw new CoreException(
62                     new Status(
63                             IStatus.ERROR,
64                             WorkbenchPlugin.PI_WORKBENCH,
65                             0,
66                             NLS.bind(WorkbenchMessages.ExternalEditor_errorMessage, path),
67                             null));
68         }
69     }
70
71     /**
72      * open the editor.
73      * @exception Throws a CoreException if the external editor could not be opened.
74      */

75     public void openWithUserDefinedProgram() throws CoreException {
76         // We need to determine if the command refers to a program in the plugin
77
// install directory. Otherwise we assume the program is on the path.
78

79         String JavaDoc programFileName = null;
80         IConfigurationElement configurationElement = descriptor
81                 .getConfigurationElement();
82
83         // Check if we have a config element (if we don't it is an
84
// external editor created on the resource associations page).
85
if (configurationElement != null) {
86             try {
87                 Bundle bundle = Platform.getBundle(configurationElement
88                         .getNamespace());
89                 // See if the program file is in the plugin directory
90
URL JavaDoc entry = bundle.getEntry(descriptor.getFileName());
91                 if (entry != null) {
92                     // this will bring the file local if the plugin is on a server
93
URL JavaDoc localName = Platform.asLocalURL(entry);
94                     File JavaDoc file = new File JavaDoc(localName.getFile());
95                     //Check that it exists before we assert it is valid
96
if (file.exists()) {
97                         programFileName = file.getAbsolutePath();
98                     }
99                 }
100             } catch (IOException JavaDoc e) {
101                 // Program file is not in the plugin directory
102
}
103         }
104
105         if (programFileName == null) {
106             // Program file is not in the plugin directory therefore
107
// assume it is on the path
108
programFileName = descriptor.getFileName();
109         }
110
111         // Get the full path of the file to open
112
if (filePath == null) {
113             throw new CoreException(
114                     new Status(
115                             IStatus.ERROR,
116                             WorkbenchPlugin.PI_WORKBENCH,
117                             0,
118                             NLS.bind(WorkbenchMessages.ExternalEditor_errorMessage,programFileName),
119                             null));
120         }
121         String JavaDoc path = filePath.toOSString();
122
123         // Open the file
124

125         // ShellCommand was removed in response to PR 23888. If an exception was
126
// thrown, it was not caught in time, and no feedback was given to user
127

128         try {
129             Runtime.getRuntime().exec(new String JavaDoc[] { programFileName, path });
130         } catch (Exception JavaDoc e) {
131             throw new CoreException(
132                     new Status(
133                             IStatus.ERROR,
134                             WorkbenchPlugin.PI_WORKBENCH,
135                             0,
136                             NLS.bind(WorkbenchMessages.ExternalEditor_errorMessage,programFileName),
137                             e));
138         }
139     }
140 }
141
Popular Tags