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 manages all the GUI components used to display information * about a method and its parameters. This class also responds to UI events * triggered by these GUI components. */ // Author: Brian Laird // Version History // Nov 19, 2000 - added algorithm pseudocode // Nov 30, 2000 - (Mike Hebron) updated class description // Feb 9, 2001 - (Phillip Hansen) performed code walkthrough // Feb 18, 2001 - Wes Strickland- added code for getParameters() for Stage Three // Also checked code for InvokeMethodListener actionPerformed() // for Stage Three // Feb 25, 2001 - Wes Strickland - fixed code for getParameters() for Stage Three // Feb 25, 2001 - (Phillip Hansen) Added new action listener inner class as part // of resolution for change request 2 // Feb 27, 2001 - (Phillip Hansen) Added the line to hook up the listeners to the // combo boxes for change request 2 (disable name when not valid params) // Also added and implemented the setEnabled method. public class MethodRow extends RowAbstract implements MethodRowInterface { /** * Constructs a MethodRow. *

* Pre-conditions: none * Post-conditions: constructs a MethodRow * @param newMethodData the interface for MethodData */ public MethodRow(MethodDataInterface newMethodData) { // CALL getName of newMethodData returns nameString // CALL constructor of JButton name with nameString // ADD ActionListener to name with new InvokeMethodListener // CONSTRUCT JLabel methodReturn with typeString // SET type to methodReturn // CALL getModifiers of MethodData // CONSTRUCT JPanel newType // SET ModifierPanel to newType // CALL getType of MethodData returns typeString // CONSTRUCT ParameterObjectComboBox // SET selectors to ParameterObjectComboBox // CALL getEditor methodData = newMethodData; name = new JButton(methodData.getName()); // name of the method name.addActionListener(new InvokeMethodListener()); type = new JLabel(methodData.getReturnType()); // type of the method selectors = new ParameterObjectComboBox[methodData.getParameterTypeNames().length]; for (int index = 0; index < selectors.length; index++) { selectors[index] = new ParameterObjectComboBox(methodData.getParameterTypeNames()[index]); //selectors[index].addActionListener(new CreatePrimitiveListener(selectors[index])); selectors[index].addActionListener(new ParametersListener()); } modifierPanel = new JPanel(); // panel containing the modifier information int mod = methodData.getModifiers(); // modifiers of the method if (Modifier.isStatic(mod)) { modifierPanel.add(new JLabel(new ImageIcon(this.getClass(). getClassLoader().getResource("edu/calpoly/cpe205/fetter/images/static.gif")))); } if (Modifier.isFinal(mod)) { modifierPanel.add(new JLabel(new ImageIcon(this.getClass(). getClassLoader().getResource("edu/calpoly/cpe205/fetter/images/final.gif")))); } if (Modifier.isSynchronized(mod)) { modifierPanel.add(new JLabel(new ImageIcon(this.getClass(). getClassLoader().getResource("edu/calpoly/cpe205/fetter/images/synchronized.gif")))); } if (Modifier.isNative(mod)) { modifierPanel.add(new JLabel(new ImageIcon(this.getClass(). getClassLoader().getResource("edu/calpoly/cpe205/fetter/images/native.gif")))); } if (Modifier.isAbstract(mod)) { modifierPanel.add(new JLabel(new ImageIcon(this.getClass(). getClassLoader().getResource("edu/calpoly/cpe205/fetter/images/abstract.gif")))); } } /** * Gets the parameters of the test data items in hashcode form. *

* Pre-conditions: none * Post-conditions: returns a int[] of parameter hashcodes * @param none * @return int[] hashcodes */ public ParameterDataInterface[] getParameters() { // CALL getLength of selectors[] returns length // CONSTRUCT arry of ParameterDataInterface with length // FOR index = 0 to length - 1 // CALL getSelectedIndex of selectors[index] returns i // CALL getItemAt on selectors[index] with i returns obj // SET arry[index] to obj // ENDFOR // RETURN hash[] int selectorlength = selectors.length; //length of selectors array ParameterDataInterface DataArray[] = new ParameterDataInterface[selectorlength]; int index=0; //loop control variable int returnedindex=0; for (index=0; index * Pre-conditions: none * Post-conditions: returns boolean * @param none * @return boolean valid */ public boolean isParametersValid() { // INITIALIZE valid as boolean with true boolean valid = true; // the validity of the test // CALL getLength of selectors[] returns length // FOR index = 1 to length // CALL getSelectedIndex on selectors[index] return value // IF value = 0 // valid = false // ENDIF // ENDFOR for (int index = 0; index < selectors.length; index++) { if (selectors[index].getSelectedIndex() == 0) { valid = false; } } // RETURN valid return valid; } /** * Enables the components of the Row, if the parameters aren't valid, name is false. *

* Pre-conditions: none * Post-conditions: all components within row are enabled */ public void setEnabled(boolean bool) { // CALL setEnabled of RowAbstract // IF not isParametersValid // CALL setEnabled on name with false // END IF // CALL getLength of selectors returns length // IF bool // FOR index = 1 to length // CALL unselect of selectors[index - 1] // ENDFOR // ENDIF super.setEnabled(bool); if (!isParametersValid()) { name.setEnabled(false); } for (int index = 0; bool && index < selectors.length; index++) { selectors[index].unselect(); } } /** * The interface for the MethodData object */ protected MethodDataInterface methodData; /** * Waits for an action Event then invokes the method selected * Pre-conditions: none * Post-conditions: none */ protected class InvokeMethodListener implements ActionListener { /** * Performs a method invoking after an action event * Pre-conditions: none * Post-conditions: method is invoked * @param evt action event to invoke a method */ public void actionPerformed(ActionEvent evt) { // DECLARE paramsOK as boolean // CALL isParametersValid returns b // IF NOT b THEN // SET paramsOK to false // ENDIF boolean paramsOK; // are the parameters ok paramsOK = isParametersValid(); // IF paramsOK THEN // CALL invokeMethod of methodData // ENDIF if (paramsOK) { methodData.invokeMethod(); } } } /** * The listener for the creating a new primitive value. * Pre-conditions: none * Post-conditions: none */ protected class CreatePrimitiveListener implements ActionListener { /** * Constructs a CreatePrimitiveListener * Pre-conditions: none * Post-conditions: CreatePrimitiveListner is constructed * @param none */ public CreatePrimitiveListener(ParameterObjectComboBox box) { // SET comboBox to box comboBox = box; } /** * Waits for an action Event then invokes the method selected * Pre-conditions: none * Post-conditions: method is invoked * @param none * @return none */ public void actionPerformed(ActionEvent evt) { // CALL getTypeName of comboBox returns str // CALL getEditor of comboBox returns edtr // CALL getItem of edtr returns val // CALL createPrimitive of methodData with val and str // CALL selectLast of comboBox // only stubs comboBox.getTypeName(); methodData.createPrimitive("val", "str"); comboBox.selectLast(); } /** * the JComboBox the listener listens to */ protected ParameterObjectComboBox comboBox; } /** * This listener checks to see if parameters have been selected in all of the combo boxes. * Pre-conditions: none * Post-conditions: none */ protected class ParametersListener implements ActionListener { /** * Checks if parameters are set in the combo boxes after an action event. * Pre-conditions: none * Post-conditions: if a parameter is not set, method name is disabled * @param evt action event to invoke a method */ public void actionPerformed(ActionEvent evt) { // CALL isParametersValid returns bool // CALL setEnabled of name with bool name.setEnabled(isParametersValid()); } } }