preparing new release
[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.actions.ImportResourcesAction;
19 import org.eclipse.ui.part.ViewPart;
20 import org.eclipse.ui.part.WorkbenchPart;
21
22 /**
23  * This class provides backward compatibility between versions of Eclipse for
24  * known differences.
25  * 
26  * @author BC
27  */
28 public class VersioningHelper {
29     
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;
34
35     /**
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.  
39      * 
40      * @param fontDialog
41      * @param fontData
42      */    
43     public static void setFont(FontDialog fontDialog, FontData[] fontData) {
44         try {
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] });
53             }
54         } catch (NoSuchMethodException e) {
55             // should not happen
56         } catch (IllegalArgumentException e) {
57             // should not happen
58         } catch (IllegalAccessException e) {
59             // should not happen
60         } catch (InvocationTargetException e) {
61             // should not happen
62         }
63     }
64     
65     public static void setPartName(ViewPart viewPart, String partName) {
66         try {
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});
71             } else {
72                 Method method = WorkbenchPart.class.getDeclaredMethod(
73                     "setTitle", new Class[] { FontData.class });
74                 method.invoke(method, new Object[] { partName });
75             }
76         } catch (NoSuchMethodException e) {
77             // should not happen
78         } catch (IllegalArgumentException e) {
79             // should not happen
80         } catch (IllegalAccessException e) {
81             // should not happen
82         } catch (InvocationTargetException e) {
83             // should not happen
84         }
85     }
86     
87     public static ExportResourcesAction createExportResourcesAction(IWorkbenchWindow window) {
88         ExportResourcesAction action = null;
89         
90         try {
91                 if (isEclipse21OrHigher()) {
92                         Constructor constructor = ExportResourcesAction.class.getConstructor(
93                                 new Class[] { IWorkbenchWindow.class });
94                         action = (ExportResourcesAction) constructor.newInstance(
95                                 new Object[] { window });
96                 } else {
97                         Constructor constructor = ExportResourcesAction.class.getConstructor(
98                                 new Class[] { IWorkbench.class });
99                         action = (ExportResourcesAction) constructor.newInstance(
100                                 new Object[] { window.getWorkbench() });
101                 }
102         } catch (NoSuchMethodException e) {
103             // should not happen
104         } catch (IllegalArgumentException e) {
105             // should not happen
106         } catch (IllegalAccessException e) {
107             // should not happen
108         } catch (InvocationTargetException e) {
109             // should not happen
110         } catch (InstantiationException e) {
111             // should not happen
112         }
113         return action;
114     }
115     
116     public static ImportResourcesAction createImportResourcesAction(IWorkbenchWindow window) {
117         ImportResourcesAction action = null;
118         
119         try {
120                 if (isEclipse21OrHigher()) {
121                         Constructor constructor = ImportResourcesAction.class.getConstructor(
122                                 new Class[] { IWorkbenchWindow.class });
123                         action = (ImportResourcesAction) constructor.newInstance(
124                                 new Object[] { window });
125                 } else {
126                         Constructor constructor = ImportResourcesAction.class.getConstructor(
127                                 new Class[] { IWorkbench.class });
128                         action = (ImportResourcesAction) constructor.newInstance(
129                                 new Object[] { window.getWorkbench() });
130                 }
131         } catch (NoSuchMethodException e) {
132             // should not happen
133         } catch (IllegalArgumentException e) {
134             // should not happen
135         } catch (IllegalAccessException e) {
136             // should not happen
137         } catch (InvocationTargetException e) {
138             // should not happen
139         } catch (InstantiationException e) {
140             // should not happen
141         }
142         return action;
143     }
144
145     public static void registerActionToKeyBindingService(
146         IWorkbenchPartSite site, String[] scopes, IAction action) {
147                 
148         try {
149                 if (isEclipse21OrHigher()) {
150                 Method method = IWorkbenchPartSite.class.getMethod(
151                     "getKeyBindingService", new Class[0]);
152                 IKeyBindingService service = (IKeyBindingService) method.invoke(site, null);
153                 
154                 method = IKeyBindingService.class.getMethod(
155                         "setScopes", new Class[] { String[].class });
156                 method.invoke(service, new Object[] { scopes});
157                 
158                         service.registerAction(action);
159                 }
160         } catch (NoSuchMethodException e) {
161             // should not happen
162         } catch (IllegalArgumentException e) {
163             // should not happen
164         } catch (IllegalAccessException e) {
165             // should not happen
166         } catch (InvocationTargetException e) {
167             // should not happen
168         }
169     }
170     
171     public static void main(String[] args) {
172         System.out.println(SWT.getVersion());
173     }
174
175         /**
176          * @return
177          */
178         public static boolean isEclipse30() {
179                 return SWT.getVersion() >= 3000;
180         }
181
182         /**
183          * @return
184          */
185         public static boolean isEclipse21OrHigher() {
186                 return SWT.getVersion() >= 2100;
187         }
188         /**
189          * Method getDescriptor.
190          * @param registry
191          * @param imageName
192          * @return ImageDescriptor
193          */
194         public static ImageDescriptor getDescriptor(
195                 ImageRegistry registry,
196                 String imageName) {
197                 ImageDescriptor descriptor = null;
198         try {
199             if (isEclipse21OrHigher()) {
200                 Method method = ImageRegistry.class.getMethod(
201                     "getDescriptor", new Class[] { String.class });
202                 descriptor = (ImageDescriptor) method.invoke(registry, new Object[] {imageName});
203             }
204         } catch (NoSuchMethodException e) {
205             // should not happen
206         } catch (IllegalArgumentException e) {
207             // should not happen
208         } catch (IllegalAccessException e) {
209             // should not happen
210         } catch (InvocationTargetException e) {
211             // should not happen
212         }
213         return descriptor;
214         }
215
216 }