通过java程序操纵richfaces的panelmenu标签动态生成导航菜单

化了好长时间研究这个东东,现在拿出来给大家分享,希望对大家有用。

Menu实体类
@Entity
public class MyMenu implements Serializable {

private Long id;
private String type;
private String menuid;
private String pmenuid;
private String viewlabel;
private String link;
         //getter and setter
...
}

Menu的Action
public class SimplePanelMenuAction {

@In(required = false)
private EntityManager entityManager;

@In(required = false)
@Out(required = false)
private MenuState menuState;

HtmlPanelMenu panelMenu;

public MenuState getMenuState() {
return menuState;
}

public void setMenuState(MenuState menuState) {
this.menuState = menuState;
}

public void select(ActionEvent event) {
menuState.setSelectedMenuItem(event.getComponent().getId());
}

private void loadMenu() {

panelMenu = new HtmlPanelMenu();
FacesContext facesContext = FacesContext.getCurrentInstance();
Class[] params = { ActionEvent.class };
Map<String, Boolean> menu = new HashMap<String, Boolean>();
ActionListener actl= new MethodExpressionActionListener(Expressions.instance().createMethodExpression("#{simplePanelMenu1.select}", Void.TYPE, params).toUnifiedMethodExpression());

List<MyMenu> mymenugrops = entityManager
.createQuery(
"select m from MyMenu m where m.type = 'G' and m.pmenuid = '000000'")
.getResultList();
for (MyMenu mymenugrop : mymenugrops) {
HtmlPanelMenuGroup panelGroup = new HtmlPanelMenuGroup();
panelGroup.setId(mymenugrop.getViewlabel());
panelGroup.setName(mymenugrop.getViewlabel());
panelGroup.setLabel(mymenugrop.getViewlabel());
    menu.put(panelGroup.getId(), false);  

List<MyMenu> submymenugrops = entityManager
.createQuery(
"select m from MyMenu m where m.type = 'G' and m.pmenuid = :p1")
.setParameter("p1", mymenugrop.getMenuid()).getResultList();
for (MyMenu submymenugrop : submymenugrops) {
HtmlPanelMenuGroup subpanelGroup = new HtmlPanelMenuGroup();
subpanelGroup.setId(submymenugrop.getViewlabel());
subpanelGroup.setName(submymenugrop.getViewlabel());
subpanelGroup.setLabel(submymenugrop.getViewlabel());

List<MyMenu> mymenuitems = entityManager
.createQuery(
"select m from MyMenu m where m.type = 'I' and m.pmenuid = :p1")
.setParameter("p1", submymenugrop.getMenuid())
.getResultList();
for (MyMenu mymenuitem : mymenuitems) {
HtmlPanelMenuItem subpanelMenuItem = new HtmlPanelMenuItem();
subpanelMenuItem.setId(mymenuitem.getViewlabel());
subpanelMenuItem.setName(mymenuitem.getViewlabel());
subpanelMenuItem.setLabel(mymenuitem.getViewlabel());

subpanelMenuItem.addActionListener(actl);
subpanelMenuItem
        .setActionExpression(Expressions.instance().createMethodExpression(mymenuitem.getLink()).toUnifiedMethodExpression());
subpanelGroup.getChildren().add(subpanelMenuItem);
}
panelGroup.getChildren().add(subpanelGroup);
}

List<MyMenu> mymenuitems = entityManager
.createQuery(
"select m from MyMenu m where m.type = 'I' and m.pmenuid = :p1")
.setParameter("p1", mymenugrop.getMenuid()).getResultList();
for (MyMenu mymenuitem : mymenuitems) {
HtmlPanelMenuItem panelMenuItem = new HtmlPanelMenuItem();
panelMenuItem.setId(mymenuitem.getViewlabel());
panelMenuItem.setName(mymenuitem.getViewlabel());
panelMenuItem.setLabel(mymenuitem.getViewlabel());

panelMenuItem.addActionListener(actl);
panelMenuItem
        .setActionExpression(Expressions.instance().createMethodExpression(mymenuitem.getLink()).toUnifiedMethodExpression());
panelGroup.getChildren().add(panelMenuItem);
}

panelGroup.setValueExpression("value",Expressions.instance().createValueExpression("#{simplePanelMenu.menuState.menu['"+panelGroup.getLabel()+"']}").toUnifiedValueExpression());

panelMenu.getChildren().add(panelGroup);
menu.put(panelGroup.getLabel(), false);
}
menuState.setMenu(menu);
}

public HtmlPanelMenu getPanelMenu() {
if (panelMenu == null) {
loadMenu();
}
return panelMenu;
}

public  void setPanelMenu(HtmlPanelMenu panelMenu) {
this.panelMenu=panelMenu;
}

@Remove
public void destroy() {
}
}

用于保存菜单状态的Bean
private Map <String, Boolean> menu;  
   
   @In(required=false)
   @Out(required=false)
   private String selectedMenuItem;  
   
   public String getSelectedMenuItem() {  
    return selectedMenuItem;  
   }  
   public Map<String, Boolean> getMenu() {  
    return menu;  
   }      
   public void setMenu(Map<String, Boolean> menu) {  
    this.menu = menu;  
   }  
   public void setSelectedMenuItem(String selectedMenuItem) {  
    this.selectedMenuItem = selectedMenuItem;  
   }  
   public MenuState() {  
   }  
}

页面:
<rich:panelMenu id="panelmenu" binding="#{simplePanelMenu.panelMenu}" style="width:185px"
  selectedChild="#{simplePanelMenu.menuState.selectedMenuItem}"
  iconExpandedGroup="disc"   
  iconCollapsedGroup="disc"   
  iconExpandedTopGroup="chevronUp"   
  iconGroupTopPosition="right"   
  iconCollapsedTopGroup="chevronDown"   
  iconCollapsedTopPosition="right" > 
</rich:panelMenu>
<h1>This is TEST2</h1>

你可能感兴趣的:(java,swing,jboss,Richfaces,seam)