001: /*
002: * RolloverButton.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.event.MouseListener;
027: import java.awt.event.MouseEvent;
028: import java.awt.Insets;
029:
030: import javax.swing.JButton;
031: import javax.swing.ImageIcon;
032: import javax.swing.Action;
033: import javax.swing.BorderFactory;
034: import javax.swing.JToolTip;
035: import javax.swing.UIManager;
036: import javax.swing.border.Border;
037: import org.underworldlabs.swing.plaf.UIUtils;
038: import org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI;
039:
040: /* ----------------------------------------------------------
041: * CVS NOTE: Changes to the CVS repository prior to the
042: * release of version 3.0.0beta1 has meant a
043: * resetting of CVS revision numbers.
044: * ----------------------------------------------------------
045: */
046:
047: /**
048: * This class creates a JButton where the borders are painted only
049: * when the mouse is positioned over it. <br>
050: *
051: * When the mouse pointer is moved away from the button, the borders are removed.
052: *
053: * @author Takis Diakoumis
054: * @version $Revision: 1.8 $
055: * @date $Date: 2006/07/15 12:52:21 $
056: */
057: public class RolloverButton extends JButton implements MouseListener {
058:
059: private String toolTip;
060: private String imagePath;
061: private String selectImagePath;
062: private ImageIcon image;
063: private ImageIcon selectImage;
064: private boolean selectionEnabled;
065:
066: private static Insets buttonInsets;
067:
068: /**
069: * Creates a new RolloverButton with an associated image
070: * and tool tip.
071: *
072: * @param img the ImageIcon to be displayed on the button
073: * @param tip the button's tool tip
074: */
075: public RolloverButton(ImageIcon img, String tip) {
076: super (img);
077: toolTip = tip;
078: init();
079: }
080:
081: /**
082: * Creates a new RolloverButton with an associated image
083: * and tool tip.
084: *
085: * @param a the Action to be associated with this button
086: * @param tip the button's tool tip
087: */
088: public RolloverButton(Action a, String tip) {
089: super (a);
090: toolTip = tip;
091: init();
092: }
093:
094: /**
095: * Creates a new RolloverButton with an associated image
096: * and tool tip.
097: *
098: * @param label the button's label
099: * @param tip the button's tool tip
100: */
101: public RolloverButton(String label, String tip, int h, int w) {
102: super (label);
103: toolTip = tip;
104: init();
105: setButtonSize(h, w);
106: }
107:
108: /**
109: * Creates a new RolloverButton with an associated image
110: * and tool tip.
111: *
112: * @param label the button's label
113: * @param tip the button's tool tip
114: */
115: public RolloverButton(ImageIcon img, String tip, int h, int w) {
116: super (img);
117: toolTip = tip;
118: init();
119: setButtonSize(h, w);
120: }
121:
122: /**
123: * Creates a new RolloverButton with an associated image
124: * and tool tip.
125: *
126: * @param imgPath the path relative to this class of
127: * the button icon image
128: * @param tip the button's tool tip
129: */
130: public RolloverButton(String imgPath, String tip) {
131: this (imgPath, tip, -1);
132: }
133:
134: /**
135: * Creates a new RolloverButton with an associated image
136: * and tool tip.
137: *
138: * @param imgPath the path relative to this class of
139: * the button icon image
140: * @param tip the button's tool tip
141: */
142: public RolloverButton(String imgPath, String tip, int size) {
143: super ();
144: setButtonIcon(imgPath);
145: selectImagePath = null;
146: toolTip = tip;
147: init();
148: }
149:
150: public RolloverButton() {
151: init();
152: }
153:
154: static {
155: buttonInsets = new Insets(1, 1, 1, 1);
156: }
157:
158: /**
159: * Initialises the state of the button.
160: */
161: private void init() {
162: selectionEnabled = true;
163: setMargin(buttonInsets);
164: setToolTipText(toolTip);
165: setBorderPainted(false);
166: setContentAreaFilled(false);
167: addMouseListener(this );
168: }
169:
170: /**
171: * Resets the buttons rollover state.
172: */
173: public void reset() {
174: mouseOver = false;
175: setBorderPainted(false);
176: setContentAreaFilled(false);
177: }
178:
179: private void setButtonSize(int height, int width) {
180: setPreferredSize(new Dimension(width, height));
181: setMaximumSize(new Dimension(width, height));
182: }
183:
184: /**
185: * Sets the image associated with the button.
186: *
187: * @param path the path relative to this class of
188: * the button icon image
189: */
190: public void setButtonIcon(String path) {
191: image = new ImageIcon(RolloverButton.class.getResource(path));
192: setIcon(image);
193: }
194:
195: public void enableSelectionRollover(boolean enable) {
196: selectionEnabled = enable;
197: }
198:
199: public boolean isSelectionRolloverEnabled() {
200: return selectionEnabled;
201: }
202:
203: /** indicates a current rollover */
204: private boolean mouseOver;
205:
206: /**
207: * Paints the button's borders as the mouse pointer enters.
208: *
209: * @param e the MouseEvent that created this event
210: */
211: public void mouseEntered(MouseEvent e) {
212: if (isEnabled() && isSelectionRolloverEnabled()) {
213: mouseOver = true;
214: setBorderPainted(true);
215: setContentAreaFilled(true);
216: }
217: }
218:
219: /** Override the <code>isFocusable()</code>
220: * method of <code>Component</code> (JDK1.4) to
221: * return false so the button never maintains
222: * the focus.
223: *
224: * @return false
225: */
226: public boolean isFocusable() {
227: return false;
228: }
229:
230: /**
231: * Sets the button's borders unpainted as the mouse
232: * pointer exits.
233: *
234: * @param e the MouseEvent that created this event
235: */
236: public void mouseExited(MouseEvent e) {
237: mouseOver = false;
238: setBorderPainted(false);
239: setContentAreaFilled(false);
240: }
241:
242: public void mouseReleased(MouseEvent e) {
243: }
244:
245: public void mousePressed(MouseEvent e) {
246: }
247:
248: public void mouseClicked(MouseEvent e) {
249: }
250:
251: public JToolTip createToolTip() {
252: JToolTip tip = new PaintedButtonToolTip();
253: tip.setComponent(this );
254: return tip;
255: }
256:
257: class PaintedButtonToolTip extends JToolTip {
258: public void updateUI() {
259: setUI(new AcceleratorToolTipUI());
260: }
261: } // class PaintedButtonToolTip
262:
263: }
|