Listener on open file in Eclipse - eclipse

I am currently working on a Eclipse Plugin, where I need to make an action, when a person opens a file with certain properties. However I'm not sure on how to set a listener, I have been looking into the IWorkspace and IResource API, but I can't find the simple API call saying "AddListenerToOnOpenFile".
The file is expected to be opened in the package explorer view.

Use the answer supplied by #MarttiKäärik to find out when editors are open. Then you can use the IEditorInput to see if it is an IResource you care about.
if (part instanceof IEditorPart) {
IEditorPart editor = (IEditorPart) part;
IResource resource = editor.getEditorInput().getAdapter(IResource.class);
// ...
}

Question already answered, so only to make it a bit more complete...
You don't necessarily have to implement a view or action (as described in the question linked to by Martti Käärik in a comment) to get a window for your listener. Call to PlatformUI.getWorkbench().get...() can be used as well. See the older, probably duplicate, question called just Eclipse Plugin.
BTW Eclipse Wiki FAQ page contains a good description of the ways how to obtain the current workbench window and possible "gotchas".
Moreover, you can even listen for newly opened windows if there is a need:
PlatformUI.getWorkbench().addWindowListener(listener);

Related

Show error marker for external resource

I have written an eclipse editor that performs syntax checking and if it finds an error, it will display it in the editor.
Currently I am doing this by creating a marker in the respective IResource and that is working well until the user uses the File -> Open File... option in order to open a non-project file with that editor.
The problem is that the EditorInput is no longer an IFileEditorInput (it's a FileStoreEditorInput) that allowed me to retrieve the connected IResource via getFile() (on which I can add the marker via createMarker()). In fact the core problem is that the opned file isn't an IResource at all (at least that is what I'm guessing as it is not in the eclipse workspace).
Is there another way of showing the error markers in the editor? It doesn't need to be savable or anything... Just a way to tell the editor to create the same markers in the source code as if there were some IMarker attached to the opened IResource.
Okay the thing was that I was actually looking for the creation of an Annotation in the AnnotationModel of the DocumentProvider. There it could be added via addAnnotation.
That approachs works fine. However I haven't found a list of available annotation types (as there are for Markers), so it's a little tricky to actually get the proper annotation...

How can I access which files the user currently has open from an eclipse plugin?

I'm very new to eclipse plugin development, and frankly to eclipse itself. I am trying to find a way to access a list of which files are currently being worked on by the user, and possibly even more specifically, what part of these files (which class, method, block of code, etc.).
I am thinking that I would like to have the plugin grab information on which files are currently open in the tabs, and then go from there, but I can't even figure out how to do this. I've been searching the eclipse documentation at help.eclipse.org, but I still haven't found anything useful for what I want to do. Does anyone have any ideas?
Try the following code:
IWorkbenchPage[] pages = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getPages();
for (IWorkbenchPage page : pages) {
IEditorReference[] references = page.getEditorReferences();
for (IEditorReference reference : references) {
IEditorInput input = reference.getEditorInput();
}
}
You should be able to walk your way from the workbench to a workbench page (IWorkbenchPage), which as a findEditors method.

Eclipse RCP StatusManager - how to change the title

I am trying to use StatusManager to display and/or log messages. Not necessarily bad and nasty problems, but also info's or warnings. This is what I basically do:
Status status= new Status(IStatus.INFO, MainPlugin.ID, "Some info message");
StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
Works well, but I can't find the way to modify the default title of the popup dialog - it always says "Problem Occurred". Going through the maze of eclipse code brought me here to ask this question because I am totally lost... Any ideas?
Unfortunately setting dialog title is not possible now. The dialog is generated based on IStatus object when you give a proper flag (such as StatusManager.SHOW), but the interface does not define a title. To allow what you want, the IStatus interface should be extended.
The same problem occurs in Eclipse 4 (there is a new StatusReporter class).
You can follow the bug Bug 386023 which postulates exactly what you want (on Eclipse 4 platform).

adding python interpreters programmatically

Is there a simple way to add and change interpreters using the Pydev plugin interface? I'm running pydev 1.6.1 and I'd like to be able to add and use a given interpreter based on a list of available interpreters in my environment.
Right now I can see the PythonInterpreterManager has a createInterpreterInfo call, but that doesn't seem to do anything. Looking at the source for pydev, it seems like I have to actually work with the preference pages to keep track of all of them.
Is there a simpler set of functions I can call to add these?
PythonInterpreterManager manager = (PythonInterpreterManager)PydevPlugin.getPythonInterpreterManager(true);
IInterpreterInfo info = manager.createInterpreterInfo(execPath, new NullProgressMonitor());
manager.addInterpreterInfo(info);
I can already do the above, but that only caches it, but doesn't display it as a valid interpreter option in the preferences.
I've even tried doing reflection to add these without much luck. I can call addNewInput on the editor as the Add button does, but then it says it doesn't have any knowledge of that interpreter. I've tried creating a popup preferences page and adding the values to the various members via reflection as getNewInput() would, but still don't see any more options in the preference page. I'm not sure if this is because I'm missing something or the popup preference page I make is totally unrelated to the page that pops up using the Window->"Preferences" pulldown.
The API is something as:
IInterpreterManager iMan = PydevPlugin.getPythonInterpreterManager(true);
IInterpreterInfo interpreterInfo = iMan.createInterpreterInfo("c:/python/python.exe", monitor, false);
iMan.setInfos(new IInterpreterInfo[]{interpreterInfo}, null, null);
Note that if you have 'manager.addInterpreterInfo' in there, you probably have an old version of PyDev... (and at that call you set all the interpreters available, so, if you want to keep some configuration, you should query it and add them back).
You can use: org.python.pydev.editor.codecompletion.revisited.javaintegration.AbstractWorkbenchTestCase.createPythonInterpreterManager(NullProgressMonitor) as a reference.

How do I get a view part instance in an Eclipse e4 application?

I'm trying to get the instance of a view part in an Eclipse e4 application but I can't find the PlatformUI class. Has the name changed since Eclipse 3 or is it located in a different package?
When looking at Eclipse e4 Parts:
bugs like 371405 can be instructive:
org.eclipse.ui.presentations
This API no longer works in 4.2, and we never intend to make it work.
It is incompatible with the pluggable rendering story in 4.2. Decisions that could once be made by the presentation extensions are now up to the renderer.
Affected API that needs deprecation:
Entire API package: org.eclipse.ui.presentations
Extension point:org.eclipse.ui.presentationFactories
org.eclipse.ui.IWorkbenchPreferenceConstants#PRESENTATION_FACTORY_ID
org.eclipse.ui.IWorkbenchWindowConfigurer#getPresentationFactory
org.eclipse.ui.IWorkbenchWindowConfigurer#setPresentationFactory
The rest of the Tutorial explains how to declare "parts" (editors or views)
The OP August Karlstrom mentions:
This used to work:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("‌​some view");
Using a singleton like PlatformUI is a bad practice and one of the reason of the introduction, in e4, of Context. See this presentation on Context.
Paul Webster (IBM Eclipse Platform team member) comments:
In Eclipse4 you would use org.eclipse.e4.ui.workbench.modeling.EPartService.findPart(String) to find an MPart by ID.
The MPart contains the injected part in its object property.
As the page Workbench_Services details:
In e4, the notion of a workbench page will not be present.
The part service API will essentially be a merge of the existing 3.x IPartService and WorkbenchPage interfaces.
Note that this isn't ideal, as bug 372488 illustrates (following this thread):
An MPart for an MPartDescriptor is created with EPartService.createPart(descriptor_id), where descriptor_id is the identifier of the MPartDescriptor.
This part can be found again with EPartService.findPart(descriptor_id) -- if there is only one.
The problem is, that one may need do create more than one MPart for one MPartDescriptor.
An editor may be one example: one may want to edit different instances of one and the same kind.
The creation of more than one MPart for a given MPartDescriptor is possible, but there is no convenient method to find these parts.
EPartService.findPart(descriptor_id) will return the first MPart created for a particular MPartDescriptor, even if there is more than one.
So there are three problems, for a given MPartDescriptor:
EPartService.findPart(id) does not tell that there is more than one MPart.
There is no convenient way to get all MParts for this descriptor.
There is no API-way to get the particular MPart for given descriptor and "content" or "reference".
Currently the way to go is using EPartService.getParts() which unfortunately
returns all MParts, not only those corresponding to one particular
MPartDescriptor.
Then one would need to check, whether there is one MPart for the particular MPartDescriptor having a particular "content".
So something is missing that will find an MPart for a given MPartDescriptor
with particular "content" or "reference".
Just have the same question. After found this thread and tried with:
MPart mPart = epartService.findPart("MyPart");
MyPart myPart = (MyPart)mPart.getObject();
then I got my view part.