KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > CompatibleWorkbenchPage


1 /*******************************************************************************
2  * Copyright (c) 2004, 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;
12
13 import java.lang.reflect.Constructor JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.runtime.IPluginDescriptor;
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.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IEditorRegistry;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.PartInitException;
28
29 /**
30  * Internal class used in providing increased binary compatibility for pre-3.0
31  * plug-ins. This declaration masks the empty class of the same name declared in
32  * the Workbench proper. This class implements IWorkbenchPage that existed in
33  * 2.1 but were removed in 3.0 because they referenced resource API.
34  * <p>
35  * Plug-ins should not refer to this type or its containing fragment from their
36  * class path. It is intended only to provide binary compatibility for pre-3.0
37  * plug-ins, and should not be referenced at development time.
38  * </p>
39  *
40  * @since 3.0
41  */

42 public class CompatibleWorkbenchPage implements ICompatibleWorkbenchPage {
43
44     /**
45      * openEditor(IFile) is declared on IWorkbenchPage in 2.1. This method was
46      * removed in 3.0 because it references resource API.
47      */

48     public IEditorPart openEditor(IFile input) throws PartInitException {
49         // invoke org.eclipse.ui.ide.IDE.openEditor(IWorkbenchPage, IFile,
50
// boolean);
51
return openEditor(new Class JavaDoc[] { IWorkbenchPage.class, IFile.class,
52                 boolean.class },
53                 new Object JavaDoc[] { this, input, new Boolean JavaDoc(true) });
54     }
55
56     /**
57      * openEditor(IFile,String) is declared on IWorkbenchPage in 2.1. This
58      * method was removed in 3.0 because it references resource API.
59      */

60     public IEditorPart openEditor(IFile input, String JavaDoc editorID)
61             throws PartInitException {
62         return openEditor(input, editorID, true);
63     }
64
65     /**
66      * openEditor(IFile,String,boolean) is declared on IWorkbenchPage in 2.1.
67      * This method was removed in 3.0 because it references resource API.
68      */

69     public IEditorPart openEditor(IFile input, String JavaDoc editorID, boolean activate)
70             throws PartInitException {
71         return ((IWorkbenchPage) this).openEditor(getFileEditorInput(input),
72                 editorID);
73     }
74
75     /**
76      * openEditor(IMarker) is declared on IWorkbenchPage in 2.1. This method was
77      * removed in 3.0 because it references resource API.
78      */

79     public IEditorPart openEditor(IMarker marker) throws PartInitException {
80         return openEditor(marker, true);
81     }
82
83     /**
84      * openEditor(IMarker,boolean) is declared on IWorkbenchPage in 2.1. This
85      * method was removed in 3.0 because it references resource API.
86      */

87     public IEditorPart openEditor(IMarker marker, boolean activate)
88             throws PartInitException {
89         // invoke org.eclipse.ui.ide.IDE.openEditor(IWorkbenchPage, IMarker,
90
// boolean);
91
return openEditor(new Class JavaDoc[] { IWorkbenchPage.class, IMarker.class,
92                 boolean.class }, new Object JavaDoc[] { this, marker,
93                 new Boolean JavaDoc(activate) });
94     }
95
96     /**
97      * openSystemEditor(IFile) is declared on IWorkbenchPage in 2.1. This method
98      * was removed in 3.0 because it references resource API.
99      */

100     public void openSystemEditor(IFile file) throws PartInitException {
101         ((IWorkbenchPage) this).openEditor(getFileEditorInput(file),
102                 IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
103
104     }
105
106     /*
107      * Implementation support: Use reflection for the following code pattern:
108      * new org.eclipse.ui.part.FileEditorInput(file) The class FileEditorInput
109      * is found in the org.eclipse.ui.ide plug-in.
110      */

111     private IEditorInput getFileEditorInput(IFile file)
112             throws PartInitException {
113         IPluginDescriptor desc = Platform.getPluginRegistry()
114                 .getPluginDescriptor("org.eclipse.ui.ide"); //$NON-NLS-1$
115
Exception JavaDoc problem;
116         try {
117             Class JavaDoc clazz = desc.getPluginClassLoader().loadClass(
118                     "org.eclipse.ui.part.FileEditorInput"); //$NON-NLS-1$
119
Constructor JavaDoc constructor = clazz
120                     .getConstructor(new Class JavaDoc[] { IFile.class });
121             return (IEditorInput) constructor
122                     .newInstance(new Object JavaDoc[] { file });
123         } catch (NullPointerException JavaDoc e) {
124             problem = e;
125         } catch (ClassNotFoundException JavaDoc e) {
126             problem = e;
127         } catch (NoSuchMethodException JavaDoc e) {
128             problem = e;
129         } catch (IllegalArgumentException JavaDoc e) {
130             problem = e;
131         } catch (IllegalAccessException JavaDoc e) {
132             problem = e;
133         } catch (InvocationTargetException JavaDoc e) {
134             problem = e;
135         } catch (InstantiationException JavaDoc e) {
136             problem = e;
137         }
138         IStatus status = new Status(
139                 IStatus.ERROR,
140                 WorkbenchPlugin.PI_WORKBENCH,
141                 0,
142                 "openEditor() compatibility support failed - new FileEditorInput(file)", problem); //$NON-NLS-1$
143
WorkbenchPlugin.log(status.getMessage(), status);
144         throw new PartInitException(status);
145     }
146
147     /*
148      * Implementation support: Use reflection to invoke the appropriate static
149      * openEditor(...) method on IDE The IDE class is found in the
150      * org.eclipse.ui.ide plug-in.
151      */

152     private IEditorPart openEditor(Class JavaDoc[] argTypes, Object JavaDoc[] args)
153             throws PartInitException {
154         IPluginDescriptor desc = Platform.getPluginRegistry()
155                 .getPluginDescriptor("org.eclipse.ui.ide"); //$NON-NLS-1$
156
Throwable JavaDoc problem;
157         try {
158             Class JavaDoc clazz = desc.getPluginClassLoader().loadClass(
159                     "org.eclipse.ui.ide.IDE"); //$NON-NLS-1$
160
Method JavaDoc method = clazz.getMethod("openEditor", argTypes); //$NON-NLS-1$
161
return (IEditorPart) method.invoke(null, args);
162         } catch (NullPointerException JavaDoc e) {
163             problem = e;
164         } catch (ClassNotFoundException JavaDoc e) {
165             problem = e;
166         } catch (NoSuchMethodException JavaDoc e) {
167             problem = e;
168         } catch (IllegalArgumentException JavaDoc e) {
169             problem = e;
170         } catch (IllegalAccessException JavaDoc e) {
171             problem = e;
172         } catch (InvocationTargetException JavaDoc e) {
173             problem = e;
174         }
175         IStatus status = new Status(
176                 IStatus.ERROR,
177                 WorkbenchPlugin.PI_WORKBENCH,
178                 0,
179                 "openEditor() compatibility support failed - IDE.openEditor()", problem); //$NON-NLS-1$
180
WorkbenchPlugin.log(status.getMessage(), status);
181         throw new PartInitException(status);
182     }
183 }
184
Popular Tags