View Javadoc

1   package net.sf.jlayercheck.util.graph;
2   
3   import java.awt.image.BufferedImage;
4   import java.io.File;
5   import java.util.ArrayList;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Set;
9   import java.util.TreeMap;
10  import java.util.TreeSet;
11  
12  import javax.imageio.ImageIO;
13  import javax.swing.JApplet;
14  import javax.swing.JPanel;
15  
16  import net.sf.jlayercheck.util.exceptions.CycleFoundException;
17  
18  import org.jgraph.JGraph;
19  import org.jgraph.plugins.layouts.JGraphLayoutAlgorithm;
20  import org.jgraph.plugins.layouts.SugiyamaLayoutAlgorithm;
21  import org.jgrapht.alg.CycleDetector;
22  import org.jgrapht.ext.JGraphModelAdapter;
23  import org.jgrapht.graph.DefaultEdge;
24  import org.jgrapht.graph.ListenableDirectedGraph;
25  
26  public class GraphModuleDependencies extends JApplet {
27  
28  	/**
29  	 * 
30  	 */
31  	private static final long serialVersionUID = -5249185186644485494L;
32  	private JGraphModelAdapter m_jgAdapter;
33  	protected JGraph jgraph;
34  
35      protected List cells = new ArrayList();
36  
37      public static void main(String args[]) throws Exception {
38      	Map<String, Set<String>> testmap = new TreeMap<String, Set<String>>();
39      	
40      	Set<String> guiset = new TreeSet<String>();
41      	guiset.add("domain");
42      	guiset.add("persistency");
43      	testmap.put("gui3", guiset);
44      	
45      	Set<String> pset = new TreeSet<String>();
46      	pset.add("domain");
47      	testmap.put("persistency", pset);
48  
49      	Set<String> dset = new TreeSet<String>();
50      	dset.add("gui3");
51      	testmap.put("domain", dset);
52  
53      	BufferedImage bi = new GraphModuleDependencies(testmap).getImage();
54      	ImageIO.write(bi, "png", new File("/tmp/test2.png"));
55      }
56      
57      /**
58       * Creates a graph from the given dependencies. The dependency graph
59       * must be free of cycles.
60       * 
61       * @param modulemap
62       * @throws CycleFoundException 
63       */
64      public GraphModuleDependencies(Map<String, Set<String>> modulemap) throws CycleFoundException {
65          // create a JGraphT graph
66          ListenableDirectedGraph<String, String> g = new ListenableDirectedGraph( DefaultEdge.class );
67  
68          // create a visualization using JGraph, via an adapter
69          m_jgAdapter = new JGraphModelAdapter( g );
70  
71          jgraph = new JGraph( m_jgAdapter );
72          JPanel headless = new JPanel();
73          
74          // add some sample data (graph manipulated via JGraphT)
75          for(String module : modulemap.keySet()) {
76          	g.addVertex( module );
77          }
78          for(String module : modulemap.keySet()) {
79          	for(String edgeto : modulemap.get(module)) {
80          		g.addEdge( module, edgeto );
81          	}
82          }
83  
84          CycleDetector<String, String> cd = new CycleDetector<String, String>(g);
85          
86          if (cd.detectCycles()) {
87          	throw new CycleFoundException();
88          }
89          
90          JGraphLayoutAlgorithm rtla = new SugiyamaLayoutAlgorithm(); //SpringEmbeddedLayoutAlgorithm();
91  
92          JGraphLayoutAlgorithm.applyLayout(jgraph, rtla, jgraph.getRoots());
93  
94          headless.setDoubleBuffered(false);
95          headless.add(jgraph);
96          headless.setVisible(true);
97          headless.setEnabled(true);
98          headless.addNotify();
99          headless.validate();
100     }
101 
102     /**
103      * Returns a image containing the graph.
104      * 
105      * @return BufferedImage
106      */
107     public BufferedImage getImage() {
108     	BufferedImage bi = jgraph.getImage(jgraph.getBackground(), 5);
109     	return bi;
110     }
111 }