View Javadoc

1   /*
2    * Copyright (c) 2003, 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   package edu.uci.ics.jung.visualization3d;
9   
10  import javax.media.j3d.Appearance;
11  import javax.media.j3d.LineArray;
12  import javax.media.j3d.Material;
13  import javax.media.j3d.Node;
14  import javax.media.j3d.Shape3D;
15  import javax.vecmath.Color3f;
16  import javax.vecmath.Point3f;
17  
18  import org.apache.commons.collections15.Transformer;
19  import org.apache.commons.collections15.functors.ConstantTransformer;
20  
21  import com.sun.j3d.utils.geometry.Box;
22  import com.sun.j3d.utils.geometry.Cylinder;
23  import com.sun.j3d.utils.geometry.Sphere;
24  
25  import edu.uci.ics.jung.graph.Graph;
26  import edu.uci.ics.jung.graph.util.Context;
27  import edu.uci.ics.jung.visualization.picking.PickedState;
28  
29  
30  /**
31   */
32  @SuppressWarnings("unchecked")
33  public class PluggableRenderContext<V, E> implements RenderContext<V, E> {
34  
35  	protected Transformer<E,Appearance> edgeAppearanceTransformer;
36  	protected Transformer<Context<Graph<V,E>,E>,Node> edgeShapeTransformer;
37  	protected PickedState<E> pickedEdgeState;
38  	protected PickedState<V> pickedVertexState;
39  	protected Transformer<V,Appearance> vertexAppearanceTransformer;
40  	protected Transformer<V,String> vertexStringer = new ConstantTransformer(null);
41  	protected Transformer<V,Node> vertexShapeTransformer;
42  	
43  	
44  	
45  	public PluggableRenderContext() {
46  		super();
47  		Color3f lightGray = new Color3f(0.7f, 0.7f, 0.7f);
48  		Color3f black = new Color3f(0,0,0);
49  		Color3f white = new Color3f(1,1,1);
50  		Color3f gray  = new Color3f(.2f, .2f, .2f);
51  		Color3f red = new Color3f(1, 0, 0);
52  		Color3f yellow = new Color3f(0,1,1);
53  		Material lightGrayMaterial = new Material(lightGray, black,
54  				lightGray, white, 100.0f);
55  		Material blackMaterial = new Material(lightGray, black,
56  				black, lightGray, 10.0f);
57  		Material whiteMaterial = new Material(white, white,
58  				white, white, 100.0f);
59  		Material grayMaterial = new Material(gray, black,
60  				gray, gray, 100.0f);
61  		Material redMaterial = new Material(red, black,
62  				red, red, 100.0f);
63  		Material yellowMaterial = new Material(yellow, black,
64  				yellow, yellow, 100.0f);
65  
66  		final Appearance lightGrayLook = new Appearance();
67  		lightGrayLook.setMaterial(lightGrayMaterial);
68  		Appearance blackLook = new Appearance();
69  		blackLook.setMaterial(blackMaterial);
70  		Appearance whiteLook = new Appearance();
71  		whiteLook.setMaterial(whiteMaterial);
72  		Appearance grayLook = new Appearance();
73  		grayLook.setMaterial(grayMaterial);
74  		
75  //		grayLook.setCapability(Appearance.ALLOW_MATERIAL_READ);
76  //		grayLook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
77  
78  		final Appearance redLook = new Appearance();
79  		redLook.setMaterial(redMaterial);
80  		final Appearance yellowLook = new Appearance();
81  		yellowLook.setMaterial(yellowMaterial);
82  		
83  		 final Cylinder cylinder = new Cylinder(1, 1, 
84  				 Cylinder.GENERATE_NORMALS |
85  				 Cylinder.ENABLE_GEOMETRY_PICKING,
86  				 26, 26, lightGrayLook);
87  	      final Sphere sphere = new Sphere(10, 
88  	    		  Sphere.GENERATE_NORMALS | 
89  	    		  Sphere.ENABLE_GEOMETRY_PICKING, redLook);
90  	      final Box box = new Box(10,10,10,
91  	    		  Box.GENERATE_NORMALS |
92  	    		  Box.ENABLE_GEOMETRY_PICKING,
93  	    		  redLook);
94  
95  		this.edgeAppearanceTransformer = new ConstantTransformer(lightGrayLook);
96  		this.edgeShapeTransformer = new Transformer<Context<Graph<V,E>,E>,Node>() {
97  
98  			public Node transform(Context<Graph<V,E>,E> ec) {
99  				LineArray lineArray = new LineArray(2, LineArray.COORDINATES | LineArray.COLOR_3);
100 				lineArray.setCoordinates(0, new Point3f[]{new Point3f(0,-.5f,0),new Point3f(0,.5f,0)});
101 				lineArray.setColor(0, new Color3f(1,1,1));
102 				lineArray.setColor(1, new Color3f(1,1,1));
103 				Shape3D shape = new Shape3D();
104 				shape.setGeometry(lineArray);
105 				return shape;
106 //				return new Cylinder(1, 1, 
107 //						Cylinder.GENERATE_NORMALS |
108 //						Cylinder.ENABLE_GEOMETRY_PICKING,
109 //						 26, 26, lightGrayLook);
110 			}};
111 		this.vertexAppearanceTransformer = new ConstantTransformer(redLook);
112 		this.vertexShapeTransformer = new Transformer<V,Node>() {
113 
114 			public Node transform(V arg0) {
115 				return new Sphere(10, 
116 						Sphere.GENERATE_NORMALS | 
117 						Sphere.ENABLE_GEOMETRY_PICKING|
118 						Sphere.ENABLE_APPEARANCE_MODIFY, redLook);
119 			}};
120 	}
121 	
122 
123 	public Transformer<E, Appearance> getEdgeAppearanceTransformer() {
124 		return edgeAppearanceTransformer;
125 	}
126 
127 	public Transformer<Context<Graph<V,E>,E>, Node> getEdgeShapeTransformer() {
128 		return edgeShapeTransformer;
129 	}
130 
131 	public PickedState<E> getPickedEdgeState() {
132 		return pickedEdgeState;
133 	}
134 
135 	public PickedState<V> getPickedVertexState() {
136 		return pickedVertexState;
137 	}
138 
139 	public Transformer<V, Appearance> getVertexAppearanceTransformer() {
140 		return vertexAppearanceTransformer;
141 	}
142 
143 	public Transformer<V, Node> getVertexShapeTransformer() {
144 		return vertexShapeTransformer;
145 	}
146 
147 	public Transformer<V, String> getVertexStringer() {
148 		return vertexStringer;
149 	}
150 
151 	public void setEdgeAppearanceTransformer(Transformer<E, Appearance> edgeAppearanceTransformer) {
152 		this.edgeAppearanceTransformer = edgeAppearanceTransformer;
153 		
154 	}
155 
156 	public void setEdgeShapeTransformer(Transformer<Context<Graph<V,E>,E>, Node> edgeShapeTransformer) {
157 		this.edgeShapeTransformer = edgeShapeTransformer;
158 	}
159 
160 	public void setPickedEdgeState(PickedState<E> pickedEdgeState) {
161 			this.pickedEdgeState = pickedEdgeState;
162 	}
163 
164 	public void setPickedVertexState(PickedState<V> pickedVertexState) {
165 		this.pickedVertexState = pickedVertexState;
166 	}
167 
168 	public void setVertexAppearanceTransformer(Transformer<V, Appearance> vertexAppearanceTransformer) {
169 		this.vertexAppearanceTransformer = vertexAppearanceTransformer;
170 	}
171 
172 	public void setVertexShapeTransformer(Transformer<V, Node> vertexShapeTransformer) {
173 		this.vertexShapeTransformer = vertexShapeTransformer;
174 	}
175 
176 	public void setVertexStringer(Transformer<V, String> vertexStringer) {
177 		this.vertexStringer = vertexStringer;
178 	}
179 }
180 
181