Back to index

wims  3.65+svn20090927
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
ptolemy.plot.PlotFrame Class Reference

PlotFrame is a versatile two-dimensional data plotter that runs as part of an application, but in its own window. More...

Inheritance diagram for ptolemy.plot.PlotFrame:
Inheritance graph
[legend]
Collaboration diagram for ptolemy.plot.PlotFrame:
Collaboration graph
[legend]

List of all members.

Classes

class  EPSFileFilter
 Display only .eps files. More...
class  FileMenuListener
class  FormatListener
class  PLTOrXMLFileFilter
 Display only .plt and .xml files. More...
class  SpecialMenuListener

Public Member Functions

 PlotFrame ()
 Construct a plot frame with a default title and by default contains an instance of Plot.
 PlotFrame (String title)
 Construct a plot frame with the specified title and by default contains an instance of Plot.
 PlotFrame (String title, PlotBox plotArg)
 Construct a plot frame with the specified title and the specified instance of PlotBox.
void samplePlot ()
 Create a sample plot.
void setVisible (boolean visible)
 Set the visibility.

Public Attributes

PlotBox plot
 The plot object held by this frame.

Protected Member Functions

void _about ()
void _close ()
 Close the window.
void _editFormat ()
 Interactively edit the file format in a modal dialog.
void _export ()
 Query the user for a filename and export the plot to that file.
void _help ()
 Display more detailed information than given by _about().
void _open ()
 Open a new file and plot its data.
void _print ()
 Print the plot.
void _printCrossPlatform ()
 Print using the cross platform dialog.
void _printNative ()
 Print using the native dialog.
void _read (URL base, InputStream in) throws IOException
 Read the specified stream.
void _save ()
 Save the plot to the current file, determined by the and _file protected variable.
void _saveAs ()
 Query the user for a filename and save the plot to that file.

Protected Attributes

JMenuBar _menubar = new JMenuBar()
 Menubar for this frame.
JMenu _editMenu = new JMenu("Edit")
 Edit menu for this frame.
JMenu _fileMenu = new JMenu("File")
 File menu for this frame.
JMenu _specialMenu = new JMenu("Special")
 Special menu for this frame.
File _directory = null
 Directory that contains the input file.
File _file = null
 The input file.

Detailed Description

PlotFrame is a versatile two-dimensional data plotter that runs as part of an application, but in its own window.

It can read files compatible with the old Ptolemy plot file format (currently only ASCII). It is extended with the capability to read PlotML files in PlotMLFrame. An application can also interact directly with the contained Plot object, which is visible as a public member, by invoking its methods.

An application that uses this class should set up the handling of window-closing events. Presumably, the application will exit when all windows have been closed. This is done with code something like:

 plotFrameInstance.addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent e) {
Handle the event
 }
 });
 

PlotFrame contains an instance of PlotBox. PlotBox is the base class for classes with drawing capability, e.g. Plot, LogicAnalyzer. If not specified in the constructor, the default is to contain a Plot object. This field is set once in the constructor and immutable afterwards.

See also:
Plot
PlotBox
Author:
Christopher Brooks and Edward A. Lee
Version:
Id:
PlotFrame.java,v 1.84 2007/12/16 07:29:48 cxh Exp
Since:
Ptolemy II 0.2 .ProposedRating Yellow (cxh) .AcceptedRating Yellow (cxh)

Definition at line 98 of file PlotFrame.java.


Constructor & Destructor Documentation

Construct a plot frame with a default title and by default contains an instance of Plot.

After constructing this, it is necessary to call setVisible(true) to make the plot appear.

Definition at line 103 of file PlotFrame.java.

                       {
        this("Ptolemy Plot Frame");
    }
ptolemy.plot.PlotFrame.PlotFrame ( String  title) [inline]

Construct a plot frame with the specified title and by default contains an instance of Plot.

After constructing this, it is necessary to call setVisible(true) to make the plot appear.

Parameters:
titleThe title to put on the window.

Definition at line 112 of file PlotFrame.java.

                                   {
        this(title, null);
    }
ptolemy.plot.PlotFrame.PlotFrame ( String  title,
PlotBox  plotArg 
) [inline]

Construct a plot frame with the specified title and the specified instance of PlotBox.

After constructing this, it is necessary to call setVisible(true) to make the plot appear.

