/** * Dialog box for editing individual Finch Actions. * *

* Approach: *

* *

* @author Andrew H. Fagg * */ import javax.swing.JColorChooser; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.SpringLayout; 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 FinchActionDialogAbstract extends JDialog { protected FinchData finchData; // new for project 5 // Panel that contains a label and a combo box to represent the different // types of Finch Actions. protected JComboBox aComboBox = new JComboBox(new String[]{"Jog", "Jog Scaled", "Beak Color", "Tone", "Guarded Jog", "Guarded Orientation", "Turn", "Meta-Action"}); protected JLabel lComboBox = new JLabel("Action Type: "); protected JPanel pComboBox = new JPanel(); protected JPanel panel = new JPanel(); // Main panel // Index constants that correspond to the strings used to define the Combo Box static final protected int JOG = 0; static final protected int JOG_SCALED = 1; static final protected int BEAK = 2; static final protected int TONE = 3; static final protected int GUARDED_JOG = 4; static final protected int GUARDED_ORIENT = 5; static final protected int TURN = 6; static final protected int META = 7; // Panel that contains the label and the text field for the name of the action protected JTextField aName = new JTextField("", 15); protected JLabel lName = new JLabel("Name: "); protected JPanel pName = new JPanel(); // Panel that contains the label and slider for the duration protected JSlider aDuration = new JSlider(0, 10000, 1000); protected JLabel lDuration = new JLabel("Duration (ms): "); protected JPanel pDuration = new JPanel(); // Panel containing the buttons at the bottom of the dialog box protected JPanel jpbuttons = new JPanel(); protected JButton jbtOK = new JButton("OK"); protected JButton jbtCancel = new JButton("Cancel"); /////// // Finch action that corresponds to the values entered into the // dialog box. This field is filled in as the dialog box is // closing. A value of null indicates no action has been specified // (e.g., in the case of a 'Cancel' action). protected FinchAction action; // The nose color for the FlashBeak Action protected Color noseColor = new Color(0,0,0); /////////////////////////////////////////////////////// /** * Constructor * *

* Creates the action type combo box and the action name. * */ public FinchActionDialogAbstract(FinchData finchData){ // Call the JDialog constructor super((java.awt.Frame) null, true); //panel.setLayout(new SpringLayout()); // Remember the reference to the FinchData object this.finchData = finchData; // Configure the look of the dialog box this.setTitle("Edit Finch Action"); //this.setLayout(new GridLayout(0,1, 10, 10)); panel.setLayout(new GridLayout(0,2,10,10)); //this.setLayout(new SpringLayout()); ////// // First row: combo box containing the combo box for selecting action type // Label pComboBox.add(lComboBox); // Combo box pComboBox.add(aComboBox); // Add this panel to the dialog frame this.add(pComboBox, BorderLayout.NORTH); // When the combo box state changes, update the fields that are visible aComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ reconfigureDialog(); } }); ////// // Second row: Text field containing the name of the Finch Action this.add(panel, BorderLayout.CENTER); // Label pName.add(lName); // The actual text field pName.add(aName); // Add this panel to the dialog frame panel.add(pName); //////////////////////// // Action listeners for the Buttons. Note: // the buttons have not been added to the frame // OK jbtOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ // Create the action that corresponds to the values in the dialog box createNewAction(); // Close the dialog box setVisible(false); } }); // Cancel button jbtCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ // Close the dialog box setVisible(false); } }); ///////////////////////////////////////////////////////////// } /** * Given an existing FinchAction, configure the dialog box values * to reflect this action as soon as it is opened. *

* *

* Algorithm: *

    *
  1. Set all of the dialog box values to reasonable defaults *
  2. For the specified action, copy the values from the action * into the relevant dialog box values. Note that any action type * will only specify a subset of the dialog box values. *
* * @param action A FinchAction or null * */ abstract public void setAction(FinchAction action); /** * Create a FinchAction that corresponds to the current configuration * of the dialog box. * *

* * Postconditions: *

* *

* * Algorithm: *

* */ abstract protected void createNewAction(); //////////////////////////////////// /** * Accessor to action * * @return The new FinchAction that has been created by the dialog box * (may be null) */ public FinchAction getAction(){ return(action); } /** * Reconfigure the look of the dialog box to show only the components that * correspond to the Finch Action type that is selected by the combo box. * *

* Algorithm: *

    *
  1. For all property values that are not part of all Finch Action types, * turn off the visibility (using the setVisible() method for the panels). *
  2. For the currently selected Finch Action type (determined by the combo * box), turn the visibility on for the necessary components. *
  3. If the action type is FlashBeak, then set the color of the component * that indicates the currently selected color of this.noseColor *
* */ abstract protected void reconfigureDialog(); /** * Open the color chooser dialog box to specify the nose color * *

* * Algorithm: *

    *
  1. Open a color chooser dialog box. *
  2. If the returned color is valid, then copy this color to * this.noseColor *
*/ abstract protected void colorChooser(); }