package edu.calpoly.cpe205.fetter; import javax.swing.*; import java.util.*; import java.io.*; import java.lang.reflect.*; import java.lang.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; /** * This class is used by the InstantiateClassDialog to hold the GUI components * used to display a visual representation of a constructor and its * parameters. * * @see InstantiateClassDialog */ // Author: Michael Hebron // Version History: // Nov 21, 2000 - comments and pseudo code first added // Nov 30, 2000 - update class description, made class public // Jan 17, 2001 - (Jonathon Lee) - added ETA.out calls to all functions and calls to non-jdk2 classes per pseudocode // Feb 10, 2001 - (Jonathon Lee) - updated to coding standards public class ConstructorRow { /** * Creates a new instance of a ConstructorRow *
* Pre-conditions: The ParameterObjectComboBoxes in
* Pre-conditions:
* Pre-conditions: none
* @return a reference to the radio button for a constructor
*/
public JRadioButton getConstructorButton()
{
// RETURN constructorButton
return constructorButton;
}
/**
* The ParameterObjectComboBoxes used to represent the parameters
* for a constructor
*/
protected ParameterObjectComboBox[] parameterValues;
/**
* A JRadioButton used to display the name of a constructor
*/
protected JRadioButton constructorButton;
/**
* This class is used by the ConstructorRow to listen to the constructorButton
* and enable or disable all of the
* ParameterObjectComboBoxes associated with the parameters for the constructor
*/
protected class ParameterEnabler implements ChangeListener
{
/**
* Enables or disables all of the ParameterObjectComboBoxes for a
* constructor, depending on whether the source of the event
* (values
* must already be set up as ParameterDataListeners on the model
* Post-conditions: The fields of the ConstructorRow become initialized
* @param constructorName A JRadioButton whose name is that of a constructor in
* the form
* ObjectName:(Type, Type, ...)
* @param values an array of ParameterObjectComboBoxes representing parameters
* for a constructor
*/
public ConstructorRow(JRadioButton constructorName, ParameterObjectComboBox[] values)
{
// SET constructorButton to constructorName
// SET parameterValues to values
// CALL addChangeListener of constructorButton with NEW ParameterEnabler
// CALL setSelected of constructorButton with false
constructorButton = constructorName;
parameterValues = values;
constructorButton.addChangeListener(new ParameterEnabler());
constructorButton.setSelected(false);
}
/**
* Returns the selected ParameterDataInterfaces from the
* ParameterObjectComboBoxes
* isAllSelected should be called before this
* function to make sure that valid ParameterDataInterfaces are returned
* @return the selected ParameterDataInterfaces from the
* ParameterObjectComboBoxes, null if the constructor has
* no parameters
*/
public ParameterDataInterface[] getParameterValues()
{
ParameterDataInterface pdiArray[]; // array that this function returns
// IF length of parameterValues = 0 THEN
// INITIALIZE pdiArray as NEW array of ParameterDataInterface with length of 0
/* set the array to zero if there are no values, otherwise set values */
if (parameterValues.length == 0)
{
pdiArray = new ParameterDataInterface[0];
}
// ELSE
// INITIALIZE pdiArray as NEW array of ParameterDataInterface with length of parameterValues
// FOR x = 0 to length of parameterValues - 1
// CALL getSelectedItem of parameterValues[x] returns pdi
// SET pdiArray[x] to pdi
// ENDFOR
// ENDIF
else
{
pdiArray = new ParameterDataInterface[parameterValues.length];
/* set the array items to each of the values */
for (int x = 0; x < parameterValues.length; x++)
{
pdiArray[x] = (ParameterDataInterface) parameterValues[x].getSelectedItem();
}
}
// RETURN pdiArray
return pdiArray;
}
/**
* Checks to see if all of the ParameterObjectComboBoxes have valid Test Data
* Items selected.
*
* Post-conditions: none
* @return true if all valid Test Data Items have been selected,
* false if otherwise.
*/
public boolean isAllSelected()
{
// IF length of parameterValues = 0 THEN
// RETURN true
/* exit if there aren't any values */
if (parameterValues.length == 0)
{
return true;
}
// CALL getLength of parameterValues returns i
// FOR x = 0 to i - 1
// IF getSelectedIndex of parameterValues[x] = 0 THEN
// RETURN false
// ENDIF
// ENDFOR
/* for each value */
for (int x = 0; x < parameterValues.length; x++)
{
/* exit if no value is selected */
if (parameterValues[x].getSelectedIndex() == 0)
{
return false;
}
}
// RETURN true
return true;
}
/**
* Returns a reference to the radio button used to display the name of a
* constructor
* constructButton) is selected
* Pre-condition: The source of the action event must be
* constructButton
*/
public void stateChanged(ChangeEvent evt)
{
// CALL getSource of evt returns consButton
// CALL isSelected of consButton returns enable
// FOR x = 0 to length of parameterValues - 1
// CALL setEnabled of parameterValues[x] with enable
// ENDFOR
JRadioButton consButton = (JRadioButton) evt.getSource(); // the row to check
boolean enable = consButton.isSelected(); // is the button selected
/* enable or disable each combo box */
for (int x = 0; x < parameterValues.length; x++)
{
parameterValues[x].setEnabled(enable);
} // end for
} // end inner class function
} // end inner class
} // end class