Creating a network component: a sample
The network component to be created is shown in the following figure.
A Styled Network
This example describes the steps for creating a frame as a network container, creating an instance of
IlpNetwork, creating a data source and connecting it to the network, and finally reading in the network data.
How to create a basic network component
1. Create a frame to contain the network.
// Create a frame.
JFrame frame = new JFrame("JTGO network sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2. Create the network component.
IlpNetwork network = new IlpNetwork(networkDescriptionFileName,context);
frame.add(network);
You need to create a new instance of a network and make sure that the appropriate configuration is assigned. The network configuration is normally read in from a CSS file. You ensure that the network configuration file is taken into account and parsed by passing the URL and context or, as in the example, by passing the filename and context as arguments of IlpNetwork. You then add the network component to the frame.
3. Create the data source and connect it to the network component.
// Connect the data source to the network component.
IltDefaultDataSource dataSource = new IltDefaultDataSource(context);
network.setDataSource(dataSource);
The data source holds the business objects that will be converted into representation objects by the network adapter through the data source API.
4. Read in an XML file, network.xml, that contains the network nodes and links.
dataSource.parse("network.xml");
The default data source creates the business objects by parsing an XML file or stream where the objects are described, as shown.
You can create your own data sources from a file or database, or even create complex data sources based on proprietary object definitions.
How to add business objects to the data source for a network component through an XML file
The easiest way to populate a network is to read an XML data file into its data source.
The business object IDs must be unique within the given network.
The following XML code shows how to add a link.
<addObject id="1004035002697 60">
<class>
ilog.tgo.model.IltLink
</class>
...
</addObject>
For information on how to define an XML file, refer to
Defining the business model in XML.
How to add business objects to the data source for a network component through the API
You can create business objects and insert them in the data source through the API, although this process is slower and less dynamic than reading an XML file. The following example shows how to insert an JViews TGO network element identified by its name, type and status into the data source as a node business object identified as node1.
// Put some objects into the data source.
IlpObject node1 =
new IltNetworkElement("washington",IltNetworkElement.Type.NE,
new IltObjectState());
dataSource.addObject(node1);
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.