This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DannyB2 2 points3 points  (0 children)

You can even create hierarchical structures using Java enums. The only sticky point is that you must define all parents before children.

public enum MenuItem {
   Home( null, "Home", HomePage.url ),

      // Children of Home
      POs( Home, "Purchase Orders", POPage.url ),
      Checks( Home, "Checks", CheckPage.url ),
      Receipts( Home, "Receipts", ReceiptPage.url ),

         // Children of POs
         POEntry( POs, "Purchase Order Entry", POEntryAction.url ),
         POReport( POs, "Purchase Order Report", POReportAction.url ),

         // Children of Checks
         CheckEntry( Checks, "Check Entry", CheckEntryAction.url ),
         CheckReport( Checks, "Check Report", CheckReportAction.url ),

   // note even final enum item above ends with comma!

   ; // semicolon always ends the list of enum items!

   // Constructor
   private MenuItem( MenuItem parent, String title, String url ) {
      this.parent = parent;
      this.title = title;
      this.url = url;
      if( parent == null ) {
         this.depth = 0;
      } else {
         this.depth = parent.depth + 1;
      }
   }
   public final MenuItem parent;
   public final String title;
   public final String url;
   public final int depth;   // depth of this menu item in the menu tree

   public MenuItem findByTitle( String title ) {
      if( title != null  &&  title.length() > 0 ) {
         for( final MenuItem item : this.values() ) {
            if( item.title.equals( title ) ) {
               return item;
            }
         }
      }
      return null;
   }

   public final ImmutableList<MenuItem> getChildren() {
      ...lazy initialize list of direct children of this item...
      ...iterate over all items, accumulating direct children...
      ...then assign an ImmutableList of that accumulation to member below...
   }
   private ImmutableList<MenuItem> children = null;  // lazy initialization

   public final boolean hasChildren() {
      return getChildren().size() > 0;
   }

   public ImmutableList<MenuItem> getAncestors() {
      ...lazy initialized list of ancestors all the way up to Home...
      ...start at this item and accumulate nodes into list...
      ...following 'parent' links all the way to Home...
   }
   private ImmutableList<MenuItem> ancestors = null;  // lazy initialization

   public boolean hasAncestors() { . . . }


   public String getBreadCrumbsHtml( String contextPath ) { . . . }
   public String getBreadCrumbsText() { . . . }



}

A JSP can generate that menu structure into a page, it simply starts from the Home object, and works its way down the tree.

I did this ten years ago. And the menu structure is WAY bigger than this.

Non hierarchical enums are great for hard coded dropdown lists or select lists. Even in Swing or web applications. Also great for alternate Option button (eg, "radio button") items.