Wicket menuコンポーネントを認証用に、(2)

前回のつづき、Wicket-auth 認証用のメニューコンポーネントの実装
最後のサンプルのように、メニューを生成定義が、直観的になる。

import java.util.List;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
/**
 * AuthHorizonDropdownMenu.
 */

public abstract class AuthHorizonDropdownMenu extends Panel implements IHeaderContributor{
   // CSS CSS 読込みリファレンス
   protected abstract ResourceReference getCSSResourceReference();
   // リンク click 時の実行
   protected abstract void processResponse(AuthMenuItem menuItem);

   public AuthHorizonDropdownMenu(String id,List<AuthMenuItem> menuItemList){
      super(id);
      final AuthenticatedWebSession session = (AuthenticatedWebSession)getSession();
      ListView<AuthMenuItem> primaryMenuListView = new ListView<AuthMenuItem>("menuItem",menuItemList){
         @Override
         protected void populateItem(ListItem<AuthMenuItem> item){
            final AuthMenuItem menuItem = item.getModelObject();
            Link link = new Link("menuLink"){
               @Override
               public void onClick(){
                  if (menuItem != null){
                     processResponse(menuItem);
                  }
               }
            };
            link.add(new Label("linkText",menuItem.getMenuText()){
               @Override
               public boolean isVisible(){
                  return menuItem.isView(session);
               }

            });
            item.add(link);
            WebMarkupContainer subMenuListContainer = new WebMarkupContainer("subMenuListContainer");
            ListView<AuthMenuItem> subMenuListView = new ListView<AuthMenuItem>("subMenuItem",menuItem.getSubMenuItemList()){
               @Override
               protected void populateItem(ListItem<AuthMenuItem> _item){
                  final AuthMenuItem subMenuItem = _item.getModelObject();
                  Link subMenuLink = new Link("subMenuLink"){
                     @Override
                     public void onClick(){
                        if (subMenuItem != null){
                           processResponse(subMenuItem);
                        }
                     }
                  };
                  subMenuLink.add(new Label("subMenuLinkText",subMenuItem.getMenuText()){
                     @Override
                     public boolean isVisible(){
                        return subMenuItem.isView(session);
                     }

                  });
                  _item.add(subMenuLink);
                  _item.setVisible(subMenuItem.isView(session));
               }
            };
            subMenuListContainer.add(subMenuListView);
            if (menuItem.getSubMenuItemList() != null && menuItem.getSubMenuItemList().size()==0){
               subMenuListContainer.setVisible(false);
            }
            item.add(subMenuListContainer);
         }
      };
      add(primaryMenuListView);
   }
   protected void processResponse(AuthMenuItem menuItem){
      switch(menuItem.getDestinationType()){
         case DestinationType.EXTERNAL_LINK:
            menuItem.getExternalLink();
            break;
         case DestinationType.WEB_PAGE_CLASS:
            setResponsePage(menuItem.getResponsePageClass());
            break;
         case DestinationType.WEB_PAGE_INSTANCE:
            setResponsePage(menuItem.getResponsePage());
            break;
         case DestinationType.NONE:
            break;
         default:
            throw new RuntimeException("Destination type not valid!");
      }
   }
   @Override
   public void renderHead(IHeaderResponse response){
      response.renderCSSReference(getCSSResourceReference());
   }
}

-------------- サンプル ----------------

public abstract class BlueAuthHorizonDropdownMenu extends AuthHorizonDropdownMenu{
   public BlueAuthHorizonDropdownMenu(String id,List<AuthMenuItem> menuItemList){
      super(id,menuItemList);
   }
   @Override
   protected ResourceReference getCSSResourceReference(){
      return new CompressedResourceReference(AuthHorizonDropdownMenu.class,"blue_menu.css");
   }
}
public class BaseWebPage extends WebPage{
   public BaseWebPage(){
      // 用意する認証セッションで判定
      final MySession session = (MySession)getSession();
      if (!session.isSignedIn()) setResponsePage(LoginPage.class);
      List<AuthMenuItem> primaryMenuList = buildMenu();

      add(new BlueAuthHorizonDropdownMenu("horizonDropMenu",primaryMenuList));

   }
   private List<AuthMenuItem> buildMenu(){
      AuthMenuItem primaryMenu1 = new AuthMenuItem("HOME",HomePage.class);
      AuthMenuItem primaryMenu2 = new AuthMenuItem("OPEN",OpenPage.class        ,Roles.ADMIN,Roles.USER,RolesX.GUEST);
      AuthMenuItem primaryMenu3 = new AuthMenuItem("USER",UserPage.class        ,Roles.ADMIN,Roles.USER);
      AuthMenuItem primaryMenu4 = new AuthMenuItem("Admin",AdminIndexPage.class ,Roles.ADMIN);
      AuthMenuItem primaryMenu5 = new AuthMenuItem("etc");
      AuthMenuItem logoutMenu   = new AuthMenuItem("Logout",LogoutPage.class);

      primaryMenu2.addSubmenu(new AuthMenuItem("open  1",OpenPage1.class  ,Roles.ADMIN,Roles.USER,RolesX.GUEST));
      primaryMenu2.addSubmenu(new AuthMenuItem("open  2",OpenPage2.class  ,Roles.ADMIN,Roles.USER,RolesX.GUEST));

      primaryMenu3.addSubmenu(new AuthMenuItem("user  1",UserPage1.class  ,Roles.ADMIN,Roles.USER));
      primaryMenu3.addSubmenu(new AuthMenuItem("user  2",UserPage2.class  ,Roles.ADMIN,Roles.USER));

      primaryMenu4.addSubmenu(new AuthMenuItem("admin  1",AdminPage1.class  ,Roles.ADMIN));
      primaryMenu4.addSubmenu(new AuthMenuItem("admin  2",AdminPage2.class  ,Roles.ADMIN));

      primaryMenu5.addSubmenu(new AuthMenuItem("open  1",OpenPage1.class  ,Roles.ADMIN,Roles.USER,RolesX.GUEST));
      primaryMenu5.addSubmenu(new AuthMenuItem("user  2",UserPage2.class  ,Roles.ADMIN,Roles.USER));
      primaryMenu5.addSubmenu(new AuthMenuItem("admin  2",AdminPage2.class  ,Roles.ADMIN));

      List<AuthMenuItem> primaryMenuList = new ArrayList<AuthMenuItem>();
      primaryMenuList.add(primaryMenu1);
      primaryMenuList.add(primaryMenu2);
      primaryMenuList.add(primaryMenu3);
      primaryMenuList.add(primaryMenu4);
      primaryMenuList.add(primaryMenu5);
      primaryMenuList.add(logoutMenu);
      return primaryMenuList;
   }
}