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.text.JTextComponent; import java.sql.Time; /** * This class is used for both the "Instantiate Class" and the * "New Test Data Item - Object Type Constructor" dialogs. It is used to * prompt the user to select a constructor and appropriate Test Data Items * to pass as parameters to that constructor. */ // Author: Michael Hebron // Version History: // Nov 21, 2000 - comments/pseudo code added // Nov 30, 2000 - updated pseudocode for onOK // Nov 30, 2000 - updated class description // Jan 15, 2001 - (Jonathon Lee) coded functionality for onCancel, onOk, getStatus, getParameters, getSelectedConstructor // - updated pseudocode for InstantiateClassDialog, getSelectedConstructor and onCancel // - partially coded functionality for InstantiateClassDialog // Jan 16, 2001 - (Jonathon Lee) gui dialog box added to constructor, no functionality though // Jan 17, 2001 - (Jonathon Lee) dialog box now looks like the one in the manual, no functionality // - added simple switch for "mode" // Jan 18, 2001 - (Jonathon Lee) added ok/cancel action listeners // - commented out functions, used ETA.out instead to prevent exceptions // Jan 28, 2001 - (Jonathon Lee) re-enabled all functions // - implemented constructor // Jan 29, 2001 - (Jonathon Lee) constructor half working - adding combo boxes not implemented // Jan 31, 2001 - (Jonathon Lee) constructor fully functional // - getParameters not working, don't know if getSelectedConstructor or getName work // Feb 8, 2001 - (Jonathon Lee) updated class to coding standards public class InstantiateClassDialog extends JDialog { /** * Creates a new instance of an InstantiateClassDialog *

