001: /*
002: * Copyright (c) 2005-2007 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.flamingo.common.ui;
031:
032: import java.awt.MouseInfo;
033: import java.awt.PointerInfo;
034: import java.awt.event.FocusEvent;
035: import java.awt.event.MouseEvent;
036: import java.security.AccessControlException;
037:
038: import javax.swing.AbstractButton;
039: import javax.swing.ButtonModel;
040:
041: import org.jvnet.flamingo.common.ui.BasicPopupButtonListener;
042:
043: /**
044: * Button listener for rollover effects. Tracks the mouse motion and focus
045: * interaction for the associated button. This class is <b>for internal use only</b>.
046: *
047: * @author Kirill Grouchnikov
048: */
049: public class RolloverPopupButtonListener extends
050: BasicPopupButtonListener {
051: /**
052: * If the mouse pointer is currently inside the associated button area,
053: * <code>this</code> flag is <code>true</code>.
054: */
055: private boolean isMouseInside;
056:
057: /**
058: * The associated button.
059: */
060: private AbstractButton button;
061:
062: /**
063: * Simple constructor.
064: *
065: * @param b
066: * The associated button.
067: */
068: public RolloverPopupButtonListener(AbstractButton b) {
069: super (b);
070: this .button = b;
071: this .isMouseInside = false;
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
078: */
079: @Override
080: public void mouseEntered(MouseEvent e) {
081: super .mouseEntered(e);
082: // AbstractButton button = (AbstractButton) e.getSource();
083: this .isMouseInside = true;
084: this .button.getModel().setRollover(true);
085: // FadeTracker.getInstance().trackFadeIn(FadeKind.ROLLOVER, button,
086: // false, null);
087: }
088:
089: /*
090: * (non-Javadoc)
091: *
092: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
093: */
094: @Override
095: public void mouseExited(MouseEvent e) {
096: super .mouseExited(e);
097: // AbstractButton button = (AbstractButton) e.getSource();
098: this .isMouseInside = false;
099: this .button.getModel().setRollover(false);
100: // FadeTracker.getInstance().trackFadeOut(FadeKind.ROLLOVER, button,
101: // false, null);
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
108: */
109: @Override
110: public void mouseReleased(MouseEvent e) {
111: super .mouseReleased(e);
112: this .button.getModel().setRollover(this .isMouseInside);
113: // button.getModel().setRollover(this.isMouseInside);
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see javax.swing.plaf.basic.BasicButtonListener#focusGained(java.awt.event.FocusEvent)
120: */
121: @Override
122: public void focusGained(FocusEvent e) {
123: super .focusGained(e);
124: if (!this .button.isShowing()) {
125: // shouldn't happen. Is some lurking Swing bug
126: return;
127: }
128: try {
129: PointerInfo pi = MouseInfo.getPointerInfo();
130: int px = pi.getLocation().x
131: - this .button.getLocationOnScreen().x;
132: int py = pi.getLocation().y
133: - this .button.getLocationOnScreen().y;
134: this .button.getModel().setRollover(
135: this .button.contains(px, py));
136: } catch (AccessControlException ace) {
137: // sandbox - give up
138: }
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see javax.swing.plaf.basic.BasicButtonListener#focusLost(java.awt.event.FocusEvent)
145: */
146: @Override
147: public void focusLost(FocusEvent e) {
148: super .focusLost(e);
149: this .button.getModel().setRollover(false);
150: // FadeTracker.getInstance().trackFadeOut(button);
151: }
152:
153: /**
154: * Clears model of the associated button.
155: */
156: public void clearState() {
157: this .isMouseInside = false;
158: ButtonModel model = this .button.getModel();
159: model.setArmed(false);
160: model.setPressed(false);
161: model.setRollover(false);
162: model.setSelected(false);
163: }
164: }
|