Example: Using IFSTextFileDocument

Note: Read the Code example disclaimer for important legal information.
/////////////////////////////////////////////////////////////////////////
//
// IFS text file document example.  This program demonstrates how to
// use a document that is associated with a text file in the AS/400
// integrated file system.
//
// Command syntax:
//    IFSTextFileDocumentExample system path
//
/////////////////////////////////////////////////////////////////////////

import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class IFSTextFileDocumentExample
{



    private static IFSTextFileDocument  document;
    private static JTextPane            text;



    public static void main (String[] args)
    {
        // If a system or path was not specified, then display
        // help text and exit.
        if (args.length != 2)
        {
            System.out.println("Usage:  IFSTextFileDocumentExample system path");
            return;
        }

        try
        {
            // Create two frames.
            JFrame f = new JFrame ("IFS text file document example");

            // Create an error dialog adapter.  This will display
            // any errors to the user.
            ErrorDialogAdapter errorHandler = new ErrorDialogAdapter (f);

            // Create a working cursor adapter.  This will adjust
            // the cursor whenever the text file is read or written.
            WorkingCursorAdapter cursorAdapter = new WorkingCursorAdapter (f);

            // Create an AS400 object.  The system name was passed
            // as the first command line argument.
            AS400 system = new AS400 (args[0]);

            // Create and load the IFS text file document.
            document = new IFSTextFileDocument (system, args[1]);
            document.addErrorListener (errorHandler);
            document.addWorkingListener (cursorAdapter);
            document.load ();

            // Create the text pane used to present the document.
            text = new JTextPane (document);
            text.setSize (new Dimension (500, 500));

            // Set up a scroll pane to use with the text pane.
            JScrollPane scroll = new JScrollPane (text);
            scroll.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

            // Create a menu bar with a single menu.
            MenuBar menuBar = new MenuBar ();
            Menu menu = new Menu ("File");
            menuBar.add (menu);

            // Add menu items to load and save.
            MenuItem load = new MenuItem ("Load");
            load.addActionListener (new ActionListener ()
            {
                public void actionPerformed (ActionEvent event)
                {
                    document.load ();
                }
            });
            menu.add (load);

            MenuItem save = new MenuItem ("Save");
            save.addActionListener (new ActionListener ()
            {
                public void actionPerformed (ActionEvent event)
                {
                    document.save ();
                }
            });
            menu.add (save);

            // When the the frame closes, exit.
            f.addWindowListener (new WindowAdapter () {
                public void windowClosing (WindowEvent event)
                {
                    System.exit (0);
                }
            });

            // Layout the frame.
            f.getContentPane ().setLayout (new BorderLayout ());
            f.getContentPane ().add ("Center", scroll);
            f.setMenuBar (menuBar);
            f.pack ();
            f.show ();
        }
        catch (Exception e)
        {
           System.out.println ("Error: " + e.getMessage ());
           System.exit (0);
        }
    }


}