Parameters:
titleThe title to put on the window.
plotArgthe plot object to put in the frame, or null to create an instance of Plot.

Definition at line 123 of file PlotFrame.java.

                                                    {
        super(title);

        // The Java look & feel is pretty lame, so we use the native
        // look and feel of the platform we are running on.
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // Ignore exceptions, which only result in the wrong look and feel.
        }

        if (plotArg == null) {
            plot = new Plot();
        } else {
            plot = plotArg;
        }

        // Background color is a light grey.
        plot.setBackground(new Color(0xe5e5e5));

        _fileMenu.setMnemonic(KeyEvent.VK_F);
        _editMenu.setMnemonic(KeyEvent.VK_E);
        _specialMenu.setMnemonic(KeyEvent.VK_S);

        // File menu
        JMenuItem[] fileMenuItems = { new JMenuItem("Open", KeyEvent.VK_O),
                new JMenuItem("Save", KeyEvent.VK_S),
                new JMenuItem("SaveAs", KeyEvent.VK_A),
                new JMenuItem("Export", KeyEvent.VK_E),
                new JMenuItem("Print", KeyEvent.VK_P),
                new JMenuItem("Close", KeyEvent.VK_C), };

        // Open button = ctrl-o.
        fileMenuItems[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
                Event.CTRL_MASK));

        // Save button = ctrl-s.
        fileMenuItems[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                Event.CTRL_MASK));

        // Print button = ctrl-p.
        fileMenuItems[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
                Event.CTRL_MASK));

        // Close button = ctrl-w.
        fileMenuItems[5].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
                Event.CTRL_MASK));

        FileMenuListener fml = new FileMenuListener();

        // Set the action command and listener for each menu item.
        for (int i = 0; i < fileMenuItems.length; i++) {
            fileMenuItems[i].setActionCommand(fileMenuItems[i].getText());
            fileMenuItems[i].addActionListener(fml);
            _fileMenu.add(fileMenuItems[i]);
        }

        _menubar.add(_fileMenu);

        // Edit menu
        JMenuItem format = new JMenuItem("Format", KeyEvent.VK_F);
        FormatListener formatListener = new FormatListener();
        format.addActionListener(formatListener);
        _editMenu.add(format);
        _menubar.add(_editMenu);

        // Special menu
        JMenuItem[] specialMenuItems = { new JMenuItem("About", KeyEvent.VK_A),
                new JMenuItem("Help", KeyEvent.VK_H),
                new JMenuItem("Clear", KeyEvent.VK_C),
                new JMenuItem("Fill", KeyEvent.VK_F),
                new JMenuItem("Reset axes", KeyEvent.VK_R),
                new JMenuItem("Sample plot", KeyEvent.VK_S), };
        SpecialMenuListener sml = new SpecialMenuListener();

        // Set the action command and listener for each menu item.
        for (int i = 0; i < specialMenuItems.length; i++) {
            specialMenuItems[i].setActionCommand(specialMenuItems[i].getText());
            specialMenuItems[i].addActionListener(sml);
            _specialMenu.add(specialMenuItems[i]);
        }

        _menubar.add(_specialMenu);

        setJMenuBar(_menubar);

        getContentPane().add(plot, BorderLayout.CENTER);

        // FIXME: This should not be hardwired in here.
        setSize(500, 300);

        // Center.
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        int x = (screenSize.width - frameSize.width) / 2;
        int y = (screenSize.height - frameSize.height) / 2;
        setLocation(x, y);
    }

Here is the call graph for this function:


Member Function Documentation

void ptolemy.plot.PlotFrame._about ( ) [inline, protected]

Reimplemented in ptolemy.plot.PlotApplication.

Definition at line 276 of file PlotFrame.java.

                            {
        JOptionPane
                .showMessageDialog(
                        this,
                        "PlotFrame class\n"
                                + "By: Edward A. Lee "
                                + "and Christopher Brooks\n"
                                + "Version "
                                + PlotBox.PTPLOT_RELEASE
                                + ", Build: $Id: PlotFrame.java,v 1.84 2007/12/16 07:29:48 cxh Exp $\n\n"
                                + "For more information, see\n"
                                + "http://ptolemy.eecs.berkeley.edu/java/ptplot\n\n"
                                + "Copyright (c) 1997-2005, "
                                + "The Regents of the University of California.",
                        "About Ptolemy Plot", JOptionPane.INFORMATION_MESSAGE);
    }

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._close ( ) [inline, protected]

