compat -> ui.compat
[fanfix.git] / src / be / nikiroo / utils / ui / UIUtils.java
CommitLineData
86057589 1package be.nikiroo.utils.ui;
8caeb8bd 2
ef13cd7f
NR
3import java.awt.Color;
4import java.awt.GradientPaint;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.Paint;
8import java.awt.RadialGradientPaint;
9import java.awt.RenderingHints;
10
a917f100
NR
11import javax.swing.JComponent;
12import javax.swing.JScrollPane;
8caeb8bd
NR
13import javax.swing.UIManager;
14import javax.swing.UnsupportedLookAndFeelException;
15
16/**
17 * Some Java Swing utilities.
18 *
19 * @author niki
20 */
21public class UIUtils {
22 /**
6fdf8ca3 23 * Set a fake "native Look & Feel" for the application if possible
8caeb8bd
NR
24 * (check for the one currently in use, then try GTK).
25 * <p>
26 * <b>Must</b> be called prior to any GUI work.
6fdf8ca3
NR
27 *
28 * @return TRUE if it succeeded
8caeb8bd 29 */
6fdf8ca3 30 static public boolean setLookAndFeel() {
8caeb8bd 31 // native look & feel
6fdf8ca3
NR
32 String noLF = "javax.swing.plaf.metal.MetalLookAndFeel";
33 String lf = UIManager.getSystemLookAndFeelClassName();
34 if (lf.equals(noLF))
35 lf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
36
37 return setLookAndFeel(lf);
38 }
39
40 /**
41 * Switch to the given Look &amp; Feel for the application if possible
42 * (check for the one currently in use, then try GTK).
43 * <p>
44 * <b>Must</b> be called prior to any GUI work.
45 *
46 * @param laf
47 * the Look &amp; Feel to use
48 *
49 * @return TRUE if it succeeded
50 */
51 static public boolean setLookAndFeel(String laf) {
8caeb8bd 52 try {
6fdf8ca3
NR
53 UIManager.setLookAndFeel(laf);
54 return true;
8caeb8bd
NR
55 } catch (InstantiationException e) {
56 } catch (ClassNotFoundException e) {
57 } catch (UnsupportedLookAndFeelException e) {
58 } catch (IllegalAccessException e) {
59 }
6fdf8ca3
NR
60
61 return false;
8caeb8bd 62 }
a917f100 63
db0af0d9
NR
64 /**
65 * Draw a 3D-looking ellipse at the given location, if the given
66 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
67 * simple ellipse if not.
68 *
69 * @param g
70 * the {@link Graphics} to draw on
71 * @param color
72 * the base colour
73 * @param x
74 * the X coordinate
75 * @param y
76 * the Y coordinate
77 * @param width
78 * the width radius
79 * @param height
80 * the height radius
81 */
a917f100
NR
82 static public void drawEllipse3D(Graphics g, Color color, int x, int y,
83 int width, int height) {
db0af0d9
NR
84 drawEllipse3D(g, color, x, y, width, height, true);
85 }
ef13cd7f
NR
86
87 /**
88 * Draw a 3D-looking ellipse at the given location, if the given
89 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
90 * simple ellipse if not.
91 *
92 * @param g
93 * the {@link Graphics} to draw on
94 * @param color
95 * the base colour
96 * @param x
97 * the X coordinate
98 * @param y
99 * the Y coordinate
100 * @param width
101 * the width radius
102 * @param height
103 * the height radius
a917f100
NR
104 * @param fill
105 * fill the content of the ellipse
ef13cd7f
NR
106 */
107 static public void drawEllipse3D(Graphics g, Color color, int x, int y,
db0af0d9 108 int width, int height, boolean fill) {
ef13cd7f
NR
109 if (g instanceof Graphics2D) {
110 Graphics2D g2 = (Graphics2D) g;
111 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
112 RenderingHints.VALUE_ANTIALIAS_ON);
113
114 // Retains the previous state
115 Paint oldPaint = g2.getPaint();
116
117 // Base shape
118 g2.setColor(color);
db0af0d9
NR
119 if (fill) {
120 g2.fillOval(x, y, width, height);
121 } else {
122 g2.drawOval(x, y, width, height);
123 }
a917f100 124
ef13cd7f
NR
125 // Compute dark/bright colours
126 Paint p = null;
db0af0d9
NR
127 Color dark = color.darker().darker();
128 Color bright = color.brighter().brighter();
ef13cd7f
NR
129 Color darkEnd = new Color(dark.getRed(), dark.getGreen(),
130 dark.getBlue(), 0);
131 Color darkPartial = new Color(dark.getRed(), dark.getGreen(),
132 dark.getBlue(), 64);
133 Color brightEnd = new Color(bright.getRed(), bright.getGreen(),
134 bright.getBlue(), 0);
135
136 // Adds shadows at the bottom left
137 p = new GradientPaint(0, height, dark, width, 0, darkEnd);
138 g2.setPaint(p);
db0af0d9
NR
139 if (fill) {
140 g2.fillOval(x, y, width, height);
141 } else {
142 g2.drawOval(x, y, width, height);
143 }
ef13cd7f
NR
144 // Adds highlights at the top right
145 p = new GradientPaint(width, 0, bright, 0, height, brightEnd);
146 g2.setPaint(p);
db0af0d9
NR
147 if (fill) {
148 g2.fillOval(x, y, width, height);
149 } else {
150 g2.drawOval(x, y, width, height);
151 }
a917f100 152
ef13cd7f
NR
153 // Darken the edges
154 p = new RadialGradientPaint(x + width / 2f, y + height / 2f,
155 Math.min(width / 2f, height / 2f), new float[] { 0f, 1f },
156 new Color[] { darkEnd, darkPartial },
157 RadialGradientPaint.CycleMethod.NO_CYCLE);
158 g2.setPaint(p);
db0af0d9
NR
159 if (fill) {
160 g2.fillOval(x, y, width, height);
161 } else {
162 g2.drawOval(x, y, width, height);
163 }
ef13cd7f
NR
164
165 // Adds inner highlight at the top right
166 p = new RadialGradientPaint(x + 3f * width / 4f, y + height / 4f,
167 Math.min(width / 4f, height / 4f),
168 new float[] { 0.0f, 0.8f },
169 new Color[] { bright, brightEnd },
170 RadialGradientPaint.CycleMethod.NO_CYCLE);
171 g2.setPaint(p);
db0af0d9
NR
172 if (fill) {
173 g2.fillOval(x * 2, y, width, height);
174 } else {
175 g2.drawOval(x * 2, y, width, height);
176 }
ef13cd7f
NR
177
178 // Reset original paint
179 g2.setPaint(oldPaint);
180 } else {
181 g.setColor(color);
db0af0d9
NR
182 if (fill) {
183 g.fillOval(x, y, width, height);
184 } else {
185 g.drawOval(x, y, width, height);
186 }
ef13cd7f
NR
187 }
188 }
a917f100
NR
189
190 /**
191 * Add a {@link JScrollPane} around the given panel and use a sensible (for
192 * me) increment for the mouse wheel.
193 *
194 * @param pane
195 * the panel to wrap in a {@link JScrollPane}
196 * @param allowHorizontal
197 * allow horizontal scrolling (not always desired)
198 *
199 * @return the {@link JScrollPane}
200 */
201 static public JScrollPane scroll(JComponent pane, boolean allowHorizontal) {
3af909c1
NR
202 return scroll(pane, allowHorizontal, true);
203 }
204
205 /**
206 * Add a {@link JScrollPane} around the given panel and use a sensible (for
207 * me) increment for the mouse wheel.
208 *
209 * @param pane
210 * the panel to wrap in a {@link JScrollPane}
211 * @param allowHorizontal
212 * allow horizontal scrolling (not always desired)
213 * @param allowVertical
214 * allow vertical scrolling (usually yes, but sometimes you only
215 * want horizontal)
216 *
217 * @return the {@link JScrollPane}
218 */
219 static public JScrollPane scroll(JComponent pane, boolean allowHorizontal,
220 boolean allowVertical) {
a917f100 221 JScrollPane scroll = new JScrollPane(pane);
3af909c1 222
a917f100 223 scroll.getVerticalScrollBar().setUnitIncrement(16);
3af909c1
NR
224 scroll.getHorizontalScrollBar().setUnitIncrement(16);
225
a917f100
NR
226 if (!allowHorizontal) {
227 scroll.setHorizontalScrollBarPolicy(
228 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
229 }
3af909c1
NR
230 if (!allowVertical) {
231 scroll.setVerticalScrollBarPolicy(
232 JScrollPane.VERTICAL_SCROLLBAR_NEVER);
233 }
234
a917f100
NR
235 return scroll;
236 }
8caeb8bd 237}