View Javadoc
1   package edu.uci.ics.jung.visualization.control;
2   
3   import java.awt.geom.Point2D;
4   
5   import com.google.common.base.Supplier;
6   
7   import edu.uci.ics.jung.algorithms.layout.Layout;
8   import edu.uci.ics.jung.graph.Graph;
9   import edu.uci.ics.jung.visualization.BasicVisualizationServer;
10  
11  /** 
12   * sample implementation showing how to use the VertexSupport interface member of the
13   * EditingGraphMousePlugin.
14   * override midVertexCreate and endVertexCreate for more elaborate implementations
15   * @author tanelso
16   *
17   * @param <V> the vertex type
18   * @param <E> the edge type
19   */
20  public class SimpleVertexSupport<V,E> implements VertexSupport<V,E> {
21  
22  	protected Supplier<V> vertexFactory;
23  	
24  	public SimpleVertexSupport(Supplier<V> vertexFactory) {
25  		this.vertexFactory = vertexFactory;
26  	}
27  	
28  	public void startVertexCreate(BasicVisualizationServer<V, E> vv,
29  			Point2D point) {
30  		V newVertex = vertexFactory.get();
31  		Layout<V,E> layout = vv.getGraphLayout();
32  		Graph<V,E> graph = layout.getGraph();
33  		graph.addVertex(newVertex);
34  		layout.setLocation(newVertex, vv.getRenderContext().getMultiLayerTransformer().inverseTransform(point));
35  		vv.repaint();
36  	}
37  
38  	public void midVertexCreate(BasicVisualizationServer<V, E> vv,
39  			Point2D point) {
40  		// noop
41  	}
42  
43  	public void endVertexCreate(BasicVisualizationServer<V, E> vv,
44  			Point2D point) {
45  		//noop
46  	}
47  
48  	public Supplier<V> getVertexFactory() {
49  		return vertexFactory;
50  	}
51  
52  	public void setVertexFactory(Supplier<V> vertexFactory) {
53  		this.vertexFactory = vertexFactory;
54  	}
55  
56  }