KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > registry > CheatSheetItemExtensionElement


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.cheatsheets.registry;
12
13 import java.lang.reflect.Constructor JavaDoc;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.ui.cheatsheets.AbstractItemExtensionElement;
17 import org.eclipse.ui.internal.cheatsheets.*;
18 import org.eclipse.ui.model.*;
19 import org.osgi.framework.Bundle;
20
21 /**
22  * Instances represent registered cheat sheet item extensions.
23  */

24 public class CheatSheetItemExtensionElement extends WorkbenchAdapter implements IAdaptable {
25     private String JavaDoc className;
26     private String JavaDoc itemAttribute;
27     private IConfigurationElement configurationElement;
28     private final Class JavaDoc[] stringArray = { String JavaDoc.class };
29
30     /**
31      * Create a new instance of this class
32      *
33      */

34     public CheatSheetItemExtensionElement() {
35     }
36
37     /**
38      * Returns an object which is an instance of the given class
39      * associated with this object. Returns <code>null</code> if
40      * no such object can be found.
41      */

42     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
43         if (adapter == IWorkbenchAdapter.class) {
44             return this;
45         }
46         return Platform.getAdapterManager().getAdapter(this, adapter);
47     }
48
49     /**
50      *
51      * @return IConfigurationElement
52      */

53     public IConfigurationElement getConfigurationElement() {
54         return configurationElement;
55     }
56
57     /**
58      * Answer the classname parameter of this element
59      *
60      * @return java.lang.String
61      */

62     public String JavaDoc getClassName() {
63         return className;
64     }
65
66     /**
67      * Answer the itemAttribute parameter of this element
68      *
69      * @return java.lang.String
70      */

71     public String JavaDoc getItemAttribute() {
72         return itemAttribute;
73     }
74
75     /**
76      *
77      * @param newConfigurationElement IConfigurationElement
78      */

79     public void setConfigurationElement(IConfigurationElement newConfigurationElement) {
80         configurationElement = newConfigurationElement;
81     }
82
83     /**
84      * Set the className parameter of this element
85      *
86      * @param value java.lang.String
87      */

88     public void setClassName(String JavaDoc value) {
89         className = value;
90     }
91
92     /**
93      * Set the itemAttribute parameter of this element
94      *
95      * @param value java.lang.String
96      */

97     public void setItemAttribute(String JavaDoc value) {
98         itemAttribute = value;
99     }
100
101     public AbstractItemExtensionElement createInstance() {
102         Class JavaDoc extClass = null;
103         AbstractItemExtensionElement extElement = null;
104         String JavaDoc pluginId = configurationElement.getContributor().getName();
105
106         try {
107             Bundle bundle = Platform.getBundle(pluginId);
108             extClass = bundle.loadClass(className);
109         } catch (Exception JavaDoc e) {
110             String JavaDoc message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object JavaDoc[] {className}));
111             IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
112             CheatSheetPlugin.getPlugin().getLog().log(status);
113         }
114         try {
115             if (extClass != null) {
116                 Constructor JavaDoc c = extClass.getConstructor(stringArray);
117                 Object JavaDoc[] parameters = { itemAttribute };
118                 extElement = (AbstractItemExtensionElement) c.newInstance(parameters);
119             }
120         } catch (Exception JavaDoc e) {
121             String JavaDoc message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object JavaDoc[] {className}));
122             IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
123             CheatSheetPlugin.getPlugin().getLog().log(status);
124         }
125         
126         if (extElement != null){
127             return extElement;
128         }
129
130         return null;
131     }
132 }
133
Popular Tags