Posts Tagged ‘plugins’

* Eclipse Plugin Q&A

Posted on March 25th, 2009 by Dave Johnson. Filed under Web, phonegap, programming.


If you are one of the unfortunate few that has decided to, for profit or insanity, write an Eclipse plugin here are a few of the questions that I had during my ordeal and the answer or closest thing to an answer that I can come up with.

Q: How should I get started with Eclipse plug-ins?

A: Read a book. I did not do this and really regret it now I think. There are few different ones available that I assume explain all the intricacies. The Google can only help so much. Having said that, the approach that I took was to look at the code of another Eclipse plug-in and borrow from that :) Some of the better links I put up on Delicious.

Q: How do I make my plugin show up in the new project list?

A: This one seems like it should be easy to find out how to do but it actually wasn’t that easy. So it is pretty easy to get started using one of the samples in Eclipse like the file editor sample that creates a “New file” wizard. While that wizard does not launch from the “New project” menu it can be augmented to be a project creation wizard pretty easily. The important bit is setting project=”true” on your wizard in the plugin.xml file for the Eclipse newWizards extension.

<extension point="org.eclipse.ui.newWizards">
  <wizard
    project="true" ... />

Q: Now that I have a new project wizard how do I know that a project was created using that wizard or is that type of project so that you can create debug or run configurations accordingly?

A: This is where project “Natures” come in. A project can have different natures associated with it to support different functionality in Eclipse. You need to create a class that implements the IProjectNature interface.

public class PhoneGapProjectNature implements IProjectNature

You can reference that project Nature from the plugin.xml through the org.eclipse.core.resources.natures extension point.

<extension id="com.phonegap.phonegapnature" name="PhoneGap Nature"
  point="org.eclipse.core.resources.natures">
  <runtime>
    <run class="com.phonegap.project.PhoneGapProjectNature"/>
  </runtime>
  <builder id="com.phonegap.phonegapbuilder"></builder>
</extension>

This enables one to connect certain builders to a nature - in the case of PhoneGap we don’t actually use a builder since it is just HTML and JavaScript but we do set and read the nature from the plug-in code.

To set the nature of the project when it is created in the new project wizard we can set the project nature(s) on the project description like this:

IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProjectDescription description =
  workspace.newProjectDescription(newProjectHandle.getName());
description.setNatureIds(new String[] { PhoneGapPlugin.PHONEGAP_NATURE_ID });

In the case of PhoneGap, we use this project nature information in the properties of the Run and Debug configurations such that a developer can create configurations and choose the project that the configuration should launch when they click Run or Debug. In the configuration we iterate over the projects collection of the workspace and check if each project has the PhoneGap nature, if it does we add it to a combo box.

boolean bb = projects[i].hasNature(PhoneGapPlugin.PHONEGAP_NATURE_ID);
if (projects[i].hasNature(PhoneGapPlugin.PHONEGAP_NATURE_ID)) {
  projectCombo.add(projects[i].getName());
}

Q: How do I store preferences and configuration default values?

A: In your AddLaunch method of the LaunchAdder, which you call from the createProject method of your NewProjecWizard class like this:

SimulatorLaunchAdder.addSimulatorLaunch(projectHandle.getName(),
  projectHandle.getName(), "");

you will need have some code like this:

ILaunchConfigurationWorkingCopy wc = DebugPlugin.getDefault()
.getLaunchManager().getLaunchConfigurationType(
  "com.phonegap.debug.SimulatorLaunchConfigurationType")
.newInstance(null, DebugPlugin.getDefault()
.getLaunchManager().generateUniqueLaunchConfigurationNameFrom(projectName));
wc.setAttribute("KEY", "value");
final ILaunchConfiguration config = wc.doSave();

Q: How do I access resources like files to populate a new project with?

A: Put any files that you will want to copy to a project into a namespace like com.phonegap.resources then you can access it like this:

String rootContent = StreamUtils.readContent(
this.getClass().getResourceAsStream("/com/phonegap/resources/root.html"), null
);

Where StreamUtils is just a wrapper for doing the stream reading.

Q: I am getting a “class not found” error when I debug what should I do?

A: Get Eclipse 3.4 if you don’t already have it.

Q: I want to support the debug / run menus but to do that I need org.eclipse.debug.ui/core references - where do I find those?

A: Add them to the dependcies list in the plugin.xml visual editor. First add core then the ui one will be available.

That’s about all I can muster for the moment but will try to put up some more tips later!

Tags: , , , , , .