@@ -19,38 +19,67 @@ import java.awt.Color
1919import java.awt.GradientPaint
2020import java.awt.Graphics
2121import java.awt.Graphics2D
22+ import java.awt.Insets
2223import javax.swing.JToolBar
24+ import javax.swing.UIManager
25+ import javax.swing.border.BevelBorder
26+ import javax.swing.border.EmptyBorder
2327
2428class GradientToolbar extends JToolBar {
29+
30+ private def isOracleLookAndFeel () {
31+ val laf = UIManager . lookAndFeel? . name
32+ if (laf == " Oracle Look and Feel version 2" ) {
33+ return true
34+ } else {
35+ return false
36+ }
37+ }
38+
39+ new () {
40+ super ()
41+ if (oracleLookAndFeel) {
42+ this . border = new EmptyBorder (new Insets (2 , 2 , 2 , 2 )) // top, left, bottom, right
43+ } else {
44+ this . border = new BevelBorder (BevelBorder . RAISED )
45+ }
46+ }
47+
2548 override paintComponent (Graphics g ) {
26- // default for non-opaque components
27- if (! opaque) {
49+ if (oracleLookAndFeel) {
50+ // emulate Oracle toolbar
51+ // 1. default for non-opaque components
52+ if (! opaque) {
53+ super . paintComponent(g)
54+ return
55+ }
56+
57+ // 2. paint gradient background from top to bottom with separator line at the bottom
58+ val g2d = g as Graphics2D
59+ val w = width
60+ val h = height - 1
61+ val h2 = height / 2 as int
62+ val colorTop = new Color (237 , 237 , 237 )
63+ val colorMiddle = new Color (244 , 244 , 244 )
64+ val colorBottom = new Color (254 , 254 , 254 )
65+ val colorBottomLine = Color . LIGHT_GRAY
66+ val gp1 = new GradientPaint (0 , 0 , colorTop, 0 , h2, colorMiddle)
67+ g2d. paint = gp1
68+ g2d. fillRect(0 , 0 , w, h2)
69+ val gp2 = new GradientPaint (0 , h2, colorMiddle, 0 , h, colorBottom)
70+ g2d. paint = gp2
71+ g2d. fillRect(0 , h2, w, h)
72+ g2d. paint = colorBottomLine
73+ g2d. fillRect(0 , h, w, h+ 1 )
74+
75+ // 3. do rest, changing opaque to ensure background is not overwritten
76+ setOpaque(false )
77+ super . paintComponent(g)
78+ setOpaque(true )
79+ } else {
80+ // default logic
2881 super . paintComponent(g)
29- return
3082 }
31-
32- // paint gradient background from top to bottom with separator line at the bottom
33- val g2d = g as Graphics2D
34- val w = width
35- val h = height - 1
36- val h2 = height / 2 as int
37- val colorTop = new Color (237 , 237 , 237 )
38- val colorMiddle = new Color (244 , 244 , 244 )
39- val colorBottom = new Color (254 , 254 , 254 )
40- val colorBottomLine = Color . LIGHT_GRAY
41- val gp1 = new GradientPaint (0 , 0 , colorTop, 0 , h2, colorMiddle)
42- g2d. paint = gp1
43- g2d. fillRect(0 , 0 , w, h2)
44- val gp2 = new GradientPaint (0 , h2, colorMiddle, 0 , h, colorBottom)
45- g2d. paint = gp2
46- g2d. fillRect(0 , h2, w, h)
47- g2d. paint = colorBottomLine
48- g2d. fillRect(0 , h, w, h+ 1 )
49-
50- // do rest, changing opaque to ensure background is not overwritten
51- setOpaque(false )
52- super . paintComponent(g)
53- setOpaque(true )
5483 }
5584
5685}
0 commit comments