Spring and JUNG

First application

I did my first non-trivial application with Spring and JUNG ! It opens an XML file, and displays it as a graph. I used this interface for plugability of different implementations of Swing Graph Displayers :
public interface GraphDisplayer {
    public void setGraph(Graph graph);
    public JComponent getJComponent();
    public void setGraphLayout(Layout l);
}
Although quite simple, this interface says a lot about GraphDisplayer:
Here Graph and Layout are JUNG interfaces. I implemented GraphDisplayer using the GraphDraw JUNG class. The Spring configuration file (spring.xml) that allows plugability of different implementations is :
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="XMLGraph" class="org.diagxml.jung.JUNGXMLGraph" singleton="false">
</bean>
<bean id="graphDisplayer" class="org.diagxml.jung.JUNGGraphDisplayer" singleton="false">
</bean>
</beans>
The initialization code using the Spring Framework is :
SimpleXMLGraphApp(String url) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"spring.xml");
XMLGraph xmlgraph = (XMLGraph) ctx.getBean("XMLGraph");
xmlgraph.add(new URL(url));
graphDraw_ = (GraphDisplayer) ctx.getBean("graphDisplayer");
graphDraw_.setGraph(xmlgraph);
}
Please note that thanks to Spring this application has no dependance to the graph technology used to represent and display the graph. I could use JGraph or Grappa ( a port of a subset of GraphViz to Java ) or touchGraph to display the graph, provided that I write another adapter implementing GraphDisplayer.

Enhance layout and design

The default Layout is the SpringLayout. It lasts about 1 second for the relatively small test document. I changed that to the Fruchterman-Reingold algorithm for node layout (FRLayout) , that also does a good job by avoiding most edge crossings.

The default Layout can be changed without changing the Java code. Basically I set the "graphLayout" property of the "graphDisplayer" object. Note that, as the FRLayout constructor needs an argument of type Graph, I provided it as a reference to the "XMLGraph" bean. But as consequences I had to change another thing : declare the "XMLGraph" bean to be a singleton; otherwise a new bean is created, and it doesn't work.
The new Spring configuration :
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="XMLGraph" class="org.diagxml.jung.JUNGXMLGraph" singleton="true">
</bean>
<bean id="graphDisplayer" class="org.diagxml.jung.JUNGGraphDisplayer" singleton="true">
<property name="graphLayout">
<bean id="frlayout" class="edu.uci.ics.jung.visualization.FRLayout" >
<constructor-arg ><ref bean="XMLGraph"/></constructor-arg>
</bean>
</property>
</bean>
</beans>
Order of initializations:

Ressources

Download eclipse project: graph/spring-jung.zip
Programming ressources:
JUNG Manual
Spring - Java/J2EE Application Framework
Inversion of Control Containers and the Dependency Injection pattern