I am currently having all project names in my workspace like this.
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
Is there a way to get project name which currently open in the editor or selected one without using ISelection or IStructuredSelection.
Eclipse has no concept of a 'current project' or anything like that.
You either have to use the selection service to find out the current selection in a view such as 'Project Explorer' or you look at the active editor and see what that is editing (see for example here)
Found one way
private IFile getFullPath(URI uri)
{
String platformstring = uri.toPlatformString(true);
IFile f = (IFile)ResourcesPlugin.getWorkspace().getRoot().findMember(platformstring);
return f;
}
//and then call
IFile projectFile = getFullPath(uri);
String projectName = projectFile.getProject().getName();
Related
I'm currently writing an eclipse plugin, and in it, I have a new project creation and a new file creation wizard.
In the new project wizard, I create it, so I have no problem getting it and creating new files in it. (like creating the Main class for your project)
But when I'm in my New file wizard, I have literally no idea how to select the right project, and I would like some help please.
Since it is a wizard, I would like to avoid needing an opened editor, and since it's a new wizard, it doesn't have a handler so I can't get it from there...
Thank you in advance,
Cordially,
Okay, I looked at it from another approach and it feels quite stupid right now.
When you initialize your wizard, you get an init method that contains the workbench and the selection, so you can keep it.
private IWorkbench wb;
private IStructuredSelection sel;
#Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
wb = workbench;
sel = selection;
}
Then, I had found a code snippet earlier on Eclipse website, linked under here, that I had to change a bit, and it does what I want it to do.
// Get selected resource (can get project from it)
// https://wiki.eclipse.org/FAQ_How_do_I_access_the_active_project%3F
private IResource extractSelection() {
Object element = sel.getFirstElement();
if (element instanceof IResource)
return (IResource) element;
if (!(element instanceof IAdaptable))
return null;
IAdaptable adaptable = (IAdaptable)element;
Object adapter = adaptable.getAdapter(IResource.class);
return (IResource) adapter;
}
With this, I can get my project in my wizard by doing
IProject project = extractSelection().getProject();
In my eclipse plugin I need to get the selection in the package explorer.
I found out that this works like this:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelectionService service = window.getSelectionService();
IStructuredSelection structured = (IStructuredSelection) service.getSelection("org.eclipse.jdt.ui.PackageExplorer");
Object selection = structured.getFirstElement();
This works fine in 99% of all cases but I recently ran into a case where the getSelection("org.eclipse.jdt.ui.PackageExplorer"); returns null although I can clearly see that I have something selected in the package explorer...
How can that be?
Check that the view is not the Project Explorer rather than Package Explorer. They can look very similar and both default to appearing in the same place.
Can anyone please tell me how to get the current or active working set and also to get all the projects in the working set in IProject type? I am trying to achieve it by using the below code -
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
IWorkingSetManager manager = window.getWorkbench().getWorkingSetManager();
IWorkingSet[] workingSets = manager.getWorkingSets();
This will get me all the working sets that are present in eclipse? The IWorkingSet.getElements() provides all the project details in the IAdaptable type which I cannot cast into IProject?
Thank You in Advance!
You convert an IAdaptable value to an IProject using:
IProject project = (IProject)adaptable.getAdapter(IProject.class);
I am not sure if the working set element provides an adapter directly to IProject, you might need to use:
IResource resource = (IResource)adaptable.getAdapter(IResources.class);
if (resource instanceof IProject)
{
}
I am currently developing a plugin for eclipse that analyzes dependencies and references between projects within the Eclipse Workspace and displays them in its own View in a UML-like diagram.
To increase the usefulness of my plugin, I wish to add interactivity to the diagram by allowing users to open a project in the package explorer and if applicable open it in an editor by clicking on the graph displayed.
However, my problem is that while I know how to obtain a given selection from the package explorer, I have not been able to find a way to change the selection or simply open up a project in the package explorer programmatically.
Does anyone have a solution for this problem?
This answer extends what the accepted answer states but takes it further for folks who mind the "Discouraged Access" warning on the use of PackageExplorerPart.
Exact warning (more for easier searching off Google) that you see is
Discouraged access: The type PackageExplorerPart is not accessible due
to restriction on required library
/eclipse_install_path/eclipse/plugins/org.eclipse.jdt.ui_3.9.1.v20130820-1427.jar
Code Sample:
final IWorkbenchPart activePart = getActivePart();
if (activePart != null && activePart instanceof IPackagesViewPart) {
((IPackagesViewPart) activePart).selectAndReveal(newElement);
}
Supporting Code:
private IWorkbenchPart getActivePart() {
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
if (activeWindow != null) {
final IWorkbenchPage activePage = activeWindow.getActivePage();
if (activePage != null) {
return activePage.getActivePart();
}
}
return null;
}
I have found the solution. Eclipse does offer direct access to the package explorer in org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart , but it is discouraged.
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
...
PackageExplorerPart part= PackageExplorerPart.getFromActivePerspective();
IResource resource = /*any IResource to be selected in the explorer*/;
part.selectAndReveal(resource);
This will highlight whatever IResource resource is and expand the tree as necessary.
I'm writing an Eclipse command plugin and want to retrieve the currently selected node in the package explorer view. I want to be able to get the absolute filepath, where the selected node resides on the filesystem (i.e. c:\eclipse\test.html), from the returned result.
How do I do this ?
The first step is to get a selection service, e.g. from any view or editor like this:
ISelectionService service = getSite().getWorkbenchWindow()
.getSelectionService();
Or, as VonC wrote, you could get it via the PlatformUI, if you are neither in a view or an editor.
Then, get the selection for the Package Explorer and cast it to an IStructuredSelection:
IStructuredSelection structured = (IStructuredSelection) service
.getSelection("org.eclipse.jdt.ui.PackageExplorer");
From that, you can get your selected IFile:
IFile file = (IFile) structured.getFirstElement();
Now to get the full path, you will have to get the location for the IFile:
IPath path = file.getLocation();
Which you then can finally use to get the real full path to your file (among other things):
System.out.println(path.toPortableString());
You can find more information on the selection service here: Using the Selection Service.
The code would be like:
IWorkbenchWindow window =
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
You view an example in an Action like this LuaFileWizardAction class.