package edu.calpoly.cpe205.fetter; import javax.swing.*; import java.util.*; import java.io.*; import java.lang.reflect.*; import java.lang.Integer; import java.lang.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.sql.Time; /** * This dialog allows the user to create a primitive Test Data Item which gets * stored in the Object Pool. */ // Version History // 11/18/00 Brian Laird added algorithm pseudocode // 11/18/00 Michael Hebron updated class description // 11/18/00 Michael Hebron extended class from JDialog // 01/17/01 Apel Yahinian implemented CreatePrimitiveDialog constructor/stubbed // One line commented out in onOk(PrimitiveValueFactory) // 01/17/01 Michael Hebron finished implementing constructor // implemented onCancel // added dialogOK member data // 01/29/01 Apel Yahinian implemented getObject() method // implemented Constructor // implemented getStatus() method // 2/10/01 Jonathon Lee updated class to coding standards // removed default value of '5' and name of 'intFive' public class CreatePrimitiveDialog extends JDialog { /** * Constructs a CreatePrimitiveDialog. *

* Pre-conditions: none * Post-conditions: constructs a CreatePrimitiveDialog * @param parent the dialog that CreatePrimitiveDialog is modeled */ public CreatePrimitiveDialog(JDialog parent) { // SET okButton to NEW JButton // SET cancelButton to NEW JButton // SET primitiveName to NEW JLabel // SET userSelectedName to NEW JTextField // SET primitiveTypeLabel to NEW JLabel // SET primitiveTypeSelector to NEW JComboBox // SET userSelectedValue to NEW JTextField // SET valueLabel to NEW JLabel super(parent, "New Test Data Item - Primitive Type", true); Container contentPane = getContentPane(); // container for dialog GridBagLayout gridbag = new GridBagLayout(); // bag to hold dialog GridBagConstraints constraints = new GridBagConstraints(); // constraints for dialog contentPane.setLayout(gridbag); primitiveTypeLabel = new JLabel("Type"); primitiveTypeSelector = new JComboBox(); valueLabel = new JLabel("Value"); userSelectedValue = new JTextField(""); primitiveName = new JLabel("Test Data Item Name"); userSelectedName = new JTextField(""); okButton = new JButton("OK"); cancelButton = new JButton("Cancel"); Iterator tempIterator; // iterate through each primitive type tempIterator = PrimitiveValueFactory.getPrimitiveValueFactories(); /* go to next primivite type */ while (tempIterator.hasNext()) { primitiveTypeSelector.addItem( tempIterator.next() ); } constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; constraints.ipadx = 4; constraints.ipady = 4; constraints.weightx = 0.3; constraints.gridx = 0; constraints.gridy = 0; contentPane.add(primitiveTypeLabel, constraints); constraints.gridy = 1; contentPane.add(valueLabel, constraints); constraints.gridy = 2; contentPane.add(primitiveName, constraints); constraints.weightx = 0.7; constraints.gridx = 1; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; contentPane.add(primitiveTypeSelector, constraints); constraints.gridy = 1; constraints.fill = GridBagConstraints.HORIZONTAL; contentPane.add(userSelectedValue, constraints); constraints.gridy = 2; contentPane.add(userSelectedName, constraints); JPanel buttonPanel = new JPanel(); // panel for ok/cancel buttons buttonPanel.add(okButton); buttonPanel.add(cancelButton); okButton.setMnemonic('o'); cancelButton.setMnemonic('c'); constraints.gridx = 0; constraints.gridwidth = 2; constraints.gridy = 3; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; contentPane.add(buttonPanel, constraints); pack(); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onOK(); } }); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onCancel(); } }); } public void setVisible(boolean vis) { //IF vis // CALL setText on userSelectedValue with "" // CALL setText on userSelectedName with "" //ENDIF //CALL setVisible of JDialog with vis if(vis) { userSelectedValue.setText(""); userSelectedName.setText(""); } super.setVisible(vis); } /** * Gets value from within the dialog and converts it into an object. Then * makes the dialog invisible. *

* Pre-conditions: none
* Post-conditions: value is stored as an object. * @param none */ protected void onOK() { // CALL getText of userSelectedValue returns val // CALL getValue of PrimitiveValueFactory with val // CALL setVisible with "FALSE" String val = userSelectedValue.getText(); // value of the primitive if (userSelectedName.getText().length() == 0) { JOptionPane.showMessageDialog(null, "Invalid Input, must have a name."); return; } if (primitiveTypeSelector.getSelectedItem().toString().equalsIgnoreCase("boolean")) { if (!val.equalsIgnoreCase("true") && !val.equalsIgnoreCase("false")) { JOptionPane.showMessageDialog(null, "Invalid Input, must be true or false"); return; } } try { ((PrimitiveValueFactory) primitiveTypeSelector.getSelectedItem()).getValue(val); setVisible(false); status = true; ETA.log.println("[" + (new Time(System.currentTimeMillis())).toString().substring(0, 5) + "] Create Test Data Item '" + userSelectedName.getText() + "' - " + primitiveTypeSelector.getSelectedItem() + " with value " + userSelectedValue.getText()); } catch (NumberFormatException exception) { JOptionPane.showMessageDialog(null,"Invalid Input" + exception.getMessage()); return; } } /** * Makes dialog invisible and sets status flag stating dialog closed * without getting a value. *

* Pre-conditions: none * Post-conditions: makes the dialog invisible * @param none */ protected void onCancel() { // CALL setVisible passing a boolean "FALSE" // sets a cancel status flag setVisible(false); status = false; } /** * Gets the name the user inputed from the JTextField, "userSelectedName". *

* Pre-conditions: none * Post-conditions: returns a String of the name the user created * @param none */ public String getName() { // RETURN String from the JTextField, "userSelectedName" return userSelectedName.getText(); } /** * Returns an object of the value of the JTextField "userSelectedValue" *

* Pre-conditions: none * Post-conditions: returns an object of the value the user created * @param none */ public Object getObject() { //RETURN Object from the JTextField "userSelectedValue" //ETA.out.println("CreatePrimitiveDialog, getObject"); PrimitiveValueFactory tempFactory; // type of primitive selected tempFactory = (PrimitiveValueFactory) primitiveTypeSelector.getSelectedItem(); return tempFactory.getValue(userSelectedValue.getText()); } /** * Returns a boolean value True/False *

* Pre-conditions: none * Post-conditions: returns a boolean value True/False * @param none */ public boolean getStatus() { return status; } /** * A boolean representing whether or not the user has pressed ok or cancel */ protected boolean status; /** * A JButton with the name "OK" */ protected JButton okButton; /** * A JButton with the name "Cancel" */ protected JButton cancelButton; /** * Label "New Test Value Item - Primitive Type" */ protected JLabel primitiveName; /** * A JTextField for the user to create a name of the primitive value. */ protected JTextField userSelectedName; /** * Label "Type" */ protected JLabel primitiveTypeLabel; /** * A JComboBox for the user to choose type of primitive value. */ protected JComboBox primitiveTypeSelector; /** * A JTextField for the user to create the value of the primitive value. */ protected JTextField userSelectedValue; /** * Label "Value" */ protected JLabel valueLabel; /** @link dependency */ /*#PrimitiveValueFactory lnkPrimitiveValueFactory;*/ }