InputField

This factory is meant to generate an adapted subclass of the Java GUI component JTextField of package javax.swing. For an actual type parameter int, for example, this factory generates a class IntInputField that has a public method setValue() and a public method getValue() to set and get the int value which is displayed in the JTextField GUI as string, respectively. This relieves the programmer of the burden to set and parse the textual representation of the JTextField's value.

This implementation works only for types boolean, int,float,double and String, but could be extended to provide the right GUI for any kind of simple data type. Enumeration types, for example, could be represented by a group of radio buttons. Furthermore, the factory could be extended to ensure the correctness of the input made to the GUI component directly; e.g. it could ensure that only numerical characters are typed into an IntInputField.

Factory/Java Source

The source can be found in the Factory/Java .zip-archive in factory/classes/examples/InputField.factory:

<param> <var> T </var> </param>

package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class
<apply>
    <apply>
        <class> factory.Toolbox </class>
        <method> getRelativeName </method>
        <args>
            <var> T </var>
        </args>
    </apply>
    <method> concat </method>
    <args>
        <const> "InputField" </const>
    </args>
</apply>
extends JTextField
{
    public    
    <apply>
        <apply>
            <class> factory.Toolbox </class>
            <method> getRelativeName </method>
            <args>
                <var> T </var>
            </args>
        </apply>
        <method> concat </method>
        <args>
            <const> "InputField" </const>
        </args>
    </apply>
    (int i) {
        super(i);
    }
    
    public void setValue(<var> T </var> x) {
        setText(""+x);
    }

    public <var> T </var> getValue() {
        <let> <var> CN </var>
        <apply>
            <var> T </var>
            <method> getName </method>
        </apply>
        <body>
            <if>
            <apply>
                <var> CN </var>
                <method> equals </method>
                <args> <const> "boolean" </const> </args>
            </apply>
            <then>
                return Boolean.valueOf(getText()).booleanValue();
            </then> </if>
            <if>
            <apply>
                <var> CN </var>
                <method> equals </method>
                <args> <const> "int" </const> </args>
            </apply>
            <then>
                return Integer.parseInt(getText());
            </then> </if>
            <if>
            <apply>
                <var> CN </var>
                <method> equals </method>
                <args> <const> "float" </const> </args>
            </apply>
            <then>
                return Float.parseFloat(getText());
            </then> </if>
            <if>
            <apply>
                <var> CN </var>
                <method> equals </method>
                <args> <const> "double" </const> </args>
            </apply>
            <then>
                return Double.parseDouble(getText());
            </then> </if>
            <if>
            <apply>
                <var> CN </var>
                <method> equals </method>
                <args> <const> "java.lang.String" </const> </args>
            </apply>
            <then>
                return getText();
            </then> </if>
        </body> </let>
    }
}

Test Program

For a program that uses factory InputField, see example EditFrame.