View Javadoc
1   /*
2    * Copyright (c) 2005, 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    * Created on Aug 15, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.control;
12  
13  import java.awt.Color;
14  import java.awt.Dimension;
15  import java.awt.Graphics;
16  import java.awt.Graphics2D;
17  import java.awt.Shape;
18  import java.awt.geom.AffineTransform;
19  
20  import edu.uci.ics.jung.visualization.Layer;
21  import edu.uci.ics.jung.visualization.VisualizationViewer;
22  import edu.uci.ics.jung.visualization.transform.MutableAffineTransformer;
23  import edu.uci.ics.jung.visualization.transform.shape.ShapeTransformer;
24  
25  /**
26   * A VisualizationViewer that can act as a satellite view for another
27   * (master) VisualizationViewer. In this view, the full graph is always visible
28   * and all mouse actions affect the graph in the master view.
29   * 
30   * A rectangular shape in the satellite view shows the visible bounds of
31   * the master view. 
32   * 
33   * @author Tom Nelson 
34   *
35   * 
36   */
37  @SuppressWarnings("serial")
38  public class SatelliteVisualizationViewer<V, E> 
39  	extends VisualizationViewer<V,E> {
40      
41      /**
42       * the master VisualizationViewer that this is a satellite view for
43       */
44      protected VisualizationViewer<V,E> master;
45      
46      /**
47       * @param master the master VisualizationViewer for which this is a satellite view
48       * @param preferredSize the specified size of the component
49       */
50      public SatelliteVisualizationViewer(VisualizationViewer<V,E> master,
51      		Dimension preferredSize) {
52          super(master.getModel(), preferredSize);
53          this.master = master;
54          
55          // create a graph mouse with custom plugins to affect the master view
56          ModalGraphMouse gm = new ModalSatelliteGraphMouse();
57          setGraphMouse(gm);
58          
59          // this adds the Lens to the satellite view
60          addPreRenderPaintable(new ViewLens<V,E>(this, master));
61          
62          // get a copy of the current layout transform
63          // it may have been scaled to fit the graph
64          AffineTransform modelLayoutTransform =
65              new AffineTransform(master.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).getTransform());
66          
67          // I want no layout transformations in the satellite view
68          // this resets the auto-scaling that occurs in the super constructor
69          getRenderContext().getMultiLayerTransformer().setTransformer(Layer.LAYOUT, new MutableAffineTransformer(modelLayoutTransform));
70          
71          // make sure the satellite listens for changes in the master
72          master.addChangeListener(this);
73          
74          // share the picked state of the master
75          setPickedVertexState(master.getPickedVertexState());
76          setPickedEdgeState(master.getPickedEdgeState());
77      }
78  
79      /**
80       * @return Returns the master.
81       */
82      public VisualizationViewer<V,E> getMaster() {
83          return master;
84      }
85      
86      /**
87       * A four-sided shape that represents the visible part of the
88       * master view and is drawn in the satellite view
89       * 
90       * @author Tom Nelson 
91       *
92       *
93       */
94      static class ViewLens<V,E> implements Paintable {
95  
96          VisualizationViewer<V,E> master;
97          VisualizationViewer<V,E> vv;
98          
99          public ViewLens(VisualizationViewer<V,E> vv, VisualizationViewer<V,E> master) {
100             this.vv = vv;
101             this.master = master;
102         }
103         public void paint(Graphics g) {
104             ShapeTransformer masterViewTransformer = 
105             	master.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
106             ShapeTransformer masterLayoutTransformer = master.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
107             ShapeTransformer vvLayoutTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
108 
109             Shape lens = master.getBounds();
110             lens = masterViewTransformer.inverseTransform(lens);
111             lens = masterLayoutTransformer.inverseTransform(lens);
112             lens = vvLayoutTransformer.transform(lens);
113             Graphics2D g2d = (Graphics2D)g;
114             Color old = g.getColor();
115             Color lensColor = master.getBackground();
116             vv.setBackground(lensColor.darker());
117             g.setColor(lensColor);
118             g2d.fill(lens);
119             g.setColor(Color.gray);
120             g2d.draw(lens);
121             g.setColor(old);
122         }
123 
124         public boolean useTransform() {
125             return true;
126         }
127     }
128 
129 }