1 package be
.nikiroo
.utils
.ui
;
4 import java
.awt
.Component
;
5 import java
.awt
.Desktop
;
6 import java
.awt
.GradientPaint
;
7 import java
.awt
.Graphics
;
8 import java
.awt
.Graphics2D
;
9 import java
.awt
.GraphicsConfiguration
;
10 import java
.awt
.GraphicsDevice
;
11 import java
.awt
.GraphicsEnvironment
;
12 import java
.awt
.Paint
;
13 import java
.awt
.RadialGradientPaint
;
14 import java
.awt
.RenderingHints
;
15 import java
.awt
.Window
;
16 import java
.io
.IOException
;
17 import java
.net
.URISyntaxException
;
19 import javax
.swing
.JButton
;
20 import javax
.swing
.JComponent
;
21 import javax
.swing
.JEditorPane
;
22 import javax
.swing
.JLabel
;
23 import javax
.swing
.JOptionPane
;
24 import javax
.swing
.JScrollPane
;
25 import javax
.swing
.UIManager
;
26 import javax
.swing
.UnsupportedLookAndFeelException
;
27 import javax
.swing
.event
.HyperlinkEvent
;
28 import javax
.swing
.event
.HyperlinkListener
;
30 import com
.sun
.java
.swing
.plaf
.windows
.resources
.windows
;
32 import be
.nikiroo
.utils
.Version
;
33 import be
.nikiroo
.utils
.VersionCheck
;
36 * Some Java Swing utilities.
40 public class UIUtils
{
41 static private Color buttonNormal
;
42 static private Color buttonPressed
;
45 * Set a fake "native Look & Feel" for the application if possible
46 * (check for the one currently in use, then try GTK).
48 * <b>Must</b> be called prior to any GUI work.
50 * @return TRUE if it succeeded
52 static public boolean setLookAndFeel() {
54 String noLF
= "javax.swing.plaf.metal.MetalLookAndFeel";
55 String lf
= UIManager
.getSystemLookAndFeelClassName();
57 lf
= "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
59 return setLookAndFeel(lf
);
63 * Switch to the given Look & Feel for the application if possible
64 * (check for the one currently in use, then try GTK).
66 * <b>Must</b> be called prior to any GUI work.
69 * the Look & Feel to use
71 * @return TRUE if it succeeded
73 static public boolean setLookAndFeel(String laf
) {
75 UIManager
.setLookAndFeel(laf
);
77 } catch (InstantiationException e
) {
78 } catch (ClassNotFoundException e
) {
79 } catch (UnsupportedLookAndFeelException e
) {
80 } catch (IllegalAccessException e
) {
87 * Draw a 3D-looking ellipse at the given location, if the given
88 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
89 * simple ellipse if not.
92 * the {@link Graphics} to draw on
104 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
105 int width
, int height
) {
106 drawEllipse3D(g
, color
, x
, y
, width
, height
, true);
110 * Draw a 3D-looking ellipse at the given location, if the given
111 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
112 * simple ellipse if not.
115 * the {@link Graphics} to draw on
119 * the X coordinate of the upper left corner
121 * the Y coordinate of the upper left corner
127 * fill the content of the ellipse
129 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
130 int width
, int height
, boolean fill
) {
131 if (g
instanceof Graphics2D
) {
132 Graphics2D g2
= (Graphics2D
) g
;
133 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
134 RenderingHints
.VALUE_ANTIALIAS_ON
);
136 // Retains the previous state
137 Paint oldPaint
= g2
.getPaint();
142 g2
.fillOval(x
, y
, width
, height
);
144 g2
.drawOval(x
, y
, width
, height
);
147 // Compute dark/bright colours
149 Color dark
= color
.darker().darker();
150 Color bright
= color
.brighter().brighter();
151 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
153 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
155 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
156 bright
.getBlue(), 0);
158 // Adds shadows at the bottom left
159 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
162 g2
.fillOval(x
, y
, width
, height
);
164 g2
.drawOval(x
, y
, width
, height
);
166 // Adds highlights at the top right
167 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
170 g2
.fillOval(x
, y
, width
, height
);
172 g2
.drawOval(x
, y
, width
, height
);
176 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
177 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
178 new Color
[] { darkEnd
, darkPartial
},
179 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
182 g2
.fillOval(x
, y
, width
, height
);
184 g2
.drawOval(x
, y
, width
, height
);
187 // Adds inner highlight at the top right
188 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
189 Math
.min(width
/ 4f
, height
/ 4f
),
190 new float[] { 0.0f
, 0.8f
},
191 new Color
[] { bright
, brightEnd
},
192 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
195 g2
.fillOval(x
* 2, y
, width
, height
);
197 g2
.drawOval(x
* 2, y
, width
, height
);
200 // Reset original paint
201 g2
.setPaint(oldPaint
);
205 g
.fillOval(x
, y
, width
, height
);
207 g
.drawOval(x
, y
, width
, height
);
213 * Add a {@link JScrollPane} around the given panel and use a sensible (for
214 * me) increment for the mouse wheel.
217 * the panel to wrap in a {@link JScrollPane}
218 * @param allowHorizontal
219 * allow horizontal scrolling (not always desired)
221 * @return the {@link JScrollPane}
223 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
) {
224 return scroll(pane
, allowHorizontal
, true);
228 * Add a {@link JScrollPane} around the given panel and use a sensible (for
229 * me) increment for the mouse wheel.
232 * the panel to wrap in a {@link JScrollPane}
233 * @param allowHorizontal
234 * allow horizontal scrolling (not always desired)
235 * @param allowVertical
236 * allow vertical scrolling (usually yes, but sometimes you only
239 * @return the {@link JScrollPane}
241 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
,
242 boolean allowVertical
) {
243 JScrollPane scroll
= new JScrollPane(pane
);
245 scroll
.getVerticalScrollBar().setUnitIncrement(16);
246 scroll
.getHorizontalScrollBar().setUnitIncrement(16);
248 if (!allowHorizontal
) {
249 scroll
.setHorizontalScrollBarPolicy(JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
251 if (!allowVertical
) {
252 scroll
.setVerticalScrollBarPolicy(JScrollPane
.VERTICAL_SCROLLBAR_NEVER
);
259 * Show a confirmation message to the user to show him the changes since
262 * HTML 3.2 supported, links included (the user browser will be launched if
265 * If this is already the latest version, a message will still be displayed.
267 * @param parentComponent
268 * determines the {@link java.awt.Frame} in which the dialog is
269 * displayed; if <code>null</code>, or if the
270 * <code>parentComponent</code> has no {@link java.awt.Frame}, a
271 * default {@link java.awt.Frame} is used
275 * an introduction text before the list of changes
277 * the title of the dialog
279 * @return TRUE if the user clicked on OK, false if the dialog was dismissed
281 static public boolean showUpdatedDialog(Component parentComponent
,
282 VersionCheck updates
, String introText
, String title
) {
284 StringBuilder builder
= new StringBuilder();
285 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
286 if (introText
!= null && !introText
.isEmpty()) {
287 builder
.append(introText
);
288 builder
.append("<br>");
289 builder
.append("<br>");
291 for (Version v
: updates
.getNewer()) {
292 builder
.append("\t<b>" //
293 + "Version " + v
.toString() //
295 builder
.append("<br>");
296 builder
.append("<ul>");
297 for (String item
: updates
.getChanges().get(v
)) {
298 builder
.append("<li>" + item
+ "</li>");
300 builder
.append("</ul>");
304 updateMessage
.setText("<html><body>" //
308 // handle link events
309 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
311 public void hyperlinkUpdate(HyperlinkEvent e
) {
312 if (e
.getEventType().equals(HyperlinkEvent
.EventType
.ACTIVATED
))
314 Desktop
.getDesktop().browse(e
.getURL().toURI());
315 } catch (IOException ee
) {
316 ee
.printStackTrace();
317 } catch (URISyntaxException ee
) {
318 ee
.printStackTrace();
322 updateMessage
.setEditable(false);
323 updateMessage
.setBackground(new JLabel().getBackground());
324 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
326 public void hyperlinkUpdate(HyperlinkEvent evn
) {
327 if (evn
.getEventType() == HyperlinkEvent
.EventType
.ACTIVATED
) {
328 if (Desktop
.isDesktopSupported()) {
330 Desktop
.getDesktop().browse(evn
.getURL().toURI());
331 } catch (IOException e
) {
332 } catch (URISyntaxException e
) {
339 return JOptionPane
.showConfirmDialog(parentComponent
, updateMessage
,
340 title
, JOptionPane
.DEFAULT_OPTION
) == JOptionPane
.OK_OPTION
;
344 * Set the given {@link JButton} as "pressed" (selected, but with more UI
347 * The {@link JButton} will answer {@link JButton#isSelected()} if it is
351 * the button to select/press
353 * the new "pressed" state
355 static public void setButtonPressed(JButton button
, boolean pressed
) {
356 if (buttonNormal
== null) {
357 JButton defButton
= new JButton(" ");
358 buttonNormal
= defButton
.getBackground();
359 if (buttonNormal
.getBlue() >= 128) {
360 buttonPressed
= new Color( //
361 Math
.max(buttonNormal
.getRed() - 100, 0), //
362 Math
.max(buttonNormal
.getGreen() - 100, 0), //
363 Math
.max(buttonNormal
.getBlue() - 100, 0));
365 buttonPressed
= new Color( //
366 Math
.min(buttonNormal
.getRed() + 100, 255), //
367 Math
.min(buttonNormal
.getGreen() + 100, 255), //
368 Math
.min(buttonNormal
.getBlue() + 100, 255));
372 button
.setSelected(pressed
);
373 button
.setBackground(pressed ? buttonPressed
: buttonNormal
);
377 * Set the given {@link windows} to full screen mode, on the desktop it
378 * currently resides on.
380 * Can be cancelled by calling again with a NULL value.
383 * the window to set to full screen
385 static public void setFullscreenWindow(Window win
) {
386 GraphicsEnvironment env
= GraphicsEnvironment
387 .getLocalGraphicsEnvironment();
388 GraphicsDevice
[] screens
= env
.getScreenDevices();
391 for (GraphicsDevice screen
: screens
) {
393 screen
.setFullScreenWindow(null);
400 GraphicsDevice current
= null;
401 for (GraphicsDevice screen
: screens
) {
402 GraphicsConfiguration
[] confs
= screen
.getConfigurations();
403 for (GraphicsConfiguration conf
: confs
) {
404 if (conf
.getBounds().contains(win
.getBounds())) {
414 if (current
== null) {
415 current
= GraphicsEnvironment
.getLocalGraphicsEnvironment()
416 .getDefaultScreenDevice();
419 current
.setFullScreenWindow(win
);