Eclipse Plugin: Link MulitPageEditorPart with Navigator - java

In an Eclipse-Plugin, I have an implementation of a MultiPageEditorPart and it is associated with a file-extension - let's call it f.
When I click on a file f in the standard Package Explorer the Editor is opend. This works fine so far.
But what is not working, is linking this Editor with the Package Explorer. Let's say someone has several open Editors and one of them is the custom MultiPageEditorPart. If the custom Editor becomes selected via the tabs (not the Package Explorer) the corresponding file in the Package Explorer should be selected / marked.
Following the example in "link with editor" for FormEditor I tried to achive this using an ILinkHelper and the Extension Point org.eclipse.ui.navigator.LinkHelper, but it is never called.
How is the ILinkHelper used together with a MultiPageEditorPart?
PS: I'm using Eclipse Oxygen

The MulitpageEditorPart (and other Editors) have an IEditorInput which owns a Method
<T> T getAdapter(Class<T> adapter)
If this Method is called with IResource.class or IFile.class it should return the IResource that is the source for the current Editor. This is then used by Eclipse to link the Editor with the View (Package Explorer).
Like this:
<T> T getAdapter(Class<T> adapter) {
if (IResource.class.isAssignableFrom(adapter) {
return (T) myFile;
}
}

Related

Eclipse : Is it possible to open one resource in two different editors as per requirement?

In my current project, there is a situation where we have one file let's say "File1.cfg" in project explorer. There is a default editor "Editor 1" registered using "*.editors" extension.
Requirement Function:
When a user double click on the File1.cfg, it should be opened with an "Editor 1" by default and all the time.
There is one more option provided in Toolbar which will be used to open "Editor 2". And this editor should use the resource "File1.cfg" and display the contents as per the UI.
How can this be achieved in the Eclipse?
A plug-in can specify the editor id to be used when opening an editor. See IWorkbenchPage.openEditor and IDE.openEditor.
Normally these APIs check for an editor (of any id) already being open on the file. If you want to force the editor with the given id to open regardless you need to use the IWorkbenchPage method:
IEditorPart openEditor(IEditorInput input, String editorId, boolean activate,
int matchFlags)
with the matchFlags value set to:
IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT
to only match an existing editor with the same id and input.

How to find which file is open in eclipse editor without using IEditorPart?

I want to know which file (or even project is enough) is opened in eclipse editor? I know we can do this once we get IEditorPart from doSetInput method,
IFile file = ((IFileEditorInput) iEditorPart).getFile();
But I want the name of file without using IEditorPart, how can I do the same?
Checking which is the selected file in project explorer is not of much help because, user can select multiple files at once and open all simultaneously and I did not way to distinguish which file opened at what time.
Adding more info:
I have an editor specified for a particular type of file, now every time it opens, during intializing editor I have some operation to do based on project properties.
While initializing editor, I need the file handle (of the one which user opened/double clicked) or the corresponding project handle.
I have my editor something this way:
public class MyEditor extends TextEditor{
#Override
protected void initializeEditor() {
setSourceViewerConfiguration(new MySourceViewerConfiguration(
CDTUITools.getColorManager(), store,
"MyPartitions", this));
}
//other required methods
#Override
protected void doSetInput(IEditorInput input) throws CoreException {
if(input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput) input).getFile();
}
}
}
as I have done in the doSetInput() method , I want the file handle(even project handle is sufficient). But the problem is in initializeEditor() function there is no reference to editorInput, hence I am unable to get the file handle.
In the source viewer configuration file, I set the code scanners and this needs some project specific information that will set the corresponding rules.
You never have the guarantee (even with IFileEditorInput) to know which files are "open" with editors.
There is even no definition of an "opened file" there are for example editors that show the contents of multiple files (like the Plug-In Manifest Editor from PDE). Other editors show only the contents of a URI (which maybe local)
Could you explain which problem you are trying to solve?

Creating a eclipse plugin with text hover

I would like to make a Eclipse plugin (text editor). I would "read" the text under the cursor and show a dynamical generated hover that depends on the text. Now I have the problem that I don't know how I can read the text and "add" the hover.
It's my first Eclipse Plugin so I am happy for each tip I can get.
Edit:
I'd like to integrate it into the default Eclipse Java editor. I have tried to create a new plugin with a editor template but I think it is the wrong way.
Last Edit:
The answer from PKeidel is exactly what I'm looking for :)
Thanks PKeidel
Your fault is that you created a completly new Editor instead of a plugin for the existing Java Editor. Plugins will be activated via extension points. In your case you have to use org.eclipse.jdt.ui.javaEditorTextHovers more....
<plugin>
<extension
point="org.eclipse.jdt.ui.javaEditorTextHovers">
<hover
activate="true"
class="path.to_your.hoverclass"
id="id.path.to_your.hoverclass">
</hover>
</extension>
</plugin>
The class argument holds the path to your Class that implements IJavaEditorTextHover.
public class LangHover implements IJavaEditorTextHover
{
#Override
public String getHoverInfo(ITextViewer textviewer, IRegion region)
{
if(youWantToShowAOwnHover)
return "Your own hover Text goes here"";
return null; // Shows the default Hover (Java Docs)
}
}
That should do it ;-)

How to programmatically change the selection within package explorer

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.

How to get the selected node in the package explorer from an Eclipse plugin

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.

Categories