Graphic Components > Network Component > Creating a Network Component: a Sample

This section shows you how to create the following network:

images/networkstylingsample.gif

Figure 2.1 A Styled Network

The following example walks you through the steps of 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("ILOG JTGO network sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1. 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. See Configuring a Network Component through a CSS File for more information on how to create the network configuration.
Then, you add the network component to the frame.
  1. 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 stores the business objects that will be converted into representation objects by the network adapter through the data source API. The default data source creates the business objects by parsing an XML file or stream where the objects are described (see step 4). However, users can also create their own data sources from a file, a database or even complex data sources based on proprietary object definitions.
  1. Read in an XML file, network.xml, that contains the network nodes and links.
dataSource.parse("network.xml");
How to Add Business Objects in 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 the data source.
Note that the business object IDs must be unique within the given network:
<addObject id="1004035002697 60">
  <class>
    ilog.tgo.model.IltLink
  </class>
      ...
</addObject>
How to Add Business Objects in the Data Source for a Network Component through the API
You can also create business objects and insert them in the data source through the API, although this process is slower and less dynamic. 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 datasource.
IlpObject node1 =
    new IltNetworkElement("washington",IltNetworkElement.Type.NE,
                          new IltObjectState());
dataSource.addObject(node1);

For information on how to define an XML file, refer to Defining the Business Model in XML in the Business Objects and Data Sources documentation.