View Javadoc

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