The yFiles library enables the use of Java Swing User Interface (UI) components to render nodes and to create proper editors for node-related data which are presented as "live" objects within a Graph2DView. Similar to the concept and use of Java Swing interfaces
both visual representation of nodes and editing of related data can be
accomplished by means of implementations for interfaces
NodeCellRenderer and
NodeCellEditor
, respectively.
Each interface provides a callback method to return a
JComponent which presents Swing look and feel.
Figure 6.37, “JComponents in the view” shows different JComponents within a view
at zoom level 100%.
Using Swing UI components for the visual representation of a node can be
accomplished by providing an implementation for interface
NodeCellRenderer.
This interface behaves similar to Java Swing interfaces such as
TableCellRenderer or TreeCellRenderer, and serves as a callback for the logic
that is responsible for painting the node realizer.
Following is the callback method defined by interface NodeCellRenderer:
JComponent getNodeCellRendererComponent(Graph2DView view, NodeRealizer context, Object value, boolean isSelected) |
|
Description | Parameter 'value' holds the value of the cell to be rendered, i.e., the actual node-related data that is presented in the JComponent. |
Class NodeCellRendererPainter provides
support for rendering Swing UI components with GenericNodeRealizer.
It is an implementation for interface GenericNodeRealizer.Painter which is
responsible for painting
generic node realizers.
A NodeCellRendererPainter instance can be instantiated using a NodeCellRenderer
implementation and be added to a GenericNodeRealizer configuration.
The given NodeCellRenderer implementation is then invoked to return the
JComponent that serves as the visual representation for a node.
Defining a GenericNodeRealizer configuration that uses NodeCellRendererPainter is outlined in Example 6.29, “Defining a GenericNodeRealizer configuration that supports rendering of UI components”. Tutorial demo application SwingRendererDemo.java shows how to define a corresponding NodeCellRenderer that returns a JComponent.
Example 6.29. Defining a GenericNodeRealizer configuration that supports rendering of UI components
void createJTextFieldNodeRealizer() { // Create a customized NodeCellRenderer instance that defines the visual // representation of a node. NodeCellRenderer myNCR = new MyJTextFieldNodeCellRenderer(); // Define a GenericNodeRealizer configuration that uses 'myNCR' as a callback // for class NodeCellRendererPainter. // This class provides support for rendering Swing UI components. GenericNodeRealizer.Factory factory = GenericNodeRealizer.getFactory(); Map map = factory.createDefaultConfigurationMap(); map.put( GenericNodeRealizer.Painter.class, new NodeCellRendererPainter(myNCR, NodeCellRendererPainter.USER_DATA_MAP)); map.put( GenericNodeRealizer.UserDataHandler.class, new SimpleUserDataHandler(SimpleUserDataHandler.REFERENCE_ON_FAILURE)); // Register the configuration using the given name. factory.addConfiguration("JTextField", map); }
The UI components rendered in the view provide the full range of mouse-based user interaction that is also supported for "ordinary" nodes, i.e., they can be selected, moved around, or scaled using their resize knobs, for example. They are also subject to changes of the viewport such as zooming in or out.
In addition to using Swing UI components for the visual representation of nodes by means of a NodeCellRenderer implementation (as shown above), the components can also be used as proper editors. As a special feature, these Swing-based editors remain editable even when zoomed. Figure 6.38, “Editing a zoomed JComponent” shows a zoomed-in JTextField before and after editing its value.
Enhancing a node realizer that renders a JComponent with editor functionality
can be accomplished by providing an implementation for interface
NodeCellEditor.
The section
Node-Related Editing Capabilities
describes this interface and its role for user interaction with UI components.
Class CellEditorMode is the view mode
that knows all nodes that have a Swing-based editor associated.
It recognizes whenever a user attempts to use such an editor and initiates node
editing.
See the section called “Class CellEditorMode” for a detailed description of this
view mode.
The tutorial demo application SwingRendererDemo.java shows how Java Swing UI components can be used for both rendering the visual representation of nodes and additionally providing editors for node-related data.
Copyright ©2004-2012, yWorks GmbH. All rights reserved. |