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.part.ViewPart;
 
  19 import org.eclipse.ui.part.WorkbenchPart;
 
  22  * This class provides backward compatibility between versions of Eclipse for
 
  27 public class VersioningHelper {
 
  29     public static final int ECLIPSE_VERSION_2_0_1 = 2049;
 
  30     public static final int ECLIPSE_VERSION_2_1_1 = 2135;
 
  31     public static final int ECLIPSE_VERSION_3_0_RC1 = 3054;
 
  32     public static final int ECLIPSE_VERSION_3_0_RC3 = 3061;
 
  35      * Set the font in a FontDialog.  In Eclipse 2.1.1, the 
 
  36      * <code>setFontData()</code> method was deprecated and an alternative
 
  37      * method, <code>setFontList()</code> was suggested in its place.  
 
  42     public static void setFont(FontDialog fontDialog, FontData[] fontData) {
 
  44             if (SWT.getVersion() >= ECLIPSE_VERSION_2_1_1) {
 
  45                 Method method = fontDialog.getClass().getMethod(
 
  46                     "setFontList", new Class[] { fontData.getClass()});
 
  47                 method.invoke(fontDialog, new Object[] {fontData});
 
  48             } else if (fontData.length > 0) {
 
  49                 Method method = fontDialog.getClass().getMethod(
 
  50                     "setFontData", new Class[] { FontData.class });
 
  51                 method.invoke(fontDialog, new Object[] { fontData[0] });
 
  53         } catch (NoSuchMethodException e) {
 
  55         } catch (IllegalArgumentException e) {
 
  57         } catch (IllegalAccessException e) {
 
  59         } catch (InvocationTargetException e) {
 
  64     public static void setPartName(ViewPart viewPart, String partName) {
 
  66             if (SWT.getVersion() >= ECLIPSE_VERSION_3_0_RC1) {
 
  67                 Method method = WorkbenchPart.class.getDeclaredMethod(
 
  68                     "setPartName", new Class[] { String.class });
 
  69                 method.invoke(viewPart, new Object[] {partName});
 
  71                 Method method = WorkbenchPart.class.getDeclaredMethod(
 
  72                     "setTitle", new Class[] { FontData.class });
 
  73                 method.invoke(method, new Object[] { partName });
 
  75         } catch (NoSuchMethodException e) {
 
  77         } catch (IllegalArgumentException e) {
 
  79         } catch (IllegalAccessException e) {
 
  81         } catch (InvocationTargetException e) {
 
  86     public static ExportResourcesAction createExportResourcesAction(IWorkbenchWindow window) {
 
  87         ExportResourcesAction action = null;
 
  90                 if (isEclipse21OrHigher()) {
 
  91                         Constructor constructor = ExportResourcesAction.class.getConstructor(
 
  92                                 new Class[] { IWorkbenchWindow.class });
 
  93                         action = (ExportResourcesAction) constructor.newInstance(
 
  94                                 new Object[] { window });
 
  96                         Constructor constructor = ExportResourcesAction.class.getConstructor(
 
  97                                 new Class[] { IWorkbench.class });
 
  98                         action = (ExportResourcesAction) constructor.newInstance(
 
  99                                 new Object[] { window.getWorkbench() });
 
 101         } catch (NoSuchMethodException e) {
 
 103         } catch (IllegalArgumentException e) {
 
 105         } catch (IllegalAccessException e) {
 
 107         } catch (InvocationTargetException e) {
 
 109         } catch (InstantiationException e) {
 
 115     public static void registerActionToKeyBindingService(
 
 116         IWorkbenchPartSite site, String[] scopes, IAction action) {
 
 119                 if (isEclipse21OrHigher()) {
 
 120                 Method method = IWorkbenchPartSite.class.getMethod(
 
 121                     "getKeyBindingService", new Class[0]);
 
 122                 IKeyBindingService service = (IKeyBindingService) method.invoke(site, null);
 
 124                 method = IKeyBindingService.class.getMethod(
 
 125                         "setScopes", new Class[] { String[].class });
 
 126                 method.invoke(service, new Object[] { scopes});
 
 128                         service.registerAction(action);
 
 130         } catch (NoSuchMethodException e) {
 
 132         } catch (IllegalArgumentException e) {
 
 134         } catch (IllegalAccessException e) {
 
 136         } catch (InvocationTargetException e) {
 
 141     public static void main(String[] args) {
 
 142         System.out.println(SWT.getVersion());
 
 148         public static boolean isEclipse30() {
 
 149                 return SWT.getVersion() >= 3000;
 
 155         public static boolean isEclipse21OrHigher() {
 
 156                 return SWT.getVersion() >= 2100;
 
 159          * Method getDescriptor.
 
 162          * @return ImageDescriptor
 
 164         public static ImageDescriptor getDescriptor(
 
 165                 ImageRegistry registry,
 
 167                 ImageDescriptor descriptor = null;
 
 169             if (isEclipse21OrHigher()) {
 
 170                 Method method = ImageRegistry.class.getMethod(
 
 171                     "getDescriptor", new Class[] { String.class });
 
 172                 descriptor = (ImageDescriptor) method.invoke(registry, new Object[] {imageName});
 
 174         } catch (NoSuchMethodException e) {
 
 176         } catch (IllegalArgumentException e) {
 
 178         } catch (IllegalAccessException e) {
 
 180         } catch (InvocationTargetException e) {