1 7 8 package org.jdesktop.swing; 9 10 import java.awt.Component ; 11 12 import java.awt.event.MouseEvent ; 13 import java.awt.event.MouseAdapter ; 14 15 import java.util.logging.Level ; 16 17 import javax.swing.AbstractButton ; 18 import javax.swing.Action ; 19 import javax.swing.MenuElement ; 20 21 import org.jdesktop.swing.event.MessageEvent; 22 import org.jdesktop.swing.event.MessageListener; 23 import org.jdesktop.swing.event.MessageSource; 24 25 45 public class MouseMessagingHandler extends MouseAdapter { 46 47 private Object source; 48 private MessageSource messageSource; 49 private MessageListener messageListener; 50 51 55 public MouseMessagingHandler(Object source, MessageSource messageSource) { 56 setSource(source); 57 setMessageSource(messageSource); 58 } 59 60 64 public MouseMessagingHandler(Object source, MessageListener messageListener) { 65 setSource(source); 66 setMessageListener(messageListener); 67 } 68 69 72 public void setSource(Object 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 evt) { 85 fireMessage(""); 86 } 87 88 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 106 public void registerListeners(MenuElement [] elements) { 107 for (int i = 0; i < elements.length; i++) { 108 if (elements[i] instanceof AbstractButton ) { 109 ((AbstractButton )elements[i]).addMouseListener(this); 110 } 111 registerListeners(elements[i].getSubElements()); 112 } 113 } 114 115 public void unregisterListeners(MenuElement [] elements) { 116 for (int i = 0; i < elements.length; i++) { 117 if (elements[i] instanceof AbstractButton ) { 118 ((AbstractButton )elements[i]).removeMouseListener(this); 119 } 120 unregisterListeners(elements[i].getSubElements()); 121 } 122 } 123 124 127 public void registerListeners(Component [] components) { 128 for (int i = 0; i < components.length; i++) { 129 if (components[i] instanceof AbstractButton ) { 130 components[i].addMouseListener(this); 131 } 132 } 133 } 134 135 public void unregisterListeners(Component [] components) { 136 for (int i = 0; i < components.length; i++) { 137 if (components[i] instanceof AbstractButton ) { 138 components[i].removeMouseListener(this); 139 } 140 } 141 } 142 143 private void fireMessage(String 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 |