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
59
60
61
62
63
64 public GraphModuleDependencies(Map<String, Set<String>> modulemap) throws CycleFoundException {
65
66 ListenableDirectedGraph<String, String> g = new ListenableDirectedGraph( DefaultEdge.class );
67
68
69 m_jgAdapter = new JGraphModelAdapter( g );
70
71 jgraph = new JGraph( m_jgAdapter );
72 JPanel headless = new JPanel();
73
74
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();
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
104
105
106
107 public BufferedImage getImage() {
108 BufferedImage bi = jgraph.getImage(jgraph.getBackground(), 5);
109 return bi;
110 }
111 }