2009-07-30 27 views

Respuesta

25

Este GMF class tiene una respuesta directa, si tiene acceso al nombre ResourcesPlugin:

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(myBundleName); 

La respuesta genérica (de una potentially outdated code) podría ser como (si usted tiene un editor abierto):

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 
} 

Si se abre ningún editor:

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(); 
+0

Para GMF cambiar la parte MyPlugin a:.... 'IViewPart [] = {partes yourPluginName} DiagramEditorPlugin.getInstance() getWorkbench() getActiveWorkbenchWindow() getActivePage() getViews();' Su nombre de plugin GMF se puede encontrar en su modelo .gen. – Ring

7

Usando el servicio de selección le dará el objeto seleccionado en ese momento, luego puede verificar el tipo de selección y obtener el proyecto según la selección.

Si crea un ISelectionListener y se registra como oyente en el ISelectionService, se le notificará siempre que la selección activa cambie, y se le dará una referencia a la selección y la parte propietaria.

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(); 
    } 
} 

Consulte este article on the Eclipse selection service para obtener más información.

+0

Probablemente una respuesta más precisa que la mía. +1 – VonC

+1

+1 pero mejor use PlatformUI.getWorkbench() en lugar de Workbench.getInstance(). –

2

Rich Seller y VonC han brindado buenas respuestas pero no parecían lo suficientemente completas/usaban clases internas. He venido con lo siguiente.

@Override 
public void createPartControl(Composite parent) 
{ 
    .... 

    // Register to be notified about selections 
    getSite().getWorkbenchWindow().getSelectionService() 
     .addPostSelectionListener(this); 

    // Detect the current selection 
    detectCurrentSelection(); 
} 

@Override 
public void selectionChanged(IWorkbenchPart part, ISelection selection) 
{ 
    IProject project = convertSelection(part, selection); 
    setProject(project); 
} 

private void detectCurrentSelection() 
{ 
    IProject project = null; 

    IWorkbenchWindow window = getSite().getWorkbenchWindow(); 
    IWorkbenchPage activePage = window.getActivePage(); 

    if (activePage != null) 
    { 
    IEditorPart part = activePage.getActiveEditor(); 

    if (part != null) 
    { 
     project = convertSelection(part); 
    } 
    else 
    { 
     IViewReference[] viewReferences = activePage.getViewReferences(); 

     for (IViewReference viewRef : viewReferences) 
     { 
     IViewPart view = viewRef.getView(false); 
     ISelection selection = null; 

     if (view instanceof IPackagesViewPart) 
     { 
      IPackagesViewPart viewPart = (IPackagesViewPart) view; 
      TreeViewer treeViewer = viewPart.getTreeViewer(); 
      selection = treeViewer.getSelection(); 
     } 
     else if (view instanceof CommonNavigator) 
     { 
      CommonNavigator navigator = (CommonNavigator) view; 
      CommonViewer commonViewer = navigator.getCommonViewer(); 
      selection = commonViewer.getSelection(); 
     } 

     if (selection instanceof IStructuredSelection) 
     { 
      IStructuredSelection structuredSelection = (IStructuredSelection) selection; 

      project = convertSelection(structuredSelection); 

      if (project != null) 
      break; 
     } 
     } 
    } 
    } 

    setProject(project); 
} 

private IProject convertSelection(IWorkbenchPart part, ISelection selection) 
{ 
    IProject project = null; 
    if (selection instanceof IStructuredSelection) 
    { 
    IStructuredSelection structuredSelection = (IStructuredSelection) selection; 
    project = convertSelection(structuredSelection); 
    } 
    else if (selection instanceof ITextSelection) 
    { 
    if (part instanceof IEditorPart) 
    { 
     IEditorPart editorPart = (IEditorPart) part; 
     IResource resource = (IResource)editorPart.getEditorInput().getAdapter(IResource.class); 
     if (resource != null) 
     { 
     project = resource.getProject(); 
     } 
    } 
    } 

    return project; 
} 

private IProject convertSelection(IEditorPart part) 
{ 
    IProject project = null; 
    IResource resource = (IResource)part.getEditorInput().getAdapter(IResource.class); 
    if (resource != null) 
    { 
    project = resource.getProject(); 
    } 
    return project; 
} 

private IProject convertSelection(IStructuredSelection structuredSelection) 
{ 
    IProject project = null; 
    Object element = structuredSelection.getFirstElement(); 

    if (element instanceof IResource) 
    { 
    project = ((IResource) element).getProject(); 
    } 
    else if (element instanceof IJavaElement) 
    { 
    IJavaElement javaElement = (IJavaElement) element; 
    project = javaElement.getJavaProject().getProject(); 
    } 
    else if (element instanceof IAdaptable) 
    { 
    IAdaptable adaptable = (IAdaptable) element; 
    IWorkbenchAdapter adapter = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class); 
    if (adapter != null) 
    { 
     Object parent = adapter.getParent(adaptable); 
     if (parent instanceof IJavaProject) 
     { 
     IJavaProject javaProject = (IJavaProject) parent; 
     project = javaProject.getProject(); 
     } 
    } 
    } 

    return project; 
} 
5

He creado una buena función de las publicaciones anteriores, además he agregado algunas actualizaciones. Funciona con el eclipse actual.

public static IProject getCurrentSelectedProject() { 
    IProject project = null; 
    ISelectionService selectionService = 
     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService(); 

    ISelection selection = selectionService.getSelection(); 

    if(selection instanceof IStructuredSelection) { 
     Object element = ((IStructuredSelection)selection).getFirstElement(); 

     if (element instanceof IResource) { 
      project= ((IResource)element).getProject(); 
     } else if (element instanceof PackageFragmentRoot) { 
      IJavaProject jProject = 
       ((PackageFragmentRoot)element).getJavaProject(); 
      project = jProject.getProject(); 
     } else if (element instanceof IJavaElement) { 
      IJavaProject jProject= ((IJavaElement)element).getJavaProject(); 
      project = jProject.getProject(); 
     } 
    } 
    return project; 
} 
Cuestiones relacionadas