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    */
9   package edu.uci.ics.jung.samples;
10  
11  import java.awt.BorderLayout;
12  import java.awt.Color;
13  import java.awt.Container;
14  import java.awt.event.ActionEvent;
15  import java.awt.event.ActionListener;
16  import java.awt.event.ItemEvent;
17  import java.awt.event.ItemListener;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.swing.Icon;
22  import javax.swing.ImageIcon;
23  import javax.swing.JButton;
24  import javax.swing.JCheckBox;
25  import javax.swing.JFrame;
26  import javax.swing.JPanel;
27  
28  import org.apache.commons.collections15.Transformer;
29  
30  import edu.uci.ics.jung.algorithms.layout.FRLayout;
31  import edu.uci.ics.jung.graph.DirectedSparseGraph;
32  import edu.uci.ics.jung.graph.Graph;
33  import edu.uci.ics.jung.graph.util.EdgeType;
34  import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
35  import edu.uci.ics.jung.visualization.VisualizationViewer;
36  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
37  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
38  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
39  import edu.uci.ics.jung.visualization.control.ScalingControl;
40  import edu.uci.ics.jung.visualization.decorators.DefaultVertexIconTransformer;
41  import edu.uci.ics.jung.visualization.decorators.EllipseVertexShapeTransformer;
42  import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
43  import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
44  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
45  import edu.uci.ics.jung.visualization.decorators.VertexIconShapeTransformer;
46  import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
47  import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
48  
49  /**
50   * A demo that shows flag images as vertices, and uses unicode
51   * to render vertex labels.
52   * 
53   * @author Tom Nelson 
54   * 
55   */
56  public class UnicodeLabelDemo {
57  
58      /**
59       * the graph
60       */
61      Graph<Integer,Number> graph;
62  
63      /**
64       * the visual component and renderer for the graph
65       */
66      VisualizationViewer<Integer,Number> vv;
67      
68      boolean showLabels;
69      
70      public UnicodeLabelDemo() {
71          
72          // create a simple graph for the demo
73          graph = new DirectedSparseGraph<Integer,Number>();
74          Integer[] v = createVertices(10);
75          createEdges(v);
76          
77          vv =  new VisualizationViewer<Integer,Number>(new FRLayout<Integer,Number>(graph));
78          vv.getRenderContext().setVertexLabelTransformer(new UnicodeVertexStringer<Integer>(v));
79          vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
80          vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));
81          VertexIconShapeTransformer<Integer> vertexIconShapeFunction =
82              new VertexIconShapeTransformer<Integer>(new EllipseVertexShapeTransformer<Integer>());
83          DefaultVertexIconTransformer<Integer> vertexIconFunction = new DefaultVertexIconTransformer<Integer>();
84          vv.getRenderContext().setVertexShapeTransformer(vertexIconShapeFunction);
85          vv.getRenderContext().setVertexIconTransformer(vertexIconFunction);
86          loadImages(v, vertexIconFunction.getIconMap());
87          vertexIconShapeFunction.setIconMap(vertexIconFunction.getIconMap());
88          vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.white,  Color.yellow));
89          vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.lightGray));
90  
91          vv.setBackground(Color.white);
92  
93          // add my listener for ToolTips
94          vv.setVertexToolTipTransformer(new ToStringLabeller<Integer>());
95          
96          // create a frome to hold the graph
97          final JFrame frame = new JFrame();
98          Container content = frame.getContentPane();
99          final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
100         content.add(panel);
101         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
102         
103         final ModalGraphMouse gm = new DefaultModalGraphMouse<Integer,Number>();
104         vv.setGraphMouse(gm);
105         
106         final ScalingControl scaler = new CrossoverScalingControl();
107 
108         JButton plus = new JButton("+");
109         plus.addActionListener(new ActionListener() {
110             public void actionPerformed(ActionEvent e) {
111                 scaler.scale(vv, 1.1f, vv.getCenter());
112             }
113         });
114         JButton minus = new JButton("-");
115         minus.addActionListener(new ActionListener() {
116             public void actionPerformed(ActionEvent e) {
117                 scaler.scale(vv, 1/1.1f, vv.getCenter());
118             }
119         });
120 
121         JCheckBox lo = new JCheckBox("Show Labels");
122         lo.addItemListener(new ItemListener(){
123             public void itemStateChanged(ItemEvent e) {
124                 showLabels = e.getStateChange() == ItemEvent.SELECTED;
125                 vv.repaint();
126             }
127         });
128         lo.setSelected(true);
129         
130         JPanel controls = new JPanel();
131         controls.add(plus);
132         controls.add(minus);
133         controls.add(lo);
134         controls.add(((DefaultModalGraphMouse<Integer,Number>) gm).getModeComboBox());
135         content.add(controls, BorderLayout.SOUTH);
136 
137         frame.pack();
138         frame.setVisible(true);
139     }
140     
141     
142     class UnicodeVertexStringer<V> implements Transformer<V,String> {
143 
144         Map<V,String> map = new HashMap<V,String>();
145         Map<V,Icon> iconMap = new HashMap<V,Icon>();
146         String[] labels = {
147                 "\u0057\u0065\u006C\u0063\u006F\u006D\u0065\u0020\u0074\u006F\u0020JUNG\u0021",               
148                 "\u6B22\u8FCE\u4F7F\u7528\u0020\u0020JUNG\u0021",
149                 "\u0414\u043E\u0431\u0440\u043E\u0020\u043F\u043E\u0436\u0430\u043B\u043E\u0432\u0430\u0422\u044A\u0020\u0432\u0020JUNG\u0021",
150                 "\u0042\u0069\u0065\u006E\u0076\u0065\u006E\u0075\u0065\u0020\u0061\u0075\u0020JUNG\u0021",
151                 "\u0057\u0069\u006C\u006B\u006F\u006D\u006D\u0065\u006E\u0020\u007A\u0075\u0020JUNG\u0021",
152                 "JUNG\u3078\u3087\u3045\u3053\u305D\u0021",
153 //                "\u0053\u00E9\u006A\u0061\u0020\u0042\u0065\u006D\u0076\u0069\u006E\u0064\u006F\u0020JUNG\u0021",
154                "\u0042\u0069\u0065\u006E\u0076\u0065\u006E\u0069\u0064\u0061\u0020\u0061\u0020JUNG\u0021"
155         };
156         
157         public UnicodeVertexStringer(V[] vertices) {
158             for(int i=0; i<vertices.length; i++) {
159                 map.put(vertices[i], labels[i%labels.length]);
160             }
161         }
162         
163         /**
164          * @see edu.uci.ics.jung.graph.decorators.VertexStringer#getLabel(edu.uci.ics.jung.graph.Vertex)
165          */
166         public String getLabel(V v) {
167             if(showLabels) {
168                 return (String)map.get(v);
169             } else {
170                 return "";
171             }
172         }
173         
174 		public String transform(V input) {
175 			return getLabel(input);
176 		}
177     }
178     
179     /**
180      * create some vertices
181      * @param count how many to create
182      * @return the Vertices in an array
183      */
184     private Integer[] createVertices(int count) {
185         Integer[] v = new Integer[count];
186         for (int i = 0; i < count; i++) {
187             v[i] = new Integer(i);
188             graph.addVertex(v[i]);
189         }
190         return v;
191     }
192 
193     /**
194      * create edges for this demo graph
195      * @param v an array of Vertices to connect
196      */
197     void createEdges(Integer[] v) {
198         graph.addEdge(new Double(Math.random()), v[0], v[1], EdgeType.DIRECTED);
199         graph.addEdge(new Double(Math.random()), v[0], v[3], EdgeType.DIRECTED);
200         graph.addEdge(new Double(Math.random()), v[0], v[4], EdgeType.DIRECTED);
201         graph.addEdge(new Double(Math.random()), v[4], v[5], EdgeType.DIRECTED);
202         graph.addEdge(new Double(Math.random()), v[3], v[5], EdgeType.DIRECTED);
203         graph.addEdge(new Double(Math.random()), v[1], v[2], EdgeType.DIRECTED);
204         graph.addEdge(new Double(Math.random()), v[1], v[4], EdgeType.DIRECTED);
205         graph.addEdge(new Double(Math.random()), v[8], v[2], EdgeType.DIRECTED);
206         graph.addEdge(new Double(Math.random()), v[3], v[8], EdgeType.DIRECTED);
207         graph.addEdge(new Double(Math.random()), v[6], v[7], EdgeType.DIRECTED);
208         graph.addEdge(new Double(Math.random()), v[7], v[5], EdgeType.DIRECTED);
209         graph.addEdge(new Double(Math.random()), v[0], v[9], EdgeType.DIRECTED);
210         graph.addEdge(new Double(Math.random()), v[9], v[8], EdgeType.DIRECTED);
211         graph.addEdge(new Double(Math.random()), v[7], v[6], EdgeType.DIRECTED);
212         graph.addEdge(new Double(Math.random()), v[6], v[5], EdgeType.DIRECTED);
213         graph.addEdge(new Double(Math.random()), v[4], v[2], EdgeType.DIRECTED);
214         graph.addEdge(new Double(Math.random()), v[5], v[4], EdgeType.DIRECTED);
215     }
216 
217     /**
218      * A nested class to demo ToolTips
219      */
220     
221     protected void loadImages(Integer[] vertices, Map<Integer,Icon> imageMap) {
222         
223         ImageIcon[] icons = null;
224         try {
225             icons = new ImageIcon[] {
226                     new ImageIcon(getClass().getResource("/images/united-states.gif")),
227                     new ImageIcon(getClass().getResource("/images/china.gif")),
228                     new ImageIcon(getClass().getResource("/images/russia.gif")),
229                     new ImageIcon(getClass().getResource("/images/france.gif")),
230                     new ImageIcon(getClass().getResource("/images/germany.gif")),
231                     new ImageIcon(getClass().getResource("/images/japan.gif")),
232                     new ImageIcon(getClass().getResource("/images/spain.gif"))
233             };
234         } catch(Exception ex) {
235             System.err.println("You need flags.jar in your classpath to see the flag icons.");
236         }
237         for(int i=0; icons != null && i<vertices.length; i++) {
238             imageMap.put(vertices[i],icons[i%icons.length]);
239         }
240     }
241     /**
242      * a driver for this demo
243      */
244     public static void main(String[] args) 
245     {
246         new UnicodeLabelDemo();
247     }
248 }