dgMaster logo:dice dgMaster: data generator, simple.

Developer's guide - 2.1 Panel definition

Next, the class which creates the panel is shown:

/*
 * PanelFullname.java
 *
 * Created on 29 April 2007, 23:54
 */

package generator.panels;

import generator.extenders.RandomiserInstance;
import generator.extenders.RandomiserPanel;
import java.util.LinkedHashMap;
import javax.swing.JOptionPane;

/**
 * A simple names generator that generates data of the form:
 *  [Title] [Firstname|Initial] [MiddleInitial] [Lastname]
 *
 * At least one of the fields is required.
 */
public class PanelFullnameRandomiser extends RandomiserPanel
{
    
    /** Creates new form PanelFullname */
    public PanelFullnameRandomiser()
    {
        initComponents();
        radFirstInitialOnly.setSelected(true);
    }
    
                        
    private void initComponents()
    {
        //lots of GUI related code to generate the GUI...
    }// </editor-fold>                        
    
    private void chkFirstnameActionPerformed(java.awt.event.ActionEvent evt)                                             
    {                                                 
        boolean selected = chkFirstname.isSelected();
        
        //if the user does not want a firstname, then these options are not relevant
        //probably, the radio buttons should be hidden rather than disabled in such a case
        if(selected)
        {
            radFirstFull.setEnabled(true);
            radFirstInitialOnly.setEnabled(true);
        }
        else
        {
            radFirstFull.setEnabled(false);
            radFirstInitialOnly.setEnabled(false);
        }
    }                                            
    
    
    
    
    /*
     * Performs a validation check prior to saving the data. Each user-defined
     * panel may have an arbitrary number of information on it that the developer
     * would like to validate before saving. This is the method that handles this
     * validation.
     *
     * This method is automatically called by dgMaster, when the user
     * clicks the Save button.
     */
    public boolean isFormValid()
    {
        String name;
        Integer nullField;
        boolean lTitle, lFirst, lMiddle, lLastname;
        boolean result;
        
        //get name value, no need to perform check on description, that can be empty.
        name = txtName.getText().trim();
        nullField = (IntegerspinNull.getModel().getValue();
        
        //run checks
        if(name.length()==0)
        {
            JOptionPane.showMessageDialog(this,"Please provide a value for the name","Required field",JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        //since we have checkfields, we would ideally like the user to select something.
        //if the user selects none of the checkfields, this constitutes an invalid form,
        //the generator should actually generate something! We can let the generator handle this
        //but we would like to have valid data on the form as well.
        lTitle = chkTitle.isSelected();
        lFirst = chkFirstname.isSelected();
        lMiddle = chkMiddle.isSelected();
        lLastname = chkLast.isSelected();
        
        //now perform the check, we want at least one checkbox selected
        result = lTitle || lFirst || lMiddle || lLastname;
        if(result==false)
        {
            JOptionPane.showMessageDialog(this,"Please tick at least one of the check boxes","Required field",JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        ifnullField<|| nullField>100 )
        {
            JOptionPane.showMessageDialog(this,"Please provide a value in the range [0..100] for the Null field","Required field",JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    
    
    /*
     * Gathers the data from the form and creates a RandomiserInstance object,
     * which is returned to the dgMaster application, (so as to be saved in xml format).
     * This method will only be called if isFormValid() returns true.
     */
    public RandomiserInstance getRandomiserInstance()
    {
        String name, description;
        boolean lTitle, lFirstName, lFirstNameFull, lFirstInitial, lMiddle, lLastname;
        Integer nullField;
        LinkedHashMap hashMap = new LinkedHashMap();
        
        //this is what will be returned
        RandomiserInstance ri = new RandomiserInstance();
        
        //get field values
        name = txtName.getText().trim();
        description = txtDescription.getText().trim();
        nullField = (IntegerspinNull.getModel().getValue();
        
        //get the boolean variables
        lTitle = chkTitle.isSelected();
        lFirstName = chkFirstname.isSelected();
        lFirstNameFull = radFirstFull.isSelected();
        lFirstInitial = radFirstInitialOnly.isSelected();
        lMiddle = chkMiddle.isSelected();
        lLastname = chkLast.isSelected();
        
        //create the hashmap, this will be converted to xml by dgMaster
        hashMap.put("includeTitle",""+lTitle);
        hashMap.put("includeFirstName",""+lFirstName);
        hashMap.put("firstNameFull",""+lFirstNameFull);
        hashMap.put("firstNameInitial",""+lFirstInitial);
        hashMap.put("includeInitialMiddle",""+lMiddle);
        hashMap.put("includeLastName",""+lLastname);
        hashMap.put("nullField",nullField.toString());
        
        ri.setRandomiserType("FullnameRandomiser");
        ri.setName(name);
        ri.setDescription(description);
        ri.setProperties(hashMap);
        
        return ri;
    }
    
    
    /*
     * Populates the panel with existing information. This is the xml information
     * as returned by the RandomserInstance in method getRandomiserInstance()
     */    
    public void initialise(RandomiserInstance ri)
    {
        //get the values from the RandomiserInstance
        //and put them on the form        
        txtName.setTextri.getName() );
        txtDescription.setTextri.getDescription() );
        
        String sNull;
        boolean lTitle, lFirstName, lFirstNameFull, lFirstInitial, lMiddle, lLastname;
        String  sTitle, sFirstName, sFirstNameFull, sFirstInitial, sMiddle, sLastname;
        LinkedHashMap hashMap;
        
        //retrieve the hashmap values, these are always Strings
        hashMap = ri.getProperties();
        sTitle  = (StringhashMap.get("includeTitle");
        sFirstName    = (String)hashMap.get("includeFirstName");
        sFirstNameFull= (String)hashMap.get("firstNameFull");
        sFirstInitial = (String)hashMap.get("firstNameInitial");
        sMiddle       = (String)hashMap.get("includeInitialMiddle");
        sLastname     = (String)hashMap.get("includeLastName");
        
        //convert retrieved Strings to boolean
        lTitle        = Boolean.valueOf(sTitle);
        lFirstName    = Boolean.valueOf(sFirstName);
        lFirstNameFull= Boolean.valueOf(sFirstNameFull);
        lFirstInitial = Boolean.valueOf(sFirstInitial);
        lMiddle       = Boolean.valueOf(sMiddle);
        lLastname     = Boolean.valueOf(sLastname);
        
        //... and set the values of the check and radio boxes
        chkTitle.setSelected(lTitle);
        
        chkFirstname.setSelected(lFirstName);
        radFirstInitialOnly.setSelected(lFirstInitial);
        radFirstFull.setSelected(lFirstNameFull);
        
        chkMiddle.setSelected(lMiddle);
        chkLast.setSelected(lLastname);
        
        //...and the null value spinner
        sNull  = (StringhashMap.get("nullField");
        spinNull.setValue(Integer.parseInt(sNull));
    }
    
    
    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox chkFirstname;
    private javax.swing.JCheckBox chkLast;
    private javax.swing.JCheckBox chkMiddle;
    private javax.swing.JCheckBox chkTitle;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton radFirstFull;
    private javax.swing.JRadioButton radFirstInitialOnly;
    private javax.swing.JSpinner spinNull;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtName;
    // End of variables declaration                       
}

That's it with regards to the form implementation: The implemented methods were:

  • isFormValid() called just before the getRandomiserInstance().
  • getRandomiserInstance(), called only if isFormValid() returns true. In this method the form data are actually gathered and put in a RandomiserInstance. This object is converted to xml and put in the file UserRepository.xml.
  • initialise(RandomiserInstace), called when the user clicks on the tree browser on the left panel; this actually causes dgMaster to try to load the specific panel. If the user clicked on an existing definition, the xml data is put into a RandomiserInstance object and this method is called. It is used to load the data into the form.

There is only one step that remains to be done, so as dgMaster can actually load the panel: a new entry needs to be made in the SystemDefinitions.xml file as follows:

<randomiser-type name="SQLTimeRandomiser" jtype="3" panel="generator.panels.PanelSQLTimeRandomiser" generator="generator.randomisers.SQLTimeRandomiser"/>


<< Previous ToC Next >>