001: /*******************************************************************************
002: * Copyright (c) 2002, 2006 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 java.lang.reflect.Constructor;
013: import org.eclipse.core.runtime.*;
014: import org.eclipse.osgi.util.NLS;
015: import org.eclipse.ui.cheatsheets.AbstractItemExtensionElement;
016: import org.eclipse.ui.internal.cheatsheets.*;
017: import org.eclipse.ui.model.*;
018: import org.osgi.framework.Bundle;
019:
020: /**
021: * Instances represent registered cheat sheet item extensions.
022: */
023: public class CheatSheetItemExtensionElement extends WorkbenchAdapter
024: implements IAdaptable {
025: private String className;
026: private String itemAttribute;
027: private IConfigurationElement configurationElement;
028: private final Class[] stringArray = { String.class };
029:
030: /**
031: * Create a new instance of this class
032: *
033: */
034: public CheatSheetItemExtensionElement() {
035: }
036:
037: /**
038: * Returns an object which is an instance of the given class
039: * associated with this object. Returns <code>null</code> if
040: * no such object can be found.
041: */
042: public Object getAdapter(Class adapter) {
043: if (adapter == IWorkbenchAdapter.class) {
044: return this ;
045: }
046: return Platform.getAdapterManager().getAdapter(this , adapter);
047: }
048:
049: /**
050: *
051: * @return IConfigurationElement
052: */
053: public IConfigurationElement getConfigurationElement() {
054: return configurationElement;
055: }
056:
057: /**
058: * Answer the classname parameter of this element
059: *
060: * @return java.lang.String
061: */
062: public String getClassName() {
063: return className;
064: }
065:
066: /**
067: * Answer the itemAttribute parameter of this element
068: *
069: * @return java.lang.String
070: */
071: public String getItemAttribute() {
072: return itemAttribute;
073: }
074:
075: /**
076: *
077: * @param newConfigurationElement IConfigurationElement
078: */
079: public void setConfigurationElement(
080: IConfigurationElement newConfigurationElement) {
081: configurationElement = newConfigurationElement;
082: }
083:
084: /**
085: * Set the className parameter of this element
086: *
087: * @param value java.lang.String
088: */
089: public void setClassName(String value) {
090: className = value;
091: }
092:
093: /**
094: * Set the itemAttribute parameter of this element
095: *
096: * @param value java.lang.String
097: */
098: public void setItemAttribute(String value) {
099: itemAttribute = value;
100: }
101:
102: public AbstractItemExtensionElement createInstance() {
103: Class extClass = null;
104: AbstractItemExtensionElement extElement = null;
105: String pluginId = configurationElement.getContributor()
106: .getName();
107:
108: try {
109: Bundle bundle = Platform.getBundle(pluginId);
110: extClass = bundle.loadClass(className);
111: } catch (Exception e) {
112: String message = NLS.bind(Messages.ERROR_LOADING_CLASS,
113: (new Object[] { className }));
114: IStatus status = new Status(IStatus.ERROR,
115: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
116: IStatus.OK, message, e);
117: CheatSheetPlugin.getPlugin().getLog().log(status);
118: }
119: try {
120: if (extClass != null) {
121: Constructor c = extClass.getConstructor(stringArray);
122: Object[] parameters = { itemAttribute };
123: extElement = (AbstractItemExtensionElement) c
124: .newInstance(parameters);
125: }
126: } catch (Exception e) {
127: String message = NLS.bind(Messages.ERROR_CREATING_CLASS,
128: (new Object[] { className }));
129: IStatus status = new Status(IStatus.ERROR,
130: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
131: IStatus.OK, message, e);
132: CheatSheetPlugin.getPlugin().getLog().log(status);
133: }
134:
135: if (extElement != null) {
136: return extElement;
137: }
138:
139: return null;
140: }
141: }
|