Enum (value of type)

From JRapid

Jump to: navigation, search

enum is a value of type.


Contents

Description

The enum type is short for "enumerated data". The developer can define a fixed set of words or values that a property of type enum can take as its value. Properties of enum type define the possible values during development time from which the end user may choose one.

These values may be defined as enum elements children of the property or by specifying a value for the enumset attribute of the property. In this case, an enumset must be created.

The enum elements may specify the value attribute or not. If not, the label is used as the value. Keep in mind that values for enum elements should be valid Java identifiers and that, in order to follow naming best practices, these names should be uppercase. Remember you cannot begin an identifier name with a number.

By default, properties of type enum generate a select widget in the form that lists the options. Enum properties may use the radio widget too.

When a property of type enum is as well a collection, the default and only built-in widget available is the checkbox.


Enum properties generate a varchar column in the corresponding database table where the value of the selected option is stored.

Usage

<property
    ...
    type = "enum"
    enumset = ENUMSET
    (enum*) />
</property>

Example

  • This simple example shows hot to define a Card entity using two properties of type enum with values defined inline.
     <entity label="Card" menu="Menu" name="Card">
        <property display="primary" label="Suit" name="suit" type="enum">
            <enum  value="CLUBS">Clubs</enum>
            <enum  value="DIAMONDS">Diamonds</enum>
            <enum  value="HEARTS">Hearts</enum>
            <enum  value="SPADES">Spades</enum>
        </property>
        <property display="primary" label="Rank" name="rank" type="enum">
            <enum  value="DEUCE">Deuce</enum>
            <enum  value="THREE">Three</enum>
            <enum  value="FOUR">Four</enum>
            <enum  value="FIVE">Five</enum>
            <enum  value="SIX">Six</enum>
            <enum  value="SEVEN">Seven</enum>
            <enum  value="EIGHT">Eight</enum>
            <enum  value="NINE">Nine</enum>
            <enum  value="TEN">Ten</enum>
            <enum  value="JACK">Jack</enum>
            <enum  value="QUEEN">Queen</enum>
            <enum  value="KING">King</enum>
            <enum  value="ACE">Ace</enum>
        </property>
    </entity>

The resulting form looks like this.

Card entity using two enum type properties


  • You may use a radio widget on properties of type enum, but make sure the number of options is not too large as this may make the form a little more difficult to use.
     <entity label="Card" menu="Menu" name="Card">
        <property display="primary" label="Suit" name="suit" type="enum" widget="radio">
            <enum  value="CLUBS">Clubs</enum>
            <enum  value="DIAMONDS">Diamonds</enum>
            <enum  value="HEARTS">Hearts</enum>
            <enum  value="SPADES">Spades</enum>
        </property>
        <property display="primary" label="Rank" name="rank" type="enum">
            <enum  value="DEUCE">Deuce</enum>
            <enum  value="THREE">Three</enum>
            <enum  value="FOUR">Four</enum>
            <enum  value="FIVE">Five</enum>
            <enum  value="SIX">Six</enum>
            <enum  value="SEVEN">Seven</enum>
            <enum  value="EIGHT">Eight</enum>
            <enum  value="NINE">Nine</enum>
            <enum  value="TEN">Ten</enum>
            <enum  value="JACK">Jack</enum>
            <enum  value="QUEEN">Queen</enum>
            <enum  value="KING">King</enum>
            <enum  value="ACE">Ace</enum>
        </property>
    </entity>

And the suit property now shows options as radio.

Card entity using two enum type properties


  • Another way to define a property of type enum is to specify the values in an enumset element. This allows the developer to reuse value definitions.
    <enumset name="CompassDirection">
        <enum value="N">North</enum>
        <enum value="E">East</enum>
        <enum value="S">South</enum>
        <enum value="W">West</enum>
    </enumset>

    <entity label="Path" menu="Menu" name="Path">
        <property display="primary" label="Name" name="name"/>
        <property enumset="CompassDirection" label="Direction" name="direction" type="enum"/>
        <property label="Distance" name="distance" type="integer"/>
    </entity>

The direction property shows the available options for the property.

Path entity using enum type with CompassDirection enumset

  • This next example shows how the enum elements may be used without specifying the value attribute explicitly.
    <entity label="Article" menu="Menu" name="Article">
        <property display="primary" label="Title" name="title"/>
        <property label="Body" name="body" type="text"/>
        <property label="Language" name="language" type="enum">
            <enum>ENGLISH</enum>
            <enum>SPANISH</enum>
            <enum>FRENCH</enum>
            <enum>GERMAN</enum>
            <enum>ITALIAN</enum>
        </property>
    </entity>

And the resulting form shows:

Article entity using enum type for language property

See also

Personal tools