How to get current project from a plugin [message #41941] |
Wed, 21 May 2003 10:05  |
Eclipse User |
|
|
|
Originally posted by: john.c.duncan.cambridge.pfizer.com
How does one get a reference to the current project from within a plugin.
Specifically I would like to determine in which project a wizard was
invoked.
Thanks
|
|
|
Re: How to get current project from a plugin [message #42001 is a reply to message #41941] |
Wed, 21 May 2003 10:25  |
Eclipse User |
|
|
|
Originally posted by: richkulp.NOSPAM.us.ibm.com
Wizards aren't invoked from a project. They are invoked from actions.
Actions may be on the toolbar or a menu or a popup menu. They don't
belong to a project.
It actually depends on the action. If it is an IActionDelegate it is
told what was currently selected when it is executed (actually it is
told before it is executed, and it needs to save it locally). It is the
actions responsibility to give the selection to the Wizard, and the
wizard needs to provide an API for it to be given the selection.
If you can get the selection from the action, and the selection is an
IStructuredSelection, you can query each element of the selection, see
if it is an IResource, or can be adapted through the IAdaptable
interface, to be an IResource, they you can ask each IResource for its
project. However, be aware, there could be more than one project that
results from this query.
However, if there is no way to get the selection from the wizard or the
action, you can always query what the current selection is. Here's an
example from the NewWizardAction in Eclipse: (where window is an
IWorkbenchWindow, usually given to the wizard in some way, again it all
depends upon the wizard and the action).
ISelection selection = window.getSelectionService().getSelection();
IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
if (selection instanceof IStructuredSelection) {
selectionToPass = (IStructuredSelection) selection;
} else {
// Build the selection from the IFile of the editor
IWorkbenchPart part = window.getPartService().getActivePart();
if (part instanceof IEditorPart) {
IEditorInput input = ((IEditorPart) part).getEditorInput();
if (input instanceof IFileEditorInput) {
selectionToPass = new StructuredSelection(((IFileEditorInput)
input).getFile());
}
}
}
Rich
|
|
|
Powered by
FUDForum. Page generated in 0.02909 seconds