ILOG JViews TGO 8.1 Samples


ILOG JViews TGO 8.1 includes a rich set of sample code to be used as reference for the development of an application. Samples fall into three areas: application samples, applet samples, and server-side samples.
All sample source code can be found in subdirectories of <installdir>/jviews-tgo81/samples.

ILOG JViews TGO 8.1 is based on the Java Platform, Standard Edition. To run an ILOG JViews TGO 8.1 client application, you need to have the Java Platform, Standard Edition Runtime Environment. To run an ILOG JViews TGO 8.1 server-side application within the provided Tomcat server, you need to run Tomcat with a Java SE installation (see http://java.sun.com/javase). To see an ILOG JViews TGO 8.1 sample applet, you need to have the Java Plug-in installed in your browser (see http://java.sun.com/products/plugin/).

This section contains the following topics:

Running the Samples

The samples provided in the ILOG JViews TGO 8.1 distribution are already compiled so you do not need to compile them to be able to run them. If you want to modify a sample and recompile it, every sample contains a build.xml file that allows you to compile and run the sample. This file is an Ant project file. Ant is a Java-based build tool that is developed by the Apache Software Foundation. (See http://ant.apache.org.) The ILOG JViews TGO 8.1 distribution contains a version of Ant that you can use if you do not have Ant already installed on your computer. The Ant installation can be found in the <installdir>/jviews-framework81/tools directory.

To use Ant, you must set the following environment variables:

JAVA_HOME to point to the Java Platform, Standard Edition (Java SE) you are using. This release works with Java SE 5 and higher.

The build.bat and build.sh scripts use the version of Ant provided in the ILOG JViews TGO distribution. If you use these scripts, you do not need to set any other environment variables. If you want to run Ant directly, for example to use your own Ant installation, then you will need to set:

ANT_HOME to point to your Ant directory.

Add the Ant bin directory to your PATH.

To recompile a sample, go to the samples directory and do:

> build.bat (or build.sh on UNIX and Linux)

If you have ant on your path then you can just run Ant directly:

> ant build

To remove compiled classes do:

> clean.bat (or clean.sh on UNIX and Linux)

Or using Ant directly:

> ant clean

The samples that can run as applications also have a 'run' target in their build.xml files and run.[bat|sh] batch files:

> run.bat (or run.sh on UNIX and Linux)

Or using Ant directly:

> ant run

To know how to use the build.xml file of a sample use:

> ant -projecthelp

You can read the Ant documentation at http://ant.apache.org/manual/index.html.

Copying a Demo

Demos that run as an application, this excludes applets and server-side demos, can be copied by copying the demo directory wherever you like. Once the demo is copied you must either change the jviews.home property in the Ant build file, or set the environment variable JVIEWS_HOME, to point to the installation directory where your ILOG JViews products are installed.

Running the Server-Side Samples

The ILOG JViews TGO 8.1 distribution contains several samples that show the use of ILOG JViews TGO 8.1 running in a Web client. To run the server-side samples, the ILOG JViews TGO 8.1 library requires a Web Server that provides Java Servlet 2.3 (or later) with JSP 1.2 technology (or later). The Tomcat Web server is supplied with the ILOG JViews TGO distribution so that you do not need to download a Web server to run these samples. Tomcat is the official reference implementation of the Servlet and JSP specifications. To get more information on Tomcat go to http://tomcat.apache.org/.

For your convenience, all server-side samples are already installed and ready to run under Tomcat. All you have to do is to run the Tomcat Web server.

Here are the steps for running the samples on the Tomcat Web server supplied with the ILOG JViews TGO 8.1 installation:

Note that the DHTML/Ajax samples require one the following browsers:

The server-side samples contain a WAR file (Web Archive) that allows you to easily install the sample. You can check the latest list of servers that support Servlet 2.3 and JSP 1.2 or later at http://java.sun.com/products/servlet/industry.html.

Running the Samples without an X Server

The ILOG JViews TGO 8.1 server-side components can run with the headless support that is built into the Java SE. For more information on this feature, please refer to the Java SE release notes. You can also run with a virtual X server like Xvfb (X Virtual Frame Buffer) if you are not running on Windows. This solution is described on the Sun Web site.

Common Features of Samples

All samples are built on a common baseclass, AbstractSample. This class provides a number of features that some or all samples share. These features are accessible from the File menu of the menu bar that is present in most samples.

This section explains these features, and gives some information on how to build a sample using the common base class. It contains the following topics:

Reloading Business and Style Data

Some samples have menu items labeled Reload business objects and Reload styles. If these are present, they allow you to reload the original files.

If you have made any changes to these files since launching the sample, they will become visible. Otherwise, the original state of the sample will be restored.

How to Use the AbstractSample class

The AbstractSample class provides support for running the same code both as an applet or as a standalone application. You can find the source code for this class in the file <installdir>/samples/<sampledir>/src/AbstractSample.java, for example in:

<installdir>/samples/network/basic/src/AbstractSample.java

This section explains the usage of the class. You may find this useful if you want to extend one of the samples, or write a new sample from scratch. However, you do not need to know the details of the implementation in order to use or understand the samples.

AbstractSample initializes the application correctly depending on the mode of operation, then calls a virtual method doSample(Container), passing the Java Container to which you should add the GUI components. To know whether a sample is being run as an applet or not, you can call the isApplet() method.

To build a sample that can be run as a standalone application, the user should provide a main method, and call the constructor of the sample, as well as the shared initialization method init(Container).

A basic sample framework looks like this:

public class Main extends AbstractSample {
  public static final String sampleTitle = "My Sample";
  public static final int sampleWidth = 770;
  public static final int sampleHeight = 626;

  /**
   * Execute the main part of the sample.
   */
  protected void doSample (Container container) {
    // Initialize JTGO. Check whether we are in an applet or not
    if (isApplet())
      IltSystem.Init(this);
    else
      IltSystem.Init();
    // Add graphic components, for example:
    container.add(new JButton("Hello Sample!!!"));
  }

  public static void main (String[] args) {
    // Create top level Frame
    JFrame frame = new JFrame("ILOG JTGO " + sampleTitle);
    // Create sample instance
    Main sample = new Main();
    // Call base class initialization method
    sample.init(frame.getContentPane());
    // Resize the frame and make it visible
    frame.setSize(sampleWidth,sampleHeight);
    frame.setVisible(true);
  }

If you want to use the default menu bar, you should create a constructor like this:

  public Main(){
    // Create panel with a menubar
    super(sampleTitle, sampleLoggerName, true);
  }

This example creates the sample with a title and a logger name, and specifies whether the default sample menu bar should be created.

Each sample contains a predefined logger, that is created according to the given logger name. The JViews TGO samples have a logger name based on the distribution hierarchy. For example, the logger name in <installdir/samples/network/basic> is "samples.network.basic".

You can use the newly created logger directly in your sample through the sample attribute "log", as follows:

try {
    dataSource.parse(sampleDataSourceFile);
  } catch(Exception e){
    log.severe("Exception caught while reloading business objects");
    e.printStackTrace();
  }

You can customize the default menu bar by overloading the method populateMenuBar(JMenuBar). This method is called by the base class when the menu bar is constructed.

For example, you could write:

  protected void populateMenuBar(JMenuBar menubar) {
    super.populateMenuBar(menubar, true, true);
  }

Here we call another base class method which adds the default actions to reload the business objects and styles.

You can also add your own actions, or remove the predefined ones.

If the default actions to reload the business objects and styles are enabled, you need to overload the virtual methods reloadBusinessObjects and reloadCSSStyles() to provide the desired behavior. These methods might typically look like:

  protected void reloadBusinessObjects() {
    dataSource.clear();
    try {
      dataSource.parse(sampleDataSourceFile);
    } catch(Exception e){
      log.severe("Exception caught while reloading business objects");
      e.printStackTrace();
    }
  }

  protected void reloadCSSStyles(){
    String[] css = new String[] {sampleCSSFilename};
    try {
      networkComponent.setStyleSheets(css);
    } catch(Exception e){
      e.printStackTrace();
    }
 }

You can internationalize the samples by adding labels, mnemonics and titles to a properties file called "resources.SampleMessages".

The contents of this file is available in the sample as a ResourceBundle. To initialize the resource bundle, you need to call the method initContext(IlpContext) just after initializing the JViews TGO application context, as illustrated below:

       // ------------------------
       // Initialize JTGO
       // ------------------------
       // Read a deployment descriptor file to initialize JTGO services
       if (isApplet())
         IltSystem.Init(this, "deploy.xml");
       else
         IltSystem.Init("deploy.xml");

       // Initialize the context and resources
       initContext(IltSystem.GetDefaultContext());

Once the resource bundle is initialized, you can access resources from this resource bundle using the method ResourceUtils.getString(String resourceName). For example:

String selStr = ResourceUtils.getString("label.selection.empty");

Common Features of Faces Samples

Most faces samples are based on the class AbstractSampleContext. This class provides the common features that are needed for the faces samples to operate, initialize, and be easily integrated.

This section helps you understand these features. It contains the following topics:

What is the AbstractSampleContext

The AbstractSampleContext is the abstraction used as a context for the samples. It is abstract because the actual objects that reside in this context are sample-specific. For example, the graphic component that is to be used in a given sample (IlpNetwork or IlpEquipment) is defined at the implementation level by overriding the method AbstractSampleContext.createComponent().

The AbstractSampleContext also ensures that samples have a well-defined approach for initializing themselves. For example, if your sample needs to create custom types, or adjust JViews TGO settings, or create or query resources of some sort, the appropriate approach is to override the method AbstractSampleContext.initializeSample() that is called when the AbstractSampleContext instance is created.

What Objects Does the AbstractSampleContext Provide

In its original form, the AbstractSampleContext provides the IlpContext used in the sample.

In addition, given that most samples are associated with a graphic component (IlpNetwork or IlpEquipment), AbstractSampleContext also hosts a generic IlpGraphicComponent instance. This generic instance can be exposed at the implementation level through a more narrow accessor, like public IlpNetwork getNework() or public IlpEquipment getEquipment().

AbstractSampleContext can provide any object that is of interest. You can create additional bean properties at the implementation level. For example, in the network faces samples, an additional property, network, is created to expose the IlpNetwork.

What Services Does the AbstractSampleContext Provide

The AbstractSampleContext currently provides access to a sample Logger that can be used to log messages of any sort.

How Does the AbstractSampleContext Get Initialized

When AbstractSampleContext is instantiated by the JSF Managed Bean facility, it enforces the following steps in its constructor:

  1. Create the JViews TGO IlpContext implementation used by the sample.
  2. This is done by calling AbstractSampleContext.createContext(), which by default returns an IltFacesDefaultContext instance.
  3. Initialize the context with the provided deployment file.
  4. It uses by default a deployment descriptor file that is located in resources/configuration/deployment/deploy.xml. You can override this default setting by using the AbstractSampleContext(String deploymentFile, String loggerName) constructor.
  5. Initialize the sample's Logger.
  6. Initialize the sample by calling AbstractSampleContext.initializeSample().
  7. Create the graphic component by calling AbstractSampleContext.createComponent ().

How Can I Access the AbstractSampleContext

The samples can access the AbstractSampleContext instance through the JSF Managed Bean facility. The typical name for the managed bean is sampleContext and the default scope is session.

In your JSP page, you can access the sample context by using the standard JSF Expression Language syntax. For example, to access the IlpContext bean property of the AbstractSampleContext, you would use this Expression Language fragment: "#{sampleContext.context}".

How Do the JViews TGO Faces Samples Use the AbstractSampleContext

Each JViews TGO faces sample has a concrete implementation of the AbstractSampleContext, named SampleContext, which implements the protected abstract methods to provide the necessary logic.

The SampleContext also adds bean properties that are needed for each specific sample.

Tip
Check the JViews TGO faces samples for concrete examples of what has been explained in this section.

Customer Support | Help
Copyright © 1987-2007 ILOG S.A. All rights reserved.   Documentation homepage.    All rights reserved. Legal terms.