KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2002, 2007 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 org.eclipse.core.runtime.*;
14 import org.eclipse.osgi.util.NLS;
15 import org.eclipse.ui.IPluginContribution;
16 import org.eclipse.ui.cheatsheets.CheatSheetListener;
17 import org.eclipse.ui.internal.cheatsheets.*;
18 import org.eclipse.ui.model.WorkbenchAdapter;
19 import org.eclipse.ui.model.IWorkbenchAdapter;
20 import org.osgi.framework.Bundle;
21
22 /**
23  * Instances represent registered cheatsheets or cheatsheets opened using the content file.
24  */

25 public class CheatSheetElement extends WorkbenchAdapter implements IAdaptable, IPluginContribution {
26     private String JavaDoc contentFile;
27     private String JavaDoc id;
28     private String JavaDoc name;
29     private String JavaDoc description;
30     private IConfigurationElement configurationElement;
31     private String JavaDoc listenerClass;
32     private boolean composite;
33     private boolean registered = false;
34     private String JavaDoc contentXml;
35     private String JavaDoc href;
36
37     /**
38      * Create a new instance of this class
39      *
40      * @param name java.lang.String
41      */

42     public CheatSheetElement(String JavaDoc name) {
43         this.name = name;
44     }
45
46     /**
47      * Returns an object which is an instance of the given class
48      * associated with this object. Returns <code>null</code> if
49      * no such object can be found.
50      */

51     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
52         if (adapter == IWorkbenchAdapter.class) {
53             return this;
54         }
55         return Platform.getAdapterManager().getAdapter(this, adapter);
56     }
57
58     /**
59      *
60      * @return IConfigurationElement
61      */

62     public IConfigurationElement getConfigurationElement() {
63         return configurationElement;
64     }
65
66     /**
67      * Get the content File path of this element
68      *
69      * @return java.lang.String
70      */

71     public String JavaDoc getContentFile() {
72         return contentFile;
73     }
74
75     /**
76      * Answer the description parameter of this element
77      *
78      * @return java.lang.String
79      */

80     public String JavaDoc getDescription() {
81         return description;
82     }
83
84     /**
85      * Answer the id as specified in the extension.
86      *
87      * @return java.lang.String
88      */

89     public String JavaDoc getID() {
90         return id;
91     }
92
93     /**
94      * Returns the name of this cheatsheet element.
95      */

96     public String JavaDoc getLabel(Object JavaDoc element) {
97         return name;
98     }
99
100     /**
101      * Returns the listener class name of this cheatsheet element.
102      */

103     public String JavaDoc getListenerClass() {
104         return listenerClass;
105     }
106
107     /**
108      *
109      * @param newConfigurationElement IConfigurationElement
110      */

111     public void setConfigurationElement(IConfigurationElement newConfigurationElement) {
112         configurationElement = newConfigurationElement;
113     }
114
115     /**
116      * Set the contentFile parameter of this element
117      *
118      * @param value the path of the content file
119      */

120     public void setContentFile(String JavaDoc value) {
121         contentFile = value;
122     }
123
124     /**
125      * Set the description parameter of this element
126      *
127      * @param value java.lang.String
128      */

129     public void setDescription(String JavaDoc value) {
130         description = value;
131     }
132
133     /**
134      * Set the id parameter of this element
135      *
136      * @param value java.lang.String
137      */

138     public void setID(String JavaDoc value) {
139         id = value;
140     }
141
142     /**
143      * Set the listener class name of this element.
144      */

145     public void setListenerClass(String JavaDoc value) {
146         listenerClass = value;
147     }
148
149     public CheatSheetListener createListenerInstance() {
150         if(listenerClass == null || configurationElement == null) {
151             return null;
152         }
153
154         Class JavaDoc extClass = null;
155         CheatSheetListener listener = null;
156         String JavaDoc pluginId = configurationElement.getContributor().getName();
157
158         try {
159             Bundle bundle = Platform.getBundle(pluginId);
160             extClass = bundle.loadClass(listenerClass);
161         } catch (Exception JavaDoc e) {
162             String JavaDoc message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object JavaDoc[] {listenerClass}));
163             IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
164             CheatSheetPlugin.getPlugin().getLog().log(status);
165         }
166         try {
167             if (extClass != null) {
168                 listener = (CheatSheetListener) extClass.newInstance();
169             }
170         } catch (Exception JavaDoc e) {
171             String JavaDoc message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object JavaDoc[] {listenerClass}));
172             IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
173             CheatSheetPlugin.getPlugin().getLog().log(status);
174         }
175         
176         if (listener != null){
177             return listener;
178         }
179
180         return null;
181     }
182
183     public String JavaDoc getLocalId() {
184         return id;
185     }
186
187     public String JavaDoc getPluginId() {
188         return configurationElement.getContributor().getName();
189     }
190     
191     public void setComposite(boolean composite) {
192         this.composite = composite;
193     }
194
195     public boolean isComposite() {
196         return composite;
197     }
198
199     /**
200      * Get a URL which is saved with the state so the cheatsheet can later be
201      * reopened from the state file.
202      * @return null if the cheatsheet was opened from the registry otherwise
203      * the URL of the content file.
204      */

205     public String JavaDoc getRestorePath() {
206         if (!registered) {
207             return contentFile;
208         }
209         return null;
210     }
211
212     public void setRegistered(boolean registered) {
213         this.registered = registered;
214     }
215
216     public boolean isRegistered() {
217         return registered;
218     }
219
220     public void setContentXml(String JavaDoc xml) {
221         this.contentXml = xml;
222     }
223     
224     public String JavaDoc getContentXml() {
225         return contentXml;
226     }
227
228     public void setHref(String JavaDoc contentPath) {
229         this.href = contentPath;
230     }
231
232     public String JavaDoc getHref() {
233         return href;
234     }
235
236 }
237
Popular Tags