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