Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / gui / HighlightJButton.java @ 15280

History | View | Annotate | Download (3.88 KB)

1
/*
2
 *  $Id: HighlightJButton.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2001-2004 Julien Ponge
5
 *
6
 *  File :               HighlightJButton.java
7
 *  Description :        A button that highlights when the mouse passes over.
8
 *  Author's email :     julien@izforge.com
9
 *  Author's Website :   http://www.izforge.com
10
 *
11
 *  This program is free software; you can redistribute it and/or
12
 *  modify it under the terms of the GNU General Public License
13
 *  as published by the Free Software Foundation; either version 2
14
 *  of the License, or any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
 */
25
package com.izforge.izpack.gui;
26

    
27
import java.awt.Color;
28
import java.awt.event.MouseAdapter;
29
import java.awt.event.MouseEvent;
30

    
31
import javax.swing.Action;
32
import javax.swing.Icon;
33
import javax.swing.JButton;
34

    
35
/**
36
 *  A button that highlights when the button passes over.
37
 *
38
 * @author     Julien Ponge
39
 */
40
public class HighlightJButton extends JButton
41
{
42
  /**
43
   *  The constructor (use ButtonFactory to create button).
44
   *
45
   * @param  icon   The icon to display.
46
   * @param  color  The highlight color.
47
   */
48
  HighlightJButton(Icon icon, Color color)
49
  {
50
    super(icon);
51
    initButton(color);
52
  }
53

    
54
  /**
55
   *  The constructor (use ButtonFactory to create button).
56
   *
57
   * @param  text   The text to display.
58
   * @param  color  The highlight color.
59
   */
60
  HighlightJButton(String text, Color color)
61
  {
62
    super(text);
63
    initButton(color);
64
  }
65

    
66
  /**
67
   *  The constructor (use ButtonFactory to create button).
68
   *
69
   * @param  text   The text to display.
70
   * @param  icon   The icon to display.
71
   * @param  color  The highlight color.
72
   */
73
  HighlightJButton(String text, Icon icon, Color color)
74
  {
75
    super(text, icon);
76
    initButton(color);
77
  }
78

    
79
  /**
80
   *  The constructor (use ButtonFactory to create button).
81
   *
82
   * @param  a      The action.
83
   * @param  color  The highlight color.
84
   */
85
  HighlightJButton(Action a, Color color)
86
  {
87
    super(a);
88
    initButton(color);
89
  }
90

    
91
  /**
92
   *  Does the extra initialisations.
93
   *
94
   * @param  highlightColor  The highlight color.
95
   */
96
  protected void initButton(Color highlightColor)
97
  {
98
    this.highlightColor = highlightColor;
99
    defaultColor = getBackground();
100

    
101
    addMouseListener(new MouseHandler());
102
  }
103

    
104
  /**
105
   *  Overriden to ensure that the button won't stay highlighted if it had the
106
   *  mouse over it.
107
   *
108
   * @param  b  Button state.
109
   */
110
  public void setEnabled(boolean b)
111
  {
112
    reset();
113
    super.setEnabled(b);
114
  }
115

    
116
  /**  Forces the button to unhighlight.  */
117
  protected void reset()
118
  {
119
    setBackground(defaultColor);
120
  }
121

    
122
  /**  The highlighted color. */
123
  protected Color highlightColor;
124

    
125
  /**  The default color. */
126
  protected Color defaultColor;
127

    
128
  /**
129
   *  The mouse handler which makes the highlighting.
130
   *
131
   * @author     Julien Ponge
132
   */
133
  private class MouseHandler extends MouseAdapter
134
  {
135
    /**
136
     *  When the mouse passes over the button.
137
     *
138
     * @param  e  The event.
139
     */
140
    public void mouseEntered(MouseEvent e)
141
    {
142
      if (isEnabled())
143
        setBackground(highlightColor);
144
    }
145

    
146
    /**
147
     *  When the mouse passes out of the button.
148
     *
149
     * @param  e  The event.
150
     */
151
    public void mouseExited(MouseEvent e)
152
    {
153
      if (isEnabled())
154
        setBackground(defaultColor);
155
    }
156
  }
157
}