View Javadoc

1   /*
2    * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    */
8   package edu.uci.ics.jung.samples;
9   
10  import java.awt.BorderLayout;
11  import java.awt.Color;
12  import java.awt.Component;
13  import java.awt.GridLayout;
14  import java.awt.event.ActionEvent;
15  import java.awt.event.ActionListener;
16  import java.lang.reflect.Constructor;
17  import java.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.HashSet;
20  import java.util.List;
21  
22  import javax.swing.DefaultListCellRenderer;
23  import javax.swing.JApplet;
24  import javax.swing.JButton;
25  import javax.swing.JComboBox;
26  import javax.swing.JFrame;
27  import javax.swing.JList;
28  import javax.swing.JPanel;
29  
30  import org.apache.commons.collections15.Factory;
31  
32  import edu.uci.ics.jung.algorithms.generators.random.MixedRandomGraphGenerator;
33  import edu.uci.ics.jung.algorithms.layout.CircleLayout;
34  import edu.uci.ics.jung.algorithms.layout.FRLayout;
35  import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
36  import edu.uci.ics.jung.algorithms.layout.KKLayout;
37  import edu.uci.ics.jung.algorithms.layout.Layout;
38  import edu.uci.ics.jung.algorithms.layout.SpringLayout;
39  import edu.uci.ics.jung.algorithms.layout.SpringLayout2;
40  import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
41  import edu.uci.ics.jung.graph.Graph;
42  import edu.uci.ics.jung.graph.SparseMultigraph;
43  import edu.uci.ics.jung.graph.util.TestGraphs;
44  import edu.uci.ics.jung.visualization.VisualizationViewer;
45  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
46  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
47  import edu.uci.ics.jung.visualization.control.ScalingControl;
48  import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
49  import edu.uci.ics.jung.visualization.layout.LayoutTransition;
50  import edu.uci.ics.jung.visualization.util.Animator;
51  
52  
53  /**
54   * Demonstrates several of the graph layout algorithms.
55   * Allows the user to interactively select one of several graphs, and one of
56   * several layouts, and visualizes the combination.
57   * 
58   * @author Danyel Fisher
59   * @author Joshua O'Madadhain
60   */
61  @SuppressWarnings("serial")
62  public class ShowLayouts extends JApplet {
63      protected static Graph<? extends Object, ? extends Object>[] g_array;
64      protected static int graph_index;
65      protected static String[] graph_names = {"Two component graph", 
66          "Random mixed-mode graph", "Miscellaneous multicomponent graph", 
67          "Random directed acyclic graph", "One component graph", 
68          "Chain+isolate graph", "Trivial (disconnected) graph"};
69      
70  	public static class GraphChooser implements ActionListener
71      {
72          private JComboBox layout_combo;
73  
74          public GraphChooser(JComboBox layout_combo)
75          {
76              this.layout_combo = layout_combo;
77          }
78          
79          public void actionPerformed(ActionEvent e)
80          {
81              JComboBox cb = (JComboBox)e.getSource();
82              graph_index = cb.getSelectedIndex();
83              layout_combo.setSelectedIndex(layout_combo.getSelectedIndex()); // rebuild the layout
84          }
85      }
86  
87      /**
88  	 * 
89  	 * @author danyelf
90  	 */
91  	
92  	private static final class LayoutChooser implements ActionListener
93      {
94          private final JComboBox jcb;
95          private final VisualizationViewer<Integer,Number> vv;
96  
97          private LayoutChooser(JComboBox jcb, VisualizationViewer<Integer,Number> vv)
98          {
99              super();
100             this.jcb = jcb;
101             this.vv = vv;
102         }
103 
104         public void actionPerformed(ActionEvent arg0)
105         {
106             Object[] constructorArgs =
107                 { g_array[graph_index]};
108 
109             Class<? extends Layout<Integer,Number>> layoutC = 
110                 (Class<? extends Layout<Integer,Number>>) jcb.getSelectedItem();
111 //            Class lay = layoutC;
112             try
113             {
114                 Constructor<? extends Layout<Integer, Number>> constructor = layoutC
115                         .getConstructor(new Class[] {Graph.class});
116                 Object o = constructor.newInstance(constructorArgs);
117                 Layout<Integer,Number> l = (Layout<Integer,Number>) o;
118                 l.setInitializer(vv.getGraphLayout());
119                 l.setSize(vv.getSize());
120                 
121 				LayoutTransition<Integer,Number> lt =
122 					new LayoutTransition<Integer,Number>(vv, vv.getGraphLayout(), l);
123 				Animator animator = new Animator(lt);
124 				animator.start();
125 				vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
126 				vv.repaint();
127                 
128             }
129             catch (Exception e)
130             {
131                 e.printStackTrace();
132             }
133         }
134     }
135 
136     private static JPanel getGraphPanel()
137     {
138         g_array = 
139             (Graph<? extends Object,? extends Object>[])
140             new Graph<?,?>[graph_names.length];
141         
142     	Factory<Graph<Integer,Number>> graphFactory =
143     		new Factory<Graph<Integer,Number>>() {
144     		public Graph<Integer,Number> create() {
145     			return new SparseMultigraph<Integer,Number>();
146     		}
147     	};
148 
149     	Factory<Integer> vertexFactory = new Factory<Integer>() {
150     			int count;
151 				public Integer create() {
152 					return count++;
153 				}};
154 		Factory<Number> edgeFactory = new Factory<Number>() {
155 			int count;
156 				public Number create() {
157 					return count++;
158 				}};
159 
160             
161         g_array[0] = TestGraphs.createTestGraph(false);
162         g_array[1] = MixedRandomGraphGenerator.generateMixedRandomGraph(graphFactory, 
163         		vertexFactory, edgeFactory, new HashMap<Number,Number>(), 20, true, new HashSet<Integer>());
164         g_array[2] = TestGraphs.getDemoGraph();
165         g_array[3] = TestGraphs.createDirectedAcyclicGraph(4, 4, 0.3);
166         g_array[4] = TestGraphs.getOneComponentGraph();
167         g_array[5] = TestGraphs.createChainPlusIsolates(18, 5);
168         g_array[6] = TestGraphs.createChainPlusIsolates(0, 20);
169 
170         Graph<? extends Object, ? extends Object> g = g_array[4]; // initial graph
171 
172         final VisualizationViewer<Integer,Number> vv = 
173             new VisualizationViewer<Integer,Number>(new FRLayout(g));
174         
175         vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.red, Color.yellow));
176         
177         final DefaultModalGraphMouse<Integer,Number> graphMouse = new DefaultModalGraphMouse<Integer,Number>();
178         vv.setGraphMouse(graphMouse);
179         
180         final ScalingControl scaler = new CrossoverScalingControl();
181 
182         JButton plus = new JButton("+");
183         plus.addActionListener(new ActionListener() {
184             public void actionPerformed(ActionEvent e) {
185                 scaler.scale(vv, 1.1f, vv.getCenter());
186             }
187         });
188         JButton minus = new JButton("-");
189         minus.addActionListener(new ActionListener() {
190             public void actionPerformed(ActionEvent e) {
191                 scaler.scale(vv, 1/1.1f, vv.getCenter());
192             }
193         });
194         JButton reset = new JButton("reset");
195         reset.addActionListener(new ActionListener() {
196 			public void actionPerformed(ActionEvent e) {
197 				Layout<Integer,Number> layout = vv.getGraphLayout();
198 				layout.initialize();
199 				Relaxer relaxer = vv.getModel().getRelaxer();
200 				if(relaxer != null) {
201 //				if(layout instanceof IterativeContext) {
202 					relaxer.stop();
203 					relaxer.prerelax();
204 					relaxer.relax();
205 				}
206 			}});
207         
208         JComboBox modeBox = graphMouse.getModeComboBox();
209         modeBox.addItemListener(((DefaultModalGraphMouse<Integer,Number>)vv.getGraphMouse()).getModeListener());
210 
211         JPanel jp = new JPanel();
212         jp.setBackground(Color.WHITE);
213         jp.setLayout(new BorderLayout());
214         jp.add(vv, BorderLayout.CENTER);
215         Class[] combos = getCombos();
216         final JComboBox jcb = new JComboBox(combos);
217         // use a renderer to shorten the layout name presentation
218         jcb.setRenderer(new DefaultListCellRenderer() {
219             public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
220                 String valueString = value.toString();
221                 valueString = valueString.substring(valueString.lastIndexOf('.')+1);
222                 return super.getListCellRendererComponent(list, valueString, index, isSelected,
223                         cellHasFocus);
224             }
225         });
226         jcb.addActionListener(new LayoutChooser(jcb, vv));
227         jcb.setSelectedItem(FRLayout.class);
228 
229         JPanel control_panel = new JPanel(new GridLayout(2,1));
230         JPanel topControls = new JPanel();
231         JPanel bottomControls = new JPanel();
232         control_panel.add(topControls);
233         control_panel.add(bottomControls);
234         jp.add(control_panel, BorderLayout.NORTH);
235         
236         final JComboBox graph_chooser = new JComboBox(graph_names);
237         
238         graph_chooser.addActionListener(new GraphChooser(jcb));
239         
240         topControls.add(jcb);
241         topControls.add(graph_chooser);
242         bottomControls.add(plus);
243         bottomControls.add(minus);
244         bottomControls.add(modeBox);
245         bottomControls.add(reset);
246         return jp;
247     }
248 
249     public void start()
250     {
251         this.getContentPane().add(getGraphPanel());
252     }
253 
254     /**
255      * @return
256      */
257     @SuppressWarnings("unchecked")
258     private static Class<? extends Layout>[] getCombos()
259     {
260         List<Class<? extends Layout>> layouts = new ArrayList<Class<? extends Layout>>();
261         layouts.add(KKLayout.class);
262         layouts.add(FRLayout.class);
263         layouts.add(CircleLayout.class);
264         layouts.add(SpringLayout.class);
265         layouts.add(SpringLayout2.class);
266         layouts.add(ISOMLayout.class);
267         return layouts.toArray(new Class[0]);
268     }
269 
270     public static void main(String[] args)
271     {
272         JPanel jp = getGraphPanel();
273 
274         JFrame jf = new JFrame();
275         jf.getContentPane().add(jp);
276         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
277         jf.pack();
278         jf.setVisible(true);
279     }
280 }