dgMaster logo:dice dgMaster: data generator, simple.

Developer's guide - 2.1 Case study 1

Let's suppose that you want to write an English names generator. You want to generate full names, but you want to let the users decide on the details such as including a title (Mr, Mrs, Ms, Miss, Dr), including a middle name, etc. So, firstly it is necessary to have a form which allows the user to fine-tune the generator. For our needs, a form like the following will do:

There is not much complication involved here. The user will fine tune the generator by making various selections. Since all user definitions are saved in the Repository, it is "mandatory" for the data generator forms to have a name and a description field. The name is used to identify the generator in the application. As you may have noticed by now, there is no Save button in the forms the developer creates. Saving the data is handled by the dgMaster application. However, the application needs to have a way to collect user's data from the form.

The developer needs to provide a method in his form that actually returns a data object with all the information that is on the form. This data object is made up of two Strings (name and description), and a LinkedHashmap object. Such an object can be created by making use of the RandomiserInstance class. dgMaster knows that it needs to call the method getRandomiserInstance() in your panel. Essentially, your panel needs to implement a simple interface (IGeneratorPanel) which defines three methods:

public interface IGeneratorPanel
{    
    //called whenever the user clicks on an existing data-definition and the panel needs to be populated with this existing information.
    public void initialise(RandomiserInstance ri);

    //called just before saving the data of the form. If this method returns false, then no data are saved. 
    //If it returns yes, then the method below is called.
    public boolean isFormValid();

    //called to save the data on the form. The panel class needs to create an appropriate RandomiserInstance object,
    //(just a bean object really) if the data on the form are valid.    
    public RandomiserInstance getRandomiserInstance();
}

Alternatively, you can extend the RandomiserPanel which is an abstract JPanel class that implements IGeneratorPanel:

package generator.extenders;
import javax.swing.JPanel;

/**
 *
 * Extends JPanel and implements IGeneratorPanel,
 * (well, not actually, the class is abstract).
 * You will need to extend this and implement IGenerator's methods,
 * so as to create a Panel that can be loaded from dgMaster.
 */
public abstract class RandomiserPanel extends JPanel implements IGeneratorPanel
{
    
     
}

Here is the RandomiserInstance class, an object of this class needs to be returned by the getRandomiserInstance of each panel.

 /**
 * Provides a convenient way to save the data from a data-generator form.
 * Each user-defined GeneratorPanel or JPanel that implements IGeneratorPanel
 * needs to return an object of this class. 
 */
public class RandomiserInstance
{
    //provide a mapping from java type to sql
    private String randomiserType; 
    
    //name and description are standard fields for all the user-defined cases.
    private String name;       
    private String description;
    
    //any object can be stored in the properties hashmap, however, it will be
    //its String representation that will be saved in the XML file.
    private LinkedHashMap properties;
    
    
    public RandomiserInstance()
    {        
    }
    

    public RandomiserInstanceString randomiserType,
                               String name,
                               String description,
                               LinkedHashMap properties)
    {
        this.randomiserType = randomiserType;
        this.name = name;
        this.description = description;
        this.properties = properties;
    }
    
    
    public String getRandomiserType()
    {
        return randomiserType;
    }

    public void setRandomiserType(String randomiserType)
    {
        this.randomiserType = randomiserType;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public LinkedHashMap getProperties()
    {
        return properties;
    }

    public void setProperties(LinkedHashMap properties)
    {
        this.properties = properties;
    }

    public String toString()
    {
        return name;        
    }
}


<< Previous    ToC     Next >>