ContextMenuSource.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ContextMenuSource.java,v 1.4 2005/10/13 08:59:57 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.plaf;
  22. import java.awt.event.ActionEvent;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import javax.swing.AbstractAction;
  26. import javax.swing.Action;
  27. import javax.swing.ActionMap;
  28. import javax.swing.JComponent;
  29. import javax.swing.UIManager;
  30. /**
  31.  * @author Jeanette Winzenburg
  32.  */
  33. public abstract class ContextMenuSource {
  34.     private Map<String, String> names;
  35.     public abstract String[] getKeys();
  36.     public String getName(String actionKey) {
  37.         return getNames().get(actionKey);
  38.     }
  39.     public abstract void updateActionEnabled(JComponent component, ActionMap map);
  40.     /**
  41.      * returns an ActionMap for usage in default context menues.
  42.      * @param component
  43.      * @return
  44.      */
  45.     public ActionMap createActionMap(JComponent component) {
  46.         ActionMap map = new ActionMap();
  47.         String[] keys = getKeys();
  48.         for (int i = 0; i < keys.length; i++) {
  49.             if (keys[i] != null) {
  50.                 Action action = createDelegateAction(component, keys[i]);
  51.                 if (action != null) {
  52.                     map.put(keys[i], action);
  53.                 }
  54.             }
  55.         }
  56.         return map;
  57.     }
  58.     protected Map<String, String> getNames() {
  59.         if (names == null) {
  60.             names = new HashMap<String, String>();
  61.             initNames(names);
  62.         }
  63.         return names;
  64.     }
  65.     protected String getValue(String key, String defaultValue) {
  66.         String value = UIManager.getString(getResourcePrefix() + key);
  67.         return value != null ? value : defaultValue;
  68.     }
  69.     protected abstract void initNames(Map<String, String> names);
  70.     
  71.     protected abstract String getResourcePrefix();
  72.  
  73.     protected Action createDelegateAction(JComponent component,
  74.             String actionKey) {
  75.         Action action = component.getActionMap().get(actionKey);
  76.         if (action != null) {
  77.          return new DelegateAction(getName(actionKey),
  78.                 action, component);
  79.         }
  80.         return null;
  81.     }
  82.     public static class DelegateAction extends AbstractAction {
  83.         private Action delegatee;
  84.         private JComponent target;
  85.         public DelegateAction(String name, Action delegatee, JComponent target) {
  86.             super(name);
  87.             this.delegatee = delegatee;
  88.             this.target = target;
  89.         }
  90.         public void actionPerformed(ActionEvent e) {
  91.             delegatee.actionPerformed(createActionEvent(e));
  92.         }
  93.         private ActionEvent createActionEvent(ActionEvent e) {
  94.             if (target != null) {
  95.                 return new ActionEvent(target, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers());
  96.             }
  97.             return e;
  98.         }
  99.     }
  100. }