KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > MouseMessagingHandler


1 /*
2  * $Id: MouseMessagingHandler.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Component JavaDoc;
11
12 import java.awt.event.MouseEvent JavaDoc;
13 import java.awt.event.MouseAdapter JavaDoc;
14
15 import java.util.logging.Level JavaDoc;
16
17 import javax.swing.AbstractButton JavaDoc;
18 import javax.swing.Action JavaDoc;
19 import javax.swing.MenuElement JavaDoc;
20
21 import org.jdesktop.swing.event.MessageEvent;
22 import org.jdesktop.swing.event.MessageListener;
23 import org.jdesktop.swing.event.MessageSource;
24
25 /**
26  * Mouse handler which listens to mouse entered events on action based components
27  * and send the value of the LONG_DESCRIPTION as a transient message
28  * to the MessageListener and the listeners registered to the MessageSource.
29  * <p>
30  * Components can be registered using the register methods. For example, to
31  * register all the components of a toolbar and all menu items in a menu bar
32  * to send mouse over messages to a status bar:
33  * <pre>
34  * handler = new MouseMessagingHandler(this, statusBar);
35  * if (toolBar != null) {
36  * handler.registerListeners(toolBar.getComponents());
37  * }
38  * if (menuBar != null) {
39  * handler.registerListeners(menuBar.getSubElements());
40  * }
41  * </pre>
42  *
43  * @author Mark Davidson
44  */

45 public class MouseMessagingHandler extends MouseAdapter JavaDoc {
46
47     private Object JavaDoc source;
48     private MessageSource messageSource;
49     private MessageListener messageListener;
50
51     /**
52      * @param source the source of the MesageEvents
53      * @param messageSource messages will be sent to these listeners
54      */

55     public MouseMessagingHandler(Object JavaDoc source, MessageSource messageSource) {
56     setSource(source);
57     setMessageSource(messageSource);
58     }
59     
60     /**
61      * @param source the source of the MesageEvents
62      * @param messageListener messages will be sent to this listener
63      */

64     public MouseMessagingHandler(Object JavaDoc source, MessageListener messageListener) {
65     setSource(source);
66     setMessageListener(messageListener);
67     }
68
69     /**
70      * Set the source object of the MessageEvents.
71      */

72     public void setSource(Object JavaDoc source) {
73     this.source = source;
74     }
75
76     public void setMessageSource(MessageSource source) {
77     this.messageSource = source;
78     }
79
80     public void setMessageListener(MessageListener listener) {
81     this.messageListener = listener;
82     }
83
84     public void mouseExited(MouseEvent JavaDoc evt) {
85     fireMessage("");
86     }
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 JavaDoc evt) {
93     if (evt.getSource()instanceof AbstractButton JavaDoc) {
94         AbstractButton JavaDoc button = (AbstractButton JavaDoc) evt.getSource();
95         Action JavaDoc action = button.getAction();
96         if (action != null) {
97         fireMessage((String JavaDoc)action.getValue(Action.LONG_DESCRIPTION));
98         }
99     }
100     }
101
102     /**
103      * Helper method to recursively register all MenuElements with
104      * this messaging handler.
105      */

106     public void registerListeners(MenuElement JavaDoc[] elements) {
107     for (int i = 0; i < elements.length; i++) {
108         if (elements[i] instanceof AbstractButton JavaDoc) {
109         ((AbstractButton JavaDoc)elements[i]).addMouseListener(this);
110         }
111         registerListeners(elements[i].getSubElements());
112     }
113     }
114
115     public void unregisterListeners(MenuElement JavaDoc[] elements) {
116     for (int i = 0; i < elements.length; i++) {
117         if (elements[i] instanceof AbstractButton JavaDoc) {
118         ((AbstractButton JavaDoc)elements[i]).removeMouseListener(this);
119         }
120         unregisterListeners(elements[i].getSubElements());
121     }
122     }
123
124     /**
125      * Helper method to register all components with this message handler
126      */

127     public void registerListeners(Component JavaDoc[] components) {
128     for (int i = 0; i < components.length; i++) {
129         if (components[i] instanceof AbstractButton JavaDoc) {
130         components[i].addMouseListener(this);
131         }
132     }
133     }
134
135     public void unregisterListeners(Component JavaDoc[] components) {
136     for (int i = 0; i < components.length; i++) {
137         if (components[i] instanceof AbstractButton JavaDoc) {
138         components[i].removeMouseListener(this);
139         }
140     }
141     }
142
143     private void fireMessage(String JavaDoc message) {
144     MessageEvent evt = new MessageEvent(source, message, Level.FINE);
145         
146     if (messageListener != null) {
147         messageListener.message(evt);
148     }
149         
150     if (messageSource != null) {
151         MessageListener[] ls = messageSource.getMessageListeners();
152         for (int i = 0; i < ls.length; i++) {
153         ls[i].message(evt);
154         }
155     }
156     }
157 }
158
159
160
Popular Tags