/**
* Copyright (C) IntelliBitz Technologies., Muthu Ramadoss
* 168, Medavakkam Main Road, Madipakkam, Chennai 600091, Tamilnadu, India.
* http://www.intellibitz.com
* training@intellibitz.com
* +91 44 2247 5106
* http://groups.google.com/group/etoe
* http://sted.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* STED, Copyright (C) 2007 IntelliBitz Technologies
* STED comes with ABSOLUTELY NO WARRANTY;
* This is free software, and you are welcome
* to redistribute it under the GNU GPL conditions;
*
* Visit http://www.gnu.org/ for GPL License terms.
*/
/**
* $Id:ButtonTabComponent.java 55 2007-05-19 05:55:34Z sushmu $
* $HeadURL: svn+ssh://sushmu@svn.code.sf.net/p/sted/code/FontTransliterator/trunk/src/intellibitz/sted/widgets/ButtonTabComponent.java $
*/
package intellibitz.sted.widgets;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* Component to be used as tabComponent; Contains a JLabel to show the text and
* a JButton to close the tab it belongs to
*/
public class ButtonTabComponent
extends JPanel
implements ActionListener
{
private JLabel tabTitle;
protected ActionListener actionListener = null;
public ButtonTabComponent(String title, final JTabbedPane pane)
{
//unset default FlowLayout' gaps
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
if (pane == null)
{
throw new NullPointerException("TabbedPane is null");
}
setOpaque(false);
//make JLabel read titles from JTabbedPane
tabTitle = new JLabel(title);
add(tabTitle);
//add more space between the tabTitle and the button
tabTitle.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
//tab button
JButton tabButton = new TabButton();
//Close the proper tab by clicking the button
tabButton.addActionListener(this);
add(tabButton);
//add more space to the top of the component
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
public JLabel getTabTitle()
{
return tabTitle;
}
public void actionPerformed(ActionEvent e)
{
//TODO: HACKETY HACK! DOUBLE CHECK THIS IS SAFE..
e.setSource(this);
actionListener.actionPerformed(e);
}
public void addActionListener(ActionListener actionListener)
{
this.actionListener = actionListener;
}
private class TabButton
extends JButton
{
public TabButton()
{
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("close this tab");
//Make the button looks the same for all Laf's
setUI(new BasicButtonUI());
//Make it transparent
setContentAreaFilled(false);
//No need to be focusable
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
//Making nice rollover effect
//we use the same listener for all buttons
addMouseListener(buttonMouseListener);
setRolloverEnabled(true);
}
//we don't want to update UI for this button
public void updateUI()
{
}
//paint the cross
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
//shift the image for pressed buttons
if (getModel().isPressed())
{
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.BLACK);
if (getModel().isRollover())
{
g2.setColor(Color.MAGENTA);
}
int delta = 6;
g2.drawLine(delta, delta, getWidth() - delta - 1,
getHeight() - delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta,
getHeight() - delta - 1);
g2.dispose();
}
}
private final static MouseListener buttonMouseListener = new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
Component component = e.getComponent();
if (component instanceof AbstractButton)
{
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
public void mouseExited(MouseEvent e)
{
Component component = e.getComponent();
if (component instanceof AbstractButton)
{
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
};
}