/** * This is the hook through which all menu items are created. It registers the result with the * menuitem hashtable so that it can be fetched with getMenuItem(). * * @see #getMenuItem */ protected JMenuItem createMenuItem(String cmd) { JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix)); 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; } mi.setActionCommand(astr); Action myaction = getAction(astr); // if this is a known action if (myaction != null) { mi.addActionListener(myaction); myaction.addPropertyChangeListener(createActionChangeListener(mi)); // System.out.println("myaction not null: astr:"+astr+" enabled:"+myaction.isEnabled()); mi.setEnabled(myaction.isEnabled()); } else { System.err.println("Error:TextViewer: createMenuItem: myaction is null: astr:" + astr); // causes the item to be greyed out mi.setEnabled(false); } menuItems.put(cmd, mi); return mi; }
/** Configures a JCheckBoxMenuItem for an Action. */ public static void configureJCheckBoxMenuItem(final JCheckBoxMenuItem mi, final Action a) { mi.setSelected((Boolean) a.getValue(Actions.SELECTED_KEY)); PropertyChangeListener propertyHandler = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals(Actions.SELECTED_KEY)) { mi.setSelected((Boolean) a.getValue(Actions.SELECTED_KEY)); } } }; a.addPropertyChangeListener(propertyHandler); mi.putClientProperty("actionPropertyHandler", propertyHandler); }
public void addAction(final Action action) { action.addPropertyChangeListener(this); final LinkLabel label = new LinkLabel( null, null, new LinkListener() { public void linkSelected(final LinkLabel aSource, final Object aLinkData) { action.actionPerformed( new ActionEvent( Banner.this, ActionEvent.ACTION_PERFORMED, Action.ACTION_COMMAND_KEY)); } }); myActions.put(action, label); myActionsPanel.add(label); updateAction(action); }
/** This is the hook through which all menu items are created. */ protected JMenuItem createMenuItem(String cmd) { JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix)); URL url = getResource(cmd + imageSuffix); if (url != null) { mi.setHorizontalTextPosition(JButton.RIGHT); mi.setIcon(new ImageIcon(url)); } String astr = getProperty(cmd + actionSuffix); if (astr == null) { astr = cmd; } 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); } return mi; }