Two Minute Tutorial

Download and Install

Download the jar file and include it in your classpath.

Write simple Component

public class Component {

	private String value;
    
	public Component(String value) {
	    this.value = value;
	}
	
	public String getValue() {
	    return value;
	}
	
	public void setValue(String value) {
	    this.value = value;
	}
}
  			

Write Adhesive class that want's to use the Component

import ch.mimo.swingglue.Adhesive;

public class ComponentAdhesive implements Adhesive {

	// attribute ---------------------------------------------------------------

	/**
	 * the attribute that will be populated by IoC
	 */
    private Component component;
    
    // adhesive methods --------------------------------------------------------
    
    /* (non-Javadoc)
     * @see ch.mimo.swingglue.Adhesive#setup()
     */
    public void setup() throws Exception {
        System.out.println(this.getClass().getName() + ".setup()");
    }
    
    /* (non-Javadoc)
     * @see ch.mimo.swingglue.Adhesive#adhere()
     */
    public void adhere() throws Exception {
        System.out.println(this.getClass().getName() + ".adhere() component value: " + component.getValue());
    }

    
    // getters and setters -----------------------------------------------------
    
    /**
     * @return Returns the component.
     */
    public Component getComponent() {
        return component;
    }
    /**
     * @param component The component to set.
     */
    public void setComponent(Component component) {
        System.out.println(this.getClass().getName() + ".setComponent() invoked with value: " + component.getValue());
        this.component = component;
    }
}
  			

Assemble Component and Adhesive Implementation

String value = "The value that is stored in Component";

Component component = new Component(value);
        
GlueFactory factory = GlueFactory.getFactory();
Glue glue = factory.getGlue();

glue.addElement("component",component);
        
ComponentAdhesive adhesive = (ComponentAdhesive)glue.applyAdhesive(ComponentAdhesive.class);
        
String result = adhesive.getComponent().getValue();

// result == value
  			

How does it work?

The Component Instance has to be registered as an element in the Glue instance. Inside the Glue sit's a weak hash map that holds that instance reference in relation to it's name.

glue.addElement("component",component);
  			
The above code registers the instance by the name of "component".

The Adhesive class that implements the interface Adhesive is a bean style class. It can be consideret to be a POJO. If the Adhesive class, like in our example needs to have a dependency to the Component that we have registered in the Glue it has to do two things:

  • add a private (or public for that matter) member that has the same name as we registered the instance under in the Glue.
  • add a valid setter for that member variable
Now we istanciate the Adhesive class
ComponentAdhesive adhesive = (ComponentAdhesive)glue.applyAdhesive(ComponentAdhesive.class);
				
The Glue, using reflection, will search for members that have a matching name and type to one of the registered instances. If the member also has a valid setter it will call that setter with the registered instance.

That's all Folks...