Simulated Enums – supporting choices

In my last post I showed how to simulate enums, and then Giorgio asked in the comments as to how this fits in with the choices() method, used to provide a drop-down list of values.

To start with, let’s add an all() method to StockType:

import org.nakedobjects.applib.DomainObjectContainer;

public class StockType extends EnumeratedAbstract {

  private static final String CD = "cd";
  private static final String BOOK = "book";

  public static StockType Book(DomainObjectContainer container) {
    return lookup(container, StockType.class, BOOK);
  }

  public static StockType Cd(DomainObjectContainer container) {
    return lookup(container, StockType.class, CD);
  }

  public static List all(DomainObjectContainer container) {
    return Arrays.asList(Book(container), Cd(container));
  }

  public static void createAll(DomainObjectContainer container) {
    create(container, StockType.class, BOOK);
    create(container, StockType.class, CD);
  }

}

Then, in the StockType domain object we just add the choices() method, for example:

    // {{ StockType
    private StockType stockType;
    @Disabled
    @MemberOrder(sequence = "2")
    public StockType getStockType() {
      return stockType;
    }

    public void setStockType(final StockType stockType) {
      this.stockType = stockType;
    }
    public List choicesStockType() {
        return StockType.all(getContainer());
    }
    // }}

Short and sweet. In the next post I’ll show how we can teach Naked Objects to support Java 5 enums natively (a feature that we’ll be adding formally in Naked Objects 4.1).

Advertisement

Posted on February 27, 2010, in apache isis. Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 109 other followers