001: /*******************************************************************************
002: * Copyright (c) 2000, 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.pde.internal.ui.wizards;
011:
012: import java.util.MissingResourceException;
013: import java.util.ResourceBundle;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.pde.internal.ui.elements.NamedElement;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.ui.IPluginContribution;
021: import org.osgi.framework.Bundle;
022:
023: public class WizardElement extends NamedElement implements
024: IPluginContribution {
025: public static final String ATT_NAME = "name"; //$NON-NLS-1$
026:
027: public static final String TAG_DESCRIPTION = "description"; //$NON-NLS-1$
028:
029: public static final String ATT_ICON = "icon"; //$NON-NLS-1$
030:
031: public static final String ATT_ID = "id"; //$NON-NLS-1$
032:
033: public static final String ATT_CLASS = "class"; //$NON-NLS-1$
034:
035: public static final String ATT_TEMPLATE = "template"; //$NON-NLS-1$
036:
037: public static final String ATT_POINT = "point"; //$NON-NLS-1$
038:
039: private String description;
040:
041: private IConfigurationElement configurationElement;
042:
043: private IConfigurationElement template;
044:
045: public WizardElement(IConfigurationElement config) {
046: super (config.getAttribute(ATT_NAME));
047: this .configurationElement = config;
048: }
049:
050: public Object createExecutableExtension() throws CoreException {
051: return configurationElement
052: .createExecutableExtension(ATT_CLASS);
053: }
054:
055: public IConfigurationElement getConfigurationElement() {
056: return configurationElement;
057: }
058:
059: public String getDescription() {
060: if (description == null) {
061: IConfigurationElement[] children = configurationElement
062: .getChildren(TAG_DESCRIPTION);
063: if (children.length > 0) {
064: description = expandDescription(children[0].getValue());
065: }
066: }
067: return description;
068: }
069:
070: /**
071: * We allow replacement variables in description values as well. This is to
072: * allow extension template descriptin reuse in project template wizards.
073: * Tokens in form '%token%' will be evaluated against the contributing
074: * plug-in's resource bundle. As before, to have '%' in the description, one
075: * need to add '%%'.
076: */
077: private String expandDescription(String source) {
078: if (source == null || source.length() == 0)
079: return source;
080: if (source.indexOf('%') == -1)
081: return source;
082:
083: Bundle bundle = Platform.getBundle(configurationElement
084: .getNamespaceIdentifier());
085: if (bundle == null)
086: return source;
087:
088: ResourceBundle resourceBundle = Platform
089: .getResourceBundle(bundle);
090: if (resourceBundle == null)
091: return source;
092: StringBuffer buf = new StringBuffer();
093: boolean keyMode = false;
094: int keyStartIndex = -1;
095: for (int i = 0; i < source.length(); i++) {
096: char c = source.charAt(i);
097: if (c == '%') {
098: char c2 = source.charAt(i + 1);
099: if (c2 == '%') {
100: i++;
101: buf.append('%');
102: continue;
103: }
104: if (keyMode) {
105: keyMode = false;
106: String key = source.substring(keyStartIndex, i);
107: String value = key;
108: try {
109: value = resourceBundle.getString(key);
110: } catch (MissingResourceException e) {
111: }
112: buf.append(value);
113: } else {
114: keyStartIndex = i + 1;
115: keyMode = true;
116: }
117: } else if (!keyMode) {
118: buf.append(c);
119: }
120: }
121: return buf.toString();
122: }
123:
124: public String getID() {
125: return configurationElement.getAttribute(ATT_ID);
126: }
127:
128: public void setImage(Image image) {
129: this .image = image;
130: }
131:
132: public String getTemplateId() {
133: return configurationElement.getAttribute(ATT_TEMPLATE);
134: }
135:
136: public boolean isTemplate() {
137: return getTemplateId() != null;
138: }
139:
140: public IConfigurationElement getTemplateElement() {
141: if (template == null)
142: template = findTemplateElement();
143: return template;
144: }
145:
146: private IConfigurationElement findTemplateElement() {
147: String templateId = getTemplateId();
148: if (templateId == null)
149: return null;
150: IConfigurationElement[] templates = Platform
151: .getExtensionRegistry().getConfigurationElementsFor(
152: "org.eclipse.pde.ui.templates"); //$NON-NLS-1$
153: for (int i = 0; i < templates.length; i++) {
154: IConfigurationElement template = templates[i];
155: String id = template.getAttribute("id"); //$NON-NLS-1$
156: if (id != null && id.equals(templateId))
157: return template;
158: }
159: return null;
160: }
161:
162: public String getContributingId() {
163: IConfigurationElement tel = getTemplateElement();
164: return (tel == null) ? null : tel
165: .getAttribute("contributingId"); //$NON-NLS-1$
166: }
167:
168: /* (non-Javadoc)
169: * @see org.eclipse.ui.IPluginContribution#getLocalId()
170: */
171: public String getLocalId() {
172: return getID();
173: }
174:
175: /* (non-Javadoc)
176: * @see org.eclipse.ui.IPluginContribution#getPluginId()
177: */
178: public String getPluginId() {
179: return null;
180: }
181: }
|