001: package com.ice.util;
002:
003: import java.awt.event.ActionListener;
004:
005: import javax.swing.JMenu;
006: import javax.swing.JPopupMenu;
007: import javax.swing.JComponent;
008: import javax.swing.JMenuItem;
009:
010: public class MenuProperties extends Object {
011: static public JPopupMenu loadPopupMenu(String menuPropertyName,
012: ActionListener listener) {
013: JPopupMenu popup = new JPopupMenu();
014:
015: MenuProperties.addMenuItems(popup, menuPropertyName, listener);
016:
017: return popup;
018: }
019:
020: static public void addGenericItem(JComponent menu, JComponent item) {
021: if (menu instanceof JMenu) {
022: JMenu jm = (JMenu) menu;
023:
024: if (item == null)
025: jm.addSeparator();
026: else
027: jm.add(item);
028: } else {
029: JPopupMenu jp = (JPopupMenu) menu;
030:
031: if (item == null)
032: jp.addSeparator();
033: else
034: jp.add(item);
035: }
036: }
037:
038: static public void addMenuItems(JComponent menu,
039: String menuPropertyName, ActionListener listener) {
040: String[] itemList;
041: String itemString;
042: String menuString;
043: String itemNameStr;
044: JMenuItem mItem;
045:
046: menuString = UserProperties.getProperty("menu."
047: + menuPropertyName, null);
048:
049: if (menuString == null) {
050: ICETracer.traceWithStack("Menu definition property '"
051: + menuPropertyName + "' is not defined.");
052: return;
053: }
054:
055: itemList = StringUtilities.splitString(menuString, ":");
056:
057: if (itemList != null) {
058: for (int iIdx = 0; iIdx < itemList.length; ++iIdx) {
059: itemNameStr = "item." + menuPropertyName + "."
060: + itemList[iIdx];
061:
062: itemString = UserProperties.getProperty(itemNameStr,
063: null);
064:
065: if (itemString == null) {
066: ICETracer.traceWithStack("Menu definition '"
067: + menuPropertyName
068: + "' is missing item definition property '"
069: + itemNameStr + "'.");
070: } else {
071: int colonIdx = itemString.indexOf(':');
072:
073: if (itemString.equals("-")) {
074: MenuProperties.addGenericItem(menu, null);
075: } else if (colonIdx < 0) {
076: ICETracer.traceWithStack("Menu '"
077: + menuPropertyName + "' Item '"
078: + itemNameStr
079: + "' has invalid definition.");
080: } else {
081: String title = itemString
082: .substring(0, colonIdx);
083:
084: String command = itemString
085: .substring(colonIdx + 1);
086:
087: if (command.equals("@")) {
088: JMenu subMenu = new JMenu(title);
089:
090: String subMenuName = menuPropertyName + "."
091: + itemList[iIdx];
092:
093: MenuProperties.addMenuItems(subMenu,
094: subMenuName, listener);
095:
096: MenuProperties
097: .addGenericItem(menu, subMenu);
098: } else if (title.equals("-")) {
099: MenuProperties.addGenericItem(menu, null);
100: } else {
101: mItem = new JMenuItem(title);
102:
103: if (listener != null) {
104: mItem.addActionListener(listener);
105: mItem.setActionCommand(command);
106: }
107:
108: MenuProperties.addGenericItem(menu, mItem);
109: }
110: }
111: } // itemString != null
112: } // foreach item
113: } // itemList != null
114: }
115:
116: }
|