/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cli_fmw.main;
import cli_fmw.utils.MessageBox;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
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;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicButtonUI;
/**
*
* @author axe
*/
public class TabHead extends JPanel {
private final PageGeneric page;
private final PageContainer pc;
private JLabel label;
public TabHead(PageGeneric page) {
//TODO кнопочку потеснить вправо
super(new BorderLayout());
this.page = page;
this.pc =page.getContainer();
setOpaque(false);
label = new JLabel(page.getPageTitle());
add(label,BorderLayout.CENTER);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
//кнопочку закрыть кидаю на флоу, который лежит на востоке бордера, чтобы кнопочка не растягивалась
JPanel flowpanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
JButton button = new TabButton();
flowpanel.add(button);
add(flowpanel,BorderLayout.EAST);
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
public void setTitle(String title) {
label.setText(title);
}
public String getTitle(){
return label.getText();
}
private class TabButton extends JButton implements ActionListener {
public TabButton() {
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("Закрыть");
setText("x");
//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(new LineBorder(Color.WHITE, 1, true));
// setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
//Making nice rollover effect
//we use the same listener for all buttons
addMouseListener(buttonMouseListener);
setRolloverEnabled(true);
//Close the proper tab by clicking the button
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
pc.removePage(page, false);
} catch (PageException ex) {
MessageBox.showException(ex);
}
}
//we don't want to update UI for this button
@Override
public void updateUI() {
}
// //paint the cross
// @Override
// 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() {
@Override
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
@Override
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
};
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width = TabbedPaneBlockable.getTabWidth();
//System.out.println(size);
return size;
}
}