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.*; import javax.swing.border.*; /** * This class contains the GUI components used to display Test Data Item * information in the rows of the Test Data Item tab in the Object Pool. * This class also responds to UI events triggered by the GUI components * it contains. */ // author: Phillip Hansen // version history: // Nov 20 - added psuedo code and description // Nov 30 - (Mike Hebron) updated class description // Jan 12 - Mike Power implemented constructor // and implemented accessor methods // Jan 17 - (Phillip Hansen) stubbed uncoded methods // Jan 30 - (Phillip Hansen)implemented // ChangePrimitiveValueListener.actionPerformed(ActionEvent) and // RemoveListener.actionPerformed(ActionEvent) // Feb 9 - (Phillip Hansen) performed code walkthrough // Feb 25 - (Phillip Hansen) added inner classes ChangeNameShadeListener and // ChangePrimitiveValueShadeListener in accordance with the // resolution to change request 4. Also added code to the constructor so that // this listener gets set up public class ParameterRow { /** * creates a new instance of a ParameterRow *

* Post-conditions: a ParameterRow has been created * @param data the parameter data */ public ParameterRow(ParameterDataInterface data) { // CALL getTypeName on the ParameterDataInterface // CREATE JLabel with the returned string // CALL getDescriptiveName on the ParameterDataInterface // CREATE new text field with the returned name // SET the descriptiveName to the new text field // CALL setBackground on descriptiveName with Color.lightGray // CALL addActionListener on descriptiveName with NEW ChangeNameListener // CALL addFocusListener on descriptiveName with NEW ChangeNameShadeListener // CALL addKeyListener on descriptiveName with NEW ChangeNameShadeListener type = new JLabel(data.getTypeName()); // type of data descriptiveName = new JTextField(data.getDescriptiveName()); // name of data descriptiveName.setBackground(Color.lightGray); descriptiveName.addActionListener(new ChangeNameListener()); descriptiveName.addFocusListener(new ChangeNameShadeListener()); descriptiveName.addKeyListener(new ChangeNameShadeListener()); // CALL isPrimitive on the ParameterDataInterface // IF isPrimitive is true // CALL getValue on the ParameterDataInterface // CONSTRUCT a JTextField with the returned value // CALL setBackground on the value with Color.lightGray // CONSTRUCT a new ChangePrimitiveValueListener // CALL addActionListener on the primitiveValue // with the new ChangePrimitiveValueListener // CALL addFocusListener on the value with ChangePrimitiveValueShadeListener // CALL addKeyListener on value with ChangePrimitiveValueShadeListener // CALL setBorder on primitiveValue with raised // ELSE // CONSTRUCT a JButton with a string containing "inspect" // set inspect equal to the new JButton // CONSTRUCT a new InspectObjectValueListener // CALL addActionListener on inspect with the new // InspectObjectValueListener // CALL setBorder on inspect with raised // ENDIF if (data.isPrimitive()) { value = new JTextField(data.getValue()); // value of data if(((JTextField) value).getColumns() == 0) ((JTextField) value).setColumns(1); ((JTextField)value).addActionListener(new ChangePrimitiveValueListener()); value.setBackground(Color.lightGray); value.addFocusListener(new ChangePrimitiveValueShadeListener()); value.addKeyListener(new ChangePrimitiveValueShadeListener()); } else { value = new JButton("inspect"); // value of data ((JButton)value).addActionListener(new InspectObjectValueListener()); } value.setBorder(raised); // CONSTRUCT RemoveListener // SET removeListener to new RemoveListener // SET parameterData to data // CONSTRUCT new SelectedListener // CALL addMouseListener on type // CALL setBorder on type with raised // CALL setBorder on descriptiveName with raised removeListener = new RemoveListener(); parameterData = data; type.addMouseListener(new SelectedListener()); type.setBorder(raised); descriptiveName.setBorder(raised); remover = new RemoveListener(); } /** * determines whether or not the row is selected *

* Pre-conditions: none
* Post-conditions: none */ public boolean isSelected() { // RETURN true if the row is selected return lowered == type.getBorder();; } /** * either selects or deselects the row *

* Pre-conditions: none
* Post-conditions: none * @param select boolean determining whether or not to select the row */ public void setSelected(boolean select) { // IF select is equal to true // select the row // ELSE // deselect the row // ENDIF Border selector = select ? lowered : raised; value.setBorder(selector); type.setBorder(selector); descriptiveName.setBorder(selector); } /** * gets the name of the parameter data *

* Pre-conditions: none
* Post-conditions: none * @return JTextField containing the name */ public JTextField getNameTextField() { // RETURN descriptiveName return descriptiveName; } /** * gets the type of the parameter data *

* Pre-conditions: none
* Post-conditions: none * @return JLabel containing the type */ public JLabel getTypeLabel() { // RETURN type return type; } /** * removes the row from the object pool *

* Pre-conditions: none
* Post-conditions: row has been removed from pool */ public void remove() { ETA.out.println("Class: ParameterRow method: remove"); // CALL remove on the ParameterDataInterface parameterData.remove(); } /** * gets the value of the parameter *

* Pre-conditions: none
* Post-conditions: none * @return JComponent containing the value */ public JComponent getValueComponent() { // RETURN value return value; } /** * Returns the ActionListener that will reaspond to action events * by removing the ParameterRow */ public ActionListener getRemoveListener() { // RETURN removeListener return remover; } /** * an action listener that will listen to events and reaspond by * calling remove on its ParameterDataInterface */ protected ActionListener remover; /** * the parameter data */ protected ParameterDataInterface parameterData; /** * JComponent that will be reasponsible for displaying the value of * ParameterData. */ protected JComponent value; /** * the name of the parameter */ protected JTextField descriptiveName; /** * the type of the paramter */ protected JLabel type; /** * a RemoveListener */ protected RemoveListener removeListener; /** * indicates selection on the components of the row */ protected static Border lowered = BorderFactory.createLoweredBevelBorder(); /** * indicates non-selection on the components of the row */ protected static Border raised = BorderFactory.createRaisedBevelBorder(); /** * This class listens for button clicks on the inspect button. */ protected class InspectObjectValueListener implements ActionListener { /** * inspects the object *

* Pre-conditions: the InspectObjectValueListener has been triggered
* Post-conditions: none * @param evt the ActionEvent */ public void actionPerformed(ActionEvent evt) { // CALL inspectObjectValue on parameterData parameterData.inspectObjectValue(); } } /** * This class listens for button clicks on the remove button. */ protected class RemoveListener implements ActionListener { /** * removes the parameter data from the object pool *

* Pre-conditions: the RemoveListener has been triggered
* Post-conditions: none * @param evt the ActionEvent */ public void actionPerformed(ActionEvent evt) { // CALL getSource on evt // CALL getBorder on the descriptiveName returns bord // IF bord equals lowered // CALL remove on ParameterDataInterface // ENDIF if (descriptiveName.getBorder() == lowered) { parameterData.remove(); } } } /** * This class listens for changes to the value text field. */ protected class ChangePrimitiveValueListener implements ActionListener { /** * changes the value *

* Pre-conditions: the ChangePrimitiveValueListener has been triggered
* Post-conditions: none * @param evt the ActionEvent */ public void actionPerformed(ActionEvent evt) { // CALL getText // CALL changePrimitiveValue of ParameterDataInterface with the returned value // CALL requestFocus of type to get rid of focus (hey, it works) JTextField tempTextField = (JTextField)evt.getSource(); // text of the source try { parameterData.changePrimitiveValue(tempTextField.getText()); } catch(NumberFormatException exception) { JOptionPane.showMessageDialog(null, "Invalid Input: " + exception.getMessage()); } type.requestFocus(); } } /** * This class listens for changes to the name text field. */ protected class ChangeNameListener implements ActionListener { /** * changes the name *

* Pre-conditions: the ChangeNameListener has been triggered
* Post-conditions: none * @param evt the ActionEvent */ public void actionPerformed(ActionEvent evt) { // CALL getText on descriptiveName // CALL changeName of ParameterDataInterface with the returned string // CALL requestFocus of type to get rid of focus (hey, it works) parameterData.changeName(descriptiveName.getText()); type.requestFocus(); } } /** * This class listens for when the user clicks on any component of the row. */ protected class SelectedListener extends MouseAdapter { /** * it toggles the selection of the components *

* Pre-conditions: the SelectedListener has been triggered
* Post-conditions: none * @param evt the MouseEvent */ public void mouseClicked(MouseEvent evt) { // CALL getBorder on type // IF returned border equals lowered // SET returned border on all components with raised // ELSE // SET returned border on all components with lowered // ENDIF setSelected(type.getBorder() != lowered); } } /** * This class listens for changes to the name text field. */ protected class ChangeNameShadeListener implements FocusListener, KeyListener { /** * unshades the components *

* Pre-conditions: the FocusListener has been triggered
* Post-conditions: none * @param evt the FocusEvent */ public void focusGained(FocusEvent evt) { // CALL getSource of evt returns JTextField // CALL setBackground of the JTextField with value Color.white JTextField tempTextField = (JTextField)evt.getSource(); tempTextField.setBackground(Color.white); } /** * shades the components *

* Pre-conditions: the FocusListener has been triggered
* Post-conditions: none * @param evt the FocusEvent */ public void focusLost(FocusEvent evt) { // CALL getSource of evt returns JTextField // CALL setBackground of the JTextField with value Color.lgihtGray // CALL getDescriptiveName of parameterData and use returned value to setText JTextField tempTextField = (JTextField)evt.getSource(); tempTextField.setBackground(Color.lightGray); tempTextField.setText(parameterData.getDescriptiveName()); } /** * removes the focus if the escape key is released *

* Pre-conditions: the KeyListener has been triggered
* Post-conditions: none * @param evt the KeyEvent */ public void keyReleased(KeyEvent evt) { // CALL getKeyCode of evt returns keyCode // IF keyCode = KeyEvent.VK_ESCAPE // CALL requestFocus of type // END IF if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) { type.requestFocus(); } } public void keyPressed(KeyEvent evt) { // nothing - not needed } public void keyTyped(KeyEvent evt) { // nothing - not needed } } /** * This class listens for changes to the value text field. */ protected class ChangePrimitiveValueShadeListener implements FocusListener, KeyListener { /** * unshades the components *

* Pre-conditions: the FocusListener has been triggered
* Post-conditions: none * @param evt the FocusEvent */ public void focusGained(FocusEvent evt) { // CALL getSource of evt returns JTextField // CALL setBackground of the JTextField with value Color.white JTextField tempTextField = (JTextField)evt.getSource(); tempTextField.setBackground(Color.white); } /** * shades the components *

* Pre-conditions: the FocusListener has been triggered
* Post-conditions: none * @param evt the FocusEvent */ public void focusLost(FocusEvent evt) { // CALL getSource of evt returns JTextField // CALL setBackground of the JTextField with value Color.lgihtGray // CALL getValue of parameterData and use the returned value to setText JTextField tempTextField = (JTextField)evt.getSource(); tempTextField.setBackground(Color.lightGray); tempTextField.setText(parameterData.getValue()); } /** * removes the focus if the escape key is released *

* Pre-conditions: the KeyListener has been triggered
* Post-conditions: none * @param evt the KeyEvent */ public void keyReleased(KeyEvent evt) { // CALL getKeyCode of evt returns keyCode // IF keyCode = KeyEvent.VK_ESCAPE // CALL requestFocus of type // END IF if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) { type.requestFocus(); } } public void keyPressed(KeyEvent evt) { // nothing - not needed } public void keyTyped(KeyEvent evt) { // nothing - not needed } } }