views:

1454

answers:

4

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. That works. Now I want to add a fragment to that host. Not so simple. 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 do a "Hello2" at start() and "Goodbye2" at stop().

Thanks!

+1  A: 

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.

Nick Veys
A: 

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.

Mike Houston
A: 

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.

Mario Ortegón
A: 

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

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) and in tab called "Extentions" click "Add.." and add "org.eclipse.ui.startup" and browse class which implements org.eclipse.ui.IStartup class. 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!");

    }


}
Michal Bernhard