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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/* MetalTabbedPaneUI.java
Copyright (C) 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.plaf.metal;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
/**
* A UI delegate for the {@link JTabbedPane} component.
*/
public class MetalTabbedPaneUI extends BasicTabbedPaneUI
{
/**
* A {@link LayoutManager} responsible for placing all the tabs and the
* visible component inside the {@link JTabbedPane}. This class is only used
* for {@link JTabbedPane#WRAP_TAB_LAYOUT}.
*
* @specnote Apparently this class was intended to be protected,
* but was made public by a compiler bug and is now
* public for compatibility.
*/
public class TabbedPaneLayout
extends BasicTabbedPaneUI.TabbedPaneLayout
{
/**
* Creates a new instance of the layout manager.
*/
public TabbedPaneLayout()
{
// Nothing to do here.
}
/**
* Overridden to do nothing, because tab runs are not rotated in the
* {@link MetalLookAndFeel}.
*
* @param tabPlacement the tab placement (one of {@link #TOP},
* {@link #BOTTOM}, {@link #LEFT} or {@link #RIGHT}).
* @param selectedRun the index of the selected run.
*/
protected void rotateTabRuns(int tabPlacement, int selectedRun)
{
// do nothing, because tab runs are not rotated in the MetalLookAndFeel
}
/**
* Overridden to do nothing, because the selected tab does not have extra
* padding in the {@link MetalLookAndFeel}.
*
* @param tabPlacement the tab placement (one of {@link #TOP},
* {@link #BOTTOM}, {@link #LEFT} or {@link #RIGHT}).
* @param selectedIndex the index of the selected tab.
*/
protected void padSelectedTab(int tabPlacement, int selectedIndex)
{
// do nothing, because the selected tab does not have extra padding in
// the MetalLookAndFeel
}
}
/**
* The minimum tab width.
*/
protected int minTabWidth;
/**
* The color for the selected tab.
*/
protected Color selectColor;
/**
* The color for a highlighted selected tab.
*/
protected Color selectHighlight;
/**
* The background color used for the tab area.
*/
protected Color tabAreaBackground;
/** The graphics to draw the highlight below the tab. */
private Graphics hg;
/**
* Constructs a new instance of MetalTabbedPaneUI.
*/
public MetalTabbedPaneUI()
{
super();
}
/**
* Returns an instance of MetalTabbedPaneUI.
*
* @param component the component for which we return an UI instance
*
* @return an instance of MetalTabbedPaneUI
*/
public static ComponentUI createUI(JComponent component)
{
return new MetalTabbedPaneUI();
}
/**
* Creates and returns an instance of {@link TabbedPaneLayout}.
*
* @return A layout manager used by this UI delegate.
*/
protected LayoutManager createLayoutManager()
{
return super.createLayoutManager();
}
/**
* Paints the border for a single tab.
*
* @param g the graphics device.
* @param tabPlacement the tab placement ({@link #TOP}, {@link #LEFT},
* {@link #BOTTOM} or {@link #RIGHT}).
* @param tabIndex the index of the tab to draw the border for.
* @param x the x-coordinate for the tab's bounding rectangle.
* @param y the y-coordinate for the tab's bounding rectangle.
* @param w the width for the tab's bounding rectangle.
* @param h the height for the tab's bounding rectangle.
* @param isSelected indicates whether or not the tab is selected.
*/
protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
int x, int y, int w, int h, boolean isSelected)
{
if (tabPlacement == TOP)
paintTopTabBorder(tabIndex, g, x, y, w, h, 0, 0, isSelected);
else if (tabPlacement == LEFT)
paintLeftTabBorder(tabIndex, g, x, y, w, h, 0, 0, isSelected);
else if (tabPlacement == BOTTOM)
paintBottomTabBorder(tabIndex, g, x, y, w, h, 0, 0, isSelected);
else if (tabPlacement == RIGHT)
paintRightTabBorder(tabIndex, g, x, y, w, h, 0, 0, isSelected);
else
throw new AssertionError("Unrecognised 'tabPlacement' argument.");
}
/**
* Paints the border for a tab assuming that the tab position is at the top
* ({@link #TOP}).
*
* @param tabIndex the tab index.
* @param g the graphics device.
* @param x the x-coordinate for the tab's bounding rectangle.
* @param y the y-coordinate for the tab's bounding rectangle.
* @param w the width for the tab's bounding rectangle.
* @param h the height for the tab's bounding rectangle.
* @param btm ???
* @param rght ???
* @param isSelected indicates whether the tab is selected.
*/
protected void paintTopTabBorder(int tabIndex, Graphics g, int x, int y,
int w, int h, int btm, int rght, boolean isSelected)
{
int currentRun = getRunForTab(tabPane.getTabCount(), tabIndex);
if (shouldFillGap(currentRun, tabIndex, x, y))
{
g.translate(x, y);
g.setColor(getColorForGap(currentRun, x, y));
g.fillRect(1, 0, 5, 3);
g.fillRect(1, 3, 2, 2);
g.translate(-x, -y);
}
if (isSelected)
{
g.setColor(MetalLookAndFeel.getControlHighlight());
g.drawLine(x + 1, y + h, x + 1, y + 6);
g.drawLine(x + 1, y + 6, x + 6, y + 1);
g.drawLine(x + 6, y + 1, x + w - 1, y + 1);
}
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(x, y + h - 1, x, y + 6);
g.drawLine(x, y + 6, x + 6, y);
g.drawLine(x + 6, y, x + w, y);
g.drawLine(x + w, y, x + w, y + h - 1);
}
/**
* Paints the border for a tab assuming that the tab position is at the left
* ({@link #LEFT}).
*
* @param tabIndex the tab index.
* @param g the graphics device.
* @param x the x-coordinate for the tab's bounding rectangle.
* @param y the y-coordinate for the tab's bounding rectangle.
* @param w the width for the tab's bounding rectangle.
* @param h the height for the tab's bounding rectangle.
* @param btm ???
* @param rght ???
* @param isSelected indicates whether the tab is selected.
*/
protected void paintLeftTabBorder(int tabIndex, Graphics g, int x, int y,
int w, int h, int btm, int rght, boolean isSelected)
{
if (isSelected)
{
g.setColor(MetalLookAndFeel.getControlHighlight());
g.drawLine(x + 1, y + h, x + 1, y + 6);
g.drawLine(x + 1, y + 6, x + 6, y + 1);
g.drawLine(x + 6, y + 1, x + w - 1, y + 1);
}
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(x, y + h, x, y + 6);
g.drawLine(x, y + 6, x + 6, y);
g.drawLine(x + 6, y, x + w - 1, y);
g.drawLine(x, y + h, x + w - 1, y + h);
}
/**
* Paints the border for a tab assuming that the tab position is at the right
* ({@link #RIGHT}).
*
* @param tabIndex the tab index.
* @param g the graphics device.
* @param x the x-coordinate for the tab's bounding rectangle.
* @param y the y-coordinate for the tab's bounding rectangle.
* @param w the width for the tab's bounding rectangle.
* @param h the height for the tab's bounding rectangle.
* @param btm ???
* @param rght ???
* @param isSelected indicates whether the tab is selected.
*/
protected void paintRightTabBorder(int tabIndex, Graphics g, int x, int y,
int w, int h, int btm, int rght, boolean isSelected)
{
if (isSelected)
{
g.setColor(MetalLookAndFeel.getControlHighlight());
g.drawLine(x, y + 1, x + w - 7, y + 1);
g.drawLine(x + w - 7, y + 1, x + w - 1, y + 7);
}
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(x, y, x + w - 7, y);
g.drawLine(x + w - 7, y, x + w - 1, y + 6);
g.drawLine(x + w - 1, y + 6, x + w - 1, y + h - 1);
g.drawLine(x + w - 1, y + h, x, y + h);
}
/**
* Paints the border for a tab assuming that the tab position is at the bottom
* ({@link #BOTTOM}).
*
* @param tabIndex the tab index.
* @param g the graphics device.
* @param x the x-coordinate for the tab's bounding rectangle.
* @param y the y-coordinate for the tab's bounding rectangle.
* @param w the width for the tab's bounding rectangle.
* @param h the height for the tab's bounding rectangle.
* @param btm ???
* @param rght ???
* @param isSelected indicates whether the tab is selected.
*/
protected void paintBottomTabBorder(int tabIndex, Graphics g, int x, int y,
int w, int h, int btm, int rght, boolean isSelected)
{
int currentRun = getRunForTab(tabPane.getTabCount(), tabIndex);
if (shouldFillGap(currentRun, tabIndex, x, y))
{
g.translate(x, y);
g.setColor(getColorForGap(currentRun, x, y));
g.fillRect(1, h - 5, 3, 5);
g.fillRect(4, h - 2, 2, 2);
g.translate(-x, -y);
}
if (isSelected)
{
g.setColor(MetalLookAndFeel.getControlHighlight());
g.drawLine(x + 1, y, x + 1, y + h - 7);
g.drawLine(x + 1, y + h - 7, x + 7, y + h - 1);
}
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(x, y, x, y + h - 7);
g.drawLine(x, y + h - 7, x + 6, y + h - 1);
g.drawLine(x + 6, y + h - 1, x + w, y + h - 1);
g.drawLine(x + w, y + h - 1, x + w, y);
}
/**
* Paints the background for a tab.
*
* @param g the graphics device.
* @param tabPlacement the tab placement ({@link #TOP}, {@link #LEFT},
* {@link #BOTTOM} or {@link #RIGHT}).
* @param tabIndex the index of the tab to draw the border for.
* @param x the x-coordinate for the tab's bounding rectangle.
* @param y the y-coordinate for the tab's bounding rectangle.
* @param w the width for the tab's bounding rectangle.
* @param h the height for the tab's bounding rectangle.
* @param isSelected indicates whether or not the tab is selected.
*/
protected void paintTabBackground(Graphics g, int tabPlacement,
int tabIndex, int x, int y, int w, int h, boolean isSelected)
{
if (isSelected)
g.setColor(UIManager.getColor("TabbedPane.selected"));
else
{
// This is only present in the OceanTheme, so we must check if it
// is actually there
Color background = UIManager.getColor("TabbedPane.unselectedBackground");
if (background == null)
background = UIManager.getColor("TabbedPane.background");
g.setColor(background);
}
int[] px, py;
if (tabPlacement == TOP)
{
px = new int[] {x + 6, x + w - 1, x + w -1, x + 2, x + 2};
py = new int[] {y + 2, y + 2, y + h - 1, y + h -1, y + 6};
}
else if (tabPlacement == LEFT)
{
px = new int[] {x + 6, x + w - 1, x + w -1, x + 2, x + 2};
py = new int[] {y + 2, y + 2, y + h - 1, y + h -1, y + 6};
}
else if (tabPlacement == BOTTOM)
{
px = new int[] {x + 2, x + w - 1, x + w -1, x + 8, x + 2};
py = new int[] {y, y, y + h - 1, y + h -1, y + h - 7};
}
else if (tabPlacement == RIGHT)
{
px = new int[] {x + 2, x + w - 7, x + w - 1, x + w - 1, x + 2};
py = new int[] {y + 2, y + 2, y + 7, y + h -1, y + h - 1};
}
else
throw new AssertionError("Unrecognised 'tabPlacement' argument.");
g.fillPolygon(px, py, 5);
hg = g;
paintHighlightBelowTab();
}
/**
* Returns <code>true</code> if the tabs in the specified run should be
* padded to make the run fill the width/height of the {@link JTabbedPane}.
*
* @param tabPlacement the tab placement for the {@link JTabbedPane} (one of
* {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} and {@link #RIGHT}).
* @param run the run index.
*
* @return A boolean.
*/
protected boolean shouldPadTabRun(int tabPlacement, int run)
{
// as far as I can tell, all runs should be padded except the last run
// (which is drawn at the very top for tabPlacement == TOP)
return run < this.runCount - 1;
}
/**
* Installs the defaults for this UI. This method calls super.installDefaults
* and then loads the Metal specific defaults for TabbedPane.
*/
protected void installDefaults()
{
super.installDefaults();
selectColor = UIManager.getColor("TabbedPane.selected");
selectHighlight = UIManager.getColor("TabbedPane.selectHighlight");
tabAreaBackground = UIManager.getColor("TabbedPane.tabAreaBackground");
minTabWidth = 0;
}
/**
* Returns the color for the gap.
*
* @param currentRun - The current run to return the color for
* @param x - The x position of the current run
* @param y - The y position of the current run
*
* @return the color for the gap in the current run.
*/
protected Color getColorForGap(int currentRun, int x, int y)
{
int index = tabForCoordinate(tabPane, x, y);
int selected = tabPane.getSelectedIndex();
if (selected == index)
return selectColor;
return tabAreaBackground;
}
/**
* Returns true if the gap should be filled in.
*
* @param currentRun - The current run
* @param tabIndex - The current tab
* @param x - The x position of the tab
* @param y - The y position of the tab
*
* @return true if the gap at the current run should be filled
*/
protected boolean shouldFillGap(int currentRun, int tabIndex, int x, int y)
{
// As far as I can tell, the gap is never filled in.
return false;
}
/**
* Paints the highlight below the tab, if there is one.
*/
protected void paintHighlightBelowTab()
{
int selected = tabPane.getSelectedIndex();
int tabPlacement = tabPane.getTabPlacement();
Rectangle bounds = getTabBounds(tabPane, selected);
hg.setColor(selectColor);
int x = bounds.x;
int y = bounds.y;
int w = bounds.width;
int h = bounds.height;
if (tabPlacement == TOP)
hg.fillRect(x, y + h - 2, w, 30);
else if (tabPlacement == LEFT)
hg.fillRect(x + w - 1, y, 20, h);
else if (tabPlacement == BOTTOM)
hg.fillRect(x, y - h + 2, w, 30);
else if (tabPlacement == RIGHT)
hg.fillRect(x - 18, y, 20, h);
else
throw new AssertionError("Unrecognised 'tabPlacement' argument.");
hg = null;
}
/**
* Returns true if we should rotate the tab runs.
*
* @param tabPlacement - The current tab placement.
* @param selectedRun - The selected run.
*
* @return true if the tab runs should be rotated.
*/
protected boolean shouldRotateTabRuns(int tabPlacement,
int selectedRun)
{
// false because tab runs are not rotated in the MetalLookAndFeel
return false;
}
}
|