001 /*
002 * $Id: ContextMenuSource.java,v 1.5 2006/05/14 08:19:45 dmouse Exp $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020 */
021 package org.jdesktop.swingx.plaf;
022
023 import java.awt.event.ActionEvent;
024 import java.util.HashMap;
025 import java.util.Map;
026
027 import javax.swing.AbstractAction;
028 import javax.swing.Action;
029 import javax.swing.ActionMap;
030 import javax.swing.JComponent;
031 import javax.swing.UIManager;
032
033
034 /**
035 * @author Jeanette Winzenburg
036 */
037 public abstract class ContextMenuSource {
038
039 private Map<String, String> names;
040
041 public abstract String[] getKeys();
042
043 public String getName(String actionKey) {
044 return getNames().get(actionKey);
045 }
046
047 public abstract void updateActionEnabled(JComponent component, ActionMap map);
048
049 /**
050 * returns an ActionMap for usage in default context menus.
051 * @param component
052 * @return an <code>ActionMap</code> for usage in default context menus
053 */
054 public ActionMap createActionMap(JComponent component) {
055 ActionMap map = new ActionMap();
056 String[] keys = getKeys();
057 for (int i = 0; i < keys.length; i++) {
058 if (keys[i] != null) {
059 Action action = createDelegateAction(component, keys[i]);
060 if (action != null) {
061 map.put(keys[i], action);
062 }
063 }
064 }
065 return map;
066 }
067
068 protected Map<String, String> getNames() {
069 if (names == null) {
070 names = new HashMap<String, String>();
071 initNames(names);
072 }
073 return names;
074 }
075
076 protected String getValue(String key, String defaultValue) {
077 String value = UIManager.getString(getResourcePrefix() + key);
078 return value != null ? value : defaultValue;
079 }
080
081 protected abstract void initNames(Map<String, String> names);
082
083 protected abstract String getResourcePrefix();
084
085
086 protected Action createDelegateAction(JComponent component,
087 String actionKey) {
088 Action action = component.getActionMap().get(actionKey);
089 if (action != null) {
090 return new DelegateAction(getName(actionKey),
091 action, component);
092 }
093 return null;
094 }
095
096 public static class DelegateAction extends AbstractAction {
097
098 private Action delegatee;
099 private JComponent target;
100
101 public DelegateAction(String name, Action delegatee, JComponent target) {
102 super(name);
103 this.delegatee = delegatee;
104 this.target = target;
105 }
106
107 public void actionPerformed(ActionEvent e) {
108 delegatee.actionPerformed(createActionEvent(e));
109 }
110
111 private ActionEvent createActionEvent(ActionEvent e) {
112 if (target != null) {
113 return new ActionEvent(target, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers());
114 }
115 return e;
116 }
117 }
118
119 }