1 package org.telscenter.pas.common.ui.tab;
2
3 import javax.swing.*;
4 import javax.swing.plaf.ComponentUI;
5 import javax.swing.plaf.basic.BasicTabbedPaneUI;
6 import java.awt.*;
7
8 /***
9 * An implementation of the TabbedPaneUI that looks like the tabs that are used in Microsoft Powerpoint
10 * <p/>
11 * Copyright (C) 2005 by Jon Lipsky
12 * <p/>
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License. Y
15 * ou may obtain a copy of the License at
16 * <p/>
17 * http://www.apache.org/licenses/LICENSE-2.0
18 * <p/>
19 * Unless required by applicable law or agreed to in writing, software d
20 * istributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 */
25 public class PasTabbedPaneUI extends BasicTabbedPaneUI
26 {
27 private static final Insets TAB_INSETS = new Insets(1, 0, 0, 0);
28
29 /***
30 * The font to use for the selected tab
31 */
32 private Font boldFont;
33
34 /***
35 * The font metrics for the selected font
36 */
37 private FontMetrics boldFontMetrics;
38
39 /***
40 * The color to use to fill in the background
41 */
42 private Color selectedColor;
43
44
45 /***
46 * The color to use to fill in the background
47 */
48 private Color unselectedColor;
49
50
51
52
53
54 public static ComponentUI createUI(final JComponent c)
55 {
56 return new PasTabbedPaneUI();
57 }
58
59 protected void installDefaults()
60 {
61 super.installDefaults();
62 tabAreaInsets.left = (calculateTabHeight(0, 0, tabPane.getFont().getSize()) / 4) + 1;
63 selectedTabPadInsets = new Insets(0, 0, 0, 0);
64
65 selectedColor = tabPane.getBackground();
66 unselectedColor = tabPane.getBackground().darker();
67
68 boldFont = tabPane.getFont().deriveFont(Font.BOLD);
69 boldFontMetrics = tabPane.getFontMetrics(boldFont);
70 }
71
72
73
74
75
76 public int getTabRunCount(final JTabbedPane pane)
77 {
78 return 1;
79 }
80
81 protected Insets getContentBorderInsets(final int tabPlacement)
82 {
83 return PasTabbedPaneUI.TAB_INSETS;
84 }
85
86 protected int calculateTabHeight(final int tabPlacement, final int tabIndex, final int fontHeight)
87 {
88 int vHeight = fontHeight + 2;
89 if (vHeight % 2 == 0)
90 vHeight += 1;
91 return vHeight;
92 }
93
94 protected int calculateTabWidth(final int tabPlacement, final int tabIndex, final FontMetrics metrics)
95 {
96 return super.calculateTabWidth(tabPlacement, tabIndex, metrics) + metrics.getHeight();
97 }
98
99
100
101
102
103
104
105
106
107
108 protected void paintTabBackground(final Graphics g, final int tabPlacement, final int tabIndex, final int x, final int y, final int w, final int h, final boolean isSelected)
109 {
110 final Polygon shape = new Polygon();
111
112 shape.addPoint(x - (h / 4), y + h);
113 shape.addPoint(x + (h / 4), y);
114 shape.addPoint(x + w - (h / 4), y);
115
116 if (isSelected || (tabIndex == (rects.length - 1)))
117 {
118 if (isSelected)
119 g.setColor(selectedColor);
120 else
121 g.setColor(unselectedColor);
122 shape.addPoint(x + w + (h / 4), y + h);
123 }
124 else
125 {
126 g.setColor(unselectedColor);
127 shape.addPoint(x + w, y + (h / 2));
128 shape.addPoint(x + w - (h / 4), y + h);
129 }
130
131 g.fillPolygon(shape);
132 }
133
134 protected void paintContentBorder(final Graphics g, final int tabPlacement, final int selectedIndex) {
135 final int width = tabPane.getWidth();
136 final int height = tabPane.getHeight();
137 final Insets insets = tabPane.getInsets();
138
139 int x = insets.left;
140 int y = insets.top;
141 int w = width - insets.right - insets.left;
142 int h = height - insets.top - insets.bottom;
143
144 switch(tabPlacement) {
145 case LEFT:
146 x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
147 w -= (x - insets.left);
148 break;
149 case RIGHT:
150 w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
151 break;
152 case BOTTOM:
153 h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
154 break;
155 case TOP:
156 default:
157 y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
158 h -= (y - insets.top);
159 }
160
161 if (selectedColor == null)
162 g.setColor(tabPane.getBackground());
163 else
164 g.setColor(tabPane.getBackground());
165 g.fillRect(x,y,w,h);
166
167 paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
168 paintContentBorderLeftEdge(g, tabPlacement, selectedIndex, x, y, w, h);
169 paintContentBorderBottomEdge(g, tabPlacement, selectedIndex, x, y, w, h);
170 paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h);
171
172 }
173
174
175 protected void paintTabBorder(final Graphics g, final int tabPlacement, final int tabIndex, final int x, final int y, final int w, final int h, final boolean isSelected)
176 {
177 g.setColor(Color.WHITE);
178 g.drawLine(x - (h / 4), y + h, x + (h / 4), y);
179 g.drawLine(x + (h / 4), y, x + w - (h / 4), y);
180 g.drawLine(x + w - (h / 4), y, x + w + (h / 4), y + h);
181 }
182
183 protected void paintContentBorderTopEdge(final Graphics g, final int tabPlacement, final int selectedIndex, final int x, final int y, final int w, final int h)
184 {
185 final Rectangle selectedRect = selectedIndex < 0 ? null : getTabBounds(selectedIndex, calcRect);
186 if (selectedRect == null) return;
187 g.setColor(Color.WHITE);
188 g.drawLine(x, y, selectedRect.x - (selectedRect.height / 4), y);
189 g.drawLine(selectedRect.x + selectedRect.width + (selectedRect.height / 4), y, x + w, y);
190 g.setColor(selectedColor);
191 g.drawLine(selectedRect.x - (selectedRect.height / 4)+1, y,selectedRect.x + selectedRect.width + (selectedRect.height / 4)-1, y);
192
193 }
194
195 protected void paintContentBorderRightEdge(final Graphics g, final int tabPlacement, final int selectedIndex, final int x, final int y, final int w, final int h)
196 {
197
198 }
199
200 protected void paintContentBorderLeftEdge(final Graphics g, final int tabPlacement, final int selectedIndex, final int x, final int y, final int w, final int h)
201 {
202 final Rectangle selRect = selectedIndex < 0? null :
203 getTabBounds(selectedIndex, calcRect);
204
205 g.setColor(Color.WHITE);
206 if (tabPlacement != SwingConstants.LEFT || selectedIndex < 0 ||
207 (selRect.x + selRect.width + 1 < x) ||
208 (selRect.y < y || selRect.y > y + h))
209 g.drawLine(x, y, x, y + h);
210 }
211
212 protected void paintContentBorderBottomEdge(final Graphics g, final int tabPlacement, final int selectedIndex, final int x, final int y, final int w, final int h)
213 {
214
215 }
216
217 protected void paintFocusIndicator(final Graphics g, final int tabPlacement, final Rectangle[] rects, final int tabIndex, final Rectangle iconRect, final Rectangle textRect, final boolean isSelected)
218 {
219
220 }
221
222 protected void paintText(final Graphics g, final int tabPlacement, final Font font, final FontMetrics metrics, final int tabIndex, final String title, final Rectangle textRect, final boolean isSelected)
223 {
224 if (isSelected)
225 {
226 final int vDifference = (int) (boldFontMetrics.getStringBounds(title, g).getWidth()) - textRect.width;
227 textRect.x -= (vDifference / 2);
228 super.paintText(g, tabPlacement, boldFont, boldFontMetrics, tabIndex, title, textRect, isSelected);
229 } else
230 super.paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected);
231 }
232
233 protected int getTabLabelShiftY(final int tabPlacement, final int tabIndex, final boolean isSelected)
234 {
235 return 0;
236 }
237 }