View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University 
3    * of California
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * http://jung.sourceforge.net/license.txt for a description.
9    * Created on Mar 8, 2005
10   *
11   */
12  package edu.uci.ics.jung.visualization.control;
13  
14  import java.awt.geom.Point2D;
15  
16  import edu.uci.ics.jung.visualization.Layer;
17  import edu.uci.ics.jung.visualization.VisualizationServer;
18  import edu.uci.ics.jung.visualization.VisualizationViewer;
19  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
20  
21  /** 
22   * A scaling control that has a crossover point.
23   * When the overall scale of the view and
24   * model is less than the crossover point, the scaling is applied
25   * to the view's transform and the graph nodes, labels, etc grow
26   * smaller. This preserves the overall shape of the graph.
27   * When the scale is larger than the crossover, the scaling is
28   * applied to the graph layout. The graph spreads out, but the
29   * vertices and labels grow no larger than their original size. 
30   * 
31   * @author Tom Nelson
32   */
33  public class CrossoverScalingControl implements ScalingControl {
34  
35      /**
36       * Point where scale crosses over from view to layout.
37       */
38      protected double crossover = 1.0;
39      
40      /**
41       * Sets the crossover point to the specified value.
42       */
43  	public void setCrossover(double crossover) {
44  	    this.crossover = crossover;
45  	}
46  
47      /**
48       * Returns the current crossover value.
49       */
50      public double getCrossover() {
51          return crossover;
52      }
53      
54  	/**
55       * @see edu.uci.ics.jung.visualization.control.ScalingControl#scale(VisualizationViewer, float, Point2D)
56       */
57  	public void scale(VisualizationServer<?,?> vv, float amount, Point2D at) {
58  	        
59  	    MutableTransformer layoutTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
60  	    MutableTransformer viewTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
61  	    double modelScale = layoutTransformer.getScale();
62  	    double viewScale = viewTransformer.getScale();
63  	    double inverseModelScale = Math.sqrt(crossover)/modelScale;
64  	    double inverseViewScale = Math.sqrt(crossover)/viewScale;
65  	    double scale = modelScale * viewScale;
66  	    
67  	    Point2D transformedAt = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW, at);
68  	    
69          if((scale*amount - crossover)*(scale*amount - crossover) < 0.001) {
70              // close to the control point, return both transformers to a scale of sqrt crossover value
71              layoutTransformer.scale(inverseModelScale, inverseModelScale, transformedAt);
72              viewTransformer.scale(inverseViewScale, inverseViewScale, at);
73          } else if(scale*amount < crossover) {
74              // scale the viewTransformer, return the layoutTransformer to sqrt crossover value
75  	        viewTransformer.scale(amount, amount, at);
76  	        layoutTransformer.scale(inverseModelScale, inverseModelScale, transformedAt);
77  	    } else {
78              // scale the layoutTransformer, return the viewTransformer to crossover value
79  	        layoutTransformer.scale(amount, amount, transformedAt);
80  	        viewTransformer.scale(inverseViewScale, inverseViewScale, at);
81  	    }
82  	    vv.repaint();
83  	}
84  }