📄 saxmenuloader.java
字号:
package com.javaworld.mar2000.sax;
/*
* Sample code for "SAX Appeal", by Mark Johnson, JavaWorld, March 2000.
* Code is may be used for any legal purpose, including commercial
* purposes, with no warranty expressed or implied.
* email: mark.johnson@javaworld.com
*/
import java.io.*;
import java.util.*;
import org.xml.sax.*;
import java.awt.*;
import java.awt.event.*;
import org.xml.sax.helpers.ParserFactory;
//
// This class loads an XML JavaMenus file, and (after loading) returns
// menus by name.
//
public class SaxMenuLoader extends HandlerBase implements ActionListener, ItemListener {
private Parser _parser;
private final static String _parserClass = "com.ibm.xml.parsers.SAXParser";
private Vector _menuStack;
private Vector _menuitemhandlerStack;
private Hashtable _htMenuBars;
private Hashtable _htPopupMenus;
private Hashtable _htMenuItemHandlers;
private MenuBar _menubarCurrent;
private PopupMenu _popupmenuCurrent;
public SaxMenuLoader() {
createParser();
_menuStack = new Vector();
_htMenuBars = new Hashtable();
_htPopupMenus = new Hashtable();
_htMenuItemHandlers = new Hashtable();
_menuitemhandlerStack = new Vector();
_menubarCurrent = null;
_popupmenuCurrent = null;
}
/**
* When a MenuItem is activated, this method finds and calls the
* MenuItem's MenuItemHandler.
*/
public void actionPerformed(ActionEvent e) {
Object oSource = e.getSource();
if (oSource instanceof MenuItem) {
MenuItem mi = (MenuItem) oSource;
MenuItemHandler mih = menuitemhandlerFind(mi);
if (mih != null) {
mih.itemActivated(mi, e, mi.getActionCommand());
}
}
}
/**
* Add a menu to the current menu
*/
protected void add(Menu menu_) {
Menu menuCurrent = menuCurrent();
if (menuCurrent != null) {
menuCurrent.add(menu_);
} else {
if (_menubarCurrent != null) {
_menubarCurrent.add(menu_);
}
if (_popupmenuCurrent != null) {
_popupmenuCurrent.add(menu_);
}
}
}
/**
* Add a MenuItem to the current Menu
*/
protected void add(MenuItem menuitem_) {
String sItemName = menuitem_.getName();
// Add this item to current menu, or to popup
// If we're trying to add an item to a menu bar, generate an error.
Menu menuCurrent = menuCurrent();
if (menuCurrent != null) {
menuCurrent.add(menuitem_);
} else {
if (_menubarCurrent != null) {
System.err.println(sItemName + ": Can't add bare menu items to menu bars");
}
else if (_popupmenuCurrent != null) {
_popupmenuCurrent.add(menuitem_);
}
}
// If there's a handler defined for this context, store a reference to
// it, indexed by the menu item OBJECT.
MenuItemHandler mih = menuitemhandlerCurrent();
if (mih != null) {
registerMenuItemHandler(menuitem_, mih);
}
}
/**
* Initialize the internal parser.
*/
protected void createParser() {
_parser = null;
try {
_parser = ParserFactory.makeParser(_parserClass);
} catch (Exception ex) {
System.err.println("SaxMenuFactory.createParser: " + _parserClass + ": " + ex.getMessage());
ex.printStackTrace();
}
}
/**
* Define a checkbox menu item and add it to the current menu
*/
protected void defineCheckboxMenuItem(AttributeList attrs_) {
// Get attributes
String sItemName = attrs_.getValue("NAME");
String sItemLabel = attrs_.getValue("LABEL");
// Create new item
CheckboxMenuItem miNew = new CheckboxMenuItem(sItemName);
if (sItemName != null) {
miNew.setName(sItemName);
} else {
sItemName = miNew.getName();
}
// Set menu attributes
if (sItemLabel != null) {
miNew.setLabel(sItemLabel);
} else {
miNew.setLabel(sItemName);
}
// Add menu item to whatever's currently being built
add(miNew);
miNew.addItemListener(this);
}
/**
* Define a menu, add it the the current menu, and then make
* the new menu the current menu.
*/
protected void defineMenu(AttributeList attrs_) {
String sMenuName = attrs_.getValue("NAME");
Menu menuNew = new Menu(sMenuName);
if (sMenuName != null) {
menuNew.setName(sMenuName);
} else {
sMenuName = menuNew.getName();
}
System.out.print("Created menu " + sMenuName);
// Add to current context and make new menu the current menu to build
add(menuNew);
pushMenu(menuNew);
}
/**
* Define a menu bar and register it in the hash table
*/
protected void defineMenuBar(AttributeList attrs_) {
String sMenuName = attrs_.getValue("NAME");
_menubarCurrent = new MenuBar();
if (sMenuName != null) {
_menubarCurrent.setName(sMenuName);
}
register(_menubarCurrent);
}
/**
* Define a new MenuItem and add it to the current menu
*/
protected void defineMenuItem(AttributeList attrs_) {
// Get attributes
String sItemName = attrs_.getValue("NAME");
String sItemLabel = attrs_.getValue("LABEL");
// Create new item
MenuItem miNew = new MenuItem(sItemName);
if (sItemName != null) {
miNew.setName(sItemName);
} else {
sItemName = miNew.getName();
}
// Set menu attributes
if (sItemLabel != null) {
miNew.setLabel(sItemLabel);
} else {
miNew.setLabel(sItemName);
}
// Add menu item to whatever's currently being built
add(miNew);
miNew.addActionListener(this);
}
/**
* Define a new popup menu and register it in the hash table.
*/
protected void definePopupMenu(AttributeList attrs_) {
String sMenuName = attrs_.getValue("NAME");
_popupmenuCurrent = new PopupMenu();
if (sMenuName != null) {
_popupmenuCurrent.setName(sMenuName);
}
register(_popupmenuCurrent);
}
/**
* Report that the document parse is complete.
*/
public void endDocument() {
System.out.println("End document");
}
/**
* Handle the end of an element context. This varies by element type.
*/
public void endElement(String sName_) {
// Every element pushes something, even if it's just its parent's handler
menuitemhandlerPop();
if (sName_.equalsIgnoreCase("menu")) {
Menu menuCurrent = menuCurrent();
if (menuCurrent != null) {
System.out.println("End menu " + menuCurrent.getName());
} else {
System.out.println("End unnamed menu");
}
menuPop();
}
if (sName_.equalsIgnoreCase("menubar")) {
_menubarCurrent = null;
}
if (sName_.equalsIgnoreCase("popupmenu")) {
_popupmenuCurrent = null;
}
}
/**
* Dispatch check/uncheck events from CheckboxMenuItems.
*/
public void itemStateChanged(ItemEvent e) {
Object oSource = e.getSource();
if (oSource instanceof MenuItem) {
MenuItem mi = (MenuItem) oSource;
MenuItemHandler mih = menuitemhandlerFind(mi);
if (mih != null) {
if (e.getStateChange() == ItemEvent.SELECTED) {
mih.itemSelected(mi, e, mi.getActionCommand());
} else {
mih.itemDeselected(mi, e, mi.getActionCommand());
}
}
}
}
/**
* Load menus from a file
*/
public void loadMenus(File file_) {
try {
Reader reader = new FileReader(file_);
loadMenus(reader);
} catch (FileNotFoundException ex) {
System.err.println("SaxMenuFactory.loadMenus: " + file_ + ": " + ex.getMessage());
}
}
/**
* Load menus from an input stream
*/
public void loadMenus(InputStream istream_) {
loadMenus(new InputStreamReader(istream_));
}
/**
* Load menus from a "Reader"
*/
public void loadMenus(Reader reader_) {
if (_parser == null)
return;
_parser.setDocumentHandler(this);
try {
_parser.parse(new InputSource(reader_));
} catch (SAXException ex) {
System.out.println("Parse error: " + ex.getMessage());
} catch (Exception ex) {
System.err.println("SaxMenuFactory.loadMenus(): " + ex.getClass().getName() +
ex.getMessage());
ex.printStackTrace();
}
}
/**
* Load menus from a file
*/
public void loadMenus(String sFilename_) {
loadMenus(new File(sFilename_));
}
/**
* Return MenuBar by name.
*/
protected MenuBar menubarFind(String sMenuName_) {
Object oToReturn = _htMenuBars.get(sMenuName_);
if (oToReturn != null && oToReturn instanceof MenuBar) {
return (MenuBar)oToReturn;
}
return null;
}
/**
* Return reference to current menu
*/
protected Menu menuCurrent() {
int iStacksize = _menuStack.size();
if (iStacksize == 0) {
return null;
}
Menu menuToReturn = (Menu)_menuStack.elementAt(iStacksize - 1);
return menuToReturn;
}
/**
* Return reference to current MenuItemHandler.
*/
protected MenuItemHandler menuitemhandlerCurrent() {
if (_menuitemhandlerStack == null || _menuitemhandlerStack.size() == 0) {
return null;
}
return (MenuItemHandler)_menuitemhandlerStack.elementAt(_menuitemhandlerStack.size()-1);
}
/**
* Given a MenuItem, return its handler
*/
protected MenuItemHandler menuitemhandlerFind(MenuItem mi_) {
Object oHandler = _htMenuItemHandlers.get(mi_);
return (MenuItemHandler)oHandler;
}
/**
* Get a MenuItemHandler by name
*/
protected MenuItemHandler menuitemhandlerFind(String sName_) {
if (sName_ == null)
return null;
MenuItemHandler mih = (MenuItemHandler) _htMenuItemHandlers.get(sName_);
// Not registered. See if it's a class name, and if it is, create an
// instance of that class and register it.
if (mih == null) {
try {
Class classOfHandler = Class.forName(sName_);
MenuItemHandler newHandler = (MenuItemHandler)classOfHandler.newInstance();
registerMenuItemHandler(sName_, newHandler);
mih = newHandler;
} catch (Exception ex) {
System.err.println("Couldn't find menu item handler '" + sName_ +
": no such registered handler, and couldn't create");
System.err.println(sName_ + ": " + ex.getClass().getName() + ": " + ex.getMessage());
}
}
return mih;
}
/**
* Pop a menu item handler off of the handler stack
*/
protected MenuItemHandler menuitemhandlerPop() {
MenuItemHandler l = menuitemhandlerCurrent();
if (l != null) {
_menuitemhandlerStack.removeElementAt(_menuitemhandlerStack.size() - 1);
}
return l;
}
/**
* Pop menu from current menu stack.
*/
protected Menu menuPop() {
Menu menuToReturn = menuCurrent();
if (menuToReturn != null) {
_menuStack.removeElementAt(_menuStack.size() - 1);
}
return menuToReturn;
}
/**
* Find a popup menu by name
*/
protected PopupMenu popupmenuFind(String sMenuName_) {
Object oToReturn = _htPopupMenus.get(sMenuName_);
if (oToReturn != null && oToReturn instanceof PopupMenu) {
return (PopupMenu)oToReturn;
}
return null;
}
/**
* Push menu onto menu stack
*/
protected void pushMenu(Menu menu_) {
_menuStack.addElement(menu_);
}
/**
* Push menu item handler onto handler stack
*/
protected void pushMenuItemHandler(MenuItemHandler l) {
_menuitemhandlerStack.addElement(l);
}
/**
* Find menu item handler by name, then push it on the handler stack
*/
protected void pushMenuItemHandler(String sName_) {
MenuItemHandler l = menuitemhandlerFind(sName_);
if (l == null)
l = menuitemhandlerCurrent();
pushMenuItemHandler(l);
}
/**
* Register a menubar in the menubar hash table.
*/
protected void register(MenuBar menubar_) {
_htMenuBars.put(menubar_.getName(), menubar_);
}
/**
* Register a popup menu in the popup menu hash table
*/
protected void register(PopupMenu popupmenu_) {
_htPopupMenus.put(popupmenu_.getName(), popupmenu_);
}
/**
* Register a menu item handler in the handler table by menu item
*/
protected void registerMenuItemHandler(MenuItem mi_, MenuItemHandler l) {
_htMenuItemHandlers.put(mi_, l);
}
/**
* Register a menu item handler in the handler table by name
*/
public void registerMenuItemHandler(String sHandlerName_, MenuItemHandler l) {
_htMenuItemHandlers.put(sHandlerName_, l);
}
/**
* Report that a document has begun
*/
public void startDocument() {
System.out.println("Begin document");
}
/**
* Handler the beginning of an element.
*/
public void startElement(String sName_, AttributeList attrs_) {
// Anything may override handler for its context
String sHandler = attrs_.getValue("HANDLER");
pushMenuItemHandler(sHandler);
// If "menubar", we're building a MenuBar
if (sName_.equals("MenuBar")) {
defineMenuBar(attrs_);
}
// If "popupMenu", we're building a PopupMenu
else if (sName_.equals("PopupMenu")) {
definePopupMenu(attrs_);
}
// If "menu", then create a menu.
else if (sName_.equals("Menu")) {
defineMenu(attrs_);
}
else if (sName_.equals("MenuItem")) {
defineMenuItem(attrs_);
}
else if (sName_.equals("CheckboxMenuItem")) {
defineCheckboxMenuItem(attrs_);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -