If you want to control some actions based on the selection of an element from the project explorer, we can register a post selection changes through service registry.
//Register listener
ISelectionService ss = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
ProjectExplorerSelectionListener selectionListener = new ProjectExplorerSelectionListener();
ss.addPostSelectionListener(IPageLayout.ID_PROJECT_EXPLORER, selectionListener);
//Listener
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
{
public void selectionChanged(IWorkbenchPart part, ISelection newSelection)
{
if (newSelection instanceof IStructuredSelection)
{
Object element = ((IStructuredSelection) newSelection).getFirstElement();
if (element instanceof IAdaptable)
{
IResource resource = (IResource) ((IAdaptable) element).getAdapter(IResource.class);
final IProject project = resource.getProject();
//do your action here!!
}}
}
No comments:
Post a Comment