001/* 002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 003 * 004 * http://www.izforge.com/izpack/ 005 * http://developer.berlios.de/projects/izpack/ 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 020package com.izforge.izpack.gui; 021 022import java.awt.Color; 023import java.awt.event.MouseAdapter; 024import java.awt.event.MouseEvent; 025 026import javax.swing.Action; 027import javax.swing.Icon; 028import javax.swing.JButton; 029 030/** 031 * A button that highlights when the button passes over. 032 * 033 * @author Julien Ponge 034 */ 035public class HighlightJButton extends JButton 036{ 037 038 private static final long serialVersionUID = 3833184718324969525L; 039 040 /** 041 * The constructor (use ButtonFactory to create button). 042 * 043 * @param icon The icon to display. 044 * @param color The highlight color. 045 */ 046 HighlightJButton(Icon icon, Color color) 047 { 048 super(icon); 049 initButton(color); 050 } 051 052 /** 053 * The constructor (use ButtonFactory to create button). 054 * 055 * @param text The text to display. 056 * @param color The highlight color. 057 */ 058 HighlightJButton(String text, Color color) 059 { 060 super(text); 061 initButton(color); 062 } 063 064 /** 065 * The constructor (use ButtonFactory to create button). 066 * 067 * @param text The text to display. 068 * @param icon The icon to display. 069 * @param color The highlight color. 070 */ 071 HighlightJButton(String text, Icon icon, Color color) 072 { 073 super(text, icon); 074 initButton(color); 075 } 076 077 /** 078 * The constructor (use ButtonFactory to create button). 079 * 080 * @param a The action. 081 * @param color The highlight color. 082 */ 083 HighlightJButton(Action a, Color color) 084 { 085 super(a); 086 initButton(color); 087 } 088 089 /** 090 * Does the extra initialisations. 091 * 092 * @param highlightColor The highlight color. 093 */ 094 protected void initButton(Color highlightColor) 095 { 096 this.highlightColor = highlightColor; 097 defaultColor = getBackground(); 098 099 addMouseListener(new MouseHandler()); 100 } 101 102 /** 103 * Overriden to ensure that the button won't stay highlighted if it had the mouse over it. 104 * 105 * @param b Button state. 106 */ 107 public void setEnabled(boolean b) 108 { 109 reset(); 110 super.setEnabled(b); 111 } 112 113 /** Forces the button to unhighlight. */ 114 protected void reset() 115 { 116 setBackground(defaultColor); 117 } 118 119 /** The highlighted color. */ 120 protected Color highlightColor; 121 122 /** The default color. */ 123 protected Color defaultColor; 124 125 /** 126 * The mouse handler which makes the highlighting. 127 * 128 * @author Julien Ponge 129 */ 130 private class MouseHandler extends MouseAdapter 131 { 132 133 /** 134 * When the mouse passes over the button. 135 * 136 * @param e The event. 137 */ 138 public void mouseEntered(MouseEvent e) 139 { 140 if (isEnabled()) setBackground(highlightColor); 141 } 142 143 /** 144 * When the mouse passes out of the button. 145 * 146 * @param e The event. 147 */ 148 public void mouseExited(MouseEvent e) 149 { 150 if (isEnabled()) setBackground(defaultColor); 151 } 152 } 153}