Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.appbasics.app.menu;
 
 import java.util.Map;
 
 
Determine all menu items to show, depending on the currently selected menu item.

Author(s):
Philip Helger
 
 {
   private final IMenuTree m_aMenuTree;
   private final Map <StringBooleanm_aItems = new HashMap <StringBoolean> ();
   private final String m_sSelectedItemID;
 
   public MenuItemDeterminatorCallback (@Nonnull final IMenuTree aMenuTree, @Nullable final String sSelectedMenuItemID)
   {
      = ValueEnforcer.notNull (aMenuTree"MenuTree");
      = sSelectedMenuItemID;
     // The selected item may be null if an invalid menu item ID was passed
   }
 
   protected boolean isMenuItemValidToBeDisplayed (@Nonnull final IMenuObject aMenuObj)
   {
     return aMenuObj.matchesDisplayFilter ();
   }
 
   protected void rememberMenuItemForDisplay (@Nonnull @Nonempty final String sMenuItemIDfinal boolean bExpanded)
   {
     final Boolean aExpanded = .get (sMenuItemID);
     if (aExpanded == null || !aExpanded.booleanValue ())
       .put (sMenuItemID, Boolean.valueOf (bExpanded));
   }
 
   @Override
   public final void onItemBeforeChildren (@Nonnull final DefaultTreeItemWithID <StringIMenuObjectaItem)
   {
     boolean bShow;
     boolean bAddAllChildrenOnThisLevel = false;
     boolean bExpanded = false;
     final boolean bIsTopLevel = getLevel () == 0;
     if ( == null)
     {
       // Show only top level entries
       bShow = bIsTopLevel;
     }
     else
     {
       // 1. Top level entries are always shown
       // 2. Show this item, if it is a parent of the selected item (sub menu)
       // 3. Show this item, if it is a direct child of the selected item
       // 4. Show this item, if if is a direct child of the selected item's
       // parent
       if (bIsTopLevel)
       {
         // 1.
         bShow = true;
         bExpanded = .isSameOrChildOf (aItem);
       }
       else
       {
         // 2.
         if (.isSameOrChildOf (aItem))
        {
          bShow = true;
          bExpanded = true;
        }
        else
        {
          final DefaultTreeItemWithID <StringIMenuObjectaItemParent = aItem.getParent ();
          // 3.
          if (aItemParent.equals ())
          {
            bShow = true;
          }
          else
            // 4.
            if (aItemParent.getChildItemOfDataID () != null)
              bShow = true;
            else
              bShow = false;
        }
        bAddAllChildrenOnThisLevel = bShow;
      }
    }
    // Check display filter
    if (bShow || bAddAllChildrenOnThisLevel)
    {
      if (!isMenuItemValidToBeDisplayed (aItem.getData ()))
      {
        bShow = false;
        bAddAllChildrenOnThisLevel = false;
        bExpanded = false;
      }
    }
    if (bShow)
      rememberMenuItemForDisplay (aItem.getID (), bExpanded);
    if (bAddAllChildrenOnThisLevel)
      for (final DefaultTreeItemWithID <StringIMenuObjectaSibling : aItem.getParent ().getChildren ())
        if (isMenuItemValidToBeDisplayed (aSibling.getData ()))
          rememberMenuItemForDisplay (aSibling.getID (), false);
  }

  

Returns:
A map with all items to be displayed, where the key is the menu item ID and the value is the expansion state of the item.
  public Map <StringBooleangetAllItemIDs ()
  {
    return ContainerHelper.newMap ();
  }
  public static Map <StringBooleangetAllDisplayMenuItemIDs (@Nonnull final IMenuTree aMenuTree,
                                                                @Nullable final String sSelectedMenuItemID)
  {
    return getAllDisplayMenuItemIDs (new MenuItemDeterminatorCallback (aMenuTreesSelectedMenuItemID));
  }
  public static Map <StringBooleangetAllDisplayMenuItemIDs (@Nonnull final MenuItemDeterminatorCallback aDeterminator)
  {
    ValueEnforcer.notNull (aDeterminator"Determinator");
    TreeWalker.walkTree (aDeterminator.m_aMenuTreeaDeterminator);
    return aDeterminator.getAllItemIDs ();
  }

  
Get all menu items without a separate MenuItemDeterminatorCallback instance.

Parameters:
aMenuTree The menu tree to get all items from. May not be null.
Returns:
A non-null map with all menu item IDs as keys.
  public static Map <StringBooleangetAllMenuItemIDs (@Nonnull final IMenuTree aMenuTree)
  {
    ValueEnforcer.notNull (aMenuTree"MenuTree");
    final Map <StringBooleanret = new HashMap <StringBoolean> ();
    TreeWalker.walkTree (aMenuTreenew DefaultHierarchyWalkerCallback <DefaultTreeItemWithID <StringIMenuObject>> ()
    {
      @Override
      public void onItemBeforeChildren (@Nonnull final DefaultTreeItemWithID <StringIMenuObjectaItem)
      {
        ret.put (aItem.getID (), Boolean.valueOf (aItem.hasChildren ()));
      }
    });
    return ret;
  }
New to GrepCode? Check out our FAQ X