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.Dimension;
15  import java.awt.GridLayout;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.ActionListener;
18  
19  import javax.swing.JButton;
20  import javax.swing.JComboBox;
21  import javax.swing.JDesktopPane;
22  import javax.swing.JFrame;
23  import javax.swing.JInternalFrame;
24  import javax.swing.JOptionPane;
25  import javax.swing.JPanel;
26  
27  import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
28  import edu.uci.ics.jung.algorithms.layout.Layout;
29  import edu.uci.ics.jung.graph.Graph;
30  import edu.uci.ics.jung.graph.util.TestGraphs;
31  import edu.uci.ics.jung.visualization.VisualizationViewer;
32  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
33  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
34  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
35  import edu.uci.ics.jung.visualization.control.SatelliteVisualizationViewer;
36  import edu.uci.ics.jung.visualization.control.ScalingControl;
37  import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
38  import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
39  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
40  
41  /**
42   * Similar to the SatelliteViewDemo, but using JInternalFrame.
43   * 
44   * @author Tom Nelson
45   * 
46   */
47  public class InternalFrameSatelliteViewDemo {
48  
49      static final String instructions = 
50          "<html>"+
51          "<b><h2><center>Instructions for Mouse Listeners</center></h2></b>"+
52          "<p>There are two modes, Transforming and Picking."+
53          "<p>The modes are selected with a toggle button."+
54          
55          "<p><p><b>Transforming Mode:</b>"+
56          "<ul>"+
57          "<li>Mouse1+drag pans the graph"+
58          "<li>Mouse1+Shift+drag rotates the graph"+
59          "<li>Mouse1+CTRL(or Command)+drag shears the graph"+
60          "</ul>"+
61          
62          "<b>Picking Mode:</b>"+
63          "<ul>"+
64          "<li>Mouse1 on a Vertex selects the vertex"+
65          "<li>Mouse1 elsewhere unselects all Vertices"+
66          "<li>Mouse1+Shift on a Vertex adds/removes Vertex selection"+
67          "<li>Mouse1+drag on a Vertex moves all selected Vertices"+
68          "<li>Mouse1+drag elsewhere selects Vertices in a region"+
69          "<li>Mouse1+Shift+drag adds selection of Vertices in a new region"+
70          "<li>Mouse1+CTRL on a Vertex selects the vertex and centers the display on it"+
71          "</ul>"+
72         "<b>Both Modes:</b>"+
73         "<ul>"+
74          "<li>Mousewheel scales the layout &gt; 1 and scales the view &lt; 1";
75      
76      /**
77       * the graph
78       */
79      Graph<String,Number> graph;
80  
81      /**
82       * the visual component and renderer for the graph
83       */
84      VisualizationViewer<String,Number> vv;
85      VisualizationViewer<String,Number> satellite;
86      
87      JInternalFrame dialog;
88      
89      JDesktopPane desktop;
90  
91      /**
92       * create an instance of a simple graph with controls to
93       * demo the zoom features.
94       * 
95       */
96      public InternalFrameSatelliteViewDemo() {
97          
98          // create a simple graph for the demo
99          graph = TestGraphs.getOneComponentGraph();
100 
101         Layout<String,Number> layout = new ISOMLayout<String,Number>(graph);
102 
103         vv = new VisualizationViewer<String,Number>(layout, new Dimension(600,600));
104         vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.cyan));
105         vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(vv.getPickedVertexState(), Color.red, Color.yellow));
106 
107         // add my listener for ToolTips
108         vv.setVertexToolTipTransformer(new ToStringLabeller());
109         final ModalGraphMouse graphMouse = new DefaultModalGraphMouse();
110         vv.setGraphMouse(graphMouse);
111 
112         satellite =
113             new SatelliteVisualizationViewer<String,Number>(vv, new Dimension(200,200));
114         satellite.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(satellite.getPickedEdgeState(), Color.black, Color.cyan));
115         satellite.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(satellite.getPickedVertexState(), Color.red, Color.yellow));
116 
117         ScalingControl satelliteScaler = new CrossoverScalingControl();
118         satellite.scaleToLayout(satelliteScaler);
119         
120         JFrame frame = new JFrame();
121         desktop = new JDesktopPane();
122         Container content = frame.getContentPane();
123         JPanel panel = new JPanel(new BorderLayout());
124         panel.add(desktop);
125         content.add(panel);
126         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
127         
128         JInternalFrame vvFrame = new JInternalFrame();
129         vvFrame.getContentPane().add(vv);
130         vvFrame.pack();
131         vvFrame.setVisible(true); //necessary as of 1.3
132         desktop.add(vvFrame);
133         try {
134             vvFrame.setSelected(true);
135         } catch (java.beans.PropertyVetoException e) {}
136 
137         dialog = new JInternalFrame();
138         desktop.add(dialog);
139         content = dialog.getContentPane();
140         
141         final ScalingControl scaler = new CrossoverScalingControl();
142 
143         JButton plus = new JButton("+");
144         plus.addActionListener(new ActionListener() {
145             public void actionPerformed(ActionEvent e) {
146                 scaler.scale(vv, 1.1f, vv.getCenter());
147             }
148         });
149         JButton minus = new JButton("-");
150         minus.addActionListener(new ActionListener() {
151             public void actionPerformed(ActionEvent e) {
152                 scaler.scale(vv, 1/1.1f, vv.getCenter());
153             }
154         });
155         JButton dismiss = new JButton("Dismiss");
156         dismiss.addActionListener(new ActionListener(){
157             public void actionPerformed(ActionEvent e) {
158                 dialog.setVisible(false);
159             }
160         });
161         JButton help = new JButton("Help");
162         help.addActionListener(new ActionListener() {
163             public void actionPerformed(ActionEvent e) {
164                 JOptionPane.showInternalMessageDialog(dialog, instructions,
165                         "Instructions", JOptionPane.PLAIN_MESSAGE);
166             }
167         });
168         JPanel controls = new JPanel(new GridLayout(2,2));
169         controls.add(plus);
170         controls.add(minus);
171         controls.add(dismiss);
172         controls.add(help);
173         content.add(satellite);
174         content.add(controls, BorderLayout.SOUTH);
175          
176         JButton zoomer = new JButton("Show Satellite View");
177         zoomer.addActionListener(new ActionListener() {
178             public void actionPerformed(ActionEvent e) {
179                 dialog.pack();
180                 dialog.setLocation(desktop.getWidth()-dialog.getWidth(),0);
181                 dialog.show();
182                 try {
183                     dialog.setSelected(true);
184                 } catch (java.beans.PropertyVetoException ex) {}
185             }
186         });
187 
188         JComboBox modeBox = ((DefaultModalGraphMouse)graphMouse).getModeComboBox();
189         modeBox.addItemListener(((ModalGraphMouse)satellite.getGraphMouse()).getModeListener());
190         JPanel p = new JPanel();
191         p.add(zoomer);
192         p.add(modeBox);
193 
194         frame.getContentPane().add(p, BorderLayout.SOUTH);
195         frame.setSize(800, 800);
196         frame.setVisible(true);
197     }
198 
199     /**
200      * a driver for this demo
201      */
202     public static void main(String[] args) {
203         new InternalFrameSatelliteViewDemo();
204     }
205 }
206