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 Jul 21, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.transform.shape;
12  
13  import java.awt.Dimension;
14  
15  import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
16  import edu.uci.ics.jung.visualization.Layer;
17  import edu.uci.ics.jung.visualization.RenderContext;
18  import edu.uci.ics.jung.visualization.VisualizationViewer;
19  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
20  import edu.uci.ics.jung.visualization.control.ModalLensGraphMouse;
21  import edu.uci.ics.jung.visualization.picking.ViewLensShapePickSupport;
22  import edu.uci.ics.jung.visualization.renderers.BasicRenderer;
23  import edu.uci.ics.jung.visualization.renderers.Renderer;
24  import edu.uci.ics.jung.visualization.renderers.ReshapingEdgeRenderer;
25  import edu.uci.ics.jung.visualization.transform.AbstractLensSupport;
26  import edu.uci.ics.jung.visualization.transform.LensTransformer;
27  /**
28   * Changes various visualization settings to activate or deactivate an
29   * examining lens for a jung graph application. 
30   * 
31   * @author Tom Nelson
32   */
33  public class MagnifyImageLensSupport<V,E> extends AbstractLensSupport<V,E> {
34      
35      protected RenderContext<V,E> renderContext;
36      protected GraphicsDecorator lensGraphicsDecorator;
37      protected GraphicsDecorator savedGraphicsDecorator;
38      protected Renderer<V,E> renderer;
39      protected Renderer<V,E> transformingRenderer;
40      protected GraphElementAccessor<V,E> pickSupport;
41      protected Renderer.Edge<V,E> savedEdgeRenderer;
42      protected Renderer.Edge<V,E> reshapingEdgeRenderer;
43  
44      static final String instructions = 
45          "<html><center>Mouse-Drag the Lens center to move it<p>"+
46          "Mouse-Drag the Lens edge to resize it<p>"+
47          "Ctrl+MouseWheel to change magnification</center></html>";
48      
49      public MagnifyImageLensSupport(VisualizationViewer<V,E> vv) {
50          this(vv, new MagnifyShapeTransformer(vv),
51                  new ModalLensGraphMouse());
52      }
53      /**
54       * create the base class, setting common members and creating
55       * a custom GraphMouse
56       * @param vv the VisualizationViewer to work on
57       */
58      public MagnifyImageLensSupport(VisualizationViewer<V,E> vv, LensTransformer lensTransformer,
59              ModalGraphMouse lensGraphMouse) {
60          super(vv, lensGraphMouse);
61          this.renderContext = vv.getRenderContext();
62          this.pickSupport = renderContext.getPickSupport();
63          this.renderer = vv.getRenderer();
64          this.transformingRenderer = new BasicRenderer<V,E>();
65          this.savedGraphicsDecorator = renderContext.getGraphicsContext();
66          this.lensTransformer = lensTransformer;
67          this.savedEdgeRenderer = vv.getRenderer().getEdgeRenderer();
68          this.reshapingEdgeRenderer = new ReshapingEdgeRenderer<V,E>();
69          this.reshapingEdgeRenderer.setEdgeArrowRenderingSupport(savedEdgeRenderer.getEdgeArrowRenderingSupport());
70  
71          Dimension d = vv.getSize();
72          if(d.width == 0 || d.height == 0) {
73              d = vv.getPreferredSize();
74          }
75          lensTransformer.setViewRadius(d.width/5);
76          this.lensGraphicsDecorator = new MagnifyIconGraphics(lensTransformer);
77      }
78      
79      public void activate() {
80      	lensTransformer.setDelegate(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW));
81          if(lens == null) {
82              lens = new Lens(lensTransformer);
83          }
84          if(lensControls == null) {
85              lensControls = new LensControls(lensTransformer);
86          }
87          renderContext.setPickSupport(new ViewLensShapePickSupport<V,E>(vv));
88          lensTransformer.setDelegate(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW));
89          vv.getRenderContext().getMultiLayerTransformer().setTransformer(Layer.VIEW, lensTransformer);
90          this.renderContext.setGraphicsContext(lensGraphicsDecorator);
91          vv.getRenderer().setEdgeRenderer(reshapingEdgeRenderer);
92          vv.addPreRenderPaintable(lens);
93          vv.addPostRenderPaintable(lensControls);
94          vv.setGraphMouse(lensGraphMouse);
95          vv.setToolTipText(instructions);
96          vv.repaint();
97      }
98      
99      public void deactivate() {
100     	renderContext.setPickSupport(pickSupport);
101     	vv.getRenderContext().getMultiLayerTransformer().setTransformer(Layer.VIEW, lensTransformer.getDelegate());
102         vv.removePreRenderPaintable(lens);
103         vv.removePostRenderPaintable(lensControls);
104         this.renderContext.setGraphicsContext(savedGraphicsDecorator);
105         vv.getRenderer().setEdgeRenderer(savedEdgeRenderer);
106         vv.setToolTipText(defaultToolTipText);
107         vv.setGraphMouse(graphMouse);
108         vv.repaint();
109     }
110 }