Package realcix20.guis.utils

Source Code of realcix20.guis.utils.MenuManager

/*���ܼ�飄1�7
*    ��ݱ�ZMENU,ZMENUR����MainFrame�IJ˵���
*    ZMENU���˵���ľ������ԣ�����COMMAND��Ҫ��MainFrame��ʵ�ʱ�д��
*    ZMENURΪ��˵����ϵ��
*/
package realcix20.guis.utils;

import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import realcix20.utils.DAO;
import realcix20.utils.Resources;

public class MenuManager {            
        
    //��ڳ��򣬸�ݱ�ZMENUR�в˵����ϵ��b��Ӧ�˵������ָ����������м��ᅣ1�7
    public static JMenuBar createMenu(ActionListener listener) {           
        JMenuBar menuBar = new JMenuBar();
        DAO dao = DAO.getInstance();
        dao.query(Resources.SELECT_FROM_ZMENUR_SQL);
        dao.setString(1, "ROOT");
        ResultSet rs = dao.executeQuery();
        Vector childNames = new Vector();
        try {
            while (rs.next()) {
                childNames.add(rs.getString("MENU2"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator childNamesIter = childNames.iterator();
        while (childNamesIter.hasNext()) {
            String childName = (String)childNamesIter.next();
            JMenu menu = createMenu(childName, listener);
            if (menu != null)
                menuBar.add(menu);
        }
        return menuBar;
    }
       
    //��bmenuName�ӱ�ZMENU���ҵ�ָ��JMenu������װ��
    private static JMenu createMenu(String menuName, ActionListener listener) {
        JMenu menu = new JMenu();
        DAO dao = DAO.getInstance();
        dao.query(Resources.SELECT_FROM_ZMENU_SQL);
        dao.setString(1, menuName);
        ResultSet rs = dao.executeQuery();
        try {
            if (rs.next()) {
                int mnemonic = rs.getInt("MNEMONIC");
                if (mnemonic != 0) {
                    menu.setText(MnemonicGenerator.generateMnemonicString(getMenuTxt(menuName), mnemonic));
                    menu.setMnemonic(mnemonic);
                } else
                    menu.setText(getMenuTxt(menuName));
                String command = rs.getString("COMMAND");
                if (command != null) {
                    menu.setActionCommand(command);
                    menu.addActionListener(listener);
                }                       
                String icon = rs.getString("ICON");
                if (icon != null)
                    menu.setIcon(ImageManager.getImage(icon));
            } else {
                return null;
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        dao = DAO.getInstance();
        dao.query(Resources.SELECT_FROM_ZMENUR_SQL);
        dao.setString(1, menuName);
        rs = dao.executeQuery();
        Vector childMenuNames = new Vector();
        try {                   
            while (rs.next()) {
                childMenuNames.add(rs.getString("MENU2"));                           
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator childMenuNamesIter = childMenuNames.iterator();
        while (childMenuNamesIter.hasNext()) {
            String childMenuName = (String)childMenuNamesIter.next();
            if (childMenuName.equals("SEPORATOR"))
                menu.addSeparator();
            else if (isMenu(childMenuName)) {
                JMenu childMenu = createMenu(childMenuName, listener);
                menu.add(childMenu);
            } else {
                JMenuItem childMenuItem = createMenuItem(childMenuName, listener);
                menu.add(childMenuItem);
            }
        }
        return menu;
    }
       
    //��bmenuName�ӱ�ZMENU���ҵ�ָ��JMenuItem������װ��
    private static JMenuItem createMenuItem(String menuName, ActionListener listener) {
        JMenuItem menuItem = new JMenuItem();
        DAO dao = DAO.getInstance();
        dao.query(Resources.SELECT_FROM_ZMENU_SQL);
        dao.setString(1, menuName);
        ResultSet rs = dao.executeQuery();
        try {
            if (rs.next()) {
                int mnemonic = rs.getInt("MNEMONIC");
                if (mnemonic != 0) {
                    menuItem.setText(MnemonicGenerator.generateMnemonicString(getMenuTxt(menuName), mnemonic));
                    menuItem.setMnemonic(mnemonic);
                } else
                    menuItem.setText(getMenuTxt(menuName));
                String command = rs.getString("COMMAND");
                if (command != null) {
                    menuItem.setActionCommand(command);
                    menuItem.addActionListener(listener);
                }
                String stroke = rs.getString("STROKE");
                if (stroke != null)
                    menuItem.setAccelerator(KeyStroke.getKeyStroke(stroke));
                String icon = rs.getString("ICON");
                menuItem.setIcon(ImageManager.getImage(icon));
            } else {
                return null;
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menuItem;
    }
       
    //��ʻ����?���menuName�õ�ϵͳ���ԵIJ˵���ơᅣ1�7
    private static String getMenuTxt(String menuName) {
        return TxtManager.getTxt("VIEW.MENUBAR." + menuName);
    }
       
    //�ж�һ��˵����Ƿ����Ӳ˵��1�7
    private static boolean isMenu(String menuName) {
        boolean result = false;
        DAO dao = DAO.getInstance();
        dao.query(Resources.SELECT_FROM_ZMENUR_SQL);
        dao.setString(1, menuName);
        ResultSet rs = dao.executeQuery();
        try {
            if (rs.next())
                result = true;
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }               
   
}
TOP

Related Classes of realcix20.guis.utils.MenuManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.