KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > WizardElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.pde.internal.ui.wizards;
12
13 import java.util.MissingResourceException JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.pde.internal.ui.elements.NamedElement;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.ui.IPluginContribution;
22 import org.osgi.framework.Bundle;
23
24 public class WizardElement extends NamedElement implements IPluginContribution {
25     public static final String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
26

27     public static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
28

29     public static final String JavaDoc ATT_ICON = "icon"; //$NON-NLS-1$
30

31     public static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
32

33     public static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
34

35     public static final String JavaDoc ATT_TEMPLATE = "template"; //$NON-NLS-1$
36

37     public static final String JavaDoc ATT_POINT = "point"; //$NON-NLS-1$
38

39     private String JavaDoc description;
40
41     private IConfigurationElement configurationElement;
42
43     private IConfigurationElement template;
44
45     public WizardElement(IConfigurationElement config) {
46         super(config.getAttribute(ATT_NAME));
47         this.configurationElement = config;
48     }
49
50     public Object JavaDoc createExecutableExtension() throws CoreException {
51         return configurationElement.createExecutableExtension(ATT_CLASS);
52     }
53
54     public IConfigurationElement getConfigurationElement() {
55         return configurationElement;
56     }
57
58     public String JavaDoc getDescription() {
59         if (description == null) {
60             IConfigurationElement[] children = configurationElement
61                     .getChildren(TAG_DESCRIPTION);
62             if (children.length > 0) {
63                 description = expandDescription(children[0].getValue());
64             }
65         }
66         return description;
67     }
68
69     /**
70      * We allow replacement variables in description values as well. This is to
71      * allow extension template descriptin reuse in project template wizards.
72      * Tokens in form '%token%' will be evaluated against the contributing
73      * plug-in's resource bundle. As before, to have '%' in the description, one
74      * need to add '%%'.
75      */

76     private String JavaDoc expandDescription(String JavaDoc source) {
77         if (source == null || source.length() == 0)
78             return source;
79         if (source.indexOf('%') == -1)
80             return source;
81
82         Bundle JavaDoc bundle = Platform.getBundle(configurationElement.getNamespaceIdentifier());
83         if (bundle == null)
84             return source;
85
86         ResourceBundle JavaDoc resourceBundle = Platform.getResourceBundle(bundle);
87         if (resourceBundle == null)
88             return source;
89         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
90         boolean keyMode = false;
91         int keyStartIndex = -1;
92         for (int i = 0; i < source.length(); i++) {
93             char c = source.charAt(i);
94             if (c == '%') {
95                 char c2 = source.charAt(i + 1);
96                 if (c2 == '%') {
97                     i++;
98                     buf.append('%');
99                     continue;
100                 }
101                 if (keyMode) {
102                     keyMode = false;
103                     String JavaDoc key = source.substring(keyStartIndex, i);
104                     String JavaDoc value = key;
105                     try {
106                         value = resourceBundle.getString(key);
107                     } catch (MissingResourceException JavaDoc e) {
108                     }
109                     buf.append(value);
110                 } else {
111                     keyStartIndex = i + 1;
112                     keyMode = true;
113                 }
114             } else if (!keyMode) {
115                 buf.append(c);
116             }
117         }
118         return buf.toString();
119     }
120
121     public String JavaDoc getID() {
122         return configurationElement.getAttribute(ATT_ID);
123     }
124
125     public void setImage(Image image) {
126         this.image = image;
127     }
128
129     public String JavaDoc getTemplateId() {
130         return configurationElement.getAttribute(ATT_TEMPLATE);
131     }
132
133     public boolean isTemplate() {
134         return getTemplateId() != null;
135     }
136
137     public IConfigurationElement getTemplateElement() {
138         if (template == null)
139             template = findTemplateElement();
140         return template;
141     }
142
143     private IConfigurationElement findTemplateElement() {
144         String JavaDoc templateId = getTemplateId();
145         if (templateId == null)
146             return null;
147         IConfigurationElement[] templates = Platform.getExtensionRegistry()
148                 .getConfigurationElementsFor("org.eclipse.pde.ui.templates"); //$NON-NLS-1$
149
for (int i = 0; i < templates.length; i++) {
150             IConfigurationElement template = templates[i];
151             String JavaDoc id = template.getAttribute("id"); //$NON-NLS-1$
152
if (id != null && id.equals(templateId))
153                 return template;
154         }
155         return null;
156     }
157
158     public String JavaDoc getContributingId() {
159         IConfigurationElement tel = getTemplateElement();
160         return (tel == null) ? null : tel.getAttribute("contributingId"); //$NON-NLS-1$
161
}
162     /* (non-Javadoc)
163      * @see org.eclipse.ui.IPluginContribution#getLocalId()
164      */

165     public String JavaDoc getLocalId() {
166         return getID();
167     }
168     /* (non-Javadoc)
169      * @see org.eclipse.ui.IPluginContribution#getPluginId()
170      */

171     public String JavaDoc getPluginId() {
172         return null;
173     }
174 }
175
Popular Tags