001 /*
002 * $Id: MouseMessagingHandler.java,v 1.3 2005/10/26 14:29:53 kleopatra 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
022 package org.jdesktop.swingx;
023
024 import java.awt.Component;
025 import java.awt.event.MouseAdapter;
026 import java.awt.event.MouseEvent;
027 import java.util.logging.Level;
028
029 import javax.swing.AbstractButton;
030 import javax.swing.Action;
031 import javax.swing.MenuElement;
032
033 import org.jdesktop.swingx.event.MessageEvent;
034 import org.jdesktop.swingx.event.MessageListener;
035 import org.jdesktop.swingx.event.MessageSource;
036
037 /**
038 * Mouse handler which listens to mouse entered events on action based components
039 * and send the value of the LONG_DESCRIPTION as a transient message
040 * to the MessageListener and the listeners registered to the MessageSource.
041 * <p>
042 * Components can be registered using the register methods. For example, to
043 * register all the components of a toolbar and all menu items in a menu bar
044 * to send mouse over messages to a status bar:
045 * <pre>
046 * handler = new MouseMessagingHandler(this, statusBar);
047 * if (toolBar != null) {
048 * handler.registerListeners(toolBar.getComponents());
049 * }
050 * if (menuBar != null) {
051 * handler.registerListeners(menuBar.getSubElements());
052 * }
053 * </pre>
054 *
055 * @author Mark Davidson
056 */
057 public class MouseMessagingHandler extends MouseAdapter {
058
059 private Object source;
060 private MessageSource messageSource;
061 private MessageListener messageListener;
062
063 /**
064 * @param source the source of the MesageEvents
065 * @param messageSource messages will be sent to these listeners
066 */
067 public MouseMessagingHandler(Object source, MessageSource messageSource) {
068 setSource(source);
069 setMessageSource(messageSource);
070 }
071
072 /**
073 * @param source the source of the MesageEvents
074 * @param messageListener messages will be sent to this listener
075 */
076 public MouseMessagingHandler(Object source, MessageListener messageListener) {
077 setSource(source);
078 setMessageListener(messageListener);
079 }
080
081 /**
082 * Set the source object of the MessageEvents.
083 */
084 public void setSource(Object source) {
085 this.source = source;
086 }
087
088 public void setMessageSource(MessageSource source) {
089 this.messageSource = source;
090 }
091
092 public void setMessageListener(MessageListener listener) {
093 this.messageListener = listener;
094 }
095
096 public void mouseExited(MouseEvent evt) {
097 fireMessage("");
098 }
099
100 /**
101 * Takes the LONG_DESCRIPTION of the Action based components
102 * and sends them to the Status bar
103 */
104 public void mouseEntered(MouseEvent evt) {
105 if (evt.getSource()instanceof AbstractButton) {
106 AbstractButton button = (AbstractButton) evt.getSource();
107 Action action = button.getAction();
108 if (action != null) {
109 fireMessage((String)action.getValue(Action.LONG_DESCRIPTION));
110 }
111 }
112 }
113
114 /**
115 * Helper method to recursively register all MenuElements with
116 * this messaging handler.
117 */
118 public void registerListeners(MenuElement[] elements) {
119 for (int i = 0; i < elements.length; i++) {
120 if (elements[i] instanceof AbstractButton) {
121 ((AbstractButton)elements[i]).addMouseListener(this);
122 }
123 registerListeners(elements[i].getSubElements());
124 }
125 }
126
127 public void unregisterListeners(MenuElement[] elements) {
128 for (int i = 0; i < elements.length; i++) {
129 if (elements[i] instanceof AbstractButton) {
130 ((AbstractButton)elements[i]).removeMouseListener(this);
131 }
132 unregisterListeners(elements[i].getSubElements());
133 }
134 }
135
136 /**
137 * Helper method to register all components with this message handler
138 */
139 public void registerListeners(Component[] components) {
140 for (int i = 0; i < components.length; i++) {
141 if (components[i] instanceof AbstractButton) {
142 components[i].addMouseListener(this);
143 }
144 }
145 }
146
147 public void unregisterListeners(Component[] components) {
148 for (int i = 0; i < components.length; i++) {
149 if (components[i] instanceof AbstractButton) {
150 components[i].removeMouseListener(this);
151 }
152 }
153 }
154
155 private void fireMessage(String message) {
156 MessageEvent evt = new MessageEvent(source, message, Level.FINE);
157
158 if (messageListener != null) {
159 messageListener.message(evt);
160 }
161
162 if (messageSource != null) {
163 MessageListener[] ls = messageSource.getMessageListeners();
164 for (int i = 0; i < ls.length; i++) {
165 ls[i].message(evt);
166 }
167 }
168 }
169 }
170
171