Terai Atsuhiro 2024-02-02 (金) 12:21:37

http://terai.xrea.jp/swing/menubar/screenshot.png

public JMenuBar createMenubar() {
  JMenuBar mb = new JMenuBar();
  String[] menuKeys = tokenize(getResourceString("menubar"));
  for(int i=0;i<menuKeys.length;i++) {
    JMenu m = createMenu(menuKeys[i]);
    if(m != null) {
      mb.add(m);
    }
  }
  return mb;
}

private JMenu createMenu(String key) {
  String[] itemKeys = tokenize(getResourceString(key));
  String mitext = getResourceString(key + labelSuffix);
  JMenu menu = new JMenu(mitext);
  String mn = getResourceString(key + mneSuffix);
  if(mn!=null) {
    String tmp = mn.toUpperCase().trim();
    if(tmp.length()==1) {
      if(mitext.indexOf(tmp)<0) {
        menu.setText(mitext+" ("+tmp+")");
      }
      byte[] bt = tmp.getBytes();
      menu.setMnemonic((int) bt[0]);
    }
  }
  for(int i=0;i<itemKeys.length;i++) {
    if(itemKeys[i].equals("-")) {
      menu.addSeparator();
    }else{
      JMenuItem mi = createMenuItem(itemKeys[i]);
      menu.add(mi);
    }
  }
  menus.put(key, menu);
  return menu;
}

private JMenuItem createMenuItem(String cmd) {
  String mitext = getResourceString(cmd + labelSuffix);
  JMenuItem mi = new JMenuItem(mitext);
  URL url = getResource(cmd+imageSuffix);
  if(url!=null) {
    mi.setHorizontalTextPosition(JButton.RIGHT);
    mi.setIcon(new ImageIcon(url));
  }
  String astr = getResourceString(cmd + actionSuffix);
  if(astr == null) {
    astr = cmd;
  }
  String mn = getResourceString(cmd + mneSuffix);
  if(mn!=null) {
    String tmp = mn.toUpperCase().trim();
    if(tmp.length()==1) {
      if(mitext.indexOf(tmp)<0) {
        mi.setText(mitext+" ("+tmp+")");
      }
      byte[] bt = tmp.getBytes();
      mi.setMnemonic((int) bt[0]);
    }
  }
  mi.setActionCommand(astr);
  Action a = getAction(astr);
  if(a!=null) {
    mi.addActionListener(a);
    //a.addPropertyChangeListener(createActionChangeListener(mi));
    mi.setEnabled(a.isEnabled());
  }else{
    mi.setEnabled(false);
  }
  menuItems.put(cmd, mi);
  return mi;
}

public JMenuItem getMenuItem(String cmd) {
  return (JMenuItem) menuItems.get(cmd);
}

public JMenu getMenu(String cmd) {
  return (JMenu) menus.get(cmd);
}

public Action getAction(String cmd) {
  return (Action) commands.get(cmd);
}

public Action[] getActions() {
  return actions;
}
public Action[] defaultActions={
  new NewAction(),
  //new OpenAction(),
  new ExitAction(),
  new HelpAction(),
  new VersionAction(),
};