001: package com.jidesoft.swing;
002:
003: import javax.swing.*;
004: import javax.swing.event.MouseInputListener;
005: import java.awt.*;
006: import java.awt.event.MouseEvent;
007:
008: /**
009: * <tt>ClickThroughStyledLabel</tt> is a special ClickThroughStyledLabel that will retarget all mouse events to specified target component.
010: * <p/>
011: * For example, you need to paint some text on a JComponent. Usually you can call Java2D paint text method and paint
012: * the text. However the other way to do it is to add JLabel to JComponent and JLabel will not only paint
013: * the text but also an optional icon which is better. However if you had mouse listener added to JComponent, the mouse
014: * listener will not receive any mouse events when mouse clicks on the JLabel. By using this <tt>ClickThroughLabel</tt>,
015: * mouse event will be passed to underlying JComponent.
016: * <p/>
017: * Please note, we didn't pass all mouse events. In most cases, MOUSE_EXITED and MOUSE_ENTERED doesn't make sense to pass through.
018: * However there are cases, for example when the JLabel is at the border of JComponent, you may expect MOUSE_ENTERED event
019: * on JComponent but it will not happen. So please be aware of those cases so that you don't depend on it for important
020: * decision in your code.
021: */
022: public class ClickThroughStyledLabel extends StyledLabel implements
023: MouseInputListener {
024:
025: private Component _target;
026:
027: public ClickThroughStyledLabel() {
028: installListeners();
029: }
030:
031: public ClickThroughStyledLabel(Icon image) {
032: super (image);
033: installListeners();
034: }
035:
036: public ClickThroughStyledLabel(Icon image, int horizontalAlignment) {
037: super (image, horizontalAlignment);
038: installListeners();
039: }
040:
041: public ClickThroughStyledLabel(String text) {
042: super (text);
043: installListeners();
044: }
045:
046: public ClickThroughStyledLabel(String text, int horizontalAlignment) {
047: super (text, horizontalAlignment);
048: installListeners();
049: }
050:
051: public ClickThroughStyledLabel(String text, Icon icon,
052: int horizontalAlignment) {
053: super (text, icon, horizontalAlignment);
054: installListeners();
055: }
056:
057: public Component getTarget() {
058: return _target;
059: }
060:
061: public void setTarget(Component target) {
062: _target = target;
063: }
064:
065: protected void installListeners() {
066: addMouseListener(this );
067: addMouseMotionListener(this );
068: }
069:
070: protected void uninstallListeners() {
071: removeMouseListener(this );
072: removeMouseMotionListener(this );
073: }
074:
075: public void mouseClicked(MouseEvent e) {
076: JideSwingUtilities
077: .retargetMouseEvent(e.getID(), e, getTarget());
078: }
079:
080: public void mousePressed(MouseEvent e) {
081: JideSwingUtilities
082: .retargetMouseEvent(e.getID(), e, getTarget());
083: }
084:
085: public void mouseReleased(MouseEvent e) {
086: JideSwingUtilities
087: .retargetMouseEvent(e.getID(), e, getTarget());
088: }
089:
090: public void mouseEntered(MouseEvent e) {
091: JideSwingUtilities
092: .retargetMouseEvent(e.getID(), e, getTarget());
093: }
094:
095: public void mouseExited(MouseEvent e) {
096: JideSwingUtilities
097: .retargetMouseEvent(e.getID(), e, getTarget());
098: }
099:
100: public void mouseDragged(MouseEvent e) {
101: JideSwingUtilities
102: .retargetMouseEvent(e.getID(), e, getTarget());
103: }
104:
105: public void mouseMoved(MouseEvent e) {
106: JideSwingUtilities
107: .retargetMouseEvent(e.getID(), e, getTarget());
108: }
109: }
|