001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package com.izforge.izpack.gui;
021:
022: import java.awt.Color;
023: import java.awt.event.MouseAdapter;
024: import java.awt.event.MouseEvent;
025:
026: import javax.swing.Action;
027: import javax.swing.Icon;
028: import javax.swing.JButton;
029:
030: /**
031: * A button that highlights when the button passes over.
032: *
033: * @author Julien Ponge
034: */
035: public class HighlightJButton extends JButton {
036:
037: private static final long serialVersionUID = 3833184718324969525L;
038:
039: /**
040: * The constructor (use ButtonFactory to create button).
041: *
042: * @param icon The icon to display.
043: * @param color The highlight color.
044: */
045: HighlightJButton(Icon icon, Color color) {
046: super (icon);
047: initButton(color);
048: }
049:
050: /**
051: * The constructor (use ButtonFactory to create button).
052: *
053: * @param text The text to display.
054: * @param color The highlight color.
055: */
056: HighlightJButton(String text, Color color) {
057: super (text);
058: initButton(color);
059: }
060:
061: /**
062: * The constructor (use ButtonFactory to create button).
063: *
064: * @param text The text to display.
065: * @param icon The icon to display.
066: * @param color The highlight color.
067: */
068: HighlightJButton(String text, Icon icon, Color color) {
069: super (text, icon);
070: initButton(color);
071: }
072:
073: /**
074: * The constructor (use ButtonFactory to create button).
075: *
076: * @param a The action.
077: * @param color The highlight color.
078: */
079: HighlightJButton(Action a, Color color) {
080: super (a);
081: initButton(color);
082: }
083:
084: /**
085: * Does the extra initialisations.
086: *
087: * @param highlightColor The highlight color.
088: */
089: protected void initButton(Color highlightColor) {
090: this .highlightColor = highlightColor;
091: defaultColor = getBackground();
092:
093: addMouseListener(new MouseHandler());
094: }
095:
096: /**
097: * Overriden to ensure that the button won't stay highlighted if it had the mouse over it.
098: *
099: * @param b Button state.
100: */
101: public void setEnabled(boolean b) {
102: reset();
103: super .setEnabled(b);
104: }
105:
106: /** Forces the button to unhighlight. */
107: protected void reset() {
108: setBackground(defaultColor);
109: }
110:
111: /** The highlighted color. */
112: protected Color highlightColor;
113:
114: /** The default color. */
115: protected Color defaultColor;
116:
117: /**
118: * The mouse handler which makes the highlighting.
119: *
120: * @author Julien Ponge
121: */
122: private class MouseHandler extends MouseAdapter {
123:
124: /**
125: * When the mouse passes over the button.
126: *
127: * @param e The event.
128: */
129: public void mouseEntered(MouseEvent e) {
130: if (isEnabled())
131: setBackground(highlightColor);
132: }
133:
134: /**
135: * When the mouse passes out of the button.
136: *
137: * @param e The event.
138: */
139: public void mouseExited(MouseEvent e) {
140: if (isEnabled())
141: setBackground(defaultColor);
142: }
143: }
144: }
|