001: /*******************************************************************************
002: * Copyright (c) 2004, 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;
011:
012: import java.lang.reflect.Constructor;
013: import java.lang.reflect.InvocationTargetException;
014: import java.lang.reflect.Method;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IMarker;
018: import org.eclipse.core.runtime.IPluginDescriptor;
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.ui.IEditorInput;
023: import org.eclipse.ui.IEditorPart;
024: import org.eclipse.ui.IEditorRegistry;
025: import org.eclipse.ui.IWorkbenchPage;
026: import org.eclipse.ui.PartInitException;
027:
028: /**
029: * Internal class used in providing increased binary compatibility for pre-3.0
030: * plug-ins. This declaration masks the empty class of the same name declared in
031: * the Workbench proper. This class implements IWorkbenchPage that existed in
032: * 2.1 but were removed in 3.0 because they referenced resource API.
033: * <p>
034: * Plug-ins should not refer to this type or its containing fragment from their
035: * class path. It is intended only to provide binary compatibility for pre-3.0
036: * plug-ins, and should not be referenced at development time.
037: * </p>
038: *
039: * @since 3.0
040: */
041: public class CompatibleWorkbenchPage implements
042: ICompatibleWorkbenchPage {
043:
044: /**
045: * openEditor(IFile) is declared on IWorkbenchPage in 2.1. This method was
046: * removed in 3.0 because it references resource API.
047: */
048: public IEditorPart openEditor(IFile input) throws PartInitException {
049: // invoke org.eclipse.ui.ide.IDE.openEditor(IWorkbenchPage, IFile,
050: // boolean);
051: return openEditor(new Class[] { IWorkbenchPage.class,
052: IFile.class, boolean.class }, new Object[] { this ,
053: input, new Boolean(true) });
054: }
055:
056: /**
057: * openEditor(IFile,String) is declared on IWorkbenchPage in 2.1. This
058: * method was removed in 3.0 because it references resource API.
059: */
060: public IEditorPart openEditor(IFile input, String editorID)
061: throws PartInitException {
062: return openEditor(input, editorID, true);
063: }
064:
065: /**
066: * openEditor(IFile,String,boolean) is declared on IWorkbenchPage in 2.1.
067: * This method was removed in 3.0 because it references resource API.
068: */
069: public IEditorPart openEditor(IFile input, String editorID,
070: boolean activate) throws PartInitException {
071: return ((IWorkbenchPage) this ).openEditor(
072: getFileEditorInput(input), editorID);
073: }
074:
075: /**
076: * openEditor(IMarker) is declared on IWorkbenchPage in 2.1. This method was
077: * removed in 3.0 because it references resource API.
078: */
079: public IEditorPart openEditor(IMarker marker)
080: throws PartInitException {
081: return openEditor(marker, true);
082: }
083:
084: /**
085: * openEditor(IMarker,boolean) is declared on IWorkbenchPage in 2.1. This
086: * method was removed in 3.0 because it references resource API.
087: */
088: public IEditorPart openEditor(IMarker marker, boolean activate)
089: throws PartInitException {
090: // invoke org.eclipse.ui.ide.IDE.openEditor(IWorkbenchPage, IMarker,
091: // boolean);
092: return openEditor(new Class[] { IWorkbenchPage.class,
093: IMarker.class, boolean.class }, new Object[] { this ,
094: marker, new Boolean(activate) });
095: }
096:
097: /**
098: * openSystemEditor(IFile) is declared on IWorkbenchPage in 2.1. This method
099: * was removed in 3.0 because it references resource API.
100: */
101: public void openSystemEditor(IFile file) throws PartInitException {
102: ((IWorkbenchPage) this ).openEditor(getFileEditorInput(file),
103: IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
104:
105: }
106:
107: /*
108: * Implementation support: Use reflection for the following code pattern:
109: * new org.eclipse.ui.part.FileEditorInput(file) The class FileEditorInput
110: * is found in the org.eclipse.ui.ide plug-in.
111: */
112: private IEditorInput getFileEditorInput(IFile file)
113: throws PartInitException {
114: IPluginDescriptor desc = Platform.getPluginRegistry()
115: .getPluginDescriptor("org.eclipse.ui.ide"); //$NON-NLS-1$
116: Exception problem;
117: try {
118: Class clazz = desc.getPluginClassLoader().loadClass(
119: "org.eclipse.ui.part.FileEditorInput"); //$NON-NLS-1$
120: Constructor constructor = clazz
121: .getConstructor(new Class[] { IFile.class });
122: return (IEditorInput) constructor
123: .newInstance(new Object[] { file });
124: } catch (NullPointerException e) {
125: problem = e;
126: } catch (ClassNotFoundException e) {
127: problem = e;
128: } catch (NoSuchMethodException e) {
129: problem = e;
130: } catch (IllegalArgumentException e) {
131: problem = e;
132: } catch (IllegalAccessException e) {
133: problem = e;
134: } catch (InvocationTargetException e) {
135: problem = e;
136: } catch (InstantiationException e) {
137: problem = e;
138: }
139: IStatus status = new Status(
140: IStatus.ERROR,
141: WorkbenchPlugin.PI_WORKBENCH,
142: 0,
143: "openEditor() compatibility support failed - new FileEditorInput(file)", problem); //$NON-NLS-1$
144: WorkbenchPlugin.log(status.getMessage(), status);
145: throw new PartInitException(status);
146: }
147:
148: /*
149: * Implementation support: Use reflection to invoke the appropriate static
150: * openEditor(...) method on IDE The IDE class is found in the
151: * org.eclipse.ui.ide plug-in.
152: */
153: private IEditorPart openEditor(Class[] argTypes, Object[] args)
154: throws PartInitException {
155: IPluginDescriptor desc = Platform.getPluginRegistry()
156: .getPluginDescriptor("org.eclipse.ui.ide"); //$NON-NLS-1$
157: Throwable problem;
158: try {
159: Class clazz = desc.getPluginClassLoader().loadClass(
160: "org.eclipse.ui.ide.IDE"); //$NON-NLS-1$
161: Method method = clazz.getMethod("openEditor", argTypes); //$NON-NLS-1$
162: return (IEditorPart) method.invoke(null, args);
163: } catch (NullPointerException e) {
164: problem = e;
165: } catch (ClassNotFoundException e) {
166: problem = e;
167: } catch (NoSuchMethodException e) {
168: problem = e;
169: } catch (IllegalArgumentException e) {
170: problem = e;
171: } catch (IllegalAccessException e) {
172: problem = e;
173: } catch (InvocationTargetException e) {
174: problem = e;
175: }
176: IStatus status = new Status(
177: IStatus.ERROR,
178: WorkbenchPlugin.PI_WORKBENCH,
179: 0,
180: "openEditor() compatibility support failed - IDE.openEditor()", problem); //$NON-NLS-1$
181: WorkbenchPlugin.log(status.getMessage(), status);
182: throw new PartInitException(status);
183: }
184: }
|