📄 actioncontainerfactory.java
字号:
*/ private Action getAction(Object id) { Action action = getActionManager().getAction(id); if (action == null) { throw new RuntimeException("ERROR: No Action for " + id); } return action; } /** * Returns the button group corresponding to the groupid * * @param groupid the value of the groupid attribute for the action element * @param container a container which will further identify the ButtonGroup */ private ButtonGroup getGroup(String groupid, JComponent container) { if (groupMap == null) { groupMap = new HashMap(); } int intCode = groupid.hashCode(); if (container != null) { intCode ^= container.hashCode(); } Integer hashCode = new Integer(intCode); ButtonGroup group = (ButtonGroup)groupMap.get(hashCode); if (group == null) { group = new ButtonGroup(); groupMap.put(hashCode, group); } return group; } /** * Creates a menu item based on the attributes of the action element. * Will return a JMenuItem, JRadioButtonMenuItem or a JCheckBoxMenuItem * depending on the context of the Action. * * @return a JMenuItem or subclass depending on type. */ private JMenuItem createMenuItem(Object id, JComponent container) { return createMenuItem(getAction(id), container); } /** * Creates a menu item based on the attributes of the action element. * Will return a JMenuItem, JRadioButtonMenuItem or a JCheckBoxMenuItem * depending on the context of the Action. * * @param action a mangaged Action * @param container the parent container may be null for non-group actions. * @return a JMenuItem or subclass depending on type. */ public JMenuItem createMenuItem(Action action, JComponent container) { JMenuItem menuItem = null; if (action instanceof AbstractActionExt) { AbstractActionExt ta = (AbstractActionExt)action; if (ta.isStateAction()) { String groupid = (String)ta.getGroup(); if (groupid != null) { // If this action has a groupid attribute then it's a // GroupAction menuItem = createRadioButtonMenuItem(getGroup(groupid, container), (AbstractActionExt)action); } else { menuItem = createCheckBoxMenuItem((AbstractActionExt)action); } } } if (menuItem == null) { menuItem= new JMenuItem(action); configureMenuItem(menuItem, action); } return menuItem; } /** * Creates a menu item based on the attributes of the action. * Will return a JMenuItem, JRadioButtonMenuItem or a JCheckBoxMenuItem * depending on the context of the Action. * * @param action an action used to create the menu item * @return a JMenuItem or subclass depending on type. */ public JMenuItem createMenuItem(Action action) { return createMenuItem(action, null); } /** * Creates a button based on the attributes of the action element. * Will return a JButton or a JToggleButton. */ private AbstractButton createButton(Object id, JComponent container) { return createButton(getAction(id), container); } /** * Creates a button based on the attributes of the action. If the container * parameter is non-null then it will be used to uniquely identify * the returned component within a ButtonGroup. If the action doesn't * represent a grouped component then this value can be null. * * @param action an action used to create the button * @param container the parent container to uniquely identify * grouped components or null * @return will return a JButton or a JToggleButton. */ public AbstractButton createButton(Action action, JComponent container) { if (action == null) { return null; } AbstractButton button = null; if (action instanceof AbstractActionExt) { // Check to see if we should create a toggle button AbstractActionExt ta = (AbstractActionExt)action; if (ta.isStateAction()) { // If this action has a groupid attribute then it's a // GroupAction String groupid = (String)ta.getGroup(); if (groupid == null) { button = createToggleButton(ta); } else { button = createToggleButton(ta, getGroup(groupid, container)); } } } if (button == null) { // Create a regular button button = new JButton(action); configureButton(button, action); } return button; } /** * Creates a button based on the attributes of the action. * * @param action an action used to create the button * @return will return a JButton or a JToggleButton. */ public AbstractButton createButton(Action action) { return createButton(action, null); } /** * Adds and configures a toggle button. * @param a an abstraction of a toggle action. */ private JToggleButton createToggleButton(AbstractActionExt a) { return createToggleButton(a, null); } /** * Adds and configures a toggle button. * @param a an abstraction of a toggle action. * @param group the group to add the toggle button or null */ private JToggleButton createToggleButton(AbstractActionExt a, ButtonGroup group) { JToggleButton button = new JToggleButton(); configureButton(button, a, group); return button; } /** * * @param button * @param a * @param group * @return */ public void configureButton(JToggleButton button, AbstractActionExt a, ButtonGroup group) { button.setAction(a); button.addItemListener(a); button.setSelected(a.isSelected()); if (group != null) { group.add(button); } configureToggleButton(button, a); } /** * This method will be called after toggle buttons are created. * Override for custom configuration but the overriden method should be called * first. * * @param button the button to be configured * @param action the action used to construct the menu item. */ protected void configureToggleButton(JToggleButton button, Action action) { configureButton(button, action); // The PropertyChangeListener that gets added // to the Action doesn't know how to handle the "selected" property change // in the meantime, the corect thing to do is to add another PropertyChangeListener // to the AbstractActionExt until this is fixed. action.addPropertyChangeListener(new ToggleActionPropertyChangeListener(button)); } /** * This method will be called after buttons created from an action. * Override for custom configuration. * * @param button the button to be configured * @param action the action used to construct the menu item. */ protected void configureButton(AbstractButton button, Action action) { if (action.getValue(Action.SHORT_DESCRIPTION) == null) { button.setToolTipText((String)action.getValue(Action.NAME)); } // Use the large icon for toolbar buttons. if (action.getValue(AbstractActionExt.LARGE_ICON) != null) { button.setIcon((Icon)action.getValue(AbstractActionExt.LARGE_ICON)); } // Don't show the text under the toolbar buttons if they have an icon if (button.getIcon() != null) { button.setText(""); } } /** * This method will be called after toggle type menu items (like * JRadioButtonMenuItem and JCheckBoxMenuItem) are created. * Override for custom configuration but the overriden method should be called * first. * * @param menuItem the menu item to be configured * @param action the action used to construct the menu item. */ protected void configureToggleMenuItem(JMenuItem menuItem, Action action) { configureMenuItem(menuItem, action); // The PropertyChangeListener that gets added // to the Action doesn't know how to handle the "selected" property change // in the meantime, the corect thing to do is to add another PropertyChangeListener // to the AbstractActionExt until this is fixed. action.addPropertyChangeListener(new ToggleActionPropertyChangeListener(menuItem)); } /** * This method will be called after menu items are created. * Override for custom configuration. * * @param menuItem the menu item to be configured * @param action the action used to construct the menu item. */ protected void configureMenuItem(JMenuItem menuItem, Action action) { } /** * Helper method to add a checkbox menu item. */ private JCheckBoxMenuItem createCheckBoxMenuItem(AbstractActionExt a) { JCheckBoxMenuItem mi = new JCheckBoxMenuItem(a); mi.addItemListener(a); mi.setSelected(a.isSelected()); configureToggleMenuItem(mi, a); return mi; } /** * Helper method to add a radio button menu item. */ private JRadioButtonMenuItem createRadioButtonMenuItem(ButtonGroup group, AbstractActionExt a) { JRadioButtonMenuItem mi = new JRadioButtonMenuItem(a); mi.addItemListener(a); mi.setSelected(a.isSelected()); if (group != null) { group.add(mi); } configureToggleMenuItem(mi, a); return mi; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -