In this section, you will learn how to programmatically create visual elements and
insert them into a view when using a GraphController
implementation which does not handle this for you automatically.
Class ModelBasedGraphController is the only
GraphController implementation which provides automatic creation
and insertion of visual graph elements at this time. All other implementations do not
automatically create visual elements when model elements are created.
Example 4.3. Creating a VisualNode programmatically
// Exception handling has been ignored to shorten the example // Retrieve a factory instance which can create visual elements GraphViewFactory viewFactory = GraphViewFactory.newInstance(); // Retrieve the geometry definition using the input string. GeometryDescriptor gDesc = GeometryPool.get(geometryDesc); // Specify the geometry description while building a new visual node VisualNode visualNode = viewFactory.newVisualNode(modelNode, gDesc); // Retrieve and style descriptor using the input string StyleDescriptor sDesc = StylePool.get(styleDesc); // Apply the style descriptor to the new visual node visualNode.getBaseComposite().applyStyle(sDesc);
The method above shows you how to manually create a new VisualNode.
Please notice that you are required to specify a GeometryDescriptor
when requesting a new object from the factory. Similarly, a StyleDescriptor
is also required for the created VisualNode. Both geometries and
styles are discussed in the Repository
chapter inside this manual.
Example 4.4. Adding VisualNode objects to a VisualGraphView
// Assuming you have two visual nodes and a visual graph object graphView.addVisualNode(visualNodeA); graphView.addVisualNode(visualNodeB);
Once you have created a new VisualNode, it must be manually
added to a view.
The addVisualNode method shown in the example above is part of the
VisualGraphObjectContainer interface, from which
VisualGraphView is derived.
Example 4.5. Creating a VisualEdge instance
// Exception handling has been ignored to shorten the example.
// Get an instance of the graph view factory
GraphViewFactory viewFactory = GraphViewFactory.newInstance();
// Create a new visual edge via factory method
VisualEdge visualEdge =
viewFactory.newVisualEdge(modelEdge, source, target);
// Retrieve the geometry definition using the input string
GeometryDescriptor gDesc = GeometryPool.get(geometryDesc);
// Apply the geometry descriptor to the new visual edge
visualEdge.getBaseComposite().applyGeometry(gDesc);
// Retrieve and style descriptor using the input string
StyleDescriptor sDesc = StylePool.get(styleDesc);
// Apply the style descriptor to the new visual edge
visualEdge.getBaseComposite().applyStyle(sDesc);
The example above is quite straightforward. Via factory method you can request a new
VisualEdge instance. Subsequently, you set the geometry and style
descriptors, which define the visual edge's graphical appearance. Both geometries and
styles are discussed in the Repository
chapter inside this manual.
Example 4.6. Adding a VisualEdge to a VisualGraphView
//Assuming you have a visual edge and a visual graph object graphView.addVisualEdge(visualEdgeAB);
Once you have created a new VisualEdge, it must be manually added to a view.
The add method shown in the example above is part of the
VisualGraphObjectContainer interface, from which
VisualGraphView is derived.
© 2004, 2005 Tensegrity Software GmbH