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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ContextMenuHandler.java,v 1.4 2005/10/10 18:02:17 rbair 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.awt.event.MouseAdapter;
  24. import java.awt.event.MouseEvent;
  25. import javax.swing.AbstractAction;
  26. import javax.swing.Action;
  27. import javax.swing.ActionMap;
  28. import javax.swing.JComponent;
  29. import javax.swing.JPopupMenu;
  30. import javax.swing.SwingUtilities;
  31. import javax.swing.text.JTextComponent;
  32. /**
  33.  * Responsible for showing the default PopupMenu.
  34.  * 
  35.  * @author Jeanette Winzenburg
  36.  */
  37. public class ContextMenuHandler extends MouseAdapter {
  38.     private ActionMap actionMap;
  39.     private ContextMenuSource contextMenuSource;
  40.     private JPopupMenu popup;
  41.     /**
  42.      * creates a context handler for TextContextMenuSource.
  43.      *
  44.      */
  45.     public ContextMenuHandler() {
  46.         this(null);
  47.     }
  48.     
  49.     /**
  50.      * creates a context handler for the given ContextMenuSource.
  51.      * Defaults to TextContextMenuSource if source == null.
  52.      * 
  53.      * @param source
  54.      */
  55.     public ContextMenuHandler(ContextMenuSource source) {
  56.         contextMenuSource = source;
  57.     }
  58.     // --------------------- MouseListener
  59.     
  60.     public void mousePressed(MouseEvent e) {
  61.         maybeShowContext(e);
  62.     }
  63.     public void mouseReleased(MouseEvent e) {
  64.         maybeShowContext(e);
  65.     }
  66.     private void maybeShowContext(final MouseEvent e) {
  67.         if (!e.isPopupTrigger() || !e.getComponent().isEnabled())
  68.             return;
  69.         if (e.getComponent().hasFocus()) {
  70.             showContextPopup(e);
  71.         } else {
  72.             ((JComponent) e.getComponent()).grabFocus();
  73.             SwingUtilities.invokeLater(new Runnable() {
  74.                 public void run() {
  75.                     showContextPopup(e);
  76.                 }
  77.             });
  78.         }
  79.     }
  80.     private void showContextPopup(MouseEvent e) {
  81.         showContextPopup((JComponent) e.getComponent(), e.getX(), e
  82.                 .getY());
  83.     }
  84.     private void showContextPopup(JComponent component, int x, int y) {
  85.         JPopupMenu popup = getPopupMenu(component, true);
  86.         popup.show(component, x, y);
  87.     }
  88.     /**
  89.      * @param component
  90.      * @return
  91.      */
  92.     private JPopupMenu getPopupMenu(JComponent component, boolean synchEnabled) {
  93.         if (popup == null) {
  94.             popup = new JPopupMenu();
  95.             ActionMap map = getActionMap(component);
  96.             String[] keys = getContextMenuSource().getKeys();
  97.             for (int i = 0; i < keys.length; i++) {
  98.                 if (keys[i] != null) {
  99.                     popup.add(map.get(keys[i]));
  100.                 } else {
  101.                     popup.addSeparator();
  102.                 }
  103.             }
  104.         }  
  105.         if (synchEnabled) {
  106.             getContextMenuSource().updateActionEnabled(component, actionMap);
  107.         }
  108.         return popup;
  109.     }
  110.     private ActionMap getActionMap(JComponent component) {
  111.         if (actionMap == null) {
  112.             actionMap = getContextMenuSource().createActionMap(component);
  113.         } else {
  114.             // todo: replace actions with components?
  115.         }
  116.         return actionMap;
  117.     }
  118.     private ContextMenuSource getContextMenuSource() {
  119.         if (contextMenuSource == null) {
  120.             contextMenuSource = new TextContextMenuSource();
  121.         }
  122.         return contextMenuSource;
  123.     }
  124. }