1 11 package org.eclipse.pde.internal.ui.wizards; 12 13 import java.util.MissingResourceException ; 14 import java.util.ResourceBundle ; 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 ATT_NAME = "name"; 27 public static final String TAG_DESCRIPTION = "description"; 29 public static final String ATT_ICON = "icon"; 31 public static final String ATT_ID = "id"; 33 public static final String ATT_CLASS = "class"; 35 public static final String ATT_TEMPLATE = "template"; 37 public static final String ATT_POINT = "point"; 39 private String 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 createExecutableExtension() throws CoreException { 51 return configurationElement.createExecutableExtension(ATT_CLASS); 52 } 53 54 public IConfigurationElement getConfigurationElement() { 55 return configurationElement; 56 } 57 58 public String 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 76 private String expandDescription(String source) { 77 if (source == null || source.length() == 0) 78 return source; 79 if (source.indexOf('%') == -1) 80 return source; 81 82 Bundle bundle = Platform.getBundle(configurationElement.getNamespaceIdentifier()); 83 if (bundle == null) 84 return source; 85 86 ResourceBundle resourceBundle = Platform.getResourceBundle(bundle); 87 if (resourceBundle == null) 88 return source; 89 StringBuffer buf = new StringBuffer (); 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 key = source.substring(keyStartIndex, i); 104 String value = key; 105 try { 106 value = resourceBundle.getString(key); 107 } catch (MissingResourceException 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 getID() { 122 return configurationElement.getAttribute(ATT_ID); 123 } 124 125 public void setImage(Image image) { 126 this.image = image; 127 } 128 129 public String 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 templateId = getTemplateId(); 145 if (templateId == null) 146 return null; 147 IConfigurationElement[] templates = Platform.getExtensionRegistry() 148 .getConfigurationElementsFor("org.eclipse.pde.ui.templates"); for (int i = 0; i < templates.length; i++) { 150 IConfigurationElement template = templates[i]; 151 String id = template.getAttribute("id"); if (id != null && id.equals(templateId)) 153 return template; 154 } 155 return null; 156 } 157 158 public String getContributingId() { 159 IConfigurationElement tel = getTemplateElement(); 160 return (tel == null) ? null : tel.getAttribute("contributingId"); } 162 165 public String getLocalId() { 166 return getID(); 167 } 168 171 public String getPluginId() { 172 return null; 173 } 174 } 175 | Popular Tags |