⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configurablepopupmenu.java

📁 本系统是用 java 语言实现的一个 Email客户端
💻 JAVA
字号:
package net.suberic.util.gui;import javax.swing.*;import net.suberic.util.VariableBundle;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.MissingResourceException;import javax.swing.Action;/** * This is a JMenu which implements the ConfigurableUI interface, and * therefore may be dynamically created using a VariableBundle and key, * and updated using an array of Actions. */public class ConfigurablePopupMenu extends JPopupMenu implements ConfigurableUI {    // the latest commands list.  i'm storing this for now because i   // can't do a JButton.removeActionListeners().    protected Hashtable commands = new Hashtable();    public ConfigurablePopupMenu() {    super();  }    /**   * This creates a new ConfigurableMenu using the menuID as the   * configuration key, and vars as the source for the values of all the   * properties.   *   * If menuID doesn't exist in vars, then this returns an empty    * Menu.   */    public ConfigurablePopupMenu(String menuID, VariableBundle vars) {    super();        configureComponent(menuID, vars);  }    /**   * This configures the Menu using the given menuID and    * VariableBundle.   *   * As defined in interface net.suberic.util.gui.ConfigurableUI.   */    public void configureComponent(String key, VariableBundle vars) {    StringTokenizer iKeys = null;    try {      iKeys = new StringTokenizer(vars.getProperty(key), ":");    } catch (MissingResourceException mre) {      try {	System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey());      } catch (MissingResourceException mretwo) {	System.err.println("Unable to load resource " + mre.getKey());      }      return;          }    String currentToken;        try {      setLabel(vars.getProperty(key + ".Label"));    } catch (MissingResourceException mre) {    }        while (iKeys.hasMoreTokens()) {      currentToken=iKeys.nextToken();      if (currentToken.equals("-")) {	this.addSeparator();      } else {	JMenuItem mi = createMenuItem(key, currentToken, vars);	this.add(mi);      }    }  }  /**   * And this actually creates the menu items themselves.   */  protected JMenuItem createMenuItem(String menuID, String menuItemID, VariableBundle vars) {    // TODO:  should also make these undo-able.        if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") {      if (vars.getProperty(menuID + "." + menuItemID, "") != "") {	return new ConfigurableMenu(menuID + "." + menuItemID, vars);      }             JMenuItem mi;      try {	mi = new JMenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label"));      } catch (MissingResourceException mre) {	mi = new JMenuItem(menuItemID);      }            java.net.URL url = null;            try {	url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image"));      } catch (MissingResourceException mre) {      }            if (url != null) {	mi.setHorizontalTextPosition(JButton.RIGHT);	mi.setIcon(new ImageIcon(url));      }            String cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID);            mi.setActionCommand(cmd);	            return mi;    } else {      // this means that we have a submenu.      ConfigurableMenu m;            if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) {	m = new ConfigurableMenu(menuID + "." + menuItemID, vars);	      } else {	// this means we're using a custom Menu.		try {	  Class menuClass = Class.forName(vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableMenu"));	  m = (ConfigurableMenu) menuClass.newInstance();	  m.configureComponent(menuID + "." + menuItemID, vars);	} catch (Exception e) {	  // if we get any errors, just create a plain 	  // ConfigurableMenu.	  System.err.println("Unable to create menu with class " + vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableMenu") + ":  " + e.getMessage());	  e.printStackTrace();	  m = new ConfigurableMenu(menuID + "." + menuItemID, vars);	}      }            return m;          }  }  /**   * As defined in net.suberic.util.gui.ConfigurableUI   */  public void setActive(javax.swing.Action[] newActions) {    Hashtable tmpHash = new Hashtable();    if (newActions != null && newActions.length > 0) {      for (int i = 0; i < newActions.length; i++) {	String cmdName = (String)newActions[i].getValue(Action.NAME);	tmpHash.put(cmdName, newActions[i]);      }    }    setActive(tmpHash);	  }    /**   * As defined in net.suberic.util.gui.ConfigurableUI   */  public void setActive(Hashtable newCommands) {    clearListeners();    commands = newCommands;    setActiveMenuItems();  }    protected void setActiveMenuItems() {    for (int j = 0; j < getSubElements().length; j++) {      if (getComponent(j) instanceof ConfigurableMenu) {	((ConfigurableMenu)getComponent(j)).setActive(commands);      } else {	JMenuItem mi = (JMenuItem)getComponent(j);	Action a = getAction(mi.getActionCommand());	if (a != null) {	  //mi.removeActionListener(a);	  mi.addActionListener(a);	  mi.setEnabled(true);	} else {	  mi.setEnabled(false);	}      }    }  }	        /**   * This clears all of the current listeners on the Menu.   */      private void clearListeners() {    for (int j = 0; j < getSubElements().length; j++) {      if (getComponent(j) instanceof ConfigurableMenu) {	// we don't have to clear the listeners here because	// it will be done in setActive().	;      } else {	JMenuItem mi = (JMenuItem)getComponent(j);	Action a = getAction(mi.getActionCommand());	if (a != null) {	  mi.removeActionListener(a);	}      }    }  }    /**   * This gets an action from the supported commands.  If there is no   * supported action, it returns null   */    public Action getAction(String command) {    return (Action)commands.get(command);  }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -