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
;
10 import java
.awt
.RadialGradientPaint
;
11 import java
.awt
.RenderingHints
;
12 import java
.io
.IOException
;
13 import java
.net
.URISyntaxException
;
15 import javax
.swing
.JComponent
;
16 import javax
.swing
.JEditorPane
;
17 import javax
.swing
.JLabel
;
18 import javax
.swing
.JOptionPane
;
19 import javax
.swing
.JScrollPane
;
20 import javax
.swing
.UIManager
;
21 import javax
.swing
.UnsupportedLookAndFeelException
;
22 import javax
.swing
.event
.HyperlinkEvent
;
23 import javax
.swing
.event
.HyperlinkListener
;
25 import be
.nikiroo
.utils
.Version
;
26 import be
.nikiroo
.utils
.VersionCheck
;
29 * Some Java Swing utilities.
33 public class UIUtils
{
35 * Set a fake "native Look & Feel" for the application if possible
36 * (check for the one currently in use, then try GTK).
38 * <b>Must</b> be called prior to any GUI work.
40 * @return TRUE if it succeeded
42 static public boolean setLookAndFeel() {
44 String noLF
= "javax.swing.plaf.metal.MetalLookAndFeel";
45 String lf
= UIManager
.getSystemLookAndFeelClassName();
47 lf
= "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
49 return setLookAndFeel(lf
);
53 * Switch to the given Look & Feel for the application if possible
54 * (check for the one currently in use, then try GTK).
56 * <b>Must</b> be called prior to any GUI work.
59 * the Look & Feel to use
61 * @return TRUE if it succeeded
63 static public boolean setLookAndFeel(String laf
) {
65 UIManager
.setLookAndFeel(laf
);
67 } catch (InstantiationException e
) {
68 } catch (ClassNotFoundException e
) {
69 } catch (UnsupportedLookAndFeelException e
) {
70 } catch (IllegalAccessException e
) {
77 * Draw a 3D-looking ellipse at the given location, if the given
78 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
79 * simple ellipse if not.
82 * the {@link Graphics} to draw on
94 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
95 int width
, int height
) {
96 drawEllipse3D(g
, color
, x
, y
, width
, height
, true);
100 * Draw a 3D-looking ellipse at the given location, if the given
101 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
102 * simple ellipse if not.
105 * the {@link Graphics} to draw on
109 * the X coordinate of the upper left corner
111 * the Y coordinate of the upper left corner
117 * fill the content of the ellipse
119 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
120 int width
, int height
, boolean fill
) {
121 if (g
instanceof Graphics2D
) {
122 Graphics2D g2
= (Graphics2D
) g
;
123 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
124 RenderingHints
.VALUE_ANTIALIAS_ON
);
126 // Retains the previous state
127 Paint oldPaint
= g2
.getPaint();
132 g2
.fillOval(x
, y
, width
, height
);
134 g2
.drawOval(x
, y
, width
, height
);
137 // Compute dark/bright colours
139 Color dark
= color
.darker().darker();
140 Color bright
= color
.brighter().brighter();
141 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
143 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
145 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
146 bright
.getBlue(), 0);
148 // Adds shadows at the bottom left
149 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
152 g2
.fillOval(x
, y
, width
, height
);
154 g2
.drawOval(x
, y
, width
, height
);
156 // Adds highlights at the top right
157 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
160 g2
.fillOval(x
, y
, width
, height
);
162 g2
.drawOval(x
, y
, width
, height
);
166 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
167 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
168 new Color
[] { darkEnd
, darkPartial
},
169 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
172 g2
.fillOval(x
, y
, width
, height
);
174 g2
.drawOval(x
, y
, width
, height
);
177 // Adds inner highlight at the top right
178 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
179 Math
.min(width
/ 4f
, height
/ 4f
),
180 new float[] { 0.0f
, 0.8f
},
181 new Color
[] { bright
, brightEnd
},
182 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
185 g2
.fillOval(x
* 2, y
, width
, height
);
187 g2
.drawOval(x
* 2, y
, width
, height
);
190 // Reset original paint
191 g2
.setPaint(oldPaint
);
195 g
.fillOval(x
, y
, width
, height
);
197 g
.drawOval(x
, y
, width
, height
);
203 * Add a {@link JScrollPane} around the given panel and use a sensible (for
204 * me) increment for the mouse wheel.
207 * the panel to wrap in a {@link JScrollPane}
208 * @param allowHorizontal
209 * allow horizontal scrolling (not always desired)
211 * @return the {@link JScrollPane}
213 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
) {
214 return scroll(pane
, allowHorizontal
, true);
218 * Add a {@link JScrollPane} around the given panel and use a sensible (for
219 * me) increment for the mouse wheel.
222 * the panel to wrap in a {@link JScrollPane}
223 * @param allowHorizontal
224 * allow horizontal scrolling (not always desired)
225 * @param allowVertical
226 * allow vertical scrolling (usually yes, but sometimes you only
229 * @return the {@link JScrollPane}
231 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
,
232 boolean allowVertical
) {
233 JScrollPane scroll
= new JScrollPane(pane
);
235 scroll
.getVerticalScrollBar().setUnitIncrement(16);
236 scroll
.getHorizontalScrollBar().setUnitIncrement(16);
238 if (!allowHorizontal
) {
239 scroll
.setHorizontalScrollBarPolicy(
240 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
242 if (!allowVertical
) {
243 scroll
.setVerticalScrollBarPolicy(
244 JScrollPane
.VERTICAL_SCROLLBAR_NEVER
);
251 * Show a confirmation message to the user to show him the changes since
254 * HTML 3.2 supported, links included (the user browser will be launched if
257 * If this is already the latest version, a message will still be displayed.
259 * @param parentComponent
260 * determines the {@link java.awt.Frame} in which the dialog is
261 * displayed; if <code>null</code>, or if the
262 * <code>parentComponent</code> has no {@link java.awt.Frame}, a
263 * default {@link java.awt.Frame} is used
267 * an introduction text before the list of changes
269 * the title of the dialog
271 * @return TRUE if the user clicked on OK, false if the dialog was dismissed
273 static public boolean showUpdatedDialog(Component parentComponent
,
274 VersionCheck updates
, String introText
, String title
) {
276 StringBuilder builder
= new StringBuilder();
277 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
278 if (introText
!= null && !introText
.isEmpty()) {
279 builder
.append(introText
);
280 builder
.append("<br>");
281 builder
.append("<br>");
283 for (Version v
: updates
.getNewer()) {
284 builder
.append("\t<b>" //
285 + "Version " + v
.toString() //
287 builder
.append("<br>");
288 builder
.append("<ul>");
289 for (String item
: updates
.getChanges().get(v
)) {
290 builder
.append("<li>" + item
+ "</li>");
292 builder
.append("</ul>");
296 updateMessage
.setText("<html><body>" //
300 // handle link events
301 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
303 public void hyperlinkUpdate(HyperlinkEvent e
) {
304 if (e
.getEventType().equals(HyperlinkEvent
.EventType
.ACTIVATED
))
306 Desktop
.getDesktop().browse(e
.getURL().toURI());
307 } catch (IOException ee
) {
308 ee
.printStackTrace();
309 } catch (URISyntaxException ee
) {
310 ee
.printStackTrace();
314 updateMessage
.setEditable(false);
315 updateMessage
.setBackground(new JLabel().getBackground());
316 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
318 public void hyperlinkUpdate(HyperlinkEvent evn
) {
319 if (evn
.getEventType() == HyperlinkEvent
.EventType
.ACTIVATED
) {
320 if (Desktop
.isDesktopSupported()) {
322 Desktop
.getDesktop().browse(evn
.getURL().toURI());
323 } catch (IOException e
) {
324 } catch (URISyntaxException e
) {
331 return JOptionPane
.showConfirmDialog(parentComponent
, updateMessage
,
332 title
, JOptionPane
.DEFAULT_OPTION
) == JOptionPane
.OK_OPTION
;