newest quantum CVS sources
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / versioning / VersioningHelper.java
1 package com.quantum.util.versioning;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6
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;
20
21 /**
22  * This class provides backward compatibility between versions of Eclipse for
23  * known differences.
24  * 
25  * @author BC
26  */
27 public class VersioningHelper {
28     
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;
33
34     /**
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.  
38      * 
39      * @param fontDialog
40      * @param fontData
41      */    
42     public static void setFont(FontDialog fontDialog, FontData[] fontData) {
43         try {
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] });
52             }
53         } catch (NoSuchMethodException e) {
54             // should not happen
55         } catch (IllegalArgumentException e) {
56             // should not happen
57         } catch (IllegalAccessException e) {
58             // should not happen
59         } catch (InvocationTargetException e) {
60             // should not happen
61         }
62     }
63     
64     public static void setPartName(ViewPart viewPart, String partName) {
65         try {
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});
70             } else {
71                 Method method = WorkbenchPart.class.getDeclaredMethod(
72                     "setTitle", new Class[] { FontData.class });
73                 method.invoke(method, new Object[] { partName });
74             }
75         } catch (NoSuchMethodException e) {
76             // should not happen
77         } catch (IllegalArgumentException e) {
78             // should not happen
79         } catch (IllegalAccessException e) {
80             // should not happen
81         } catch (InvocationTargetException e) {
82             // should not happen
83         }
84     }
85     
86     public static ExportResourcesAction createExportResourcesAction(IWorkbenchWindow window) {
87         ExportResourcesAction action = null;
88         
89         try {
90                 if (isEclipse21OrHigher()) {
91                         Constructor constructor = ExportResourcesAction.class.getConstructor(
92                                 new Class[] { IWorkbenchWindow.class });
93                         action = (ExportResourcesAction) constructor.newInstance(
94                                 new Object[] { window });
95                 } else {
96                         Constructor constructor = ExportResourcesAction.class.getConstructor(
97                                 new Class[] { IWorkbench.class });
98                         action = (ExportResourcesAction) constructor.newInstance(
99                                 new Object[] { window.getWorkbench() });
100                 }
101         } catch (NoSuchMethodException e) {
102             // should not happen
103         } catch (IllegalArgumentException e) {
104             // should not happen
105         } catch (IllegalAccessException e) {
106             // should not happen
107         } catch (InvocationTargetException e) {
108             // should not happen
109         } catch (InstantiationException e) {
110             // should not happen
111         }
112         return action;
113     }
114     
115     public static void registerActionToKeyBindingService(
116         IWorkbenchPartSite site, String[] scopes, IAction action) {
117                 
118         try {
119                 if (isEclipse21OrHigher()) {
120                 Method method = IWorkbenchPartSite.class.getMethod(
121                     "getKeyBindingService", new Class[0]);
122                 IKeyBindingService service = (IKeyBindingService) method.invoke(site, null);
123                 
124                 method = IKeyBindingService.class.getMethod(
125                         "setScopes", new Class[] { String[].class });
126                 method.invoke(service, new Object[] { scopes});
127                 
128                         service.registerAction(action);
129                 }
130         } catch (NoSuchMethodException e) {
131             // should not happen
132         } catch (IllegalArgumentException e) {
133             // should not happen
134         } catch (IllegalAccessException e) {
135             // should not happen
136         } catch (InvocationTargetException e) {
137             // should not happen
138         }
139     }
140     
141     public static void main(String[] args) {
142         System.out.println(SWT.getVersion());
143     }
144
145         /**
146          * @return
147          */
148         public static boolean isEclipse30() {
149                 return SWT.getVersion() >= 3000;
150         }
151
152         /**
153          * @return
154          */
155         public static boolean isEclipse21OrHigher() {
156                 return SWT.getVersion() >= 2100;
157         }
158         /**
159          * Method getDescriptor.
160          * @param registry
161          * @param imageName
162          * @return ImageDescriptor
163          */
164         public static ImageDescriptor getDescriptor(
165                 ImageRegistry registry,
166                 String imageName) {
167                 ImageDescriptor descriptor = null;
168         try {
169             if (isEclipse21OrHigher()) {
170                 Method method = ImageRegistry.class.getMethod(
171                     "getDescriptor", new Class[] { String.class });
172                 descriptor = (ImageDescriptor) method.invoke(registry, new Object[] {imageName});
173             }
174         } catch (NoSuchMethodException e) {
175             // should not happen
176         } catch (IllegalArgumentException e) {
177             // should not happen
178         } catch (IllegalAccessException e) {
179             // should not happen
180         } catch (InvocationTargetException e) {
181             // should not happen
182         }
183         return descriptor;
184         }
185
186 }