/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.gui.customcontrols;
import java.awt.*;
import javax.swing.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.swing.*;
/**
* The SIPCommToggleButton is a flexible JToggleButton that
* allows to configure its background, its icon, the look when a mouse is over
* it, etc.
*
* @author Yana Stamcheva
*/
public class SIPCommToggleButton
extends JToggleButton
{
private Image bgImage;
private Image bgRolloverImage;
private Image iconImage;
private Image pressedImage;
/**
* Creates a button with custom background image, rollover image and
* icon image.
*
* @param bgImage The background image.
* @param rolloverImage The rollover image.
* @param iconImage The icon.
*/
public SIPCommToggleButton(Image bgImage,
Image rolloverImage,
Image iconImage,
Image pressedImage)
{
this.bgImage = bgImage;
this.bgRolloverImage = rolloverImage;
this.iconImage = iconImage;
this.pressedImage = pressedImage;
this.setPreferredSize(new Dimension(this.bgImage.getWidth(null),
this.bgImage.getHeight(null)));
this.setIcon(new ImageIcon(this.iconImage));
}
/**
* Creates a button with custom background image and rollover image.
*
* @param bgImage The background button image.
* @param rolloverImage The rollover button image.
*/
public SIPCommToggleButton(Image bgImage, Image rolloverImage)
{
this.bgImage = bgImage;
this.bgRolloverImage = rolloverImage;
this.setPreferredSize(new Dimension(this.bgImage.getWidth(null),
this.bgImage.getHeight(null)));
}
/**
* Overrides the paintComponent
method of JButton
* to paint the button background and icon, and all additional effects
* of this configururable button.
*
* @param g The Graphics object.
*/
public void paintComponent(Graphics g)
{
g = g.create();
try
{
internalPaintComponent(g);
}
finally
{
g.dispose();
}
}
private void internalPaintComponent(Graphics g)
{
AntialiasingManager.activateAntialiasing(g);
if (this.bgImage != null)
{
// If there's no icon, we make grey the backgroundImage
// when disabled.
if (this.iconImage == null && !isEnabled())
{
Image disabledImage = new ImageIcon(LightGrayFilter
.createDisabledImage(bgImage)).getImage();
g.drawImage(disabledImage, 0, 0, this);
}
else {
g.drawImage(this.bgImage, 0, 0, this);
}
}
if (this.getModel().isRollover() && this.bgRolloverImage != null)
{
g.drawImage(this.bgRolloverImage, 0, 0, this);
}
if (this.getModel().isSelected() && this.pressedImage != null)
{
g.drawImage(this.pressedImage, 0, 0, this);
}
if (this.iconImage != null)
{
if (!isEnabled())
{
Image disabledImage = new ImageIcon(LightGrayFilter
.createDisabledImage(iconImage)).getImage();
g.drawImage(disabledImage,
(this.bgImage.getWidth(null) - disabledImage
.getWidth(null)) / 2, (this.bgImage
.getHeight(null) - disabledImage
.getHeight(null)) / 2, this);
}
else
{
g.drawImage(this.iconImage,
(this.bgImage.getWidth(null) - this.iconImage
.getWidth(null)) / 2, (this.bgImage
.getHeight(null) - this.iconImage
.getHeight(null)) / 2, this);
}
}
}
/**
* Returns the background image of this button.
* @return the background image of this button.
*/
public Image getBgImage()
{
return bgImage;
}
/**
* Sets the background image to this button.
* @param bgImage The background image to set.
*/
public void setBgImage(Image bgImage)
{
this.bgImage = bgImage;
}
/**
* Returns the background rollover image of this button.
* @return the background rollover image of this button.
*/
public Image getBgRolloverImage()
{
return bgRolloverImage;
}
/**
* Sets the background rollover image to this button.
* @param bgRolloverImage The background rollover image to set.
*/
public void setBgRolloverImage(Image bgRolloverImage)
{
this.bgRolloverImage = bgRolloverImage;
}
/**
* Returns the icon image of this button.
* @return the icon image of this button.
*/
public Image getIconImage()
{
return iconImage;
}
/**
* Sets the icon image to this button.
* @param iconImage The icon image to set.
*/
public void setIconImage(Image iconImage)
{
this.iconImage = iconImage;
this.repaint();
}
}