SteveT
SteveT

Reputation: 959

Eclipse Plugin Fragment

Does anyone have a "hello world" sample or tutorial for creating an Eclipse plugin fragment?

I have a working host plugin that, for the sake of simplicity, is just this...

public void start(BundleContext context) throws Exception {
    System.out.println("Hello....");
    super.start(context);
    plugin = this;
}

public void stop(BundleContext context) throws Exception {
    System.out.println("Goodbye...");
    plugin = null;
    super.stop(context);
}

Simple enough and works. Now I want to add a fragment to that host, which seems not as simple as creating a plugin host. I just don't see how to create a fragment project and add logic to it. Let's say I just want to do something simple and have the fragment to print a "Hello2" at start() and "Goodbye2" at stop(). Can someone give me a working example?

Upvotes: 12

Views: 16361

Answers (5)

Michal Bernhard
Michal Bernhard

Reputation: 3883

  1. Eclipse -> File -> New... -> Fragment project -> set the host plugin (which is either in your workspace or in plugins in target platform).

  2. Open Plugin manifest editor (you can do it by clicking on build.properties, manifest.mf or fragment.xml - if there is no such a file, create it by hand)

  3. In tab Extentions click Add.. and add org.eclipse.ui.startup and browse class which implements org.eclipse.ui.IStartup class.

  4. Create this class and implement it. You need to implement method earlyStartup() which is entry point to the fragment.

Note: The lines below are just example. I didn't test it so there might be errors...

All you need is this (this is project structure / directory structure):

  • Fragment-Project - root dir
    • /META-INF
      • MANIFEST.MF
    • /src (which is source directory)
      • FragmentStartClass.java (which implement org.eclipse.ui.IStartup interface and earlyStartup method)
    • build.properties
    • fragment.xml

META-INF/MANIFEST.MF content:

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: FragmentProject 
Bundle-SymbolicName: FragmentProject;singleton:=true 
Bundle-Version: 1.0.0 
Bundle-ClassPath: src/,. 
Fragment-Host: *HostPluginProjectSymbolicName*;bundle-version="1.0.0" 
Bundle-RequiredExecutionEnvironment: J2SE-1.5 
Require-Bundle:  

build.properties content:

source.. = src,\
output.. = bin/
bin.includes = META-INF/,
              .,
                  fragment.xml

fragment.xml content:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<fragment>
   <extension
         point="org.eclipse.ui.startup">
      <startup
            class="FragmentStartClass">
      </startup>
   </extension>
</fragment>

FragmentStartClass.java content:

import org.eclipse.ui.IStartup;


public class FragmentStartClass implements IStartup {

    public void earlyStartup() {
       System.out.println("Hello World From Fragment!");

    }


}

Upvotes: 17

mgaert
mgaert

Reputation: 2388

For the record, I just created an Eclipse Fragment that contains some PMD Java Rule classes.

Next to the class files, there is only one file required in that Jar: META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: com.acme.tools.pmd
Bundle-SymbolicName: com.acme.tools.pmd;singleton:=true
Bundle-Version: 1.0.0
Fragment-Host: net.sourceforge.pmd.eclipse.plugin;bundle-version="3.2.6"
Bundle-RequiredExecutionEnvironment: J2SE-1.5

I tried this with current Eclipse 3.7. Deployment is by placing the Jar file into <eclipse root>/dropins/plugins (create that folder if it's not yet there).

Upvotes: 0

Mario Orteg&#243;n
Mario Orteg&#243;n

Reputation: 18900

I think what you are trying to do is better served by using extension points and separate plugins, as you want to add functionality to the base plugin.

Fragments are only used to add resources or extra classes to the base plugin.

Upvotes: 0

Kothar
Kothar

Reputation: 6629

I'm not sure if you can do this without the host plugin knowing something about loading the fragment's extra implementation code.

For example:

MyInterface o = null;
try {
    Class<?> c = getClass().getClassLoader().loadClass("my.fragment.Activator");
    o = (MyInterface) c.newInstance();
} catch (ClassNotFoundException e) {
}

if (o != null) {
    o.start();
}

where MyInterface is an interface defined in the host plugin, and implemented by your fragment.

Another way might be to have the fragment provide an extension point identifying the extended class, but that is probably more than is needed.

Upvotes: 0

Nick Veys
Nick Veys

Reputation: 23939

Well, first off what are you trying to accomplish? Are you sure fragments are the solution?

Fragments are simply additions to an existing plugin. There is no Bundle-Activator to "startup" the fragment. You just gain the additional resources in the fragment. You could use an extension point to inform the host that some particular functionality was extended by a present fragment. Think of it as merging the two at runtime.

See this note from the Eclipse Wiki.

Upvotes: 4

Related Questions