1 11 package org.eclipse.ui.internal.dialogs; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IConfigurationElement; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 import org.eclipse.ui.IPluginContribution; 21 import org.eclipse.ui.IWorkbenchWizard; 22 import org.eclipse.ui.PlatformUI; 23 import org.eclipse.ui.SelectionEnabler; 24 import org.eclipse.ui.internal.ISelectionConversionService; 25 import org.eclipse.ui.internal.WorkbenchPlugin; 26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 27 import org.eclipse.ui.internal.registry.RegistryReader; 28 import org.eclipse.ui.model.IWorkbenchAdapter; 29 import org.eclipse.ui.model.IWorkbenchAdapter2; 30 import org.eclipse.ui.model.WorkbenchAdapter; 31 import org.eclipse.ui.plugin.AbstractUIPlugin; 32 import org.eclipse.ui.wizards.IWizardCategory; 33 import org.eclipse.ui.wizards.IWizardDescriptor; 34 35 38 public class WorkbenchWizardElement extends WorkbenchAdapter implements 39 IAdaptable, IPluginContribution, IWizardDescriptor { 40 private String id; 41 42 private ImageDescriptor imageDescriptor; 43 44 private SelectionEnabler selectionEnabler; 45 46 private IConfigurationElement configurationElement; 47 48 private ImageDescriptor descriptionImage; 49 50 private WizardCollectionElement parentCategory; 51 52 55 public static final String TAG_PROJECT = "project"; 57 private static final String [] EMPTY_TAGS = new String [0]; 58 59 private static final String [] PROJECT_TAGS = new String [] {TAG_PROJECT}; 60 61 62 68 public WorkbenchWizardElement(IConfigurationElement configurationElement) { 69 this.configurationElement = configurationElement; 70 id = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 71 } 72 73 81 public boolean canHandleSelection(IStructuredSelection selection) { 82 return getSelectionEnabler().isEnabledForSelection(selection); 83 } 84 85 95 public IStructuredSelection adaptedSelection(IStructuredSelection selection) { 96 if (canHandleSelection(selection)) { 97 return selection; 98 } 99 100 IStructuredSelection adaptedSelection = convertToResources(selection); 101 if (canHandleSelection(adaptedSelection)) { 102 return adaptedSelection; 103 } 104 105 return StructuredSelection.EMPTY; 107 } 108 109 116 public Object createExecutableExtension() throws CoreException { 117 return WorkbenchPlugin.createExtension(configurationElement, 118 IWorkbenchRegistryConstants.ATT_CLASS); 119 } 120 121 126 public Object getAdapter(Class adapter) { 127 if (adapter == IWorkbenchAdapter.class 128 || adapter == IWorkbenchAdapter2.class) { 129 return this; 130 } 131 else if (adapter == IPluginContribution.class) { 132 return this; 133 } 134 else if (adapter == IConfigurationElement.class) { 135 return configurationElement; 136 } 137 return Platform.getAdapterManager().getAdapter(this, adapter); 138 } 139 140 143 public IConfigurationElement getConfigurationElement() { 144 return configurationElement; 145 } 146 147 152 public String getDescription() { 153 return RegistryReader.getDescription(configurationElement); 154 } 155 156 159 public ImageDescriptor getImageDescriptor() { 160 if (imageDescriptor == null) { 161 String iconName = configurationElement 162 .getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 163 if (iconName == null) { 164 return null; 165 } 166 imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin( 167 configurationElement.getNamespaceIdentifier(), iconName); 168 } 169 return imageDescriptor; 170 } 171 172 175 public ImageDescriptor getImageDescriptor(Object element) { 176 return getImageDescriptor(); 177 } 178 179 182 public String getLabel(Object element) { 183 return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 184 } 185 186 189 protected SelectionEnabler getSelectionEnabler() { 190 if (selectionEnabler == null) { 191 selectionEnabler = new SelectionEnabler(configurationElement); 192 } 193 194 return selectionEnabler; 195 } 196 197 207 private IStructuredSelection convertToResources( 208 IStructuredSelection originalSelection) { 209 Object selectionService = PlatformUI.getWorkbench().getService( 210 ISelectionConversionService.class); 211 if (selectionService == null || originalSelection == null) { 212 return StructuredSelection.EMPTY; 213 } 214 return ((ISelectionConversionService) selectionService) 215 .convertToResources(originalSelection); 216 } 217 218 223 public String getLocalId() { 224 return getId(); 225 } 226 227 230 public String getPluginId() { 231 return (configurationElement != null) ? configurationElement 232 .getNamespaceIdentifier() : null; 233 } 234 235 238 public ImageDescriptor getDescriptionImage() { 239 if (descriptionImage == null) { 240 String descImage = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_DESCRIPTION_IMAGE); 241 if (descImage == null) { 242 return null; 243 } 244 descriptionImage = AbstractUIPlugin.imageDescriptorFromPlugin( 245 configurationElement.getNamespaceIdentifier(), descImage); 246 } 247 return descriptionImage; 248 } 249 250 253 public String getHelpHref() { 254 return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_HREF); 255 } 256 257 260 public IWorkbenchWizard createWizard() throws CoreException { 261 return (IWorkbenchWizard) createExecutableExtension(); 262 } 263 264 267 public String getId() { 268 return id; 269 } 270 271 274 public String getLabel() { 275 return getLabel(this); 276 } 277 278 281 public IWizardCategory getCategory() { 282 return (IWizardCategory) getParent(this); 283 } 284 285 291 public WizardCollectionElement getCollectionElement() { 292 return (WizardCollectionElement) getParent(this); 293 } 294 295 298 public String [] getTags() { 299 300 String flag = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_PROJECT); 301 if (Boolean.valueOf(flag).booleanValue()) { 302 return PROJECT_TAGS; 303 } 304 305 return EMPTY_TAGS; 306 } 307 308 311 public Object getParent(Object object) { 312 return parentCategory; 313 } 314 315 321 public void setParent(WizardCollectionElement parent) { 322 parentCategory = parent; 323 } 324 325 328 public boolean canFinishEarly() { 329 return Boolean.valueOf(configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_CAN_FINISH_EARLY)).booleanValue(); 330 } 331 332 335 public boolean hasPages() { 336 String hasPagesString = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_HAS_PAGES); 337 if (hasPagesString == null) { 339 return true; 340 } 341 return Boolean.valueOf(hasPagesString).booleanValue(); 342 } 343 } 344 | Popular Tags |