View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.pkg;
21  
22  import java.awt.Color;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  
27  import org.apache.log4j.Logger;
28  import org.xnap.XNap;
29  import org.xnap.gui.component.DefaultDialog;
30  import org.xnap.pkg.DependencyGraph;
31  import org.xnap.pkg.DependencyNode;
32  import org.xnap.pkg.PackageDependencyNode;
33  import org.xnap.pkg.UnsatisfiedDependencyNode;
34  
35  import com.touchgraph.graphlayout.Edge;
36  import com.touchgraph.graphlayout.GLPanel;
37  import com.touchgraph.graphlayout.Node;
38  import com.touchgraph.graphlayout.TGException;
39  import com.touchgraph.graphlayout.TGPanel;
40  
41  public class DependencyGraphDialog extends DefaultDialog {
42  
43      //--- Constant(s) ---
44  
45      //--- Data field(s) ---
46  
47      private static Logger logger = Logger.getLogger(DependencyGraphDialog.class);
48  	
49  	private DependencyGraph graph;
50  	private TGPanel jpGraph;
51  
52      //--- Constructor(s) ---
53  
54      public DependencyGraphDialog(DependencyGraph graph) 
55      {
56  		super(BUTTON_CLOSE);
57  
58  		this.graph = graph;
59  
60  		initialize();
61      }
62  
63      //--- Method(s) ---
64  
65  	private void initialize()
66      {
67          setTitle(XNap.tr("Dependency Graph"));
68  
69  		GLPanel jsp = new GLPanel();
70  
71  		jpGraph = jsp.getTGPanel();
72  		jpGraph.clearAll();
73  
74  		try {
75  			initializeGraph();
76  		}
77  		catch (TGException e) {
78  			e.printStackTrace(System.err);
79  		}
80  
81  		setMainComponent(jsp);
82  		setSize(500, 500);
83      }
84  
85  	private void initializeGraph() throws TGException
86  	{
87  		HashSet visited = new HashSet();
88  		LinkedList stack = new LinkedList();
89  
90  		// the root node is not added to jpGraph
91  		for (Iterator i = graph.getRoot().children(); i.hasNext();) {
92  			DependencyNode node = (DependencyNode)i.next();
93  			add(node);
94  			visited.add(node);
95  			stack.addFirst(node);
96  		}				
97  
98  		while (!stack.isEmpty()) {
99  			DependencyNode parent = (DependencyNode)stack.removeFirst();
100 
101 			for (Iterator i = parent.children(); i.hasNext();) {
102 				DependencyNode node = (DependencyNode)i.next();
103 
104 				if (!visited.contains(node)) {
105 					add(node);
106 					visited.add(node);
107 					stack.addFirst(node);
108 				}				
109 
110 				addEdge(parent, node);
111 			}
112 		}
113 	}
114 
115 	private void add(DependencyNode source) throws TGException
116 	{
117 		int type = (source instanceof PackageDependencyNode) 
118 			? Node.TYPE_RECTANGLE 
119 			: Node.TYPE_ROUNDRECT;
120 		Color color = (source instanceof UnsatisfiedDependencyNode)
121 			? new Color(128, 0, 0)
122 			: (source instanceof PackageDependencyNode)
123 			? new Color(0, 128, 0)
124 			: Node.BACK_DEFAULT_COLOR;
125 
126 		Node node = new Node(source.getID().toString(), type, color,
127 							 source.toString());
128 		jpGraph.addNode(node);
129 	}
130 
131 	private void addEdge(DependencyNode source, DependencyNode target)
132 		throws TGException
133 	{
134 		Node sourceNode = jpGraph.findNode(source.getID().toString());
135 		Node targetNode = jpGraph.findNode(target.getID().toString());
136 
137 		if (sourceNode != null && targetNode != null) {
138 			jpGraph.addEdge(new Edge(sourceNode, targetNode));
139 		}
140 	}
141 
142     public static void main( String[] args ) 
143 	{
144 		DependencyGraphDialog d = new DependencyGraphDialog(null);
145 		d.setVisible(true);
146     }
147 
148 }