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("Rogue Wave® 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 globalize 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");