* @param mode true for an "Instantiate Class" dialog *
false for "New Test Data Item - Object Type Constructor" dialog * @param parent the parent dialog that this dialog is modal upon * @param constructors an array of Constructors for the class that is to * be instantiated * @param model a reference to the ETAMainModel that the * ParameterObjectComboBoxes in the dialog will be listening to */ public InstantiateClassDialog(boolean mode, JDialog parent, Constructor[] constructors, ETAMainModel model) { // CALL super class' constructor with parent // IF mode THEN // SET userSelectedNameLabel to NEW JLabel with "Test Data Item Name:" // INDEX constructors with index 0 returns construct // CALL getDeclaringClass on contruct returns cls // CALL getName on cls returns className // SET userSelectedName to NEW JTextField with className // ELSE // SET userSelectedNameLabel to NEW JLabel with "Test Data Item Name:" // SET userSelectedName to NEW JTextField // CALL add of getContentPane of this with userSelectedNameLabel // CALL add of getContentPane of this with userSelectedName // ENDIF // CALL getLength of constructors returns numCons // FOR x = 0 to numCons - 1 // CALL getParameterTypes of constructors[x] returns params // CALL getLength of params returns numParams // INITIALIZE pcbArray as NEW array of ParameterObjectComboBox with numParams // FOR y = 0 to numParams - 1 // CONSTRUCT pcb as NEW ParameterObjectComboBox with // getName of constructors[y] // CALL addParameterDataListener of model with pcb // SET pcbArray[y] to pcb // ENDFOR // CONSTRUCT rb as JRadioButton with getName of constructors[x] // CONSTRUCT consRow as ConstructorRow with rb and pcbArray // CALL add of constructorRadios with rb // CALL add of this with rb // FOR x = 0 to numParams - 1 // CALL add of getContentPane of this with pcbArray[x] // ENDFOR // CALL put of constructorMap with getModel of rb and constructors[x] // CALL put of constructorRowMap with getModel of rb and consRow // ENDFOR super(parent, true); Assert.preCondition(model != null, "Parameter model is null"); Assert.preCondition(constructors != null, "Parameter constructors is null"); Assert.preCondition(constructors.length != 0, "There are no constructors for selected class"); /* begin dialog box layout */ GridBagLayout gridbag = new GridBagLayout(); // gridbag for the dialog GridBagConstraints constraints = new GridBagConstraints(); // constraints for the gridbag getContentPane().setLayout(gridbag); constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 1.0; constraints.gridwidth = GridBagConstraints.REMAINDER; this.mode = mode; /* set label depending on the type of dialog */ if (mode) { setTitle("Instantiate Class - " + constructors[0].getName()); userSelectedNameLabel = new JLabel("Main Test Object Name:"); userSelectedName = new JTextField(constructors[0].getDeclaringClass().getName()); } else { setTitle("New Test Data Item - Object Type Constructor"); userSelectedNameLabel = new JLabel("Test Data Item Name:"); userSelectedName = new JTextField(); } /* create headings */ constraints.gridheight = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.WEST; JLabel con = new JLabel("Constructors:"); // label for the constructor column gridbag.setConstraints(con, constraints); getContentPane().add(con); constraints.gridwidth = GridBagConstraints.REMAINDER; JLabel par = new JLabel("Parameters:"); // label for the parameter column gridbag.setConstraints(par, constraints); getContentPane().add(par); int numCons = Array.getLength(constructors); // number of constructors in the class constructorMap = new HashMap(); constructorRowMap = new HashMap(); Vector publicCons = new Vector(); // a Collection of all the public constructors Vector protectedCons = new Vector(); //a Collection of all the protected constructors Vector privateCons = new Vector();//a Collection of all the private constuctors Vector packageCons = new Vector();//a Collection of all the package local constructors JRadioButton selectFirst = null; // the Radio button for the first constructor so that we can select it after everything is added for (int ndx = 0; ndx < numCons; ndx++) { if(Modifier.isPublic(constructors[ndx].getModifiers())) { publicCons.add(constructors[ndx]); } else if(Modifier.isProtected(constructors[ndx].getModifiers())) { protectedCons.add(constructors[ndx]); } else if(Modifier.isPrivate(constructors[ndx].getModifiers())) { privateCons.add(constructors[ndx]); } else if(!(Modifier.isPrivate(constructors[ndx].getModifiers()) || Modifier.isProtected(constructors[ndx].getModifiers()) || Modifier.isPublic(constructors[ndx].getModifiers()))) { packageCons.add(constructors[ndx]); } else { throw new RuntimeException("Statement should not be reached"); } } /* construct user choices of constructors */ /* add all public constructors */ if(publicCons.size() != 0) { getContentPane().add(new JLabel("Public:"), new GridBagConstraints( 0 , GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); getContentPane().add(new JPanel(), new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); for (int x = 0; x < publicCons.size(); x ++) { addConstructor((Constructor)publicCons.get(x), model); } } /* add all protected constructors */ if(protectedCons.size() != 0) { getContentPane().add(new JLabel("Protected:"), new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); getContentPane().add(new JPanel(), new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); for (int x = 0; x < protectedCons.size(); x ++) { addConstructor((Constructor)protectedCons.get(x), model); } } if(privateCons.size() != 0) { getContentPane().add(new JLabel("Private:"), new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); getContentPane().add(new JPanel(), new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); /* add all private constructors */ for (int x = 0; x < privateCons.size(); x ++) { addConstructor((Constructor)privateCons.get(x), model); } } if(packageCons.size() != 0) { getContentPane().add(new JLabel("Package Local:"), new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); getContentPane().add(new JPanel(), new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); /* add all package local constructors */ for (int x = 0; x < packageCons.size(); x ++) { addConstructor((Constructor)packageCons.get(x), model); } } getContentPane().add(userSelectedNameLabel, new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); gridbag.setConstraints(userSelectedName, constraints); getContentPane().add(userSelectedName, new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); /* set the first constructor selected */ ((JRadioButton) constructorRadios.getElements().nextElement()).doClick(); /* add OK and Cancel buttons */ FlowLayout flow = new FlowLayout(FlowLayout.RIGHT); // layout for new panel JPanel userOptions = new JPanel(flow); // panel for ok/cancel buttons okButton = new JButton("OK"); cancelButton = new JButton("Cancel"); userOptions.add(okButton); userOptions.add(cancelButton); constraints.anchor = GridBagConstraints.SOUTHEAST; gridbag.setConstraints(userOptions, constraints); getContentPane().add(userOptions); pack(); /* enables ok and cancel buttons */ okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onOk(); } }); okButton.setMnemonic('o'); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt2) { onCancel(); } }); cancelButton.setMnemonic('c'); /* end dialog box layout */ /* end constructor */ } /** * Adds a Constructor to the dialog * @param con the Constructor to be added to the Dialog */ protected void addConstructor(Constructor con, ETAMainModel model) { //CONSTRUCT JRadioButton conButton //CALL add on constructorRadios with conButton //CALL getModel on conButton returns buttonModel //CALL put on constructorMap with buttonModel and con //CALL getParameterTypes on con returns parameters //CONSTRUCT ParameterObjectComboBox[] paramBoxes with length of parameters //CALL getName on con returns conName //CALL lastIndexOf on conName with "." returns periodIndex //CALL substring on conName with periodIndex returns shortConName //FOR 1 to length of parameters // INDEX parameters returns param // CALL classToName of ETAMainModel with param returns paramName // CONSTRUCT ParameterObjectComboBox workingBox with paramName // SET paramBoxes at index to workingBox // CALL addParameterDataListener on model with working Box // SET conName to conName + paramName + " ," //IF length of parameters is 0 // SET conName to conName + ")" //ENDIF //ELSE // CALL lastIndexOf on conName with ", " returns commaIndex // CALL substring on conName with 0 and commaIndex returns tempName // SET conName to tempName + ")" //ENDELSE //CALL setText on conButton with conName //CONSTRUCT ConstructorRow conRow with conButtonn and paramBoxes //CALL put on constructorRowMap with buttonModel and conRow JRadioButton conButton = new JRadioButton(); constructorRadios.add(conButton); constructorMap.put(conButton.getModel(), con); Class[] parameters = con.getParameterTypes(); ParameterObjectComboBox[] paramBoxes = new ParameterObjectComboBox[parameters.length]; String conName = con.getName().substring(con.getName().lastIndexOf(".") + 1) + " ("; ParameterObjectComboBox workingBox = null; getContentPane().add(conButton, new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); //Add all the parameters to the dialog for(int ndx = 0; ndx < parameters.length; ndx++) { workingBox = new ParameterObjectComboBox(ETAMainModel.classToName(parameters[ndx])); paramBoxes[ndx] = workingBox; model.addParameterDataListener(workingBox); conName = conName + workingBox.getTypeName() + ", "; getContentPane().add(workingBox, new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } getContentPane().add(new JPanel(), new GridBagConstraints( GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); //when there is no parameters set constructor name to end with () if (parameters.length == 0) { conName = conName + ")"; //when there are parameters cut extra ", " off the end and add on ")" } else { conName = conName.substring(0, conName.lastIndexOf(", ")) + ")"; } conButton.setText(conName); constructorRowMap.put(conButton.getModel(), new ConstructorRow(conButton, paramBoxes)); } /** * Returns the Constructor associated with the currently selected ConstructorRow * @return the Constructor associated with the currently selected ConstructorRow */ public Constructor getSelectedConstructor() { // CALL getSelection of constructorRadios returns rb // RETURN get of constructorMap with rb return (Constructor) (constructorMap.get(constructorRadios.getSelection())); } /** * Returns the name of the selected constructor * @return the name of the selected constructor */ public String getName() { //CALL getText on userSelectedName returns str //RETURN str return userSelectedName.getText(); } /** * Returns the ParameterDataInterfaces for the selected parameters in the * ConstructorRows * @return the ParameterDataInterfaces for the selected parameters in the * ConstructorRows */ public ParameterDataInterface[] getParameters() { // CALL getSelection of constructorRadios returns rb // CALL get of constructorRowMap with rb returns consRow // RETURN getParameterValues of consRow ConstructorRow consRow = (ConstructorRow) (constructorRowMap.get(constructorRadios.getSelection())); // combo box for the selected button return consRow.getParameterValues(); } /** * Returns whether or not the user pressed the OK button of the dialog * @return true if user pressed OK, false if otherwise */ public boolean getStatus() { // RETURN dialogOK return dialogOK; } /** * Called when the user presses the OK button *

* Post-condition: The dialog is dismissed */ public void onOk() { // CALL getText of userSelectedName returns name // IF name = NULL OR length of name is 0 // CALL showMessageDialog of JOptionPane with this, // "Please enter a Test Data Item name", // "Missing Test Data Item Name", // JOptionPane.ERROR_MESSAGE // RETURN // ENDIF String name = userSelectedName.getText(); // name user typed in Test Data Item Name field if (name == null || name.length() == 0) { JOptionPane.showMessageDialog(this, "Please enter a Test Data Item name", "Missing Test Data Item Name", JOptionPane.ERROR_MESSAGE); return; } // CALL get of constructorRowMap with getSelection of constructorRadios returns selectedConsRow // IF CALL isAllSelected of selectedConsRow returns false THEN // CALL showMessageDialog of JOptionPane with this, // "Invalid test data items selected", // "Please select valid test data items for the " + // CALL getText of getConstructorButton of selectedConsRow + " constructor", // JOptionPane.ERROR_MESSAGE // RETURN // ENDIF ConstructorRow selectedConsRow = (ConstructorRow) constructorRowMap.get(constructorRadios.getSelection()); // combox box for the selected button /* alert if null variable selected */ Assert.assert(selectedConsRow != null, "variable selectedConsRow was null"); /* alert if all parameters aren't selected */ if (!selectedConsRow.isAllSelected()) { JOptionPane.showMessageDialog(this, "Please select valid test data items for the " + selectedConsRow.getConstructorButton().getText() + " constructor", "Invalid test data items selectd", JOptionPane.ERROR_MESSAGE); return; } // CALL setVisible of this with false // SET dialogOK to true setVisible(false); dialogOK = true; /* set up time */ Locale myLocale = Locale.US; // user should be in US Calendar rightNow = new GregorianCalendar(myLocale); // calendar for US Constructor selectedCons = (Constructor) constructorMap.get(constructorRadios.getSelection()); // selected constructor /* check for null constructor variable */ Assert.assert(selectedCons != null, "variable selectedCons is null"); ParameterDataInterface[] params = selectedConsRow.getParameterValues(); // list of selected parameters /* check for null parameter variable */ Assert.assert(params != null, "variable params is null"); /* record to session log that user instantiated a class */ String sessionLog = new String(); // the string to output String paramName = new String(); // the list of parameters sessionLog = "[" + (new Time(System.currentTimeMillis())). toString().substring(0, 5) + "]"; /* add dialog name */ if (mode) { sessionLog = sessionLog + " Instantiate Class - " + selectedCons.getName(); } else { sessionLog = sessionLog + " Create Test Data Item '" + userSelectedName.getText() + "' - " + selectedCons.getName(); } /* create list of parameters */ for (int x = 0; x < Array.getLength(params); x++) { paramName = paramName + ", " + params[x].getDescriptiveName(); } /* clean up list */ if (Array.getLength(params) == 0) { sessionLog = new String(sessionLog + "( )"); } else { Assert.assert(paramName.indexOf(",") != -1, "No comma in string"); int commaIndex = paramName.indexOf(","); // index of the first ',' paramName = paramName.substring(commaIndex + 2); sessionLog = sessionLog + " (" + paramName + ")"; } /* print log */ ETA.log.println(sessionLog); } /** * Called when the user presses the Cancel button *

* Post-condition: The dialog is dismissed */ public void onCancel() { // CALL setVisible of this with false // SET dialogOK to false setVisible(false); dialogOK = false; } /** * The OK button of the dialog */ protected JButton okButton; /** * The Cancel button of the dialog */ protected JButton cancelButton; /** * A boolean representing whether or not the user pressed the OK button */ protected boolean dialogOK; /** * A map of ButtonModels of JRadioButtons to ConstructorRows */ protected Map constructorRowMap; /** * A map of ButtonModels of JRadioButtons to Constructors */ protected Map constructorMap; /** * The JLabel for the test data item name prompt in the dialog */ protected JLabel userSelectedNameLabel; /** * The JTextField the user interacts with to input a name for a Test Data Item */ protected JTextField userSelectedName; /** * ButtonGroup holding the JRadioButtons for the constructors */ protected ButtonGroup constructorRadios = new ButtonGroup(); /** * Boolean that indicates what mode that dialog is in. True if Instantiate Class dialog, * false if Create Test Data Item - Object Type dialog */ protected boolean mode; }