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.BorderFactory;
20  import javax.swing.JApplet;
21  import javax.swing.JButton;
22  import javax.swing.JFrame;
23  import javax.swing.JPanel;
24  
25  import edu.uci.ics.jung.algorithms.layout.FRLayout;
26  import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
27  import edu.uci.ics.jung.algorithms.layout.Layout;
28  import edu.uci.ics.jung.graph.Graph;
29  import edu.uci.ics.jung.graph.util.TestGraphs;
30  import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
31  import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
32  import edu.uci.ics.jung.visualization.VisualizationModel;
33  import edu.uci.ics.jung.visualization.VisualizationViewer;
34  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
35  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
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  import edu.uci.ics.jung.visualization.picking.MultiPickedState;
41  import edu.uci.ics.jung.visualization.picking.PickedState;
42  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
43  
44  /**
45   * Demonstrates a single graph with 2 layouts in 2 views.
46   * They share picking, transforms, and a pluggable renderer
47   * 
48   * @author Tom Nelson
49   * 
50   */
51  @SuppressWarnings("serial")
52  public class TwoModelDemo extends JApplet {
53  
54       /**
55       * the graph
56       */
57      Graph<String,Number> graph;
58  
59      /**
60       * the visual components and renderers for the graph
61       */
62      VisualizationViewer<String,Number> vv1;
63      VisualizationViewer<String,Number> vv2;
64      
65      /**
66       * the normal transformer
67       */
68      MutableTransformer layoutTransformer;
69      
70      Dimension preferredSize = new Dimension(300,300);
71      
72      /**
73       * create an instance of a simple graph in two views with controls to
74       * demo the zoom features.
75       * 
76       */
77      public TwoModelDemo() {
78          
79          // create a simple graph for the demo
80          // both models will share one graph
81          graph = TestGraphs.getOneComponentGraph();
82          
83          // create two layouts for the one graph, one layout for each model
84          Layout<String,Number> layout1 = new FRLayout<String,Number>(graph);
85          Layout<String,Number> layout2 = new ISOMLayout<String,Number>(graph);
86  
87          // create the two models, each with a different layout
88          VisualizationModel<String,Number> vm1 =
89              new DefaultVisualizationModel<String,Number>(layout1, preferredSize);
90          VisualizationModel<String,Number> vm2 = 
91              new DefaultVisualizationModel<String,Number>(layout2, preferredSize);
92  
93          // create the two views, one for each model
94          // they share the same renderer
95          vv1 = new VisualizationViewer<String,Number>(vm1, preferredSize);
96          vv2 = new VisualizationViewer<String,Number>(vm2, preferredSize);
97          vv1.setRenderContext(vv2.getRenderContext());
98          
99          // share the model transformer between the two models
100 //        layoutTransformer = vv1.getLayoutTransformer();
101 //        vv2.setLayoutTransformer(layoutTransformer);
102 //        
103 //        // share the view transformer between the two models
104 //        vv2.setViewTransformer(vv1.getViewTransformer());
105         
106         vv2.getRenderContext().setMultiLayerTransformer(vv1.getRenderContext().getMultiLayerTransformer());
107         vv2.getRenderContext().getMultiLayerTransformer().addChangeListener(vv1);
108 
109         vv1.setBackground(Color.white);
110         vv2.setBackground(Color.white);
111         
112         // share one PickedState between the two views
113         PickedState<String> ps = new MultiPickedState<String>();
114         vv1.setPickedVertexState(ps);
115         vv2.setPickedVertexState(ps);
116         PickedState<Number> pes = new MultiPickedState<Number>();
117         vv1.setPickedEdgeState(pes);
118         vv2.setPickedEdgeState(pes);
119         
120         // set an edge paint function that will show picking for edges
121         vv1.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv1.getPickedEdgeState(), Color.black, Color.red));
122         vv1.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(vv1.getPickedVertexState(),
123                 Color.red, Color.yellow));
124         // add default listeners for ToolTips
125         vv1.setVertexToolTipTransformer(new ToStringLabeller());
126         vv2.setVertexToolTipTransformer(new ToStringLabeller());
127         
128         Container content = getContentPane();
129         JPanel panel = new JPanel(new GridLayout(1,0));
130         panel.add(new GraphZoomScrollPane(vv1));
131         panel.add(new GraphZoomScrollPane(vv2));
132 
133         content.add(panel);
134         
135         // create a GraphMouse for each view
136         final DefaultModalGraphMouse gm1 = new DefaultModalGraphMouse();
137 
138         DefaultModalGraphMouse gm2 = new DefaultModalGraphMouse();
139 
140         vv1.setGraphMouse(gm1);
141         vv2.setGraphMouse(gm2);
142 
143         // create zoom buttons for scaling the transformer that is
144         // shared between the two models.
145         final ScalingControl scaler = new CrossoverScalingControl();
146 
147         JButton plus = new JButton("+");
148         plus.addActionListener(new ActionListener() {
149             public void actionPerformed(ActionEvent e) {
150                 scaler.scale(vv1, 1.1f, vv1.getCenter());
151             }
152         });
153         JButton minus = new JButton("-");
154         minus.addActionListener(new ActionListener() {
155             public void actionPerformed(ActionEvent e) {
156                 scaler.scale(vv1, 1/1.1f, vv1.getCenter());
157             }
158         });
159         
160         JPanel zoomPanel = new JPanel(new GridLayout(1,2));
161         zoomPanel.setBorder(BorderFactory.createTitledBorder("Zoom"));
162         
163         JPanel modePanel = new JPanel();
164         modePanel.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
165         gm1.getModeComboBox().addItemListener(gm2.getModeListener());
166         modePanel.add(gm1.getModeComboBox());
167 
168         JPanel controls = new JPanel();
169         zoomPanel.add(plus);
170         zoomPanel.add(minus);
171         controls.add(zoomPanel);
172         controls.add(modePanel);
173         content.add(controls, BorderLayout.SOUTH);
174     }
175 
176     /**
177      * a driver for this demo
178      */
179     public static void main(String[] args) {
180         JFrame f = new JFrame();
181         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
182         f.getContentPane().add(new TwoModelDemo());
183         f.pack();
184         f.setVisible(true);
185     }
186 }