001: /*******************************************************************************
002: * Copyright (c) 2002, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.registry;
011:
012: import org.eclipse.core.runtime.*;
013: import org.eclipse.osgi.util.NLS;
014: import org.eclipse.ui.IPluginContribution;
015: import org.eclipse.ui.cheatsheets.CheatSheetListener;
016: import org.eclipse.ui.internal.cheatsheets.*;
017: import org.eclipse.ui.model.WorkbenchAdapter;
018: import org.eclipse.ui.model.IWorkbenchAdapter;
019: import org.osgi.framework.Bundle;
020:
021: /**
022: * Instances represent registered cheatsheets or cheatsheets opened using the content file.
023: */
024: public class CheatSheetElement extends WorkbenchAdapter implements
025: IAdaptable, IPluginContribution {
026: private String contentFile;
027: private String id;
028: private String name;
029: private String description;
030: private IConfigurationElement configurationElement;
031: private String listenerClass;
032: private boolean composite;
033: private boolean registered = false;
034: private String contentXml;
035: private String href;
036:
037: /**
038: * Create a new instance of this class
039: *
040: * @param name java.lang.String
041: */
042: public CheatSheetElement(String name) {
043: this .name = name;
044: }
045:
046: /**
047: * Returns an object which is an instance of the given class
048: * associated with this object. Returns <code>null</code> if
049: * no such object can be found.
050: */
051: public Object getAdapter(Class adapter) {
052: if (adapter == IWorkbenchAdapter.class) {
053: return this ;
054: }
055: return Platform.getAdapterManager().getAdapter(this , adapter);
056: }
057:
058: /**
059: *
060: * @return IConfigurationElement
061: */
062: public IConfigurationElement getConfigurationElement() {
063: return configurationElement;
064: }
065:
066: /**
067: * Get the content File path of this element
068: *
069: * @return java.lang.String
070: */
071: public String getContentFile() {
072: return contentFile;
073: }
074:
075: /**
076: * Answer the description parameter of this element
077: *
078: * @return java.lang.String
079: */
080: public String getDescription() {
081: return description;
082: }
083:
084: /**
085: * Answer the id as specified in the extension.
086: *
087: * @return java.lang.String
088: */
089: public String getID() {
090: return id;
091: }
092:
093: /**
094: * Returns the name of this cheatsheet element.
095: */
096: public String getLabel(Object element) {
097: return name;
098: }
099:
100: /**
101: * Returns the listener class name of this cheatsheet element.
102: */
103: public String getListenerClass() {
104: return listenerClass;
105: }
106:
107: /**
108: *
109: * @param newConfigurationElement IConfigurationElement
110: */
111: public void setConfigurationElement(
112: IConfigurationElement newConfigurationElement) {
113: configurationElement = newConfigurationElement;
114: }
115:
116: /**
117: * Set the contentFile parameter of this element
118: *
119: * @param value the path of the content file
120: */
121: public void setContentFile(String value) {
122: contentFile = value;
123: }
124:
125: /**
126: * Set the description parameter of this element
127: *
128: * @param value java.lang.String
129: */
130: public void setDescription(String value) {
131: description = value;
132: }
133:
134: /**
135: * Set the id parameter of this element
136: *
137: * @param value java.lang.String
138: */
139: public void setID(String value) {
140: id = value;
141: }
142:
143: /**
144: * Set the listener class name of this element.
145: */
146: public void setListenerClass(String value) {
147: listenerClass = value;
148: }
149:
150: public CheatSheetListener createListenerInstance() {
151: if (listenerClass == null || configurationElement == null) {
152: return null;
153: }
154:
155: Class extClass = null;
156: CheatSheetListener listener = null;
157: String pluginId = configurationElement.getContributor()
158: .getName();
159:
160: try {
161: Bundle bundle = Platform.getBundle(pluginId);
162: extClass = bundle.loadClass(listenerClass);
163: } catch (Exception e) {
164: String message = NLS.bind(Messages.ERROR_LOADING_CLASS,
165: (new Object[] { listenerClass }));
166: IStatus status = new Status(IStatus.ERROR,
167: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
168: IStatus.OK, message, e);
169: CheatSheetPlugin.getPlugin().getLog().log(status);
170: }
171: try {
172: if (extClass != null) {
173: listener = (CheatSheetListener) extClass.newInstance();
174: }
175: } catch (Exception e) {
176: String message = NLS.bind(Messages.ERROR_CREATING_CLASS,
177: (new Object[] { listenerClass }));
178: IStatus status = new Status(IStatus.ERROR,
179: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
180: IStatus.OK, message, e);
181: CheatSheetPlugin.getPlugin().getLog().log(status);
182: }
183:
184: if (listener != null) {
185: return listener;
186: }
187:
188: return null;
189: }
190:
191: public String getLocalId() {
192: return id;
193: }
194:
195: public String getPluginId() {
196: return configurationElement.getContributor().getName();
197: }
198:
199: public void setComposite(boolean composite) {
200: this .composite = composite;
201: }
202:
203: public boolean isComposite() {
204: return composite;
205: }
206:
207: /**
208: * Get a URL which is saved with the state so the cheatsheet can later be
209: * reopened from the state file.
210: * @return null if the cheatsheet was opened from the registry otherwise
211: * the URL of the content file.
212: */
213: public String getRestorePath() {
214: if (!registered) {
215: return contentFile;
216: }
217: return null;
218: }
219:
220: public void setRegistered(boolean registered) {
221: this .registered = registered;
222: }
223:
224: public boolean isRegistered() {
225: return registered;
226: }
227:
228: public void setContentXml(String xml) {
229: this .contentXml = xml;
230: }
231:
232: public String getContentXml() {
233: return contentXml;
234: }
235:
236: public void setHref(String contentPath) {
237: this .href = contentPath;
238: }
239:
240: public String getHref() {
241: return href;
242: }
243:
244: }
|