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 1, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.decorators;
12  
13  import java.awt.Image;
14  import java.awt.Shape;
15  import java.awt.geom.AffineTransform;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import javax.swing.Icon;
20  import javax.swing.ImageIcon;
21  
22  import com.google.common.base.Function;
23  
24  import edu.uci.ics.jung.visualization.util.ImageShapeUtils;
25  
26  /**
27   * A default implementation that stores images in a Map keyed on the
28   * vertex. Also applies a shaping function to images to extract the
29   * shape of the opaque part of a transparent image.
30   * 
31   * @author Tom Nelson 
32   */
33  public class VertexIconShapeTransformer<V> implements Function<V,Shape> {
34  	protected Map<Image, Shape> shapeMap = new HashMap<Image, Shape>();
35  	protected Map<V, Icon> iconMap;
36  	protected Function<V, Shape> delegate;
37       
38  	/**
39  	 * Creates an instance with the specified delegate.
40  	 * @param delegate the vertex-to-shape function to use if no image is present for the vertex
41  	 */
42      public VertexIconShapeTransformer(Function<V, Shape> delegate) {
43          this.delegate = delegate;
44      }
45  
46      /**
47       * @return Returns the delegate.
48       */
49      public Function<V,Shape> getDelegate() {
50          return delegate;
51      }
52  
53      /**
54       * @param delegate The delegate to set.
55       */
56      public void setDelegate(Function<V,Shape> delegate) {
57          this.delegate = delegate;
58      }
59  
60      /**
61       * get the shape from the image. If not available, get
62       * the shape from the delegate VertexShapeFunction
63       */
64      public Shape apply(V v) {
65  		Icon icon = iconMap.get(v);
66  		if (icon != null && icon instanceof ImageIcon) {
67  			Image image = ((ImageIcon) icon).getImage();
68  			Shape shape = (Shape) shapeMap.get(image);
69  			if (shape == null) {
70  			    shape = ImageShapeUtils.getShape(image, 30);
71  			    if(shape.getBounds().getWidth() > 0 && 
72  			            shape.getBounds().getHeight() > 0) {
73                      // don't cache a zero-sized shape, wait for the image
74  			       // to be ready
75                      int width = image.getWidth(null);
76                      int height = image.getHeight(null);
77                      AffineTransform transform = AffineTransform
78  						.getTranslateInstance(-width / 2, -height / 2);
79                      shape = transform.createTransformedShape(shape);
80                      shapeMap.put(image, shape);
81                  }
82  			}
83  			return shape;
84  		} else {
85  			return delegate.apply(v);
86  		}
87  	}
88  
89      /**
90  	 * @return the iconMap
91  	 */
92  	public Map<V, Icon> getIconMap() {
93  		return iconMap;
94  	}
95  
96  	/**
97  	 * @param iconMap the iconMap to set
98  	 */
99  	public void setIconMap(Map<V, Icon> iconMap) {
100 		this.iconMap = iconMap;
101 	}
102 
103 	/**
104 	 * @return the shapeMap
105 	 */
106 	public Map<Image, Shape> getShapeMap() {
107 		return shapeMap;
108 	}
109 
110 	/**
111 	 * @param shapeMap the shapeMap to set
112 	 */
113 	public void setShapeMap(Map<Image, Shape> shapeMap) {
114 		this.shapeMap = shapeMap;
115 	}
116 }