Programmatically call menu item with Eclipse JDT, Plug-In Development - java

I've been trying to figure out for a while now how to call an Eclipse menu item from within a plug-in that I'm developing. Say, for the sake of this posting, I want to call the eclipse "Format" menu item in the Right Click Menu to format a source, how would I go about doing this by calling that item (i.e. not just mimicking that menu item's effect, actually calling it)?
I'm not looking for how to format code, just simply the idea of calling a menu item.
I was leaning towards the APIs info on IWorkbench, Shell, ToolBar, ToolItem, etc., but I don't really know. I haven't been able to find anything on this topic in the APIs or anywhere else online. Is there a better approach to doing this rather than programmatically?
Eclipse JDT - http://help.eclipse.org/indigo/index.jsp?nav=%2F3
EDIT:
String commandId = "org.eclipse.jdt.ui.edit.text.java.format";
IHandlerService handlerService = (IHandlerService)(IHandlerService ) PlatformUI.getWorkbench().getService(IHandlerService.class);
handlerService.executeCommand(commandId, null);
Simple as that. The hardest part is finding the commandId, which can be easily searched for, as rlegendi mentioned. This is exactly what I wanted. It accesses that plugin via ID, then executes it. Simple and effective. Thanks everyone!

I'm not sure but probably what you want to read about is the Command/Delegate framework.
BTW if you install the Eclipse Platform SDK plugin (available by default), you can actually take a look on the source code of any plugin that is the part of your current Eclipse product (try Ctrl+Shift+T and typing ISourceViewer for instance, there you find the FORMAT it, for which you can do a search with Ctrl+Shift+G), and you can import any of those plugin projects to your own workspace to examine.

Related

Context sensitive menu in eclipse editor

If I use eclipse and right click on a part of Java source code, the context menu depends on the syntactic element the user clicks on (like: a method, a variable,...).
How can I implement this kind of behaviour in my own eclipse plugin, e.g. add an item to the context menu only if the user clicks on a method in Java source code. Furthermore, the plugin needs to know which method the user clicks on.
I guess I have to relate the clicking position to the abstract syntax tree that eclipse builds but I have no idea how to do that.
You do this with Eclipse Commands and you can enable them and change their visibility in many ways, depending on selection, global state, etc.
I guess I have to relate the clicking position to the abstract syntax
tree that eclipse builds but I have no idea how to do that.
The good news is you (generally) don't have to do that, Eclipse has done all that heavy lifting for you.
It is quite a broad subject, so I suggest working through some tutorials on Commands first:
Adding menus, toolbar and popup menus to the Eclipse IDE - Tutorial
Eclipse Commands Advanced - Tutorial

Eclipse Plugin Development: Extending default Java Editor/Text Hover

I've been trying develop my custom plugin for Eclipse, and basically I want to make is a "richer" version on the current TextHover. I don't know what widget(?) Eclipse uses to display the hovering text, but I want to use something different, like SWT Image or SWT Browser.
Most of the tutorials that I've read suggest that I have to implement my own Java Editor to do this, but I don't want the user to switch to my custom editor just for a simple feature (and I don't want to implement a whole editor).
Some Tests:
I've already created two Eclipse Plugin Projects. The first one is a extension for the JavaEditorTextHovers, and with this project I managed to show some custom Strings when hovering some random texts, but wasn't able to change the hover appearance. The second project was a editor plugin. With this last one I managed to get a Browser to appear when hovering a random text(this tutorial helped me), but again, this editor had nothing, no syntax coloring, no rules, etc., and for the previous reasons, I couldn't accept this has a solution.
Maybe if there was way to change the (or set a new) SourceViewerConfiguration of the current editor I could pass my custom SourceViewerConfiguration, but I'm not sure if this is possible.

RCP Start the product with Clear Workspace

So I've come across this weird bug in RCP Apps.
I've created a new RCP App with the Mail Template. I've added a new org.eclipse.ui.menus extension with a menuContribution with locationURI:toolbar:org.eclipse.ui.main.toolbar. To that, I've added a command with a little icon.
Now, if I start the app without Clear Workspace in the Debug Configurations, my action appears AFTER the Quick Access text widget. Not only that, but a few other bugs come along (e.g. views aren't closing, too many views are opening at once). This is not a one-time thing, i.e. I have to start the app with Clear Workspace each time.
This method of adding actions on the coolBar is non-deprecated. Why does it behave like a spoiled brat?
Without Clear Workspace:
With Clear Workspace:
There is a long discussion on the Eclipse forums about this here with a bug filed as a result here.
The bug is not scheduled to be fixed until Eclipse 4.4M5 but using one of the workarounds to remove the Quick Access control discussed here should help.

Reusing Eclipse Run menu in Eclipse RCP application

I want to create an eclipse RCP application for a custom language. A programs which is written using the cutom language should be run and show it's output when the user click on the Run button.
Is it possible to integrate eclipse Run menu in eclipse RCP application with it's default features as we create NEW menu item by using ActionFactory? If it is possible how to do that?
Thanks in advance.
If you "just" want the Run menu along with the default entries, just include a dependency to org.eclipse.debug.ui.
You will then need to add launch configuration types for your specific language using the various extension point from the plugins org.eclipse.debug.core and org.eclipse.debug.ui.
As always, when it comes to the more advanced functionality of Eclipse, the easiest way to get access to the functionality is via resources and examples. Although it is a bit dated, the article "We Have Lift-off" (http://www.eclipse.org/articles/Article-Launch-Framework/launch.html) is the best starting point for this. Have a look at the various references to org.eclipse.debug.core.launchConfigurationTypes to find the best example to use asa starting point - in particular the ANT Build stuff as this is pretty simple...
In general you can find the plug-in that contributes a specific entry using the PDE Menu Spy (Alt-Shift-F2 on MacOS)...

Something like Spy++ for SWT?

I am looking for something like Spy++ but for SWT. I know there are solutions that help you out when you want info about Eclipse's plugins, so I guess this must be somewhat possible.
In my specific case, I'd need a way to Spy on code other tha my own (like Spy++ allows you to), so I guess Eclipse's plugins aren't going to help me out here!
Thanks
You mean, something like SWT Spy?
Getting the exact code back from UI elements is kind of tough and as far as I know there are no tools in SWT domain which can do this.The reason that you can not do that is because the end result of some code i.e UI might be generated because of some if-elseif-else conditions. For example:
if(userSelection)
{
createSpecialToolsComposite(composite);
}else {
createSimpleToolBar(composite);
}
In the above snippet only one of the child control will be created. To infer the other one is hard.
But still there are tools which will allow you to place your mouse over a widget and get information about that widget, including:
Layout information
Bounds
Siblings
Parent Chain (back to the shell)
SWT SPY
Is there a SWT debugger/spy?
http://eclipsesource.com/blogs/2010/01/07/i-see-you-swt-spy/
SWT Spy is the correct tool to use. However, the SWT Spy page is not updated.
From Eclipse 4.7 SWT Spy is included in Eclipse PDE. So you can get it by downloading the Eclipse for RCP and RAP.
To launch SWT Spy, press: CTRL + ALT + SHIFT + F9
In case of doubts, please see: http://www.vogella.com/tutorials/EclipseCodeAccess/article.html#swt-spy

Categories

Resources