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