📄 menutools.java
字号:
String infoText = getAttributeValue(entryElement, "statustext");
String iconName = getAttributeValue(entryElement, "image");
String commandId = getAttributeValue(entryElement, "command");
String actionListenerName = (String) actionListnerList.get(commandId);
entryName = nameWithAccelerator(entryName, entryAccelerator);
//cat.debug("Menu: " + entryName + ", " + actionListenerName);
switch (type) {
case TYPE_MENUBAR:
{
addMenuEntry(menuName, entryName, infoText, iconName, actionListenerName);
break;
}
case TYPE_POPUP:
{
cat.error("not implenented: not added to popup");
break;
}
default:
{
cat.error("unsupported id <" + type + ">");
break;
}
}
} // end for menu entries
} // end for menu bar eintries
} catch (Exception e) {
cat.error(e);
}
} // end method
/**
* generates a MenuItem that can be inserted in JMenu of JPopupMenu
*
* @param iType the type of the item : ITEM_RADIO, ITEM_CHECK of ITEM_PLAIN
*/
public JMenuItem createMenuItem(int iType, String sText,
ImageIcon image, char acceleratorKey,
String sToolTip) {
// Create the item
JMenuItem menuItem;
switch (iType) {
case ITEM_RADIO:
menuItem = new JRadioButtonMenuItem();
break;
case ITEM_CHECK:
menuItem = new JCheckBoxMenuItem();
break;
default:
menuItem = new JMenuItem();
break;
}
// Add the optional icon
if ((image == null) || (image.getIconHeight() <= 0)) {
String name = imageDir + "transparent.gif";
//name = FileTools.removeFileUrlPrefix(name);
//URL iconUrl = FileTools.getFileURL(name, null);
//image = new ImageIcon(iconUrl);
URL imageUrl = this.getClass().getResource(name);
image = new ImageIcon(imageUrl);
//assert (image.getIconHeight() > 0);
}
if ((image != null) && (image.getIconHeight() > 0))
menuItem.setIcon(image);
// Add the item text
menuItem.setText(sText);
// Add the accelerator key
if (acceleratorKey > 0)
menuItem.setMnemonic(acceleratorKey);
// Add the optional tool tip text
if (sToolTip != null)
menuItem.setToolTipText(sToolTip);
// Add an action handler to this menu item
//menuItem.addActionListener(this);
//menu.add(menuItem);
return menuItem;
} // end method
public JMenuItem createMenuItem(String sText, char acceleratorKey) {
return createMenuItem(ITEM_PLAIN, sText, null, acceleratorKey, "");
}
/**
* add a component to the menuebar (without actionlistener and menuentry)
*/
public void addToolBarEntry(JComponent component1) {
JToolBar toolBar = new JToolBar(component1.getName());
toolBar.add(component1);
//toolbarEntries.put(component1.getName(), toolBar);
//mainFrame.getContentPane().add(toolBar, BorderLayout.NORTH);
toolBarContainer.add(toolBar);
toolBar.validate();
toolBar.repaint();
}
/**
* add a menuItem to menuBar
*/
public void addMenuEntry(String category1, String entry1, String description1,
String iconName1, ActionListener actionListener1) {
if (menuEntries == null)
menuEntries = new HashMap();
if (toolbarEntries == null)
toolbarEntries = new HashMap();
String iconFileName1 = imageDir + iconName1;
//cat.debug("orginal input: <" + iconFileName1 + ">");
//iconFileName1 = FileTools.removeFileUrlPrefix(iconFileName1);
//URL iconUrl = FileTools.getFileURL(iconFileName1, null);
URL iconUrl = this.getClass().getResource(iconFileName1);
//String testStr = iconUrl2.toExternalForm();
/*URL iconUrl3 = null;
try {
iconUrl3 = new URL(testStr);
} catch (MalformedURLException e) {
e.printStackTrace();
}*/
ImageIcon image1;
if (iconUrl == null) {
image1 = new ImageIcon(iconFileName1);
cat.debug("url is null. pass name as string");
//cat.debug("USING FILE: \"" + iconFileName1 + "\"");
} else {
image1 = new ImageIcon(iconUrl);
//cat.debug("USING URL: \"" + iconUrl.toExternalForm() + "\"");
//cat.debug("WORK URL: \"" + iconUrl.toExternalForm() + "\"");
}
/*if (iconUrl2 != null )
cat.debug("WORKING URL: \"" + iconUrl2.toExternalForm() + "\"");
else
cat.error("url2 is null.");*/
/*cat.debug("getImage is null: " + (image1.getImage() == null) +
", toSTring: <" + image1.toString() + ">");
*/
// ---- add the entry to the menu
MenuEntryStruct categoryData = new MenuEntryStruct(category1);
MenuEntryStruct entryData = new MenuEntryStruct(entry1);
//cat.debug(categoryData.toString());
//cat.debug(entryData.toString());
JMenu menu;
if (menuEntries.containsKey(categoryData.getText())) {
menu = (JMenu) menuEntries.get(categoryData.getText());
} else {
menu = new JMenu(categoryData.getText());
if (categoryData.getMnemonic() != 0) {
menu.setMnemonic(categoryData.getMnemonic());
}
menuEntries.put(categoryData.getText(), menu);
menuBar.add(menu);
}
/*JMenuItem menuItem = createMenuItem(java2dTools.ITEM_PLAIN, entryData.getText(),
image1, entryData.getMnemonic(), description1);
*/
JMenuItem menuItem = createMenuItem(ITEM_PLAIN, entryData.getText(),
image1, entryData.getMnemonic(), "");
menuItem.addActionListener(actionListener1);
menu.add(menuItem);
// -- add the entry to the tool bar
// add only, if the image exists
//cat.debug("GO: " + image1.getIconHeight());
if (image1.getIconHeight() > 0) {
JToolBar toolBar;
if (toolbarEntries.containsKey(categoryData.getText())) {
toolBar = (JToolBar) toolbarEntries.get(categoryData.getText());
} else {
toolBar = new JToolBar(categoryData.getText());
toolbarEntries.put(categoryData.getText(), toolBar);
//mainFrame.getContentPane().add(toolBar, BorderLayout.NORTH);
toolBarContainer.add(toolBar);
}
JButton jButton1 = new JButton();
toolBar.add(jButton1);
jButton1.setIcon(image1);
jButton1.setToolTipText(description1);
jButton1.addActionListener(actionListener1);
}
} // end method
/**
* create a own actionlistener from the name.
*/
public void addMenuEntry(String category1, String entry1, String description1,
String iconName1, String actionListenerName1) {
ActionListener actionListener = null;
try {
actionListener = createActionListener(actionListenerName1, parentClass);
} catch (Exception e) {
cat.error("Unable to create actionListener for \"" +
parentClass.getClass().getName() + "." + actionListenerName1 + "\"\n" +
"Menubar: <" + category1 + ">, Item: <" + entry1 + ">, Icon: <" + iconName1 + ">");
}
//ActionListener actionListener = getActionListener(actionListenerName1);
addMenuEntry(category1, entry1, description1, iconName1, actionListener);
}
private static ActionListener createActionListener(String actionListenerName1, Object targetClass) {
ActionListener actionListener;
actionListener = (ActionListener)
(GenericListener.create(ActionListener.class, "actionPerformed",
targetClass, actionListenerName1));
return actionListener;
}
//------------- methods for using a popup menu
// display the poputmenu
public void showPopupMenu(Component parentComponent1, int x, int y) {
if (popupMenu != null) {
popupMenu.show(parentComponent1, x, y);
}
}
public void addPopupMenuEntry(String entry1, String description1,
String iconName1, String actionListenerName1) {
ActionListener actionListener = (ActionListener)
(GenericListener.create(ActionListener.class, "actionPerformed",
parentClass, actionListenerName1));
addPopupMenuEntry(entry1, description1, iconName1, actionListener);
}
public void addPopupMenuEntry(String entry1, String description1,
String iconName1, ActionListener actionListener1) {
String iconFileName1 = imageDir + iconName1;
ImageIcon image1 = new ImageIcon(iconFileName1);
// ---- parse the entry to the menu
MenuEntryStruct entryData = new MenuEntryStruct(entry1);
//cat.debug(categoryData.toString());
//cat.debug(entryData.toString());
JMenuItem menuItem = createMenuItem(ITEM_PLAIN, entryData.getText(),
image1, entryData.getMnemonic(), description1);
menuItem.addActionListener(actionListener1);
popupMenu.add(menuItem);
} // end method
public void removeAllPopupEntries() {
if (popupMenu != null) {
popupMenu.removeAll();
}
}
public void setParentClass(Object parentClass) {
this.parentClass = parentClass;
}
public JPopupMenu getPopupMenu() {
return popupMenu;
}
} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -