Sample program using business-oriented model.

Java Means Business
Business-oriented models for complex Swing apps
Lowell Kaplan
Listing 5. Sample program using business-oriented model.


import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class Test
{
    private MasterDataModel masterModel;

    private QuoteBySectorTableModel qbsTableModel;
    private JTable table;

    private QuoteSectorTreeModel qsTreeModel;
    private JTree tree;

    public Test()
    {
        JFrame f = new JFrame(“Stock Quotes by Sector”);
        masterModel = new MasterDataModel();
        qbsTableModel = new QuoteBySectorTableModel(masterModel);
        qsTreeModel = new QuoteSectorTreeModel(masterModel);

        table = new JTable(qbsTableModel);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        tree = new JTree(qsTreeModel);
        tree.setRootVisible(false);

        tree.getSelectionModel().
    setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeSelectionListener(new TreeSelectionListener()
        {
            public void valueChanged(TreeSelectionEvent e)
            {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if (node == null)
                return;

            Sector sector = (Sector)node.getUserObject();
            qbsTableModel.setSector(sector);
        }
});

        populateData();

        JScrollPane treeScroller = new JScrollPane(tree);
        JScrollPane tableScroller = new JScrollPane(table);
        JSplitPane splitter = new
        JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroller, tableScroller);
        f.getContentPane().add(splitter);

        f.pack();
        f.setVisible(true);
    }

    private void populateData()
    {
        Sector technology = new Sector(“Technology”, null);
        Sector computers = new Sector(“Computer Hardware”, technology);
        Sector semiconductors = new Sector(“Semiconductors”, technology);
        Sector software = new Sector(“Software”, technology);
        Sector financial = new Sector(“Financial”, null);
        Sector insurance = new Sector(“Insurance”, financial);
        Sector investments = new Sector(“Investment Services”, financial);

        Stock aapl = new Stock(“AAPL”, computers);
        Stock cpq = new Stock(“CPQ”, computers);
        Stock dell = new Stock(“DELL”, computers);
        Stock txn = new Stock(“TXN”, semiconductors);
        Stock msft = new Stock(“MSFT”, software);
        Stock ratl = new Stock(“RATL”, software);
        Stock aig = new Stock(“AIG”, insurance);
        Stock cb = new Stock(“CB”, insurance);
        Stock gs = new Stock(“GS”, investments);
        Stock mwd = new Stock(“MWD”, investments);

        StockQuote q = new StockQuote(aapl, 18, -.25, 5600000);
        masterModel.receivedQuote(q);
        q = new StockQuote(cpq, 21.25, +.05, 8800000);
        masterModel.receivedQuote(q);
        q = new StockQuote(dell, 22, +0, 27700000);
        masterModel.receivedQuote(q);
        q = new StockQuote(txn, 34.50, +.50, 12900000);
        masterModel.receivedQuote(q);
        q = new StockQuote(msft, 55.88, -1.25, 30300000);
        masterModel.receivedQuote(q);
        q = new StockQuote(ratl, 41.62, +1, 1600000);
        masterModel.receivedQuote(q);
        q = new StockQuote(aig, 84, +.25, 3300000);
        masterModel.receivedQuote(q);
        q = new StockQuote(cb, 72.75, -.25, 1100000);
        masterModel.receivedQuote(q);
        q = new StockQuote(gs, 96, +0, 3200000);
        masterModel.receivedQuote(q);
        q = new StockQuote(mwd, 68.75, +1.25, 7200000);
        masterModel.receivedQuote(q);
    }

    public static void main(String[] args)
    {
        Test test = new Test();
    }
}