001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.misc;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.net.URL;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.core.runtime.Status;
022: import org.eclipse.osgi.util.NLS;
023: import org.eclipse.swt.program.Program;
024: import org.eclipse.ui.internal.WorkbenchMessages;
025: import org.eclipse.ui.internal.WorkbenchPlugin;
026: import org.eclipse.ui.internal.registry.EditorDescriptor;
027: import org.osgi.framework.Bundle;
028:
029: public class ExternalEditor {
030: private IPath filePath;
031:
032: private EditorDescriptor descriptor;
033:
034: /**
035: * Create an external editor.
036: */
037: public ExternalEditor(IPath newFilePath,
038: EditorDescriptor editorDescriptor) {
039: this .filePath = newFilePath;
040: this .descriptor = editorDescriptor;
041: }
042:
043: /**
044: * open the editor. If the descriptor has a program then use it - otherwise build its
045: * info from the descriptor.
046: * @exception Throws a CoreException if the external editor could not be opened.
047: */
048: public void open() throws CoreException {
049:
050: Program program = this .descriptor.getProgram();
051: if (program == null) {
052: openWithUserDefinedProgram();
053: } else {
054: String path = ""; //$NON-NLS-1$
055: if (filePath != null) {
056: path = filePath.toOSString();
057: if (program.execute(path)) {
058: return;
059: }
060: }
061: throw new CoreException(
062: new Status(
063: IStatus.ERROR,
064: WorkbenchPlugin.PI_WORKBENCH,
065: 0,
066: NLS
067: .bind(
068: WorkbenchMessages.ExternalEditor_errorMessage,
069: path), null));
070: }
071: }
072:
073: /**
074: * open the editor.
075: * @exception Throws a CoreException if the external editor could not be opened.
076: */
077: public void openWithUserDefinedProgram() throws CoreException {
078: // We need to determine if the command refers to a program in the plugin
079: // install directory. Otherwise we assume the program is on the path.
080:
081: String programFileName = null;
082: IConfigurationElement configurationElement = descriptor
083: .getConfigurationElement();
084:
085: // Check if we have a config element (if we don't it is an
086: // external editor created on the resource associations page).
087: if (configurationElement != null) {
088: try {
089: Bundle bundle = Platform.getBundle(configurationElement
090: .getNamespace());
091: // See if the program file is in the plugin directory
092: URL entry = bundle.getEntry(descriptor.getFileName());
093: if (entry != null) {
094: // this will bring the file local if the plugin is on a server
095: URL localName = Platform.asLocalURL(entry);
096: File file = new File(localName.getFile());
097: //Check that it exists before we assert it is valid
098: if (file.exists()) {
099: programFileName = file.getAbsolutePath();
100: }
101: }
102: } catch (IOException e) {
103: // Program file is not in the plugin directory
104: }
105: }
106:
107: if (programFileName == null) {
108: // Program file is not in the plugin directory therefore
109: // assume it is on the path
110: programFileName = descriptor.getFileName();
111: }
112:
113: // Get the full path of the file to open
114: if (filePath == null) {
115: throw new CoreException(
116: new Status(
117: IStatus.ERROR,
118: WorkbenchPlugin.PI_WORKBENCH,
119: 0,
120: NLS
121: .bind(
122: WorkbenchMessages.ExternalEditor_errorMessage,
123: programFileName), null));
124: }
125: String path = filePath.toOSString();
126:
127: // Open the file
128:
129: // ShellCommand was removed in response to PR 23888. If an exception was
130: // thrown, it was not caught in time, and no feedback was given to user
131:
132: try {
133: Runtime.getRuntime().exec(
134: new String[] { programFileName, path });
135: } catch (Exception e) {
136: throw new CoreException(
137: new Status(
138: IStatus.ERROR,
139: WorkbenchPlugin.PI_WORKBENCH,
140: 0,
141: NLS
142: .bind(
143: WorkbenchMessages.ExternalEditor_errorMessage,
144: programFileName), e));
145: }
146: }
147: }
|