1 package com.quantum.util.versioning;
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
7 import org.eclipse.jface.action.IAction;
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.resource.ImageRegistry;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.graphics.FontData;
12 import org.eclipse.swt.widgets.FontDialog;
13 import org.eclipse.ui.IKeyBindingService;
14 import org.eclipse.ui.IWorkbench;
15 import org.eclipse.ui.IWorkbenchPartSite;
16 import org.eclipse.ui.IWorkbenchWindow;
17 import org.eclipse.ui.actions.ExportResourcesAction;
18 import org.eclipse.ui.actions.ImportResourcesAction;
19 import org.eclipse.ui.part.ViewPart;
20 import org.eclipse.ui.part.WorkbenchPart;
23 * This class provides backward compatibility between versions of Eclipse for
28 public class VersioningHelper {
30 public static final int ECLIPSE_VERSION_2_0_1 = 2049;
31 public static final int ECLIPSE_VERSION_2_1_1 = 2135;
32 public static final int ECLIPSE_VERSION_3_0_RC1 = 3054;
33 public static final int ECLIPSE_VERSION_3_0_RC3 = 3061;
36 * Set the font in a FontDialog. In Eclipse 2.1.1, the
37 * <code>setFontData()</code> method was deprecated and an alternative
38 * method, <code>setFontList()</code> was suggested in its place.
43 public static void setFont(FontDialog fontDialog, FontData[] fontData) {
45 if (SWT.getVersion() >= ECLIPSE_VERSION_2_1_1) {
46 Method method = fontDialog.getClass().getMethod(
47 "setFontList", new Class[] { fontData.getClass()});
48 method.invoke(fontDialog, new Object[] {fontData});
49 } else if (fontData.length > 0) {
50 Method method = fontDialog.getClass().getMethod(
51 "setFontData", new Class[] { FontData.class });
52 method.invoke(fontDialog, new Object[] { fontData[0] });
54 } catch (NoSuchMethodException e) {
56 } catch (IllegalArgumentException e) {
58 } catch (IllegalAccessException e) {
60 } catch (InvocationTargetException e) {
65 public static void setPartName(ViewPart viewPart, String partName) {
67 if (SWT.getVersion() >= ECLIPSE_VERSION_3_0_RC1) {
68 Method method = WorkbenchPart.class.getDeclaredMethod(
69 "setPartName", new Class[] { String.class });
70 method.invoke(viewPart, new Object[] {partName});
72 Method method = WorkbenchPart.class.getDeclaredMethod(
73 "setTitle", new Class[] { FontData.class });
74 method.invoke(method, new Object[] { partName });
76 } catch (NoSuchMethodException e) {
78 } catch (IllegalArgumentException e) {
80 } catch (IllegalAccessException e) {
82 } catch (InvocationTargetException e) {
87 public static ExportResourcesAction createExportResourcesAction(IWorkbenchWindow window) {
88 ExportResourcesAction action = null;
91 if (isEclipse21OrHigher()) {
92 Constructor constructor = ExportResourcesAction.class.getConstructor(
93 new Class[] { IWorkbenchWindow.class });
94 action = (ExportResourcesAction) constructor.newInstance(
95 new Object[] { window });
97 Constructor constructor = ExportResourcesAction.class.getConstructor(
98 new Class[] { IWorkbench.class });
99 action = (ExportResourcesAction) constructor.newInstance(
100 new Object[] { window.getWorkbench() });
102 } catch (NoSuchMethodException e) {
104 } catch (IllegalArgumentException e) {
106 } catch (IllegalAccessException e) {
108 } catch (InvocationTargetException e) {
110 } catch (InstantiationException e) {
116 public static ImportResourcesAction createImportResourcesAction(IWorkbenchWindow window) {
117 ImportResourcesAction action = null;
120 if (isEclipse21OrHigher()) {
121 Constructor constructor = ImportResourcesAction.class.getConstructor(
122 new Class[] { IWorkbenchWindow.class });
123 action = (ImportResourcesAction) constructor.newInstance(
124 new Object[] { window });
126 Constructor constructor = ImportResourcesAction.class.getConstructor(
127 new Class[] { IWorkbench.class });
128 action = (ImportResourcesAction) constructor.newInstance(
129 new Object[] { window.getWorkbench() });
131 } catch (NoSuchMethodException e) {
133 } catch (IllegalArgumentException e) {
135 } catch (IllegalAccessException e) {
137 } catch (InvocationTargetException e) {
139 } catch (InstantiationException e) {
145 public static void registerActionToKeyBindingService(
146 IWorkbenchPartSite site, String[] scopes, IAction action) {
149 if (isEclipse21OrHigher()) {
150 Method method = IWorkbenchPartSite.class.getMethod(
151 "getKeyBindingService", new Class[0]);
152 IKeyBindingService service = (IKeyBindingService) method.invoke(site, null);
154 method = IKeyBindingService.class.getMethod(
155 "setScopes", new Class[] { String[].class });
156 method.invoke(service, new Object[] { scopes});
158 service.registerAction(action);
160 } catch (NoSuchMethodException e) {
162 } catch (IllegalArgumentException e) {
164 } catch (IllegalAccessException e) {
166 } catch (InvocationTargetException e) {
171 public static void main(String[] args) {
172 System.out.println(SWT.getVersion());
178 public static boolean isEclipse30() {
179 return SWT.getVersion() >= 3000;
185 public static boolean isEclipse21OrHigher() {
186 return SWT.getVersion() >= 2100;
189 * Method getDescriptor.
192 * @return ImageDescriptor
194 public static ImageDescriptor getDescriptor(
195 ImageRegistry registry,
197 ImageDescriptor descriptor = null;
199 if (isEclipse21OrHigher()) {
200 Method method = ImageRegistry.class.getMethod(
201 "getDescriptor", new Class[] { String.class });
202 descriptor = (ImageDescriptor) method.invoke(registry, new Object[] {imageName});
204 } catch (NoSuchMethodException e) {
206 } catch (IllegalArgumentException e) {
208 } catch (IllegalAccessException e) {
210 } catch (InvocationTargetException e) {