зеркало из https://github.com/mozilla/pjs.git
Changes to allow for lookups of menu items in menubars.
This commit is contained in:
Родитель
9afbb85b76
Коммит
9bd7c99c6c
|
@ -52,6 +52,9 @@ import org.xml.sax.InputSource;
|
|||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import grendel.widgets.MenuCtrl;
|
||||
import grendel.widgets.MenuBarCtrl;
|
||||
|
||||
/**
|
||||
* Build a menu bar from an XML data source. This builder supports:
|
||||
* <UL>
|
||||
|
@ -77,7 +80,7 @@ public class XMLMenuBuilder extends XMLWidgetBuilder {
|
|||
protected Hashtable button_group;
|
||||
|
||||
Hashtable actions;
|
||||
JMenuBar component;
|
||||
MenuBarCtrl component;
|
||||
|
||||
/**
|
||||
* Build a menu builder which operates on XML formatted data
|
||||
|
@ -157,7 +160,7 @@ public class XMLMenuBuilder extends XMLWidgetBuilder {
|
|||
// skip to the "menubar" tag
|
||||
node = tree.getNextElement("menubar");
|
||||
current = (Element)node;
|
||||
component = new JMenuBar();
|
||||
component = new MenuBarCtrl();
|
||||
|
||||
// iterate through every node
|
||||
node = node.getFirstChild();
|
||||
|
@ -196,21 +199,31 @@ public class XMLMenuBuilder extends XMLWidgetBuilder {
|
|||
if (node.getNodeType() != Node.ELEMENT_NODE) return; // can't process it
|
||||
|
||||
// things will recurse through here
|
||||
item = buildComponent((Element)node, (JComponent)parent);
|
||||
|
||||
item = buildComponent((Element)node);
|
||||
|
||||
// find out where we stash the item
|
||||
if (item != null) {
|
||||
Element current = (Element)node;
|
||||
parent.add(item);
|
||||
if (item instanceof JSeparator) {
|
||||
parent.add(item);
|
||||
} else if (parent instanceof MenuCtrl) {
|
||||
((MenuCtrl)parent).addItemByName(current.getAttribute(id_attr),
|
||||
(JMenuItem)item);
|
||||
System.out.println("adding " + current.getAttribute(id_attr));
|
||||
|
||||
} else if (parent instanceof MenuBarCtrl) {
|
||||
((MenuBarCtrl)parent).addItemByName(current.getAttribute(id_attr),
|
||||
(JMenuItem)item);
|
||||
System.out.println("adding " + current.getAttribute(id_attr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the component at the current XML element and add to the parent
|
||||
* @param current the current element
|
||||
* @param parent the parent to add to
|
||||
*/
|
||||
protected JComponent buildComponent(Element current, JComponent parent) {
|
||||
protected JComponent buildComponent(Element current) {
|
||||
String tag = current.getTagName();
|
||||
JComponent comp = null;
|
||||
String label = null;;
|
||||
|
@ -218,7 +231,7 @@ public class XMLMenuBuilder extends XMLWidgetBuilder {
|
|||
// menu tag
|
||||
if (tag.equals(menu_tag)) {
|
||||
Node node;
|
||||
JMenu menu = new JMenu();
|
||||
JMenu menu = new MenuCtrl();
|
||||
String my_id = current.getAttribute(id_attr);
|
||||
comp = menu;
|
||||
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The Original Code is the Grendel mail/news client.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Giao Nguyen
|
||||
* <grail@cafebabe.org>. Portions created by Giao Nguyen are Copyright
|
||||
* (C) 1999 Giao Nguyen. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package grendel.widgets;
|
||||
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* This is a menubar with a keyed lookup rather than a flat iteration model
|
||||
* which JMenuBar by default uses.
|
||||
*/
|
||||
public class MenuBarCtrl extends JMenuBar {
|
||||
|
||||
protected Hashtable controls = new Hashtable();
|
||||
protected Hashtable menus = new Hashtable();
|
||||
|
||||
/**
|
||||
* Get the control by its name.
|
||||
* @param name the control's name
|
||||
* @return the widget
|
||||
*/
|
||||
public JMenuItem getCtrlByName(String name) {
|
||||
JMenuItem item = (JMenuItem)controls.get(name);
|
||||
|
||||
// if it's not a toplevel item, it must be in one of the submenus
|
||||
if (item != null) {
|
||||
Enumeration e = controls.elements();
|
||||
|
||||
while (e.hasMoreElements()) {
|
||||
Object o = e.nextElement();
|
||||
|
||||
if (o instanceof MenuCtrl) {
|
||||
MenuCtrl c = (MenuCtrl)o;
|
||||
item = (JMenuItem)c.getItemByName(name);
|
||||
if (item != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a control to the container.
|
||||
* @param name the control's name
|
||||
* @param component the control
|
||||
*/
|
||||
public void addItemByName(String name, JMenuItem component) {
|
||||
controls.put(name, component);
|
||||
add(component);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The Original Code is the Grendel mail/news client.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Giao Nguyen
|
||||
* <grail@cafebabe.org>. Portions created by Giao Nguyen are Copyright
|
||||
* (C) 1999 Giao Nguyen. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package grendel.widgets;
|
||||
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class MenuCtrl extends JMenu {
|
||||
protected Hashtable controls = new Hashtable();
|
||||
|
||||
/**
|
||||
* Get the control by its name.
|
||||
* @param name the control's name
|
||||
* @return the menu item
|
||||
*/
|
||||
public JMenuItem getItemByName(String name) {
|
||||
JMenuItem item = (JMenuItem)controls.get(name);
|
||||
|
||||
// if it's not a toplevel item, it must be in one of the submenus
|
||||
if (item != null) {
|
||||
Enumeration e = controls.elements();
|
||||
|
||||
while (e.hasMoreElements()) {
|
||||
Object o = e.nextElement();
|
||||
|
||||
if (o instanceof MenuCtrl) {
|
||||
MenuCtrl c = (MenuCtrl)o;
|
||||
item = (JMenuItem)c.getItemByName(name);
|
||||
if (item != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a menu item to the menu.
|
||||
* @param name the name of the menu
|
||||
* @param component the menu item
|
||||
*/
|
||||
public void addItemByName(String name, JMenuItem component) {
|
||||
controls.put(name, component);
|
||||
add(component);
|
||||
}
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче