net.meddeb.md.admin.client.MenuManager.java Source code

Java tutorial

Introduction

Here is the source code for net.meddeb.md.admin.client.MenuManager.java

Source

package net.meddeb.md.admin.client;

/*--------------------------------------------------------------------
mdCore module. The boot module for Merged directory system.
Copyright (C) 2015, Abdelhamid MEDDEB (abdelhamid@meddeb.net)  
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------*/

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;

public class MenuManager {
    private MainMsg mainMsg = GWT.create(MainMsg.class);
    private MenuLauncher menuLauncher = null;
    private final MenuBar menu = new MenuBar();
    private final MenuBar fileMenu = new MenuBar(true);
    private final MenuBar securityMenu = new MenuBar(true);
    private final MenuBar settingMenu = new MenuBar(true);
    private final MenuBar maintenanceMenu = new MenuBar(true);
    private final MenuBar helpMenu = new MenuBar(true);
    private final Map<MDDAMenu, MenuItem> menuList = new HashMap<MDDAMenu, MenuItem>();

    private boolean isMenuaActive(MDDAMenu menuKey) {
        return menuKey == MDDAMenu.PQPARAMETERS;
    }

    private void initMenuStatus() {
        for (Map.Entry<MDDAMenu, MenuItem> entry : menuList.entrySet()) {
            MDDAMenu menuKey = entry.getKey();
            if ((menuKey != MDDAMenu.ROOTCONNECT) && (menuKey != MDDAMenu.HELPABOUT)
                    && (menuKey != MDDAMenu.HELPLINK))
                entry.getValue().addStyleName("disabled");
        }
    }

    public void setLoggedoutStatus() {
        for (Map.Entry<MDDAMenu, MenuItem> entry : menuList.entrySet()) {
            MDDAMenu menuKey = entry.getKey();
            if ((menuKey != MDDAMenu.HELPABOUT) && (menuKey != MDDAMenu.HELPLINK))
                if (isMenuaActive(menuKey))
                    entry.getValue().addStyleName("disabled");
            if (menuKey == MDDAMenu.ROOTCONNECT)
                entry.getValue().removeStyleName("disabled");
        }
    }

    public void setRootLoggedinStatus() {
        for (Map.Entry<MDDAMenu, MenuItem> entry : menuList.entrySet()) {
            MDDAMenu menuKey = entry.getKey();
            if (isMenuaActive(menuKey))
                entry.getValue().removeStyleName("disabled");
            if (menuKey == MDDAMenu.ROOTCONNECT)
                entry.getValue().addStyleName("disabled");
        }
    }

    private MDDAMenu getCurrentmenu() {
        String strMenu = Cookies.getCookie(MenuLauncher.MENUTAG);
        MDDAMenu rslt = MDDAMenu.fromName(strMenu);
        return rslt;
    }

    private void initMenu() {
        menu.setAutoOpen(true);
        menu.setAnimationEnabled(true);
        menu.setStyleName("gwt-MenuBar-local");
        // Create the file menu
        fileMenu.setAnimationEnabled(true);
        final MenuItem mnuFAddresses = new MenuItem(mainMsg.addresses(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu file/adresses
                //menuLauncher.launch(MDDAMenu.ADDRESSES);
            }
        });
        final MenuItem mnuFSites = new MenuItem(mainMsg.sites(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu file/sites
                //menuLauncher.launch(MDDAMenu.SITES);
            }
        });
        final MenuItem mnuFLocations = new MenuItem(mainMsg.locations(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu file/locations
                //menuLauncher.launch(MDDAMenu.LOCATIONS);
            }
        });
        final MenuItem mnuFRoles = new MenuItem(mainMsg.roles(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu file/roles
                //menuLauncher.launch(MDDAMenu.ROLES);
            }
        });
        final MenuItem mnuFGroups = new MenuItem(mainMsg.groups(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu file/groups
                //menuLauncher.launch(MDDAMenu.GROUPS);
            }
        });
        final MenuItem mnuFRootConnect = new MenuItem(mainMsg.rootConnect(), new Command() {
            @Override
            public void execute() {
                menuLauncher.launch(MDDAMenu.ROOTCONNECT);
            }
        });
        fileMenu.addItem(mnuFAddresses);
        fileMenu.addItem(mnuFSites);
        fileMenu.addItem(mnuFLocations);
        fileMenu.addItem(mnuFRoles);
        fileMenu.addItem(mnuFGroups);
        fileMenu.addSeparator();
        fileMenu.addItem(mnuFRootConnect);
        menuList.put(MDDAMenu.ADDRESSES, mnuFAddresses);
        menuList.put(MDDAMenu.SITES, mnuFSites);
        menuList.put(MDDAMenu.LOCATIONS, mnuFLocations);
        menuList.put(MDDAMenu.ROLES, mnuFRoles);
        menuList.put(MDDAMenu.GROUPS, mnuFGroups);
        menuList.put(MDDAMenu.ROOTCONNECT, mnuFRootConnect);
        //Create Security menu
        securityMenu.setAnimationEnabled(true);
        final MenuItem pqParams = new MenuItem(mainMsg.pqParams(), new Command() {
            @Override
            public void execute() {
                menuLauncher.launch(MDDAMenu.PQPARAMETERS);
            }
        });
        final MenuItem securityLevels = new MenuItem(mainMsg.securityLevels(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu security/security levels
                //menuLauncher.launch(MDDAMenu.SECURITYLEVEL);
            }
        });
        final MenuItem securityLevelsAssign = new MenuItem(mainMsg.securityLevelsAssign(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu security/Persons -> security level
                //menuLauncher.launch(MDDAMenu.SECURITYLEVELASSIGN);
            }
        });
        securityMenu.addItem(pqParams);
        securityMenu.addItem(securityLevels);
        securityMenu.addItem(securityLevelsAssign);
        menuList.put(MDDAMenu.PQPARAMETERS, pqParams);
        menuList.put(MDDAMenu.SECURITYLEVEL, securityLevels);
        menuList.put(MDDAMenu.SECURITYLEVELASSIGN, securityLevelsAssign);
        //Create Setting menu
        settingMenu.setAnimationEnabled(true);
        final MenuItem mnuStDatasource = new MenuItem(mainMsg.dataSources(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu stting/Datasources
                //menuLauncher.launch(MDDAMenu.SETDATASOURCE);
            }
        });
        final MenuItem mnuStImportDepartment = new MenuItem(mainMsg.importDepartments(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu stting/Import departments
                //menuLauncher.launch(MDDAMenu.IMPORTDEPARTMENT);
            }
        });
        final MenuItem mnuStImportPersons = new MenuItem(mainMsg.importPersons(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu stting/Import persons
                //menuLauncher.launch(MDDAMenu.IMPORTPERSON);
            }
        });
        final MenuItem mnuStSetRights = new MenuItem(mainMsg.rights(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu stting/Rights
                //menuLauncher.launch(MDDAMenu.SETRIGHTS);
            }
        });
        settingMenu.addItem(mnuStDatasource);
        settingMenu.addItem(mnuStImportDepartment);
        settingMenu.addItem(mnuStImportPersons);
        settingMenu.addItem(mnuStSetRights);
        menuList.put(MDDAMenu.SETDATASOURCE, mnuStDatasource);
        menuList.put(MDDAMenu.IMPORTDEPARTMENT, mnuStImportDepartment);
        menuList.put(MDDAMenu.IMPORTPERSON, mnuStImportPersons);
        menuList.put(MDDAMenu.SETRIGHTS, mnuStSetRights);
        //Create Maintenance menu
        maintenanceMenu.setAnimationEnabled(true);
        final MenuItem mnuMRights = new MenuItem(mainMsg.rights(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu maintenance/Rights
                //menuLauncher.launch(MDDAMenu.RIGHTS);
            }
        });
        final MenuItem mnuMDepartment = new MenuItem(mainMsg.departments(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu maintenance/Departments
                //menuLauncher.launch(MDDAMenu.DEPARTMENT);
            }
        });
        final MenuItem mnuMPerson = new MenuItem(mainMsg.persons(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu maintenance/Persons
                //menuLauncher.launch(MDDAMenu.PERSON);
            }
        });
        final MenuItem mnuMRefData = new MenuItem(mainMsg.refData(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu maintenance/Reference data
                //menuLauncher.launch(MDDAMenu.REFDATA);
            }
        });
        final MenuItem mnuMDatasource = new MenuItem(mainMsg.dataSources(), new Command() {
            @Override
            public void execute() {
                //TODO: Build functionnality and activate menu maintenance/Datasources
                //menuLauncher.launch(MDDAMenu.DATASOURCE);
            }
        });
        maintenanceMenu.addItem(mnuMRights);
        maintenanceMenu.addItem(mnuMDepartment);
        maintenanceMenu.addItem(mnuMPerson);
        maintenanceMenu.addItem(mnuMRefData);
        maintenanceMenu.addItem(mnuMDatasource);
        menuList.put(MDDAMenu.RIGHTS, mnuMRights);
        menuList.put(MDDAMenu.DEPARTMENT, mnuMDepartment);
        menuList.put(MDDAMenu.PERSON, mnuMPerson);
        menuList.put(MDDAMenu.REFDATA, mnuMRefData);
        menuList.put(MDDAMenu.DATASOURCE, mnuMDatasource);
        // Create the help menu
        helpMenu.setAnimationEnabled(true);
        final MenuItem mnuHLink = new MenuItem(mainMsg.helpLink(), new Command() {
            @Override
            public void execute() {
                menuLauncher.launch(MDDAMenu.HELPLINK);
            }
        });
        final MenuItem mnuHAbout = new MenuItem(mainMsg.helpAbout(), new Command() {
            @Override
            public void execute() {
                menuLauncher.launch(MDDAMenu.HELPABOUT);
            }
        });
        helpMenu.addItem(mnuHLink);
        helpMenu.addItem(mnuHAbout);
        menuList.put(MDDAMenu.HELPLINK, mnuHLink);
        menuList.put(MDDAMenu.HELPABOUT, mnuHAbout);

        setLoggedoutStatus();

        menu.addItem(mainMsg.file(), fileMenu);
        menu.addSeparator();
        menu.addItem(mainMsg.security(), securityMenu);
        menu.addSeparator();
        menu.addItem(mainMsg.setting(), settingMenu);
        menu.addSeparator();
        menu.addItem(mainMsg.maintenance(), maintenanceMenu);
        menu.addSeparator();
        menu.addItem(mainMsg.help(), helpMenu);
        RootPanel.get("menu").clear();
        RootPanel.get("menu").add(menu);
    }

    public MenuManager() {
        initMenu();
        initMenuStatus();
        menuLauncher = new MenuLauncher(this);
    }

    public void launchCurrent() {
        menuLauncher.initConnectStatus();
        MDDAMenu menu = getCurrentmenu();
        menuLauncher.launch(menu, true);
    }
}