/**
* FinchDisplayAbstract
*
* Main window of the program. The primary components are:
*
* - A menu for file manipulation (read and merge both text and binary files,
* and write binary files) and for exiting the program.
*
- A name filter TextField
*
- Buttons for executing the filtered action list in forward and reverse orders
*
- A panel that contains the current (filtered) set of actions (one action per
* line). Use a JList for this. Also note that you must handle the case where
* this list does not fit within the window.
*
- A set of buttons for manipulating individual finch actions: creating
* new actions, editing a selected action, deleting one or more selected actions, and
* cleaning (deleting duplicate entries).
*
*
*
* Notes:
*
* - The FinchDisplay will not directly manipulate the master Finch Action list.
* Instead, these manipulations are implemented through the FinchData class.
*
*
*
* @author Andrew H. Fagg
*
*/
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import javax.swing.border.*;
abstract public class FinchDisplayAbstract extends JFrame {
/**
* Reference to the FinchData structure.
*/
protected FinchData finchData;
/**
* Menu bar
*
*/
protected JMenuBar menuBar = new JMenuBar();
/**
* The file menu
*/
protected JMenu fileMenu = new JMenu("File");;
// Menu Items
protected JMenuItem jmiLoadTextFile = new JMenuItem("Load Text File");
protected JMenuItem jmiMergeTextFile = new JMenuItem("Merge Text File");
protected JMenuItem jmiLoadBinaryFile = new JMenuItem("Load Binary File");
protected JMenuItem jmiMergeBinaryFile = new JMenuItem("Merge Binary File");
protected JMenuItem jmiSaveBinaryFile = new JMenuItem("Save Binary File");
protected JMenuItem jmiExit = new JMenuItem("Exit");
/**
* Set to true if the program is to continue running. False will force
* the program to quit.
*/
protected boolean closeFlag = true;
/**
* Constructor
*
* @param finchData Reference to the existing FinchData object
*/
public FinchDisplayAbstract(FinchData finchData)
{
// Save reference to the finchData object
this.finchData = finchData;
// Create the menu bar
this.setJMenuBar(this.menuBar);
// Add the File menu to the menu bar
this.menuBar.add(this.fileMenu);
// Put the menu together
this.fileMenu.add(this.jmiLoadTextFile);
this.fileMenu.add(this.jmiMergeTextFile);
this.fileMenu.add(this.jmiLoadBinaryFile);
this.fileMenu.add(this.jmiMergeBinaryFile);
this.fileMenu.add(this.jmiSaveBinaryFile);
this.fileMenu.addSeparator();
this.fileMenu.add(this.jmiExit);
// Set up action listener for Load Text File
this.jmiLoadTextFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
loadText(false);
}
});
// Set up action listener for Merge Text File
this.jmiMergeTextFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
loadText(true);
}
});
// Set up action listener for Load Binary File
this.jmiLoadBinaryFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
loadBinary(false);
}
});
// Set up action listener for Merge Binary File
this.jmiMergeBinaryFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
loadBinary(true);
}
});
// Set up action listener for Save Binary File
this.jmiSaveBinaryFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
saveBinary();
}
});
// Set up action listener for Merge Text File
this.jmiExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
exitChooser();
}
});
//////////////////////////
// Detect when the window is closed
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event) {
closeFinch();
System.out.println("closing...");
}
});
};
///////////////////////////////////////////////////////////////////////////////////
/**
* Open the FinchAction editing dialog box.
*
*
* Algorithm:
*
* - If editing an existing action and an action has been selected, then
* pass this action to the dialog box (use: dialog.setAction())
*
- Otherwise, pass a "null" to the dialog box
*
- Open the dialog box: use the dialog.setVisible() method
*
- Get the action returned from the dialog box
*
- If there is no action, then do nothing
*
- If we were editing an action, then remove the old one from
* the master Finch action list
*
- Add the new action to the master Finch action list
*
- Update the display of the filtered Finch action list
*
*
*
*
* @param existFlag true if we are editing an existing action ('Edit' has
* been pressed).
*/
abstract protected void dialogAction(boolean existFlag);
/**
* Delete any actions that have been selected.
*
*
* Algorithm:
*
* - Get the list of selected actions from the JList
*
- For each action in the list, delete it from the master
* action list
*
- Update the display of the filtered action list
*
*/
abstract protected void delete_list_items();
/**
* Clean the master action list by removing duplicates.
*
*
* Algorithm:
*
* - Clean the master action list
*
- Update the display of the filtered action list
*
*
*/
abstract protected void clean();
/**
* Execute the filtered action list
*
*
* Algorithm:
*
* - Extract the filter string (and handle the case when "" is specified)
*
- Execute the master action list with the filter string as a parameter
*
*
* @param reverse True if the actions should be executed in reverse order
*/
abstract protected void execute(boolean reverse);
/**
* Reset the display of the filtered action list
*
*
* Algorithm:
*
* - Extract the filter string (and handle the case when "" is specified)
*
- Get the filtered action list (from finchData)
*
- Update the list of actions (in JList)
*
- Force the JList to repaint()
*
*
*/
abstract protected void resetActionList();
/**
* Initiate the closing down of the program
*
*/
protected void closeFinch() {
closeFlag = false;
System.out.println("Initiate closing...");
}
/**
* Load a text file
*
*
* Algorithm:
*
* - Open a JFileChooser. This file chooser should have an initial directory
* of "." and only allow the selection
* of files with a "txt" extension.
*
- If a valid file has been selected (the chooser returns a state of
* JFileChooser.APPROVE_OPTION), then load the data file and update the master
* action list.
*
*
* @param merge_flag True indicates that the loaded set of actions should be
* merged with the existing ones.
*/
abstract protected void loadText(boolean merge_flag);
/**
* Load a binary file
*
*
* Algorithm:
*
* - Open a JFileChooser. This file chooser should have an initial directory
* of "." and only allow the selection
* of files with a "dat" extension.
*
- If a valid file has been selected (the chooser returns a state of
* JFileChooser.APPROVE_OPTION), then load the data file and update the master
* action list.
*
*
* @param merge_flag True indicates that the loaded set of actions should be
* merged with the existing ones.
*/
abstract protected void loadBinary(boolean merge_flag);
/**
* Save a binary file
*
*
* Algorithm:
*
* - Open a JFileChooser. This file chooser should have an initial directory
* of "." and only allow the selection
* of files with a "dat" extension.
*
- If a valid file has been selected (the chooser returns a state of
* JFileChooser.APPROVE_OPTION), then write the master action list to the
* binary file.
*
*
*/
abstract protected void saveBinary();
/**
*
*
*
* Open a ConfirmDialog box that asks whether the user really wants to
* quit the program. If "Yes" is selected, then the program is shut
* down by calling closeFinch()
*
* Postconditions:
*
* - this.closeFinch() will have been called if indicated by the user.
*
*
*
*
* Notes:
*
* - JOptionPane.showConfirmDialog will return a "0" if "Yes" is selected.
*
- The dialog box should only present two buttons: "Yes" and "No"
*
*
*/
protected void exitChooser() {
int ret = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?",
"Exiting ...",
JOptionPane.YES_NO_OPTION);
if(ret == 0){
// The "Yes" button was pressed
closeFinch();
}
}
/**
* Indicate whether the program should continue to execute
*
* @return True if the program should continue running (this method is
* used by the driver method to continue it's wait for the termination of the
* program.
*/
public boolean getCloseFlag(){
return(closeFlag);
}
}