1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package cppcheckplus.tab;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import cppcheckplus.control.MyContorlUtil;
public class MyTabComponent extends JPanel
{
private String backgroundUnselectedImageURL;
private TexturePaint selectedPaint;
private TexturePaint unselectedPaint;
private ImageIcon icon;
private ImageIcon pressedIcon;
private Image unselectedLeftImage;
private Image unselectedRightImage;
private Image selectedLeftImage;
private Image selectedRightImage;
private JButton btnClose;
private JLabel lbTitle;
private MyTabPanel tab;
private Color selectedTitleColor;
private Color unselectedTitleColor;
private Border border;
private String oId;
public MyTabComponent(String oId,MyTabPanel tab)
{
super();
this.oId = oId;
selectedPaint = MyContorlUtil.createTexturePaint("control/images/tab_header_background.png");
unselectedPaint = MyContorlUtil.createTexturePaint("control/images/tab_header_unselected_background.png");
icon = MyContorlUtil.getImageIcon("control/images/tab_close.png");
pressedIcon = MyContorlUtil.getImageIcon("control/images/tab_close_pressed.png");
unselectedLeftImage = MyContorlUtil.getImage("control/images/tab_header_unselected_background_left.png");
unselectedRightImage = MyContorlUtil.getImage("control/images/tab_header_unselected_background_right.png");
selectedLeftImage = MyContorlUtil.getImage("control/images/tab_header_selected_background_left.png");
selectedRightImage = MyContorlUtil.getImage("control/images/tab_header_selected_background_right.png");
btnClose = new JButton();
lbTitle = new JLabel();
this.tab = null;
selectedTitleColor = new Color(120, 120, 125);
unselectedTitleColor = Color.white;
border = BorderFactory.createEmptyBorder(0, 5, 0, 5);
this.tab = tab;
init();
}
private void init()
{
btnClose.setIcon(icon);
btnClose.setPressedIcon(pressedIcon);
btnClose.setToolTipText("Close this tab");
btnClose.setMargin(MyContorlUtil.ZERO_INSETS);
btnClose.setFocusPainted(false);
btnClose.setBorder(BorderFactory.createEmptyBorder(0, 3, 1, 3));
btnClose.setContentAreaFilled(false);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
closeTab();
}
});
lbTitle.setOpaque(false);
lbTitle.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
lbTitle.setVerticalAlignment(0);
lbTitle.setFont(MyContorlUtil.FONT_12_BOLD);
setLayout(new BorderLayout());
add(btnClose, "East");
add(lbTitle, "Center");
setBorder(border);
setOpaque(false);
}
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
if (isTabSelected())
{
g2d.drawImage(selectedLeftImage, 0, 0, null);
g2d.setPaint(selectedPaint);
int x = selectedLeftImage.getWidth(null);
int y = 0;
int width = getWidth() - x - selectedRightImage.getWidth(null);
int height = getHeight();
g2d.fillRect(x, y, width, height);
g2d.drawImage(selectedRightImage, x + width, 0, null);
} else
{
g2d.drawImage(unselectedLeftImage, 0, 0, null);
g2d.setPaint(unselectedPaint);
int x = unselectedLeftImage.getWidth(null);
int y = 0;
int width = getWidth() - x - selectedRightImage.getWidth(null);
int height = getHeight();
g2d.fillRect(x, y, width, height);
g2d.drawImage(unselectedRightImage, x + width, 0, null);
g2d.setColor(MyContorlUtil.TAB_BOTTOM_LINE_COLOR);
int lineY = getHeight() - 1;
g2d.drawLine(0, lineY, getWidth(), lineY);
}
}
public Dimension getPreferredSize()
{
int width = super.getPreferredSize().width;
if (!isTabSelected())
width = Math.min(width, tab.getPreferredUnselectedTabWidth());
int height = tab.getPreferredTabHeight();
return new Dimension(width, height);
}
public boolean isTabSelected()
{
int index = tab.indexOfTabComponent(this);
int selectedIndex = tab.getSelectedIndex();
return selectedIndex == index;
}
public void setTitle(String title)
{
lbTitle.setText(title);
}
public void updateSelection(boolean selected)
{
if (selected)
lbTitle.setForeground(selectedTitleColor);
else
lbTitle.setForeground(unselectedTitleColor);
btnClose.setVisible(selected);
}
private void closeTab()
{
int index = tab.indexOfTabComponent(this);
tab.removeTabAt(index);
}
/**
* @param oId the oId to set
*/
public void setOId(String oId)
{
this.oId = oId;
}
/**
* @return the oId
*/
public String getOId()
{
return oId;
}
}
|