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.
Related
I'm making an Eclipse plug-in that requires access to code written in the Eclipse editor. I've followed the process mentioned in the link.
Accessing Eclipse editor code
But it's showing filepath instead of code in the message box. The getEditorInput() of IEditorEditor class isn't doing what it should do according to the link. Here's my code. Please help me find what i'm doing wrong.
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
IEditorInput input = (IEditorInput) editor.getEditorInput(); // accessing code from eclipse editor
String code = input.toString();
MessageDialog.openInformation(
window.getShell(),
"Project",
code);
return null;
}
And here's snap of the output.
You can do this two different ways. This way works regardless of whether the contents are backed by a file on disk or not.
This method gets the text IDocument from the editor, which is how the contents are stored and accessed for most uses. StyledText is a widget, and unless you are doing something with Widgets and Controls, it's not the right way in. For that you're going to go from the editor part, through the ITextEditor interface, and then use the IDocumentProvider with the current editor input. This is skipping the instanceof check you'd want to do beforehand, as well as anything you might have to do if this is a page in a MultiPageEditorPart (there's no standard way for handling those).
org.eclipse.jface.text.IDocument document =
((org.eclipse.ui.texteditor.ITextEditor)editor).
getDocumentProvider().
getDocument(input);
You can get, and modify, the contents through the IDocument.
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 trying to develop a simple Eclipse plugin to understand how it works.
I have two questions about this:
How can I get the content of the active editor?
Do you have a good documentation about life cycle plugin and co? I can't find real good documentation on Google.
Tonny Madsen's answer is fine, but perhaps a little more transparent (getAdapter() is very opaque) is something like:
public String getCurrentEditorContent() {
final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (!(editor instanceof ITextEditor)) return null;
ITextEditor ite = (ITextEditor)editor;
IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
return doc.get();
}
Regarding the content of the current editor, there are several ways to do this. The following code is not tested:
public String getCurrentEditorContent() {
final IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (activeEditor == null)
return null;
final IDocument doc = (IDocument) activeEditor.getAdapter(IDocument.class);
if (doc == null) return null;
return doc.get();
}
I'm assuming you're already familiar with using Eclipse as an IDE.
Create a new plug-in project using the New Plug-in Project Wizard.
On the Templates panel, choose "Plug-in with an editor"
Read the generated code
If you're serious about writing Eclipse plug-ins, the book, "Eclipse Plug-ins" by Eric Clayberg and Dan Rubel is invaluable. I couldn't make sense of the eclipse.org write-ups until after I read the book.