How do I get the name of the current eclipse project? I'm in a GMF view and need the projectname when some submenu of the popup menu ist used.
+3
A:
This GMF class has a straightforward answer, if you have access to the ResourcesPlugin name:
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(myBundleName);
The generic answer (from a potentially outdated code) could be like (if you have an editor opened):
IEditorPart editorPart =
getSite().getWorkbenchWindow().getActivePage().getActiveEditor();
if(editorPart != null)
{
IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ;
IFile file = input.getFile();
IProject activeProject = file.getProject();
String activeProjectName = activeProject.getName();
//... use activeProjectName
}
If no editor is opened:
IViewPart [] parts =
MyPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews();
IProject activeProject = null;
for(int i=0;i<parts.length;i++)
{
if(parts[i] instanceof ResourceNavigator)
{
ResourceNavigator navigator = (ResourceNavigator)parts[i];
StructuredSelection sel =
(StructuredSelection)navigator.getTreeViewer().getSelection();
IResource resource = (IResource)sel.getFirstElement();
activeProject = resource.getProject();
break;
}
}
String activeProjectName = activeProject .getName();
VonC
2009-07-30 11:53:49
+3
A:
Using the selection service will give you the currently selected object, you can then check the selection type and get the project based on the selection.
If you create an ISelectionListener and register as a listener on the ISelectionService, you'll be notified whenever the active selection changes, and be given a reference to the selection and the owning part.
ISelectionListener listener = new ISelectionListener() {
public void selectionChanged(IWorkbenchPart sourcePart, ISelection selection) {
setSourcePart(sourcePart);
setSelection(selection);
}
};
...
//register the listener
selectionService.addSelectionListener(listener);
...
//either get the selection service and get the selection, or use the stored version from the listener
ISelectionService selectionService =
Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement();
IProject project;
if (element instanceof IResource) {
project= ((IResource)element).getProject();
} else if (element instanceof PackageFragmentRootContainer) {
IJavaProject jProject =
((PackageFragmentRootContainer)element).getJavaProject();
project = jProject.getProject();
} else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}
} else if (selection instanceof ITextSelection) {
if(sourcePart instanceof JavaEditor) {
IJavaElement element = SelectionConverter.resolveEnclosingElement(sourcePart, selection);
project = element.getJavaProject().getProject();
}
}
See this article on the Eclipse selection service for more details.
Rich Seller
2009-07-30 12:20:12
Probably a more accurate answer than mine. +1
VonC
2009-07-30 12:28:01