KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editorsupport > ComponentSupport


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.editorsupport;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.internal.util.BundleUtility;
20 import org.osgi.framework.Bundle;
21
22 /**
23  * This class provides an OS independent interface to the
24  * components available on the platform
25  */

26 public final class ComponentSupport {
27
28     /**
29      * Returns whether the current platform has support
30      * for system in-place editor.
31      */

32     public static boolean inPlaceEditorSupported() {
33         // only Win32 is supported
34
return SWT.getPlatform().equals("win32"); //$NON-NLS-1$
35
}
36
37     /**
38      * Return the default system in-place editor part
39      * or <code>null</code> if not support by platform.
40      */

41     public static IEditorPart getSystemInPlaceEditor() {
42         if (inPlaceEditorSupported()) {
43             return getOleEditor();
44         }
45         return null;
46     }
47
48     /**
49      * Returns whether an in-place editor is available to
50      * edit the file.
51      *
52      * @param filename the file name in the system
53      */

54     public static boolean inPlaceEditorAvailable(String JavaDoc filename) {
55         if (inPlaceEditorSupported()) {
56             return testForOleEditor(filename);
57         } else {
58             return false;
59         }
60     }
61
62     /**
63      * Get a new OLEEditor
64      * @return IEditorPart
65      */

66     private static IEditorPart getOleEditor() {
67         // @issue currently assumes OLE editor is provided by IDE plug-in
68
// IDE plug-in is not on prereq chain of generic wb plug-in
69
// hence: IContributorResourceAdapter.class won't compile
70
// and Class.forName("org.eclipse.ui.internal.editorsupport.win32.OleEditor") won't find it
71
// need to be trickier...
72
Bundle bundle = Platform.getBundle("org.eclipse.ui.ide"); //$NON-NLS-1$
73

74         // it's not our job to activate the plug-in
75
if (!BundleUtility.isActivated(bundle)) {
76             return null;
77         }
78
79         try {
80             Class JavaDoc c = bundle
81                     .loadClass("org.eclipse.ui.internal.editorsupport.win32.OleEditor"); //$NON-NLS-1$
82
return (IEditorPart) c.newInstance();
83         } catch (ClassNotFoundException JavaDoc exception) {
84             return null;
85         } catch (IllegalAccessException JavaDoc exception) {
86             return null;
87         } catch (InstantiationException JavaDoc exception) {
88             return null;
89         }
90     }
91
92     public static boolean testForOleEditor(String JavaDoc filename) {
93         int nDot = filename.lastIndexOf('.');
94         if (nDot >= 0) {
95             try {
96                 String JavaDoc strName = filename.substring(nDot);
97                 Class JavaDoc oleClass = Class.forName("org.eclipse.swt.ole.win32.OLE"); //$NON-NLS-1$
98
Method JavaDoc findMethod = oleClass.getDeclaredMethod(
99                         "findProgramID", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
100
strName = (String JavaDoc) findMethod.invoke(null,
101                         new Object JavaDoc[] { strName });
102                 if (strName.length() > 0) {
103                     return true;
104                 }
105             } catch (ClassNotFoundException JavaDoc exception) {
106                 //Couldn't ask so return false
107
return false;
108             } catch (NoSuchMethodException JavaDoc exception) {
109                 //Couldn't find the method so return false
110
return false;
111             } catch (IllegalAccessException JavaDoc exception) {
112                 return false;
113             } catch (InvocationTargetException JavaDoc exception) {
114                 return false;
115             }
116
117         }
118         return false;
119     }
120 }
121
Popular Tags