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 be
.nikiroo
.utils
.Version
;
31 import be
.nikiroo
.utils
.VersionCheck
;
34 * Some Java Swing utilities.
38 public class UIUtils
{
39 static private Color buttonNormal
;
40 static private Color buttonPressed
;
43 * Set a fake "native Look & Feel" for the application if possible
44 * (check for the one currently in use, then try GTK).
46 * <b>Must</b> be called prior to any GUI work.
48 * @return TRUE if it succeeded
50 static public boolean setLookAndFeel() {
52 String noLF
= "javax.swing.plaf.metal.MetalLookAndFeel";
53 String lf
= UIManager
.getSystemLookAndFeelClassName();
55 lf
= "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
57 return setLookAndFeel(lf
);
61 * Switch to the given Look & Feel for the application if possible
62 * (check for the one currently in use, then try GTK).
64 * <b>Must</b> be called prior to any GUI work.
67 * the Look & Feel to use
69 * @return TRUE if it succeeded
71 static public boolean setLookAndFeel(String laf
) {
73 UIManager
.setLookAndFeel(laf
);
75 } catch (InstantiationException e
) {
76 } catch (ClassNotFoundException e
) {
77 } catch (UnsupportedLookAndFeelException e
) {
78 } catch (IllegalAccessException e
) {
85 * Draw a 3D-looking ellipse at the given location, if the given
86 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
87 * simple ellipse if not.
90 * the {@link Graphics} to draw on
102 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
103 int width
, int height
) {
104 drawEllipse3D(g
, color
, x
, y
, width
, height
, true);
108 * Draw a 3D-looking ellipse at the given location, if the given
109 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
110 * simple ellipse if not.
113 * the {@link Graphics} to draw on
117 * the X coordinate of the upper left corner
119 * the Y coordinate of the upper left corner
125 * fill the content of the ellipse
127 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
128 int width
, int height
, boolean fill
) {
129 if (g
instanceof Graphics2D
) {
130 Graphics2D g2
= (Graphics2D
) g
;
131 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
132 RenderingHints
.VALUE_ANTIALIAS_ON
);
134 // Retains the previous state
135 Paint oldPaint
= g2
.getPaint();
140 g2
.fillOval(x
, y
, width
, height
);
142 g2
.drawOval(x
, y
, width
, height
);
145 // Compute dark/bright colours
147 Color dark
= color
.darker().darker();
148 Color bright
= color
.brighter().brighter();
149 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
151 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
153 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
154 bright
.getBlue(), 0);
156 // Adds shadows at the bottom left
157 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
160 g2
.fillOval(x
, y
, width
, height
);
162 g2
.drawOval(x
, y
, width
, height
);
164 // Adds highlights at the top right
165 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
168 g2
.fillOval(x
, y
, width
, height
);
170 g2
.drawOval(x
, y
, width
, height
);
174 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
175 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
176 new Color
[] { darkEnd
, darkPartial
},
177 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
180 g2
.fillOval(x
, y
, width
, height
);
182 g2
.drawOval(x
, y
, width
, height
);
185 // Adds inner highlight at the top right
186 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
187 Math
.min(width
/ 4f
, height
/ 4f
),
188 new float[] { 0.0f
, 0.8f
},
189 new Color
[] { bright
, brightEnd
},
190 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
193 g2
.fillOval(x
* 2, y
, width
, height
);
195 g2
.drawOval(x
* 2, y
, width
, height
);
198 // Reset original paint
199 g2
.setPaint(oldPaint
);
203 g
.fillOval(x
, y
, width
, height
);
205 g
.drawOval(x
, y
, width
, height
);
211 * Add a {@link JScrollPane} around the given panel and use a sensible (for
212 * me) increment for the mouse wheel.
215 * the panel to wrap in a {@link JScrollPane}
216 * @param allowHorizontal
217 * allow horizontal scrolling (not always desired)
219 * @return the {@link JScrollPane}
221 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
) {
222 return scroll(pane
, allowHorizontal
, true);
226 * Add a {@link JScrollPane} around the given panel and use a sensible (for
227 * me) increment for the mouse wheel.
230 * the panel to wrap in a {@link JScrollPane}
231 * @param allowHorizontal
232 * allow horizontal scrolling (not always desired)
233 * @param allowVertical
234 * allow vertical scrolling (usually yes, but sometimes you only
237 * @return the {@link JScrollPane}
239 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
,
240 boolean allowVertical
) {
241 JScrollPane scroll
= new JScrollPane(pane
);
243 scroll
.getVerticalScrollBar().setUnitIncrement(16);
244 scroll
.getHorizontalScrollBar().setUnitIncrement(16);
246 if (!allowHorizontal
) {
247 scroll
.setHorizontalScrollBarPolicy(JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
249 if (!allowVertical
) {
250 scroll
.setVerticalScrollBarPolicy(JScrollPane
.VERTICAL_SCROLLBAR_NEVER
);
257 * Show a confirmation message to the user to show him the changes since
260 * HTML 3.2 supported, links included (the user browser will be launched if
263 * If this is already the latest version, a message will still be displayed.
265 * @param parentComponent
266 * determines the {@link java.awt.Frame} in which the dialog is
267 * displayed; if <code>null</code>, or if the
268 * <code>parentComponent</code> has no {@link java.awt.Frame}, a
269 * default {@link java.awt.Frame} is used
273 * an introduction text before the list of changes
275 * the title of the dialog
277 * @return TRUE if the user clicked on OK, false if the dialog was dismissed
279 static public boolean showUpdatedDialog(Component parentComponent
,
280 VersionCheck updates
, String introText
, String title
) {
282 StringBuilder builder
= new StringBuilder();
283 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
284 if (introText
!= null && !introText
.isEmpty()) {
285 builder
.append(introText
);
286 builder
.append("<br>");
287 builder
.append("<br>");
289 for (Version v
: updates
.getNewer()) {
290 builder
.append("\t<b>" //
291 + "Version " + v
.toString() //
293 builder
.append("<br>");
294 builder
.append("<ul>");
295 for (String item
: updates
.getChanges().get(v
)) {
296 builder
.append("<li>" + item
+ "</li>");
298 builder
.append("</ul>");
302 updateMessage
.setText("<html><body>" //
306 // handle link events
307 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
309 public void hyperlinkUpdate(HyperlinkEvent e
) {
310 if (e
.getEventType().equals(HyperlinkEvent
.EventType
.ACTIVATED
))
312 Desktop
.getDesktop().browse(e
.getURL().toURI());
313 } catch (IOException ee
) {
314 ee
.printStackTrace();
315 } catch (URISyntaxException ee
) {
316 ee
.printStackTrace();
320 updateMessage
.setEditable(false);
321 updateMessage
.setBackground(new JLabel().getBackground());
322 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
324 public void hyperlinkUpdate(HyperlinkEvent evn
) {
325 if (evn
.getEventType() == HyperlinkEvent
.EventType
.ACTIVATED
) {
326 if (Desktop
.isDesktopSupported()) {
328 Desktop
.getDesktop().browse(evn
.getURL().toURI());
329 } catch (IOException e
) {
330 } catch (URISyntaxException e
) {
337 return JOptionPane
.showConfirmDialog(parentComponent
, updateMessage
,
338 title
, JOptionPane
.DEFAULT_OPTION
) == JOptionPane
.OK_OPTION
;
342 * Set the given {@link JButton} as "pressed" (selected, but with more UI
345 * The {@link JButton} will answer {@link JButton#isSelected()} if it is
349 * the button to select/press
351 * the new "pressed" state
353 static public void setButtonPressed(JButton button
, boolean pressed
) {
354 if (buttonNormal
== null) {
355 JButton defButton
= new JButton(" ");
356 buttonNormal
= defButton
.getBackground();
357 if (buttonNormal
.getBlue() >= 128) {
358 buttonPressed
= new Color( //
359 Math
.max(buttonNormal
.getRed() - 100, 0), //
360 Math
.max(buttonNormal
.getGreen() - 100, 0), //
361 Math
.max(buttonNormal
.getBlue() - 100, 0));
363 buttonPressed
= new Color( //
364 Math
.min(buttonNormal
.getRed() + 100, 255), //
365 Math
.min(buttonNormal
.getGreen() + 100, 255), //
366 Math
.min(buttonNormal
.getBlue() + 100, 255));
370 button
.setSelected(pressed
);
371 button
.setBackground(pressed ? buttonPressed
: buttonNormal
);
375 * Set the given {@link windows} to full screen mode, on the desktop it
376 * currently resides on.
378 * Can be cancelled by calling again with a NULL value.
381 * the window to set to full screen
383 static public void setFullscreenWindow(Window win
) {
384 GraphicsEnvironment env
= GraphicsEnvironment
385 .getLocalGraphicsEnvironment();
386 GraphicsDevice
[] screens
= env
.getScreenDevices();
389 for (GraphicsDevice screen
: screens
) {
391 screen
.setFullScreenWindow(null);
398 GraphicsDevice current
= null;
399 for (GraphicsDevice screen
: screens
) {
400 GraphicsConfiguration
[] confs
= screen
.getConfigurations();
401 for (GraphicsConfiguration conf
: confs
) {
402 if (conf
.getBounds().contains(win
.getBounds())) {
412 if (current
== null) {
413 current
= GraphicsEnvironment
.getLocalGraphicsEnvironment()
414 .getDefaultScreenDevice();
417 current
.setFullScreenWindow(win
);