PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.ui.workbench.source_3.7.0.I20110519-0100/org/eclipse/ui/internal/dialogs/WorkbenchWizardElement.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 365 lines | 185 code | 45 blank | 135 comment | 35 complexity | e4e32032300b1a3ad525fa260677cfa6 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2011 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. * Fair Isaac Corporation <Hemant.Singh@Gmail.com> - http://bugs.eclipse.org/326695
  11. *******************************************************************************/
  12. package org.eclipse.ui.internal.dialogs;
  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.KeywordRegistry;
  28. import org.eclipse.ui.internal.registry.RegistryReader;
  29. import org.eclipse.ui.model.IWorkbenchAdapter;
  30. import org.eclipse.ui.model.IWorkbenchAdapter2;
  31. import org.eclipse.ui.model.IWorkbenchAdapter3;
  32. import org.eclipse.ui.model.WorkbenchAdapter;
  33. import org.eclipse.ui.plugin.AbstractUIPlugin;
  34. import org.eclipse.ui.wizards.IWizardCategory;
  35. import org.eclipse.ui.wizards.IWizardDescriptor;
  36. /**
  37. * Instances represent registered wizards.
  38. */
  39. public class WorkbenchWizardElement extends WorkbenchAdapter implements
  40. IAdaptable, IPluginContribution, IWizardDescriptor {
  41. private String id;
  42. private ImageDescriptor imageDescriptor;
  43. private SelectionEnabler selectionEnabler;
  44. private IConfigurationElement configurationElement;
  45. private ImageDescriptor descriptionImage;
  46. private WizardCollectionElement parentCategory;
  47. /**
  48. * TODO: DO we need to make this API?
  49. */
  50. public static final String TAG_PROJECT = "project"; //$NON-NLS-1$
  51. private static final String [] EMPTY_TAGS = new String[0];
  52. private static final String [] PROJECT_TAGS = new String[] {TAG_PROJECT};
  53. private String[] keywordLabels;
  54. /**
  55. * Create a new instance of this class
  56. *
  57. * @param configurationElement
  58. * @since 3.1
  59. */
  60. public WorkbenchWizardElement(IConfigurationElement configurationElement) {
  61. this.configurationElement = configurationElement;
  62. id = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
  63. }
  64. /**
  65. * Answer a boolean indicating whether the receiver is able to handle the
  66. * passed selection
  67. *
  68. * @return boolean
  69. * @param selection
  70. * IStructuredSelection
  71. */
  72. public boolean canHandleSelection(IStructuredSelection selection) {
  73. return getSelectionEnabler().isEnabledForSelection(selection);
  74. }
  75. /**
  76. * Answer the selection for the reciever based on whether the it can handle
  77. * the selection. If it can return the selection. If it can handle the
  78. * adapted to IResource value of the selection. If it satisfies neither of
  79. * these conditions return an empty IStructuredSelection.
  80. *
  81. * @return IStructuredSelection
  82. * @param selection
  83. * IStructuredSelection
  84. */
  85. public IStructuredSelection adaptedSelection(IStructuredSelection selection) {
  86. if (canHandleSelection(selection)) {
  87. return selection;
  88. }
  89. IStructuredSelection adaptedSelection = convertToResources(selection);
  90. if (canHandleSelection(adaptedSelection)) {
  91. return adaptedSelection;
  92. }
  93. //Couldn't find one that works so just return
  94. return StructuredSelection.EMPTY;
  95. }
  96. /**
  97. * Create an the instance of the object described by the configuration
  98. * element. That is, create the instance of the class the isv supplied in
  99. * the extension point.
  100. * @return the new object
  101. * @throws CoreException
  102. */
  103. public Object createExecutableExtension() throws CoreException {
  104. return WorkbenchPlugin.createExtension(configurationElement,
  105. IWorkbenchRegistryConstants.ATT_CLASS);
  106. }
  107. /**
  108. * Returns an object which is an instance of the given class associated
  109. * with this object. Returns <code>null</code> if no such object can be
  110. * found.
  111. */
  112. public Object getAdapter(Class adapter) {
  113. if (adapter == IWorkbenchAdapter.class
  114. || adapter == IWorkbenchAdapter2.class
  115. || adapter == IWorkbenchAdapter3.class) {
  116. return this;
  117. }
  118. else if (adapter == IPluginContribution.class) {
  119. return this;
  120. }
  121. else if (adapter == IConfigurationElement.class) {
  122. return configurationElement;
  123. }
  124. return Platform.getAdapterManager().getAdapter(this, adapter);
  125. }
  126. /**
  127. * @return IConfigurationElement
  128. */
  129. public IConfigurationElement getConfigurationElement() {
  130. return configurationElement;
  131. }
  132. /**
  133. * Answer the description parameter of this element
  134. *
  135. * @return java.lang.String
  136. */
  137. public String getDescription() {
  138. return RegistryReader.getDescription(configurationElement);
  139. }
  140. /**
  141. * Answer the icon of this element.
  142. */
  143. public ImageDescriptor getImageDescriptor() {
  144. if (imageDescriptor == null) {
  145. String iconName = configurationElement
  146. .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
  147. if (iconName == null) {
  148. return null;
  149. }
  150. imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
  151. configurationElement.getNamespaceIdentifier(), iconName);
  152. }
  153. return imageDescriptor;
  154. }
  155. /**
  156. * Returns the name of this wizard element.
  157. */
  158. public ImageDescriptor getImageDescriptor(Object element) {
  159. return getImageDescriptor();
  160. }
  161. /**
  162. * Returns the name of this wizard element.
  163. */
  164. public String getLabel(Object element) {
  165. return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
  166. }
  167. /**
  168. * Answer self's action enabler, creating it first iff necessary
  169. */
  170. protected SelectionEnabler getSelectionEnabler() {
  171. if (selectionEnabler == null) {
  172. selectionEnabler = new SelectionEnabler(configurationElement);
  173. }
  174. return selectionEnabler;
  175. }
  176. /**
  177. * Attempt to convert the elements in the passed selection into resources
  178. * by asking each for its IResource property (iff it isn't already a
  179. * resource). If all elements in the initial selection can be converted to
  180. * resources then answer a new selection containing these resources;
  181. * otherwise answer an empty selection.
  182. *
  183. * @param originalSelection the original selection
  184. * @return the converted selection or an empty selection
  185. */
  186. private IStructuredSelection convertToResources(
  187. IStructuredSelection originalSelection) {
  188. Object selectionService = PlatformUI.getWorkbench().getService(
  189. ISelectionConversionService.class);
  190. if (selectionService == null || originalSelection == null) {
  191. return StructuredSelection.EMPTY;
  192. }
  193. return ((ISelectionConversionService) selectionService)
  194. .convertToResources(originalSelection);
  195. }
  196. /*
  197. * (non-Javadoc)
  198. *
  199. * @see org.eclipse.ui.IPluginContribution#getLocalId()
  200. */
  201. public String getLocalId() {
  202. return getId();
  203. }
  204. /* (non-Javadoc)
  205. * @see org.eclipse.ui.IPluginContribution#getPluginId()
  206. */
  207. public String getPluginId() {
  208. return (configurationElement != null) ? configurationElement
  209. .getNamespaceIdentifier() : null;
  210. }
  211. /* (non-Javadoc)
  212. * @see org.eclipse.ui.wizards.INewWizardDescriptor#getDescriptionImage()
  213. */
  214. public ImageDescriptor getDescriptionImage() {
  215. if (descriptionImage == null) {
  216. String descImage = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_DESCRIPTION_IMAGE);
  217. if (descImage == null) {
  218. return null;
  219. }
  220. descriptionImage = AbstractUIPlugin.imageDescriptorFromPlugin(
  221. configurationElement.getNamespaceIdentifier(), descImage);
  222. }
  223. return descriptionImage;
  224. }
  225. /* (non-Javadoc)
  226. * @see org.eclipse.ui.wizards.INewWizardDescriptor#getHelpHref()
  227. */
  228. public String getHelpHref() {
  229. return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_HREF);
  230. }
  231. /* (non-Javadoc)
  232. * @see org.eclipse.ui.wizards.INewWizardDescriptor#createWizard()
  233. */
  234. public IWorkbenchWizard createWizard() throws CoreException {
  235. return (IWorkbenchWizard) createExecutableExtension();
  236. }
  237. /* (non-Javadoc)
  238. * @see org.eclipse.ui.IWorkbenchPartDescriptor#getId()
  239. */
  240. public String getId() {
  241. return id;
  242. }
  243. /* (non-Javadoc)
  244. * @see org.eclipse.ui.IWorkbenchPartDescriptor#getLabel()
  245. */
  246. public String getLabel() {
  247. return getLabel(this);
  248. }
  249. /* (non-Javadoc)
  250. * @see org.eclipse.ui.wizards.INewWizardDescriptor#getCategory()
  251. */
  252. public IWizardCategory getCategory() {
  253. return (IWizardCategory) getParent(this);
  254. }
  255. /**
  256. * Return the collection.
  257. *
  258. * @return the collection
  259. * @since 3.1
  260. */
  261. public WizardCollectionElement getCollectionElement() {
  262. return (WizardCollectionElement) getParent(this);
  263. }
  264. /* (non-Javadoc)
  265. * @see org.eclipse.ui.wizards.IWizardDescriptor#getTags()
  266. */
  267. public String [] getTags() {
  268. String flag = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_PROJECT);
  269. if (Boolean.valueOf(flag).booleanValue()) {
  270. return PROJECT_TAGS;
  271. }
  272. return EMPTY_TAGS;
  273. }
  274. /* (non-Javadoc)
  275. * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
  276. */
  277. public Object getParent(Object object) {
  278. return parentCategory;
  279. }
  280. /**
  281. * Set the parent category.
  282. *
  283. * @param parent the parent category
  284. * @since 3.1
  285. */
  286. public void setParent(WizardCollectionElement parent) {
  287. parentCategory = parent;
  288. }
  289. /* (non-Javadoc)
  290. * @see org.eclipse.ui.wizards.IWizardDescriptor#canFinishEarly()
  291. */
  292. public boolean canFinishEarly() {
  293. return Boolean.valueOf(configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_CAN_FINISH_EARLY)).booleanValue();
  294. }
  295. /* (non-Javadoc)
  296. * @see org.eclipse.ui.wizards.IWizardDescriptor#hasPages()
  297. */
  298. public boolean hasPages() {
  299. String hasPagesString = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_HAS_PAGES);
  300. // default value is true
  301. if (hasPagesString == null) {
  302. return true;
  303. }
  304. return Boolean.valueOf(hasPagesString).booleanValue();
  305. }
  306. public String[] getKeywordLabels() {
  307. if (keywordLabels == null) {
  308. IConfigurationElement[] children = configurationElement
  309. .getChildren(IWorkbenchRegistryConstants.TAG_KEYWORD_REFERENCE);
  310. keywordLabels = new String[children.length];
  311. KeywordRegistry registry = KeywordRegistry.getInstance();
  312. for (int i = 0; i < children.length; i++) {
  313. String id = children[i]
  314. .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
  315. keywordLabels[i] = registry.getKeywordLabel(id);
  316. }
  317. }
  318. return keywordLabels;
  319. }
  320. }