author | peterz |
Sat, 25 Apr 2009 21:17:50 +0400 | |
changeset 2658 | 43e06bc950ec |
parent 2490 | b460fb94812d |
child 4394 | 92a8ec883f5d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing.plaf.synth; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.plaf.*; |
|
30 |
import javax.swing.plaf.basic.*; |
|
31 |
import javax.swing.text.View; |
|
32 |
||
33 |
import java.awt.*; |
|
34 |
import java.awt.event.*; |
|
35 |
import java.beans.PropertyChangeListener; |
|
36 |
import java.beans.PropertyChangeEvent; |
|
37 |
import sun.swing.plaf.synth.SynthUI; |
|
38 |
import sun.swing.SwingUtilities2; |
|
39 |
||
40 |
/** |
|
41 |
* A Synth L&F implementation of TabbedPaneUI. |
|
42 |
* |
|
43 |
* @author Scott Violet |
|
44 |
*/ |
|
45 |
/** |
|
46 |
* Looks up 'selectedTabPadInsets' from the Style, which will be additional |
|
47 |
* insets for the selected tab. |
|
48 |
*/ |
|
49 |
class SynthTabbedPaneUI extends BasicTabbedPaneUI implements SynthUI, PropertyChangeListener { |
|
2658 | 50 |
/** |
51 |
* <p>If non-zero, tabOverlap indicates the amount that the tab bounds |
|
52 |
* should be altered such that they would overlap with a tab on either the |
|
53 |
* leading or trailing end of a run (ie: in TOP, this would be on the left |
|
54 |
* or right).</p> |
|
55 |
||
56 |
* <p>A positive overlap indicates that tabs should overlap right/down, |
|
57 |
* while a negative overlap indicates tha tabs should overlap left/up.</p> |
|
58 |
* |
|
59 |
* <p>When tabOverlap is specified, it both changes the x position and width |
|
60 |
* of the tab if in TOP or BOTTOM placement, and changes the y position and |
|
61 |
* height if in LEFT or RIGHT placement.</p> |
|
62 |
* |
|
63 |
* <p>This is done for the following reason. Consider a run of 10 tabs. |
|
64 |
* There are 9 gaps between these tabs. If you specified a tabOverlap of |
|
65 |
* "-1", then each of the tabs "x" values will be shifted left. This leaves |
|
66 |
* 9 pixels of space to the right of the right-most tab unpainted. So, each |
|
67 |
* tab's width is also extended by 1 pixel to make up the difference.</p> |
|
68 |
* |
|
69 |
* <p>This property respects the RTL component orientation.</p> |
|
70 |
*/ |
|
71 |
private int tabOverlap = 0; |
|
72 |
||
73 |
/** |
|
74 |
* When a tabbed pane has multiple rows of tabs, this indicates whether |
|
75 |
* the tabs in the upper row(s) should extend to the base of the tab area, |
|
76 |
* or whether they should remain at their normal tab height. This does not |
|
77 |
* affect the bounds of the tabs, only the bounds of area painted by the |
|
78 |
* tabs. The text position does not change. The result is that the bottom |
|
79 |
* border of the upper row of tabs becomes fully obscured by the lower tabs, |
|
80 |
* resulting in a cleaner look. |
|
81 |
*/ |
|
82 |
private boolean extendTabsToBase = false; |
|
83 |
||
2 | 84 |
private SynthContext tabAreaContext; |
85 |
private SynthContext tabContext; |
|
86 |
private SynthContext tabContentContext; |
|
87 |
||
88 |
private SynthStyle style; |
|
89 |
private SynthStyle tabStyle; |
|
90 |
private SynthStyle tabAreaStyle; |
|
91 |
private SynthStyle tabContentStyle; |
|
92 |
||
93 |
private Rectangle textRect; |
|
94 |
private Rectangle iconRect; |
|
95 |
||
96 |
private Rectangle tabAreaBounds = new Rectangle(); |
|
97 |
||
2658 | 98 |
//added for the Nimbus look and feel, where the tab area is painted differently depending on the |
99 |
//state for the selected tab |
|
100 |
private boolean tabAreaStatesMatchSelectedTab = false; |
|
101 |
//added for the Nimbus LAF to ensure that the labels don't move whether the tab is selected or not |
|
102 |
private boolean nudgeSelectedLabel = true; |
|
103 |
||
104 |
private boolean selectedTabIsPressed = false; |
|
105 |
||
2 | 106 |
public static ComponentUI createUI(JComponent c) { |
107 |
return new SynthTabbedPaneUI(); |
|
108 |
} |
|
109 |
||
110 |
SynthTabbedPaneUI() { |
|
111 |
textRect = new Rectangle(); |
|
112 |
iconRect = new Rectangle(); |
|
113 |
} |
|
114 |
||
115 |
private boolean scrollableTabLayoutEnabled() { |
|
116 |
return (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT); |
|
117 |
} |
|
118 |
||
119 |
protected void installDefaults() { |
|
120 |
updateStyle(tabPane); |
|
121 |
} |
|
122 |
||
123 |
private void updateStyle(JTabbedPane c) { |
|
124 |
SynthContext context = getContext(c, ENABLED); |
|
125 |
SynthStyle oldStyle = style; |
|
126 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
127 |
// Add properties other than JComponent colors, Borders and |
|
128 |
// opacity settings here: |
|
129 |
if (style != oldStyle) { |
|
130 |
tabRunOverlay = |
|
131 |
style.getInt(context, "TabbedPane.tabRunOverlay", 0); |
|
2658 | 132 |
tabOverlap = style.getInt(context, "TabbedPane.tabOverlap", 0); |
133 |
extendTabsToBase = style.getBoolean(context, |
|
134 |
"TabbedPane.extendTabsToBase", false); |
|
2 | 135 |
textIconGap = style.getInt(context, "TabbedPane.textIconGap", 0); |
136 |
selectedTabPadInsets = (Insets)style.get(context, |
|
137 |
"TabbedPane.selectedTabPadInsets"); |
|
138 |
if (selectedTabPadInsets == null) { |
|
139 |
selectedTabPadInsets = new Insets(0, 0, 0, 0); |
|
140 |
} |
|
2658 | 141 |
tabAreaStatesMatchSelectedTab = style.getBoolean(context, |
142 |
"TabbedPane.tabAreaStatesMatchSelectedTab", false); |
|
143 |
nudgeSelectedLabel = style.getBoolean(context, |
|
144 |
"TabbedPane.nudgeSelectedLabel", true); |
|
2 | 145 |
if (oldStyle != null) { |
146 |
uninstallKeyboardActions(); |
|
147 |
installKeyboardActions(); |
|
148 |
} |
|
149 |
} |
|
150 |
context.dispose(); |
|
151 |
||
152 |
if (tabContext != null) { |
|
153 |
tabContext.dispose(); |
|
154 |
} |
|
155 |
tabContext = getContext(c, Region.TABBED_PANE_TAB, ENABLED); |
|
156 |
this.tabStyle = SynthLookAndFeel.updateStyle(tabContext, this); |
|
157 |
tabInsets = tabStyle.getInsets(tabContext, null); |
|
158 |
||
159 |
||
160 |
if (tabAreaContext != null) { |
|
161 |
tabAreaContext.dispose(); |
|
162 |
} |
|
163 |
tabAreaContext = getContext(c, Region.TABBED_PANE_TAB_AREA, ENABLED); |
|
164 |
this.tabAreaStyle = SynthLookAndFeel.updateStyle(tabAreaContext, this); |
|
165 |
tabAreaInsets = tabAreaStyle.getInsets(tabAreaContext, null); |
|
166 |
||
167 |
||
168 |
if (tabContentContext != null) { |
|
169 |
tabContentContext.dispose(); |
|
170 |
} |
|
171 |
tabContentContext = getContext(c, Region.TABBED_PANE_CONTENT, ENABLED); |
|
172 |
this.tabContentStyle = SynthLookAndFeel.updateStyle(tabContentContext, |
|
173 |
this); |
|
174 |
contentBorderInsets = |
|
175 |
tabContentStyle.getInsets(tabContentContext, null); |
|
176 |
} |
|
177 |
||
178 |
protected void installListeners() { |
|
179 |
super.installListeners(); |
|
180 |
tabPane.addPropertyChangeListener(this); |
|
181 |
} |
|
182 |
||
183 |
protected void uninstallListeners() { |
|
184 |
super.uninstallListeners(); |
|
185 |
tabPane.removePropertyChangeListener(this); |
|
186 |
} |
|
187 |
||
188 |
protected void uninstallDefaults() { |
|
189 |
SynthContext context = getContext(tabPane, ENABLED); |
|
190 |
style.uninstallDefaults(context); |
|
191 |
context.dispose(); |
|
192 |
style = null; |
|
193 |
||
194 |
tabStyle.uninstallDefaults(tabContext); |
|
195 |
tabContext.dispose(); |
|
196 |
tabContext = null; |
|
197 |
tabStyle = null; |
|
198 |
||
199 |
tabAreaStyle.uninstallDefaults(tabAreaContext); |
|
200 |
tabAreaContext.dispose(); |
|
201 |
tabAreaContext = null; |
|
202 |
tabAreaStyle = null; |
|
203 |
||
204 |
tabContentStyle.uninstallDefaults(tabContentContext); |
|
205 |
tabContentContext.dispose(); |
|
206 |
tabContentContext = null; |
|
207 |
tabContentStyle = null; |
|
208 |
} |
|
209 |
||
210 |
public SynthContext getContext(JComponent c) { |
|
211 |
return getContext(c, getComponentState(c)); |
|
212 |
} |
|
213 |
||
214 |
public SynthContext getContext(JComponent c, int state) { |
|
215 |
return SynthContext.getContext(SynthContext.class, c, |
|
216 |
SynthLookAndFeel.getRegion(c),style, state); |
|
217 |
} |
|
218 |
||
219 |
public SynthContext getContext(JComponent c, Region subregion) { |
|
220 |
return getContext(c, subregion, getComponentState(c)); |
|
221 |
} |
|
222 |
||
223 |
private SynthContext getContext(JComponent c, Region subregion, int state){ |
|
224 |
SynthStyle style = null; |
|
225 |
Class klass = SynthContext.class; |
|
226 |
||
227 |
if (subregion == Region.TABBED_PANE_TAB) { |
|
228 |
style = tabStyle; |
|
229 |
} |
|
230 |
else if (subregion == Region.TABBED_PANE_TAB_AREA) { |
|
231 |
style = tabAreaStyle; |
|
232 |
} |
|
233 |
else if (subregion == Region.TABBED_PANE_CONTENT) { |
|
234 |
style = tabContentStyle; |
|
235 |
} |
|
236 |
return SynthContext.getContext(klass, c, subregion, style, state); |
|
237 |
} |
|
238 |
||
239 |
private Region getRegion(JComponent c) { |
|
240 |
return SynthLookAndFeel.getRegion(c); |
|
241 |
} |
|
242 |
||
243 |
private int getComponentState(JComponent c) { |
|
244 |
return SynthLookAndFeel.getComponentState(c); |
|
245 |
} |
|
246 |
||
247 |
protected JButton createScrollButton(int direction) { |
|
2658 | 248 |
// added for Nimbus LAF so that it can use the basic arrow buttons |
249 |
// UIManager is queried directly here because this is called before |
|
250 |
// updateStyle is called so the style can not be queried directly |
|
251 |
if (UIManager.getBoolean("TabbedPane.useBasicArrows")) { |
|
252 |
JButton btn = super.createScrollButton(direction); |
|
253 |
btn.setBorder(BorderFactory.createEmptyBorder()); |
|
254 |
return btn; |
|
255 |
} |
|
2 | 256 |
return new SynthScrollableTabButton(direction); |
257 |
} |
|
258 |
||
259 |
public void propertyChange(PropertyChangeEvent e) { |
|
260 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
261 |
updateStyle(tabPane); |
|
262 |
} |
|
263 |
} |
|
264 |
||
2658 | 265 |
/** |
266 |
* @inheritDoc |
|
267 |
* |
|
268 |
* Overridden to keep track of whether the selected tab is also pressed. |
|
269 |
*/ |
|
270 |
@Override |
|
271 |
protected MouseListener createMouseListener() { |
|
272 |
final MouseListener delegate = super.createMouseListener(); |
|
273 |
final MouseMotionListener delegate2 = (MouseMotionListener)delegate; |
|
274 |
return new MouseListener() { |
|
275 |
public void mouseClicked(MouseEvent e) { delegate.mouseClicked(e); } |
|
276 |
public void mouseEntered(MouseEvent e) { delegate.mouseEntered(e); } |
|
277 |
public void mouseExited(MouseEvent e) { delegate.mouseExited(e); } |
|
278 |
||
279 |
public void mousePressed(MouseEvent e) { |
|
280 |
if (!tabPane.isEnabled()) { |
|
281 |
return; |
|
282 |
} |
|
283 |
||
284 |
int tabIndex = tabForCoordinate(tabPane, e.getX(), e.getY()); |
|
285 |
if (tabIndex >= 0 && tabPane.isEnabledAt(tabIndex)) { |
|
286 |
if (tabIndex == tabPane.getSelectedIndex()) { |
|
287 |
// Clicking on selected tab |
|
288 |
selectedTabIsPressed = true; |
|
289 |
//TODO need to just repaint the tab area! |
|
290 |
tabPane.repaint(); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
//forward the event (this will set the selected index, or none at all |
|
295 |
delegate.mousePressed(e); |
|
296 |
} |
|
297 |
||
298 |
public void mouseReleased(MouseEvent e) { |
|
299 |
if (selectedTabIsPressed) { |
|
300 |
selectedTabIsPressed = false; |
|
301 |
//TODO need to just repaint the tab area! |
|
302 |
tabPane.repaint(); |
|
303 |
} |
|
304 |
//forward the event |
|
305 |
delegate.mouseReleased(e); |
|
306 |
||
307 |
//hack: The super method *should* be setting the mouse-over property correctly |
|
308 |
//here, but it doesn't. That is, when the mouse is released, whatever tab is below the |
|
309 |
//released mouse should be in rollover state. But, if you select a tab and don't |
|
310 |
//move the mouse, this doesn't happen. Hence, forwarding the event. |
|
311 |
delegate2.mouseMoved(e); |
|
312 |
} |
|
313 |
}; |
|
314 |
} |
|
315 |
||
316 |
@Override |
|
317 |
protected int getTabLabelShiftX(int tabPlacement, int tabIndex, boolean isSelected) { |
|
318 |
if (nudgeSelectedLabel) { |
|
319 |
return super.getTabLabelShiftX(tabPlacement, tabIndex, isSelected); |
|
320 |
} else { |
|
321 |
return 0; |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
@Override |
|
326 |
protected int getTabLabelShiftY(int tabPlacement, int tabIndex, boolean isSelected) { |
|
327 |
if (nudgeSelectedLabel) { |
|
328 |
return super.getTabLabelShiftY(tabPlacement, tabIndex, isSelected); |
|
329 |
} else { |
|
330 |
return 0; |
|
331 |
} |
|
332 |
} |
|
333 |
||
2 | 334 |
public void update(Graphics g, JComponent c) { |
335 |
SynthContext context = getContext(c); |
|
336 |
||
337 |
SynthLookAndFeel.update(context, g); |
|
338 |
context.getPainter().paintTabbedPaneBackground(context, |
|
339 |
g, 0, 0, c.getWidth(), c.getHeight()); |
|
340 |
paint(context, g); |
|
341 |
context.dispose(); |
|
342 |
} |
|
343 |
||
344 |
protected int getBaseline(int tab) { |
|
345 |
if (tabPane.getTabComponentAt(tab) != null || |
|
346 |
getTextViewForTab(tab) != null) { |
|
347 |
return super.getBaseline(tab); |
|
348 |
} |
|
349 |
String title = tabPane.getTitleAt(tab); |
|
350 |
Font font = tabContext.getStyle().getFont(tabContext); |
|
351 |
FontMetrics metrics = getFontMetrics(font); |
|
352 |
Icon icon = getIconForTab(tab); |
|
353 |
textRect.setBounds(0, 0, 0, 0); |
|
354 |
iconRect.setBounds(0, 0, 0, 0); |
|
355 |
calcRect.setBounds(0, 0, Short.MAX_VALUE, maxTabHeight); |
|
356 |
tabContext.getStyle().getGraphicsUtils(tabContext).layoutText( |
|
357 |
tabContext, metrics, title, icon, SwingUtilities.CENTER, |
|
358 |
SwingUtilities.CENTER, SwingUtilities.LEADING, |
|
2490
b460fb94812d
6815767: Bad parameter when calling another method in the class SynthTabbedPaneUI
peterz
parents:
2
diff
changeset
|
359 |
SwingUtilities.CENTER, calcRect, |
2 | 360 |
iconRect, textRect, textIconGap); |
361 |
return textRect.y + metrics.getAscent() + getBaselineOffset(); |
|
362 |
} |
|
363 |
||
364 |
public void paintBorder(SynthContext context, Graphics g, int x, |
|
365 |
int y, int w, int h) { |
|
366 |
context.getPainter().paintTabbedPaneBorder(context, g, x, y, w, h); |
|
367 |
} |
|
368 |
||
369 |
public void paint(Graphics g, JComponent c) { |
|
370 |
SynthContext context = getContext(c); |
|
371 |
||
372 |
paint(context, g); |
|
373 |
context.dispose(); |
|
374 |
} |
|
375 |
||
376 |
protected void paint(SynthContext context, Graphics g) { |
|
377 |
int selectedIndex = tabPane.getSelectedIndex(); |
|
378 |
int tabPlacement = tabPane.getTabPlacement(); |
|
379 |
||
380 |
ensureCurrentLayout(); |
|
381 |
||
382 |
// Paint tab area |
|
383 |
// If scrollable tabs are enabled, the tab area will be |
|
384 |
// painted by the scrollable tab panel instead. |
|
385 |
// |
|
386 |
if (!scrollableTabLayoutEnabled()) { // WRAP_TAB_LAYOUT |
|
387 |
Insets insets = tabPane.getInsets(); |
|
388 |
int x = insets.left; |
|
389 |
int y = insets.top; |
|
390 |
int width = tabPane.getWidth() - insets.left - insets.right; |
|
391 |
int height = tabPane.getHeight() - insets.top - insets.bottom; |
|
392 |
int size; |
|
393 |
switch(tabPlacement) { |
|
394 |
case LEFT: |
|
395 |
width = calculateTabAreaWidth(tabPlacement, runCount, |
|
396 |
maxTabWidth); |
|
397 |
break; |
|
398 |
case RIGHT: |
|
399 |
size = calculateTabAreaWidth(tabPlacement, runCount, |
|
400 |
maxTabWidth); |
|
401 |
x = x + width - size; |
|
402 |
width = size; |
|
403 |
break; |
|
404 |
case BOTTOM: |
|
405 |
size = calculateTabAreaHeight(tabPlacement, runCount, |
|
406 |
maxTabHeight); |
|
407 |
y = y + height - size; |
|
408 |
height = size; |
|
409 |
break; |
|
410 |
case TOP: |
|
411 |
default: |
|
412 |
height = calculateTabAreaHeight(tabPlacement, runCount, |
|
413 |
maxTabHeight); |
|
414 |
} |
|
415 |
||
416 |
tabAreaBounds.setBounds(x, y, width, height); |
|
417 |
||
418 |
if (g.getClipBounds().intersects(tabAreaBounds)) { |
|
419 |
paintTabArea(tabAreaContext, g, tabPlacement, |
|
420 |
selectedIndex, tabAreaBounds); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
// Paint content border |
|
425 |
paintContentBorder(tabContentContext, g, tabPlacement, selectedIndex); |
|
426 |
} |
|
427 |
||
428 |
||
429 |
protected void paintTabArea(Graphics g, int tabPlacement, |
|
430 |
int selectedIndex) { |
|
431 |
// This can be invoked from ScrollabeTabPanel |
|
432 |
Insets insets = tabPane.getInsets(); |
|
433 |
int x = insets.left; |
|
434 |
int y = insets.top; |
|
435 |
int width = tabPane.getWidth() - insets.left - insets.right; |
|
436 |
int height = tabPane.getHeight() - insets.top - insets.bottom; |
|
437 |
||
438 |
paintTabArea(tabAreaContext, g, tabPlacement, selectedIndex, |
|
439 |
new Rectangle(x, y, width, height)); |
|
440 |
} |
|
441 |
||
442 |
protected void paintTabArea(SynthContext ss, Graphics g, |
|
443 |
int tabPlacement, int selectedIndex, |
|
444 |
Rectangle tabAreaBounds) { |
|
445 |
Rectangle clipRect = g.getClipBounds(); |
|
446 |
||
2658 | 447 |
//if the tab area's states should match that of the selected tab, then |
448 |
//first update the selected tab's states, then set the state |
|
449 |
//for the tab area to match |
|
450 |
//otherwise, restore the tab area's state to ENABLED (which is the |
|
451 |
//only supported state otherwise). |
|
452 |
if (tabAreaStatesMatchSelectedTab && selectedIndex >= 0) { |
|
453 |
updateTabContext(selectedIndex, true, selectedTabIsPressed, |
|
454 |
(getRolloverTab() == selectedIndex), |
|
455 |
(getFocusIndex() == selectedIndex)); |
|
456 |
ss.setComponentState(tabContext.getComponentState()); |
|
457 |
} else { |
|
458 |
ss.setComponentState(SynthConstants.ENABLED); |
|
459 |
} |
|
460 |
||
2 | 461 |
// Paint the tab area. |
462 |
SynthLookAndFeel.updateSubregion(ss, g, tabAreaBounds); |
|
463 |
ss.getPainter().paintTabbedPaneTabAreaBackground(ss, g, |
|
464 |
tabAreaBounds.x, tabAreaBounds.y, tabAreaBounds.width, |
|
465 |
tabAreaBounds.height, tabPlacement); |
|
466 |
ss.getPainter().paintTabbedPaneTabAreaBorder(ss, g, tabAreaBounds.x, |
|
467 |
tabAreaBounds.y, tabAreaBounds.width, tabAreaBounds.height, |
|
468 |
tabPlacement); |
|
469 |
||
470 |
int tabCount = tabPane.getTabCount(); |
|
471 |
||
472 |
iconRect.setBounds(0, 0, 0, 0); |
|
473 |
textRect.setBounds(0, 0, 0, 0); |
|
474 |
||
475 |
// Paint tabRuns of tabs from back to front |
|
476 |
for (int i = runCount - 1; i >= 0; i--) { |
|
477 |
int start = tabRuns[i]; |
|
478 |
int next = tabRuns[(i == runCount - 1)? 0 : i + 1]; |
|
479 |
int end = (next != 0? next - 1: tabCount - 1); |
|
480 |
for (int j = start; j <= end; j++) { |
|
481 |
if (rects[j].intersects(clipRect) && selectedIndex != j) { |
|
482 |
paintTab(tabContext, g, tabPlacement, rects, j, iconRect, |
|
483 |
textRect); |
|
484 |
} |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
if (selectedIndex >= 0) { |
|
489 |
if (rects[selectedIndex].intersects(clipRect)) { |
|
490 |
paintTab(tabContext, g, tabPlacement, rects, selectedIndex, |
|
491 |
iconRect, textRect); |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
protected void setRolloverTab(int index) { |
|
497 |
int oldRolloverTab = getRolloverTab(); |
|
498 |
super.setRolloverTab(index); |
|
499 |
||
500 |
Rectangle r = null; |
|
501 |
||
2658 | 502 |
if (oldRolloverTab != index && tabAreaStatesMatchSelectedTab) { |
503 |
//TODO need to just repaint the tab area! |
|
504 |
tabPane.repaint(); |
|
505 |
} else { |
|
506 |
if ((oldRolloverTab >= 0) && (oldRolloverTab < tabPane.getTabCount())) { |
|
507 |
r = getTabBounds(tabPane, oldRolloverTab); |
|
508 |
if (r != null) { |
|
509 |
tabPane.repaint(r); |
|
510 |
} |
|
2 | 511 |
} |
512 |
||
2658 | 513 |
if (index >= 0) { |
514 |
r = getTabBounds(tabPane, index); |
|
515 |
if (r != null) { |
|
516 |
tabPane.repaint(r); |
|
517 |
} |
|
2 | 518 |
} |
519 |
} |
|
520 |
} |
|
521 |
||
522 |
protected void paintTab(SynthContext ss, Graphics g, |
|
523 |
int tabPlacement, Rectangle[] rects, int tabIndex, |
|
524 |
Rectangle iconRect, Rectangle textRect) { |
|
525 |
Rectangle tabRect = rects[tabIndex]; |
|
526 |
int selectedIndex = tabPane.getSelectedIndex(); |
|
527 |
boolean isSelected = selectedIndex == tabIndex; |
|
2658 | 528 |
updateTabContext(tabIndex, isSelected, isSelected && selectedTabIsPressed, |
529 |
(getRolloverTab() == tabIndex), |
|
530 |
(getFocusIndex() == tabIndex)); |
|
2 | 531 |
|
532 |
SynthLookAndFeel.updateSubregion(ss, g, tabRect); |
|
2658 | 533 |
int x = tabRect.x; |
534 |
int y = tabRect.y; |
|
535 |
int height = tabRect.height; |
|
536 |
int width = tabRect.width; |
|
537 |
int placement = tabPane.getTabPlacement(); |
|
538 |
if (extendTabsToBase && runCount > 1) { |
|
539 |
//paint this tab such that its edge closest to the base is equal to |
|
540 |
//edge of the selected tab closest to the base. In terms of the TOP |
|
541 |
//tab placement, this will cause the bottom of each tab to be |
|
542 |
//painted even with the bottom of the selected tab. This is because |
|
543 |
//in each tab placement (TOP, LEFT, BOTTOM, RIGHT) the selected tab |
|
544 |
//is closest to the base. |
|
545 |
if (selectedIndex >= 0) { |
|
546 |
Rectangle r = rects[selectedIndex]; |
|
547 |
switch (placement) { |
|
548 |
case TOP: |
|
549 |
int bottomY = r.y + r.height; |
|
550 |
height = bottomY - tabRect.y; |
|
551 |
break; |
|
552 |
case LEFT: |
|
553 |
int rightX = r.x + r.width; |
|
554 |
width = rightX - tabRect.x; |
|
555 |
break; |
|
556 |
case BOTTOM: |
|
557 |
int topY = r.y; |
|
558 |
height = (tabRect.y + tabRect.height) - topY; |
|
559 |
y = topY; |
|
560 |
break; |
|
561 |
case RIGHT: |
|
562 |
int leftX = r.x; |
|
563 |
width = (tabRect.x + tabRect.width) - leftX; |
|
564 |
x = leftX; |
|
565 |
break; |
|
566 |
} |
|
567 |
} |
|
568 |
} |
|
569 |
tabContext.getPainter().paintTabbedPaneTabBackground(tabContext, g, |
|
570 |
x, y, width, height, tabIndex, placement); |
|
2 | 571 |
tabContext.getPainter().paintTabbedPaneTabBorder(tabContext, g, |
2658 | 572 |
x, y, width, height, tabIndex, placement); |
2 | 573 |
|
574 |
if (tabPane.getTabComponentAt(tabIndex) == null) { |
|
575 |
String title = tabPane.getTitleAt(tabIndex); |
|
576 |
Font font = ss.getStyle().getFont(ss); |
|
577 |
FontMetrics metrics = SwingUtilities2.getFontMetrics(tabPane, g, font); |
|
578 |
Icon icon = getIconForTab(tabIndex); |
|
579 |
||
580 |
layoutLabel(ss, tabPlacement, metrics, tabIndex, title, icon, |
|
581 |
tabRect, iconRect, textRect, isSelected); |
|
582 |
||
583 |
paintText(ss, g, tabPlacement, font, metrics, |
|
584 |
tabIndex, title, textRect, isSelected); |
|
585 |
||
586 |
paintIcon(g, tabPlacement, tabIndex, icon, iconRect, isSelected); |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
protected void layoutLabel(SynthContext ss, int tabPlacement, |
|
591 |
FontMetrics metrics, int tabIndex, |
|
592 |
String title, Icon icon, |
|
593 |
Rectangle tabRect, Rectangle iconRect, |
|
594 |
Rectangle textRect, boolean isSelected ) { |
|
595 |
View v = getTextViewForTab(tabIndex); |
|
596 |
if (v != null) { |
|
597 |
tabPane.putClientProperty("html", v); |
|
598 |
} |
|
599 |
||
600 |
textRect.x = textRect.y = iconRect.x = iconRect.y = 0; |
|
601 |
||
602 |
ss.getStyle().getGraphicsUtils(ss).layoutText(ss, metrics, title, |
|
603 |
icon, SwingUtilities.CENTER, SwingUtilities.CENTER, |
|
2490
b460fb94812d
6815767: Bad parameter when calling another method in the class SynthTabbedPaneUI
peterz
parents:
2
diff
changeset
|
604 |
SwingUtilities.LEADING, SwingUtilities.CENTER, |
2 | 605 |
tabRect, iconRect, textRect, textIconGap); |
606 |
||
607 |
tabPane.putClientProperty("html", null); |
|
608 |
||
609 |
int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected); |
|
610 |
int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected); |
|
611 |
iconRect.x += xNudge; |
|
612 |
iconRect.y += yNudge; |
|
613 |
textRect.x += xNudge; |
|
614 |
textRect.y += yNudge; |
|
615 |
} |
|
616 |
||
617 |
protected void paintText(SynthContext ss, |
|
618 |
Graphics g, int tabPlacement, |
|
619 |
Font font, FontMetrics metrics, int tabIndex, |
|
620 |
String title, Rectangle textRect, |
|
621 |
boolean isSelected) { |
|
622 |
g.setFont(font); |
|
623 |
||
624 |
View v = getTextViewForTab(tabIndex); |
|
625 |
if (v != null) { |
|
626 |
// html |
|
627 |
v.paint(g, textRect); |
|
628 |
} else { |
|
629 |
// plain text |
|
630 |
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); |
|
631 |
||
632 |
g.setColor(ss.getStyle().getColor(ss, ColorType.TEXT_FOREGROUND)); |
|
633 |
ss.getStyle().getGraphicsUtils(ss).paintText(ss, g, title, |
|
634 |
textRect, mnemIndex); |
|
635 |
} |
|
636 |
} |
|
637 |
||
638 |
||
639 |
protected void paintContentBorder(SynthContext ss, Graphics g, |
|
640 |
int tabPlacement, int selectedIndex) { |
|
641 |
int width = tabPane.getWidth(); |
|
642 |
int height = tabPane.getHeight(); |
|
643 |
Insets insets = tabPane.getInsets(); |
|
644 |
||
645 |
int x = insets.left; |
|
646 |
int y = insets.top; |
|
647 |
int w = width - insets.right - insets.left; |
|
648 |
int h = height - insets.top - insets.bottom; |
|
649 |
||
650 |
switch(tabPlacement) { |
|
651 |
case LEFT: |
|
652 |
x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); |
|
653 |
w -= (x - insets.left); |
|
654 |
break; |
|
655 |
case RIGHT: |
|
656 |
w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); |
|
657 |
break; |
|
658 |
case BOTTOM: |
|
659 |
h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); |
|
660 |
break; |
|
661 |
case TOP: |
|
662 |
default: |
|
663 |
y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); |
|
664 |
h -= (y - insets.top); |
|
665 |
} |
|
666 |
SynthLookAndFeel.updateSubregion(ss, g, new Rectangle(x, y, w, h)); |
|
667 |
ss.getPainter().paintTabbedPaneContentBackground(ss, g, x, y, |
|
668 |
w, h); |
|
669 |
ss.getPainter().paintTabbedPaneContentBorder(ss, g, x, y, w, h); |
|
670 |
} |
|
671 |
||
672 |
private void ensureCurrentLayout() { |
|
673 |
if (!tabPane.isValid()) { |
|
674 |
tabPane.validate(); |
|
675 |
} |
|
676 |
/* If tabPane doesn't have a peer yet, the validate() call will |
|
677 |
* silently fail. We handle that by forcing a layout if tabPane |
|
678 |
* is still invalid. See bug 4237677. |
|
679 |
*/ |
|
680 |
if (!tabPane.isValid()) { |
|
681 |
TabbedPaneLayout layout = (TabbedPaneLayout)tabPane.getLayout(); |
|
682 |
layout.calculateLayoutInfo(); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
||
687 |
protected int calculateMaxTabHeight(int tabPlacement) { |
|
688 |
FontMetrics metrics = getFontMetrics(tabContext.getStyle().getFont( |
|
689 |
tabContext)); |
|
690 |
int tabCount = tabPane.getTabCount(); |
|
691 |
int result = 0; |
|
692 |
int fontHeight = metrics.getHeight(); |
|
693 |
for(int i = 0; i < tabCount; i++) { |
|
694 |
result = Math.max(calculateTabHeight(tabPlacement, i, fontHeight), result); |
|
695 |
} |
|
696 |
return result; |
|
697 |
} |
|
698 |
||
699 |
protected int calculateTabWidth(int tabPlacement, int tabIndex, |
|
700 |
FontMetrics metrics) { |
|
701 |
Icon icon = getIconForTab(tabIndex); |
|
702 |
Insets tabInsets = getTabInsets(tabPlacement, tabIndex); |
|
703 |
int width = tabInsets.left + tabInsets.right; |
|
704 |
Component tabComponent = tabPane.getTabComponentAt(tabIndex); |
|
705 |
if (tabComponent != null) { |
|
706 |
width += tabComponent.getPreferredSize().width; |
|
707 |
} else { |
|
708 |
if (icon != null) { |
|
709 |
width += icon.getIconWidth() + textIconGap; |
|
710 |
} |
|
711 |
View v = getTextViewForTab(tabIndex); |
|
712 |
if (v != null) { |
|
713 |
// html |
|
714 |
width += (int) v.getPreferredSpan(View.X_AXIS); |
|
715 |
} else { |
|
716 |
// plain text |
|
717 |
String title = tabPane.getTitleAt(tabIndex); |
|
718 |
width += tabContext.getStyle().getGraphicsUtils(tabContext). |
|
719 |
computeStringWidth(tabContext, metrics.getFont(), |
|
720 |
metrics, title); |
|
721 |
} |
|
722 |
} |
|
723 |
return width; |
|
724 |
} |
|
725 |
||
726 |
protected int calculateMaxTabWidth(int tabPlacement) { |
|
727 |
FontMetrics metrics = getFontMetrics(tabContext.getStyle().getFont( |
|
728 |
tabContext)); |
|
729 |
int tabCount = tabPane.getTabCount(); |
|
730 |
int result = 0; |
|
731 |
for(int i = 0; i < tabCount; i++) { |
|
732 |
result = Math.max(calculateTabWidth(tabPlacement, i, metrics), |
|
733 |
result); |
|
734 |
} |
|
735 |
return result; |
|
736 |
} |
|
737 |
||
738 |
protected Insets getTabInsets(int tabPlacement, int tabIndex) { |
|
2658 | 739 |
updateTabContext(tabIndex, false, false, false, |
2 | 740 |
(getFocusIndex() == tabIndex)); |
741 |
return tabInsets; |
|
742 |
} |
|
743 |
||
744 |
protected FontMetrics getFontMetrics() { |
|
745 |
return getFontMetrics(tabContext.getStyle().getFont(tabContext)); |
|
746 |
} |
|
747 |
||
748 |
protected FontMetrics getFontMetrics(Font font) { |
|
749 |
return tabPane.getFontMetrics(font); |
|
750 |
} |
|
751 |
||
752 |
private void updateTabContext(int index, boolean selected, |
|
2658 | 753 |
boolean isMouseDown, boolean isMouseOver, boolean hasFocus) { |
2 | 754 |
int state = 0; |
755 |
if (!tabPane.isEnabled() || !tabPane.isEnabledAt(index)) { |
|
756 |
state |= SynthConstants.DISABLED; |
|
757 |
if (selected) { |
|
758 |
state |= SynthConstants.SELECTED; |
|
759 |
} |
|
760 |
} |
|
761 |
else if (selected) { |
|
762 |
state |= (SynthConstants.ENABLED | SynthConstants.SELECTED); |
|
763 |
if (isMouseOver && UIManager.getBoolean("TabbedPane.isTabRollover")) { |
|
764 |
state |= SynthConstants.MOUSE_OVER; |
|
765 |
} |
|
766 |
} |
|
767 |
else if (isMouseOver) { |
|
768 |
state |= (SynthConstants.ENABLED | SynthConstants.MOUSE_OVER); |
|
769 |
} |
|
770 |
else { |
|
771 |
state = SynthLookAndFeel.getComponentState(tabPane); |
|
772 |
state &= ~SynthConstants.FOCUSED; // don't use tabbedpane focus state |
|
773 |
} |
|
774 |
if (hasFocus && tabPane.hasFocus()) { |
|
775 |
state |= SynthConstants.FOCUSED; // individual tab has focus |
|
776 |
} |
|
2658 | 777 |
if (isMouseDown) { |
778 |
state |= SynthConstants.PRESSED; |
|
779 |
} |
|
780 |
||
2 | 781 |
tabContext.setComponentState(state); |
782 |
} |
|
783 |
||
2658 | 784 |
/** |
785 |
* @inheritDoc |
|
786 |
* |
|
787 |
* Overridden to create a TabbedPaneLayout subclass which takes into |
|
788 |
* account tabOverlap. |
|
789 |
*/ |
|
790 |
@Override protected LayoutManager createLayoutManager() { |
|
791 |
if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) { |
|
792 |
return super.createLayoutManager(); |
|
793 |
} else { /* WRAP_TAB_LAYOUT */ |
|
794 |
return new TabbedPaneLayout() { |
|
795 |
@Override |
|
796 |
public void calculateLayoutInfo() { |
|
797 |
super.calculateLayoutInfo(); |
|
798 |
//shift all the tabs, if necessary |
|
799 |
if (tabOverlap != 0) { |
|
800 |
int tabCount = tabPane.getTabCount(); |
|
801 |
//left-to-right/right-to-left only affects layout |
|
802 |
//when placement is TOP or BOTTOM |
|
803 |
boolean ltr = tabPane.getComponentOrientation().isLeftToRight(); |
|
804 |
for (int i = runCount - 1; i >= 0; i--) { |
|
805 |
int start = tabRuns[i]; |
|
806 |
int next = tabRuns[(i == runCount - 1)? 0 : i + 1]; |
|
807 |
int end = (next != 0? next - 1: tabCount - 1); |
|
808 |
for (int j = start+1; j <= end; j++) { |
|
809 |
// xshift and yshift represent the amount & |
|
810 |
// direction to shift the tab in their |
|
811 |
// respective axis. |
|
812 |
int xshift = 0; |
|
813 |
int yshift = 0; |
|
814 |
// configure xshift and y shift based on tab |
|
815 |
// position and ltr/rtl |
|
816 |
switch (tabPane.getTabPlacement()) { |
|
817 |
case JTabbedPane.TOP: |
|
818 |
case JTabbedPane.BOTTOM: |
|
819 |
xshift = ltr ? tabOverlap : -tabOverlap; |
|
820 |
break; |
|
821 |
case JTabbedPane.LEFT: |
|
822 |
case JTabbedPane.RIGHT: |
|
823 |
yshift = tabOverlap; |
|
824 |
break; |
|
825 |
default: //do nothing |
|
826 |
} |
|
827 |
rects[j].x += xshift; |
|
828 |
rects[j].y += yshift; |
|
829 |
rects[j].width += Math.abs(xshift); |
|
830 |
rects[j].height += Math.abs(yshift); |
|
831 |
} |
|
832 |
} |
|
833 |
} |
|
834 |
} |
|
835 |
}; |
|
836 |
} |
|
837 |
} |
|
838 |
||
2 | 839 |
private class SynthScrollableTabButton extends SynthArrowButton implements |
840 |
UIResource { |
|
841 |
public SynthScrollableTabButton(int direction) { |
|
842 |
super(direction); |
|
843 |
} |
|
844 |
} |
|
845 |
} |