0

Dynamic Menu Generation (Using Child Parent Relation in Table )

asked 2010-08-26 06:26:57 +0800

Lalitdhuwe gravatar image Lalitdhuwe
36 1

package com.zion.controllers;

import java.awt.BorderLayout;
import java.sql.*;
import java.util.*;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.*;
import com.zion.masters.*;
import java.util.ArrayList;
import org.zkoss.zk.ui.Executions;

/**
 *
 * @author LD000754
 */
public class SessionController extends GenericForwardComposer {

    public String Login, Name, EmployeeID, Active;
    public Menubar mainMenu;
    private BorderLayout menubar;
    public List ls = new java.util.ArrayList();
    private org.zkoss.zul.Include bodyLayout;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        this.MenuCreate();
    }

    public void MenuCreate() throws SQLException {
        boolean valid = Sessions.getCurrent().getAttribute("EmployeeID") != null ? true : false;
        if (!valid) {
            //nameDisplay.setLabel("");
            //Executions.sendRedirect("ErrorHandler.zul");
        } else {
            Login = Sessions.getCurrent().getAttribute("Login").toString();
            Name = Sessions.getCurrent().getAttribute("Name").toString();
            EmployeeID = Sessions.getCurrent().getAttribute("EmployeeID").toString();
            Active = Sessions.getCurrent().getAttribute("Active").toString();

            Menu homeMenu = new Menu();
            homeMenu.setParent(mainMenu);
            homeMenu.setLabel("Home");
            homeMenu.setImage("/images/toolbar/Home.gif");
            homeMenu.setId("Home");
            homeMenu.addEventListener("onClick", new MyListener());
            CheckSession_AcessMenu checksession = new CheckSession_AcessMenu();
            String accessGroupid = checksession.AccessGroupid(EmployeeID);
            if (accessGroupid.length() > 0) {
                List rootModules = new ArrayList();
                rootModules = checksession.getRootModule(EmployeeID);
                Iterator itrootModules = rootModules.iterator();
                Sessions.getCurrent().setAttribute("AccessGroupID", accessGroupid);
                while (itrootModules.hasNext()) {
                    EmployeeAccess empAccess = (EmployeeAccess) itrootModules.next();
                    String rootModuleID = empAccess.getModuleid();
                    String rootModuleNameS = empAccess.getModuleName();
                    Menu rootMenu = new Menu();
                    rootMenu.setParent(mainMenu);
                    rootMenu.setLabel(rootModuleNameS);
                    Menupopup popupAdmin = new Menupopup();
                    popupAdmin.setParent(rootMenu);
                    menuCreation(rootModuleID, popupAdmin);

                }
            }

            Menu nameDisplay = new Menu();
            Menupopup popupName = new Menupopup();
            Menuitem itemName = new Menuitem();
            itemName.setLabel("Logout");
            itemName.setParent(popupName);
            itemName.setId("Logout");
            itemName.addEventListener("onClick", new MyListener());
            popupName.setParent(nameDisplay);
            nameDisplay.setParent(mainMenu);
            nameDisplay.setImage("/images/toolbar/tvResource.gif");
            String setLable = Name + "  ( " + Login + " )";
            nameDisplay.setLabel(setLable);

        }
    }

    public void menuCreation(String rootModuleID, Menupopup popup) throws SQLException {
        CheckSession_AcessMenu checksession = new CheckSession_AcessMenu();
        List childList = new ArrayList();
        childList = checksession.getChildModule(rootModuleID, EmployeeID);
        Iterator itChild = childList.iterator();
        while (itChild.hasNext()) {
            EmployeeAccess employeeAccess = (EmployeeAccess) itChild.next();
            String Moduleid = employeeAccess.getModuleid();
            String Modulename = employeeAccess.getModuleName();
            String URL = employeeAccess.getSubModuleURL();
            List childListInn = new ArrayList();
            childListInn = checksession.getChildModule(Moduleid, EmployeeID);
            Menuitem itemModule1 = new Menuitem();
            if (childListInn.size() == 0) {
                itemModule1.setLabel(Modulename);
                itemModule1.setParent(popup);
                itemModule1.setId(Moduleid);
                itemModule1.setAttribute("URL", URL);
                itemModule1.addEventListener("onClick", new MyListener());
            } else if (childListInn.size() > 0) {
                Menupopup childPopup = new Menupopup();
                Menu childMenu = new Menu();
                childMenu.setLabel(Modulename);
                childPopup.setParent(childMenu);
                childMenu.setParent(popup);
                menuCreation(Moduleid, childPopup);

            }
        }
    }

    class MyListener implements org.zkoss.zk.ui.event.EventListener {

        public void onEvent(org.zkoss.zk.ui.event.Event event) {
            try {
                String controlID = event.getTarget().getId();

                if (controlID.equalsIgnoreCase("Logout") || controlID.equalsIgnoreCase("Home")) {
                    if (controlID.equalsIgnoreCase("Logout")) {
                        Sessions.getCurrent().invalidate();
                        Executions.sendRedirect("index.zul");
                    }
                    if (controlID.equalsIgnoreCase("Home")) {
                        bodyLayout.setSrc("home.zul");
                    }
                } else {
                    CheckSession_AcessMenu checksession = new CheckSession_AcessMenu();
                    boolean valid = Sessions.getCurrent().getAttribute("EmployeeID") != null ? true : false;
                    if (valid) {
                        String URL = checksession.GetModuleURL(controlID).toString();
                        if (URL != null) {
                            bodyLayout.setSrc(URL);
                        }

                    } else {
                        Executions.sendRedirect("index.zul");

                    }
                }

            } catch (Exception ex) {
            }


        }
    }
}

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2010-09-01 20:35:28 +0800

iantsai gravatar image iantsai
2755 1

So, what's your problem?

link publish delete flag offensive edit

answered 2012-02-21 16:54:50 +0800

kcvilyov gravatar image kcvilyov
42
http://Crimean.us/

Does this code works? I have a difficulties with such menu generation..

link publish delete flag offensive edit

answered 2012-08-10 20:26:22 +0800

sixtocontreras gravatar image sixtocontreras
12

Hi good afternoon, I also see I have the same problem when displaying menu options dynamically. sime can help thank you very much,

link publish delete flag offensive edit

answered 2012-08-10 21:21:13 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2012-08-10 21:21:41 +0800

package com.zion.controllers;

import java.awt.BorderLayout; <-- ???????????????????????
import java.sql.*;
. . .

link publish delete flag offensive edit

answered 2012-08-10 22:06:16 +0800

sixtocontreras gravatar image sixtocontreras
12

good afternoon, from time to investigate and consult on the internet I found that you must declare to the file. zul the menu as follows

<menubar id="imsmenu" width="800px" apply="com.drishti.Admin.MenuCreation ">
</menubar>

and the class should not be
public class extends MenuCreation GenericAutowireComposer
but
public class extends MenuCreation GenericForwardComposer

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-08-26 06:26:57 +0800

Seen: 1,570 times

Last updated: Aug 10 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More