001: /*
002: * AbstractContextMenuFactory.java
003: *
004: * Created on January 31, 2004, 9:22 PM
005: */
006:
007: package org.netbeans.actions.engine.spi;
008:
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.beans.BeanInfo;
012: import java.util.Map;
013: import javax.swing.Action;
014: import javax.swing.JMenu;
015: import javax.swing.JMenuItem;
016: import javax.swing.JPopupMenu;
017: import org.netbeans.actions.spi.ActionProvider;
018: import org.netbeans.actions.spi.ContainerProvider;
019:
020: /**
021: *
022: * @author tim
023: */
024: public abstract class AbstractContextMenuFactory implements
025: ContextMenuFactory {
026: private static final String KEY_CREATOR = "creator";
027: private static final String KEY_ACTION = "action";
028: private static final String KEY_CONTAINERCONTEXT = "containercontext";
029: private AbstractEngine engine;
030:
031: /** Creates a new instance of AbstractContextMenuFactory */
032: protected AbstractContextMenuFactory(AbstractEngine engine) {
033: this .engine = engine;
034: }
035:
036: protected final AbstractEngine getEngine() {
037: return engine;
038: }
039:
040: public JPopupMenu createMenu(Map context) {
041: JPopupMenu result = new JPopupMenu();
042: populateMenu(result, context);
043: return result;
044: }
045:
046: private JMenuItem getOrCreateMenuItem(int type) {
047: JMenuItem result = type == ActionProvider.ACTION_TYPE_ITEM ? new JMenuItem()
048: : type == ActionProvider.ACTION_TYPE_SUBCONTEXT ? new JMenu()
049: : null;
050: if (type == ActionProvider.ACTION_TYPE_ITEM) {
051: result.addActionListener(getItemListener());
052: } else if (type == ActionProvider.ACTION_TYPE_SUBCONTEXT) {
053: //attachToMenu ((JMenu) result);
054: }
055: result.putClientProperty(KEY_CREATOR, this );
056: return result;
057: }
058:
059: protected void populateMenu(JPopupMenu menu, Map context) {
060: ActionProvider provider = getEngine().getActionProvider();
061: String[] names = provider
062: .getActionNames(ContainerProvider.CONTEXTMENU_CONTEXT);
063: for (int i = 0; i < names.length; i++) {
064: JMenuItem item = getOrCreateMenuItem(provider
065: .getActionType(names[i],
066: ContainerProvider.CONTEXTMENU_CONTEXT));
067: configureMenuItem(item,
068: ContainerProvider.CONTEXTMENU_CONTEXT, names[i],
069: provider, context);
070: menu.add(item);
071: }
072: // getEngine().notifyMenuShown(ContainerProvider.CONTEXTMENU_CONTEXT, menu); //XXX listener should do this
073: // addMapping (ContainerProvider.CONTEXTMENU_CONTEXT, menu, ContainerProvider.TYPE_MENU); //XXX handle popup
074: }
075:
076: private void configureMenuItem(JMenuItem item, String containerCtx,
077: String action, ActionProvider provider, Map context) {
078: // System.err.println("ConfigureMenuItem: " + containerCtx + "/" + action);
079: item.setName(action);
080: item.putClientProperty(KEY_ACTION, action);
081: item.putClientProperty(KEY_CONTAINERCONTEXT, containerCtx);
082: item.putClientProperty(KEY_CREATOR, this );
083: item.setText(provider.getDisplayName(action, containerCtx));
084: item.setToolTipText(provider.getDescription(action,
085: containerCtx));
086: int state = context == null ? ActionProvider.STATE_ENABLED
087: | ActionProvider.STATE_VISIBLE : provider.getState(
088: action, containerCtx, context);
089: boolean enabled = (state & ActionProvider.STATE_ENABLED) != 0;
090: item.setEnabled(enabled);
091: boolean visible = (state & ActionProvider.STATE_VISIBLE) != 0;
092: //Intentionally use enabled property
093: item.setVisible(enabled);
094: item.setMnemonic(provider.getMnemonic(action, containerCtx));
095: item.setDisplayedMnemonicIndex(provider.getMnemonicIndex(
096: action, containerCtx));
097: item.setIcon(provider.getIcon(action, containerCtx,
098: BeanInfo.ICON_COLOR_16x16));
099: }
100:
101: private MenuItemListener itemListener = null;
102:
103: private MenuItemListener getItemListener() {
104: if (itemListener == null) {
105: itemListener = new MenuItemListener();
106: }
107: return itemListener;
108: }
109:
110: private class MenuItemListener implements ActionListener {
111: public void actionPerformed(java.awt.event.ActionEvent e) {
112: JMenuItem item = (JMenuItem) e.getSource();
113: String actionCommand = (String) item
114: .getClientProperty(KEY_ACTION);
115: String context = (String) item
116: .getClientProperty(KEY_CONTAINERCONTEXT);
117:
118: getEngine().notifyWillPerform(actionCommand, context);
119:
120: Action action = getEngine().getAction(context,
121: actionCommand);
122:
123: if (action.isEnabled()) {
124: ActionEvent event = new ActionEvent(item,
125: ActionEvent.ACTION_PERFORMED, actionCommand);
126: action.actionPerformed(event);
127: }
128:
129: getEngine().notifyPerformed(actionCommand, context);
130: }
131: }
132:
133: }
|