Adapting working Eclipse JDT plugin to Eclipse C++ - plugins

I've developed plugin for Eclipse. It includes property page, several actions and specific view. All these features were added by specific extension points:
<page
class="MyPropertyPage"
id="MyPage1"
name="My Project"
nameFilter="*"
selectionFilter="single">
<enabledWhen>
<instanceof
value="org.eclipse.core.resources.IProject">
</instanceof>
</enabledWhen>
</page>
<page
class="MyPropertyPage"
id="MyPage2"
name="My Project"
nameFilter="*"
selectionFilter="single">
<enabledWhen>
<instanceof
value="org.eclipse.cdt.internal.core.model.CProject">
</instanceof>
</enabledWhen>
</page>
The action command class contents code:
if (obj instanceof IJavaProject) {
...
} else if (obj instanceof CompilationUnit) {
...
} else if (obj instanceof TranslationUnit) {
...
}
In debug mode, when the second eclipse is opened with C++ perspective, it works perfectly. But in runtime, it fails with error java.lang.NoClassDefFoundError: org/eclipse/cdt/internal/ui/cview/CView.
The attempt to execute my action command fails as well with similar error java.lang.NoClassDefFoundError: org/eclipse/cdt/internal/core/model/TranslationUnit.
Help to overcome the error?
Thanks a lot in advance.

Related

How to make a custom Eclipse run launcher that launches a particular Class?

I am developing an eclipse plugin. I want to build a custom run launcher that would be executed when i use it against a specific class. So the scenario is, when i run the plugin via eclipse run time environment i want to run a particular class which is already written in this particular eclipse run time. So i will execute this class with my custom run launcher. For now, i do not need any tab or custom UI. I just need to show the output of that particular class in default java console where normally outputs are shown in eclipse. I did not find great stuff in this regard. As i am new so i am rather getting confused more. Please have a look on what i have tried so far. I am not using org.eclipse.debug.core.launchConfigurationTypes extension point. Rather i am using ILaunchShortcut in this case. So i tried to call that particular class and execute it in launch method using the below code.
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy wc = type.newInstance(null, "SampleConfig");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "Test");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "Test1");
ILaunchConfiguration config = wc.doSave();
config.launch(ILaunchManager.RUN_MODE, null);
But here the problem is i am not able to resolve IJavaLaunchConfigurationConstants in my code anyway. So i am fully stuck here. See my plugin.xml file also for your convenience.
<plugin>
<extension
point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
class="launcher.LaunchShortcut"
id="launcher.shortcut2"
label="Launcher Test"
modes="run">
<contextualLaunch>
<contextLabel mode="run" label="Run Launcher" />
<enablement>
<with
variable="selection">
<count
value="1">
</count>
<iterate>
<adapt type="org.eclipse.core.resources.IResource">
<and>
<test property="org.eclipse.core.resources.name" value="Test1.java"/>
</and>
</adapt>
</iterate>
</with>
</enablement>
</contextualLaunch>
</shortcut>
</extension>
</plugin>
What should i do now to make this code successfully running? I need your suggestions and references. Thanks.
As per comment of greg-449 i added following the extension org.eclipse.jdt.launching.classpathProviders in the plugin.xml and the code worked perfectly.

Trouble deciding how to add popup menu to eclipse plugin using either action ObjectContributions or command handlers

The issue is this: Most things I read on the web says that we should avoid actions when creating popup menus because the command framework allows for more decoupling.
Example: http://wiki.eclipse.org/FAQ_What_is_the_difference_between_a_command_and_an_action%3F
Fair enough.
But I am having a heck of a time getting the command framework to add my menu when I right click on a .java file inside the editor, and only when I click inside the editor. I can get the menu to show (using 'with' and the activeEditor variable), but it also shows when I right click on the java file inside the package explorer, which I don't want. I suspect it's because the file is already open inside the editor.
I also have a menu that gets added when I right click on the java file inside the package explorer, using IComplilationUnit. That works fine.
So my problem is solved using a popup menu action for when I click inside the file. I also get access to to all the ISelection stuff there. But the coupling is to high and I lose the flexability of using handlers.
I'm looking for either:
Tell me I'm doing it the only way possible; or
Tell me how to have my popup appear only when I right click on the java file editor.
Regards
In the end it was really straight forward. The example below uses the command framework. It doesn't have the handlers, so just click the class hyperlink whenever you need a class generated.
Create a new eclipse plugin project called com.test.plugin.project
In the dependency tab of the plugin.xml file add the following dependencies
org.eclipse.jdt
org.eclipse.jdt.core
org.eclipse.jdt.ui
org.eclipse.jface.text
Put this in the plugin.xml tab section:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="popup:org.eclipse.ui.popup.any">
<menu
label="Test Project Sub Menu">
<command
commandId="com.test.plugin.project.command.packageexplorer"
id="packageexplorerId"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="org.eclipse.jdt.internal.core.CompilationUnit">
</instanceof>
</iterate>
</visibleWhen>
</command>
<command
commandId="com.test.plugin.project.command.classfile"
id="classfileId"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<and>
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
<with
variable="activeEditorId">
<equals
value="org.eclipse.jdt.ui.CompilationUnitEditor">
</equals>
</with>
</and>
</iterate>
</visibleWhen>
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
id="com.test.plugin.project.command.packageexplorer"
name="Only Show In Package Explorer">
</command>
<command
id="com.test.plugin.project.command.classfile"
name="Only Show In Class File">
</command>
</extension>
</plugin>
What this does
It creates one popup menu in a sub menu when you right click on a java file inside the package explorer (and only when you click on a java file).
It create a different popup menu in a sub menu when you right click on a java file.

Call JUnit from other Eclipse Plugin

I am developing for Eclipse, and one feature is run JUnit tests. Now my plugin detect JUnit tests on a project in workspace and after this I want to call JUnit to run this tests.
I heard about ILaunch, ILaunchConfigurationDelegate, JUnitLaunchConfigurationDelegate but I cannot found an example of this and also I'm not sure if I have to use this!
-- Thanks in advance
Please see my answer to How does Eclipse actually run Junit tests?. You will need to create a Run Configuration and then call JUnitLaunchConfigurationDelegate#launch() with the configuration.
The easiest way to do this is to add shortcuts to the extension point org.eclipse.debug.ui.launchShortcuts. With the correct configurationType, you can create the correct type and normally, Eclipse will do the rest. In fact this is exactly what we've done in the Scala IDE.
Here is the relevant XML from scala-ide:
<extension point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
label="%JUnitShortcut.label"
icon="$nl$/icons/full/obj16/julaunch.gif"
helpContextId="org.eclipse.jdt.junit.launch_shortcut"
class="org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut"
modes="run, debug"
id="scala.tools.eclipse.scalatest.junitShortcut">
<contextualLaunch>
<enablement>
<with variable="selection">
<count value="1"/>
<iterate>
<adapt type="org.eclipse.jdt.core.IJavaElement">
<test property="org.eclipse.debug.ui.matchesPattern" value="*.scala"/>
<test property="org.eclipse.jdt.core.isInJavaProject"/>
<test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="junit.framework.Test"/>
<or>
<test property="scala.tools.eclipse.launching.canLaunchAsJUnit" forcePluginActivation="true"/>
<test property="scala.tools.eclipse.launching.junit.canLaunchAsJUnit" forcePluginActivation="true"/>
</or>
</adapt>
</iterate>
</with>
</enablement>
</contextualLaunch>
<configurationType
id="org.eclipse.jdt.junit.launchconfig">
</configurationType>
<description
description="%DebugJUnitLaunchShortcut.description"
mode="debug">
</description>
<description
description="%RunJUnitLaunchShortcut.description"
mode="run">
</description>
</shortcut>
</extension>
The important element is the <contextualLaunch>, which defines a set of tests which need to be true in order that the option to launch as JUnit be presented to the user. Most of these are self explanatory, but we've also scala.tools.eclipse.launching.canLaunchAsJUnit, which references an extension point org.eclipse.core.expressions.propertyTesters. These property testers test whether the code can be launched as JUnit or not (for instance, the class under test extends TestCase or whatever).
If you need more details, I recommend downloading Scala IDE, and looking at the code, but it is written in Scala.
I rewrote part of launche junit services, then inform the project it calls the debug and run.
See my implementation to call the JUnitLaunch

Configuring Scala compiler in IntelliJ 10.5?

I'm trying to get Scala working correctly in IntelliJ 10.5.1. I installed the plugin from the "available plugins" settings and then tried starting a new Java project, selecting "scala" under available technologies. But when I try running it, Scala says I haven't defined a scala compiler in the scala facet. So I went there, but I don't see any way of setting up the compiler. Also, my .iml file in my project looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="scala" name="Scala">
<configuration>
<option name="compilerLibraryLevel" value="Global" />
<option name="pluginPaths">
<array>
<option value="$USER_HOME$/SDKs/scala/src/scala-compiler-src.jar" />
</array>
</option>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="scala " level="application" />
</component>
</module>
Notice that it does seem to be pointing to the correct jar compiler destination.
What do I need to do?
EDIT -
Compiler working now, but when I run, i get this error:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -Didea.launcher.port=7540 -Didea.launcher.bin.path=/Applications/IntelliJ IDEA 10 CE.app/bin -Dfile.encoding=UTF-8 -classpath /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/deploy.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/dt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/javaws.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/jce.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/jconsole.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/management-agent.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/plugin.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/alt-rt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/alt-string.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/charsets.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/classes.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/ui.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/apple_provider.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home:/Users/me/Projects/Scala/testing/out/test/testing:/Users/me/Projects/Scala/testing/out/production/testing:/Users/me/SDKs/scala/lib/scala-library.jar:/Users/me/SDKs/scala/lib/scala-swing.jar:/Users/me/SDKs/scala/lib/scala-dbc.jar:/Applications/IntelliJ IDEA 10 CE.app/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain hello_world
Exception in thread "main" java.lang.ClassNotFoundException: hello_world
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1
Go to project settings (that could be invoked by Ctrl+Alt+Shift+S) and choose scala-compiler in libraries list just like that:
Now FacetManager component looks like this:
<component name="FacetManager">
<facet type="scala" name="Scala">
<configuration>
<option name="compilerLibraryLevel" value="Global" />
<option name="compilerLibraryName" value="scala-compiler-2.9.0.1" />
</configuration>
</facet>
</component>
See the Project Configuration Guide.
If you use SBT to build, I recommend using sbt-idea to generate your IDEA project, and idea-sbt-plugin to delegate project compilation within IDEA to SBT.
I guess you need to add the scala compiler library to the global libraries. In the module settings-> Global Libraries -> attach jar directories and select the lib folder under scala installation directory and then do as om-nom-nom suggested to make sure the compiler is selected.

How to extend the source menu in Eclipse? (or: What is its locationURI?)

I am developing an eclipse plugin and trying to extend the source menu (mainMenubar/Source - visible when editing in the java-editor) in Eclipse 3.7.
The documentation says to rely on the org.eclipse.ui.menus-extension point since the older extension points are deprecated. It is a complete secret to me where to obtain reliable locationURIs, but I finally managed to find some plausible URI with the Plugin Spy (following an advice here).
So the following should be the extension snippet for the plugin.xml:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.jdt.ui.source.menu">
<command
commandId="some.command.id"
label="Some label"
style="push">
</command>
</menuContribution>
</extension>
Unfortunately, when running the plugin for my development IDE no command appears, and also no error message. Just nothing happens. When I set the locationURI to "menu:help", the new command appears in the help menu, so the problem seems to be really the locationURI.
This thread reports having added an entry in the main Source menu:
<!-- main menu -->
<extension point="org.eclipse.ui.actionSets">
<actionSet label="Java Coding"
description="Action set containing coding related Java actions"
visible="true"
id="org.eclipse.jdt.ui.CodingActionSet2">
<menu label="&Source"
path="edit"
id="org.eclipse.jdt.ui.source.menu">
</menu>
<action class="org.gsoc.eclipse.tostringgenerator.actions.GenerateToStringActionDelegate "
id="org.gsoc.eclipse.tostringgenerator.action"
label="Generate to&String()..."
menubarPath="org.eclipse.jdt.ui.source.menu/generateGroup">
</action>
</actionSet>
</extension>
I ran into the same problem. I finally figured out that extending the Source menu using the (recommended) extension point org.eclipse.ui.menus is not possible.
The reason is that a menu defined in an old style actionSet (like the Source menu) is created after the processing of org.eclipse.ui.menus-extensions. The way it is, these extensions can only contribute to already existing menus.
So sticking to the old API (as suggested by VonC) is probably the best option until the jdt plugin is migrated to the new approach...
You can use the popup: space instead of the menu: space. Here is a working example:
<extension point="org.eclipse.ui.commands">
<command defaultHandler="com.igenox.plugin.dpbuilder.rcp.handler.CreateBuilderHandler"
id="com.igenox.plugin.DPBuilder.CreateBuilderPattern" name="CreateBuilderPattern">
</command>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.jdt.ui.source.menu?after=DPSeparator">
<command commandId="com.igenox.plugin.DPBuilder.CreateBuilderPattern"
id="createBuilder" label="Create Builder Pattern">
</command>
</menuContribution>
<menuContribution
locationURI="popup:org.eclipse.jdt.ui.source.menu?after=additions">
<separator name="DPSeparator" visible="true">
</separator>
</menuContribution>
</extension>