1 11 package org.eclipse.ui.internal.misc; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URL ; 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 38 public ExternalEditor(IPath newFilePath, EditorDescriptor editorDescriptor) { 39 this.filePath = newFilePath; 40 this.descriptor = editorDescriptor; 41 } 42 43 48 public void open() throws CoreException { 49 50 Program program = this.descriptor.getProgram(); 51 if (program == null) { 52 openWithUserDefinedProgram(); 53 } else { 54 String path = ""; 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 75 public void openWithUserDefinedProgram() throws CoreException { 76 79 String programFileName = null; 80 IConfigurationElement configurationElement = descriptor 81 .getConfigurationElement(); 82 83 if (configurationElement != null) { 86 try { 87 Bundle bundle = Platform.getBundle(configurationElement 88 .getNamespace()); 89 URL entry = bundle.getEntry(descriptor.getFileName()); 91 if (entry != null) { 92 URL localName = Platform.asLocalURL(entry); 94 File file = new File (localName.getFile()); 95 if (file.exists()) { 97 programFileName = file.getAbsolutePath(); 98 } 99 } 100 } catch (IOException e) { 101 } 103 } 104 105 if (programFileName == null) { 106 programFileName = descriptor.getFileName(); 109 } 110 111 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 path = filePath.toOSString(); 122 123 125 128 try { 129 Runtime.getRuntime().exec(new String [] { programFileName, path }); 130 } catch (Exception 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 |