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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: MouseMessagingHandler.java,v 1.2 2005/10/10 18:01:58 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;
  22. import java.awt.Component;
  23. import java.awt.event.MouseEvent;
  24. import java.awt.event.MouseAdapter;
  25. import java.util.logging.Level;
  26. import javax.swing.AbstractButton;
  27. import javax.swing.Action;
  28. import javax.swing.MenuElement;
  29. import org.jdesktop.swingx.event.MessageEvent;
  30. import org.jdesktop.swingx.event.MessageListener;
  31. import org.jdesktop.swingx.event.MessageSource;
  32. /**
  33.  * Mouse handler which listens to mouse entered events on action based components 
  34.  * and send the value of the LONG_DESCRIPTION as a transient message
  35.  * to the MessageListener and the listeners registered to the MessageSource.
  36.  * <p>
  37.  * Components can be registered using the register methods. For example, to 
  38.  * register all the components of a toolbar and all menu items in a menu bar
  39.  * to send mouse over messages to a status bar:
  40.  * <pre>
  41.  *    handler = new MouseMessagingHandler(this, statusBar);
  42.  *    if (toolBar != null) {
  43.  *        handler.registerListeners(toolBar.getComponents());
  44.  *    }
  45.  *    if (menuBar != null) {
  46.  *   handler.registerListeners(menuBar.getSubElements());
  47.  *    }
  48.  * </pre>
  49.  * 
  50.  * @author Mark Davidson
  51.  */
  52. public class MouseMessagingHandler extends MouseAdapter {
  53.     private Object source;
  54.     private MessageSource messageSource;
  55.     private MessageListener messageListener;
  56.     /**
  57.      * @param source the source of the MesageEvents
  58.      * @param messageSource messages will be sent to these listeners
  59.      */
  60.     public MouseMessagingHandler(Object source, MessageSource messageSource) {
  61. setSource(source);
  62. setMessageSource(messageSource);
  63.     }
  64.     
  65.     /**
  66.      * @param source the source of the MesageEvents
  67.      * @param messageListener messages will be sent to this listener
  68.      */
  69.     public MouseMessagingHandler(Object source, MessageListener messageListener) {
  70. setSource(source);
  71. setMessageListener(messageListener);
  72.     }
  73.     /**
  74.      * Set the source object of the MessageEvents.
  75.      */
  76.     public void setSource(Object source) {
  77. this.source = source;
  78.     }
  79.     public void setMessageSource(MessageSource source) {
  80. this.messageSource = source;
  81.     }
  82.     public void setMessageListener(MessageListener listener) {
  83. this.messageListener = listener;
  84.     }
  85.     public void mouseExited(MouseEvent evt) {
  86. fireMessage("");
  87.     }
  88.     /**
  89.      * Takes the LONG_DESCRIPTION of the Action based components
  90.      * and sends them to the Status bar
  91.      */
  92.     public void mouseEntered(MouseEvent evt) {
  93. if (evt.getSource()instanceof AbstractButton) {
  94.     AbstractButton button = (AbstractButton) evt.getSource();
  95.     Action action = button.getAction();
  96.     if (action != null) {
  97. fireMessage((String)action.getValue(Action.LONG_DESCRIPTION));
  98.     }
  99. }
  100.     }
  101.     /**
  102.      * Helper method to recursively register all MenuElements with
  103.      * this messaging handler.
  104.      */
  105.     public void registerListeners(MenuElement[] elements) {
  106. for (int i = 0; i < elements.length; i++) {
  107.     if (elements[i] instanceof AbstractButton) {
  108. ((AbstractButton)elements[i]).addMouseListener(this);
  109.     }
  110.     registerListeners(elements[i].getSubElements());
  111. }
  112.     }
  113.     public void unregisterListeners(MenuElement[] elements) {
  114. for (int i = 0; i < elements.length; i++) {
  115.     if (elements[i] instanceof AbstractButton) {
  116. ((AbstractButton)elements[i]).removeMouseListener(this);
  117.     }
  118.     unregisterListeners(elements[i].getSubElements());
  119. }
  120.     }
  121.     /**
  122.      * Helper method to register all components with this message handler
  123.      */
  124.     public void registerListeners(Component[] components) {
  125. for (int i = 0; i < components.length; i++) {
  126.     if (components[i] instanceof AbstractButton) {
  127. components[i].addMouseListener(this);
  128.     }
  129. }
  130.     }
  131.     public void unregisterListeners(Component[] components) {
  132. for (int i = 0; i < components.length; i++) {
  133.     if (components[i] instanceof AbstractButton) {
  134. components[i].removeMouseListener(this);
  135.     }
  136. }
  137.     }
  138.     private void fireMessage(String message) {
  139. MessageEvent evt = new MessageEvent(source, message, Level.FINE);
  140.     
  141. if (messageListener != null) {
  142.     messageListener.message(evt);
  143. }
  144. if (messageSource != null) {
  145.     MessageListener[] ls = messageSource.getMessageListeners();
  146.     for (int i = 0; i < ls.length; i++) {
  147. ls[i].message(evt);
  148.     }
  149. }
  150.     }
  151. }