Close the window.

Reimplemented in ptolemy.plot.PlotApplication.

Definition at line 295 of file PlotFrame.java.

                            {
        dispose();
    }

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._editFormat ( ) [inline, protected]

Interactively edit the file format in a modal dialog.

Definition at line 301 of file PlotFrame.java.

                                 {
        PlotFormatter fmt = new PlotFormatter(plot);
        fmt.openModal();
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._export ( ) [inline, protected]

Query the user for a filename and export the plot to that file.

Currently, the only supported format is EPS.

Definition at line 309 of file PlotFrame.java.

                             {
        JFileChooser fileDialog = new JFileChooser();
        fileDialog.addChoosableFileFilter(new EPSFileFilter());
        fileDialog.setDialogTitle("Export EPS to...");

        if (_directory != null) {
            fileDialog.setCurrentDirectory(_directory);
        } else {
            // The default on Windows is to open at user.home, which is
            // typically an absurd directory inside the O/S installation.
            // So we use the current directory instead.
            String cwd = StringUtilities.getProperty("user.dir");

            if (cwd != null) {
                fileDialog.setCurrentDirectory(new File(cwd));
            }
        }

        fileDialog.setSelectedFile(new File(fileDialog.getCurrentDirectory(),
                "plot.eps"));

        int returnVal = fileDialog.showDialog(this, "Export");

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileDialog.getSelectedFile();

            try {
                FileOutputStream fout = null;

                try {
                    fout = new FileOutputStream(file);
                    plot.export(fout);
                } finally {
                    try {
                        fout.close();
                    } catch (Throwable throwable) {
                        System.out.println("Ignoring failure to close stream "
                                + "on " + file);
                        throwable.printStackTrace();
                    }
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Error exporting plot: "
                        + ex, "Ptolemy II Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._help ( ) [inline, protected]

Display more detailed information than given by _about().

Reimplemented in ptolemy.plot.PlotApplication.

Definition at line 359 of file PlotFrame.java.

                           {
        JOptionPane.showMessageDialog(this,
                "PlotFrame is a plot in a top-level window.\n"
                        + "  File formats understood: Ptplot ASCII.\n"
                        + "  Left mouse button: Zooming.",
                "About Ptolemy Plot", JOptionPane.INFORMATION_MESSAGE);
    }

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._open ( ) [inline, protected]

Open a new file and plot its data.

Definition at line 369 of file PlotFrame.java.

                           {
        JFileChooser fileDialog = new JFileChooser();
        fileDialog.setDialogTitle("Select a plot file");

        // Filter file names.
        fileDialog.addChoosableFileFilter(new PLTOrXMLFileFilter());

        if (_directory != null) {
            fileDialog.setCurrentDirectory(_directory);
        } else {
            // The default on Windows is to open at user.home, which is
            // typically an absurd directory inside the O/S installation.
            // So we use the current directory instead.
            String cwd = StringUtilities.getProperty("user.dir");

            if (cwd != null) {
                fileDialog.setCurrentDirectory(new File(cwd));
            }
        }

        int returnVal = fileDialog.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            _file = fileDialog.getSelectedFile();
            setTitle(_file.getName());
            _directory = fileDialog.getCurrentDirectory();

            try {
                plot.clear(true);
                _read(new URL("file", null, _directory.getAbsolutePath()),
                        new FileInputStream(_file));
                plot.repaint();
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(this, "File not found:\n"
                        + ex.toString(), "Ptolemy Plot Error",
                        JOptionPane.WARNING_MESSAGE);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Error reading input:\n"
                        + ex.toString(), "Ptolemy Plot Error",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._print ( ) [inline, protected]

Print the plot.

Definition at line 415 of file PlotFrame.java.

                            {
        _printCrossPlatform();

        //_printNative();
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._printCrossPlatform ( ) [inline, protected]

Print using the cross platform dialog.

FIXME: this dialog is slow and is often hidden behind other windows. However, it does honor the user's choice of portrait vs. landscape

Definition at line 426 of file PlotFrame.java.

                                         {
        // Build a set of attributes
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(plot);

        if (job.printDialog(aset)) {
            try {
                job.print(aset);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Printing failed:\n"
                        + ex.toString(), "Print Error",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    }

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._printNative ( ) [inline, protected]

Print using the native dialog.

FIXME: This method does not seem to honor the user's choice of portrait vs. landscape.

Definition at line 447 of file PlotFrame.java.

                                  {
        PrinterJob job = PrinterJob.getPrinterJob();

        //PageFormat pageFormat = job.defaultPage();
        //job.setPrintable(plot, pageFormat);
        job.setPrintable(plot);

        if (job.printDialog()) {
            try {
                // job.print() eventually
                // calls PlotBox.print(Graphics, PageFormat)
                job.print();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Printing failed:\n"
                        + ex.toString(), "Print Error",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    }
void ptolemy.plot.PlotFrame._read ( URL  base,
InputStream  in 
) throws IOException [inline, protected]

Read the specified stream.

Derived classes may override this to support other file formats.

Parameters:
baseThe base for relative file references, or null if there are not relative file references.
inThe input stream.
Exceptions:
IOExceptionIf the stream cannot be read.

Definition at line 474 of file PlotFrame.java.

                                                                      {
        plot.read(in);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._save ( ) [inline, protected]

Save the plot to the current file, determined by the and _file protected variable.

Definition at line 481 of file PlotFrame.java.

                           {
        if (_file != null) {
            try {
                FileOutputStream fout = new FileOutputStream(_file);
                plot.write(fout);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Error writing file:\n"
                        + ex.toString(), "Ptolemy Plot Error",
                        JOptionPane.WARNING_MESSAGE);
            }
        } else {
            _saveAs();
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame._saveAs ( ) [inline, protected]

Query the user for a filename and save the plot to that file.

Definition at line 498 of file PlotFrame.java.

                             {
        JFileChooser fileDialog = new JFileChooser();
        fileDialog.addChoosableFileFilter(new PLTOrXMLFileFilter());
        fileDialog.setDialogTitle("Save plot as...");

        if (_directory != null) {
            fileDialog.setCurrentDirectory(_directory);
        } else {
            // The default on Windows is to open at user.home, which is
            // typically an absurd directory inside the O/S installation.
            // So we use the current directory instead.
            String cwd = StringUtilities.getProperty("user.dir");

            if (cwd != null) {
                fileDialog.setCurrentDirectory(new File(cwd));
            }
        }

        fileDialog.setSelectedFile(new File(fileDialog.getCurrentDirectory(),
                "plot.xml"));

        int returnVal = fileDialog.showSaveDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            _file = fileDialog.getSelectedFile();
            setTitle(_file.getName());
            _directory = fileDialog.getCurrentDirectory();
            _save();
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Create a sample plot.

Definition at line 227 of file PlotFrame.java.

                             {
        _file = null;
        _directory = null;
        plot.samplePlot();
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void ptolemy.plot.PlotFrame.setVisible ( boolean  visible) [inline]

Set the visibility.

As a side effect, this method sets the background of the menus.

Parameters:
visibleTrue if the Frame is to be visible, false if it is not visible.

Definition at line 238 of file PlotFrame.java.

                                            {
        super.setVisible(visible);
        _editMenu.setBackground(_menubar.getBackground());
        _fileMenu.setBackground(_menubar.getBackground());
        _specialMenu.setBackground(_menubar.getBackground());
    }

Here is the caller graph for this function:


Member Data Documentation

File ptolemy.plot.PlotFrame._directory = null [protected]

Directory that contains the input file.

Definition at line 269 of file PlotFrame.java.

JMenu ptolemy.plot.PlotFrame._editMenu = new JMenu("Edit") [protected]

Edit menu for this frame.

Definition at line 260 of file PlotFrame.java.

File ptolemy.plot.PlotFrame._file = null [protected]

The input file.

Definition at line 272 of file PlotFrame.java.

JMenu ptolemy.plot.PlotFrame._fileMenu = new JMenu("File") [protected]

File menu for this frame.

Definition at line 263 of file PlotFrame.java.

JMenuBar ptolemy.plot.PlotFrame._menubar = new JMenuBar() [protected]

Menubar for this frame.

Definition at line 257 of file PlotFrame.java.

JMenu ptolemy.plot.PlotFrame._specialMenu = new JMenu("Special") [protected]

Special menu for this frame.

Definition at line 266 of file PlotFrame.java.

The plot object held by this frame.

Definition at line 251 of file PlotFrame.java.


The documentation for this class was generated from the following file: