3318bcc2464e23bcba5342892161c4daf248a1fa
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / utils / UiHelper.java
1 package be.nikiroo.fanfix_swing.gui.utils;
2
3 import java.awt.Color;
4 import java.awt.Component;
5
6 import javax.swing.JButton;
7 import javax.swing.JComponent;
8 import javax.swing.JOptionPane;
9 import javax.swing.JScrollPane;
10 import javax.swing.SwingUtilities;
11
12 import be.nikiroo.fanfix.Instance;
13
14 public class UiHelper {
15 static private Color buttonNormal;
16 static private Color buttonPressed;
17
18 static public void setButtonPressed(JButton button, boolean pressed) {
19 if (buttonNormal == null) {
20 JButton defButton = new JButton(" ");
21 buttonNormal = defButton.getBackground();
22 if (buttonNormal.getBlue() >= 128) {
23 buttonPressed = new Color( //
24 Math.max(buttonNormal.getRed() - 100, 0), //
25 Math.max(buttonNormal.getGreen() - 100, 0), //
26 Math.max(buttonNormal.getBlue() - 100, 0));
27 } else {
28 buttonPressed = new Color( //
29 Math.min(buttonNormal.getRed() + 100, 255), //
30 Math.min(buttonNormal.getGreen() + 100, 255), //
31 Math.min(buttonNormal.getBlue() + 100, 255));
32 }
33 }
34
35 button.setSelected(pressed);
36 button.setBackground(pressed ? buttonPressed : buttonNormal);
37 }
38
39 static public JComponent scroll(JComponent pane) {
40 JScrollPane scroll = new JScrollPane(pane);
41 scroll.getVerticalScrollBar().setUnitIncrement(16);
42 scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
43 return scroll;
44 }
45
46 /**
47 * Display an error message and log the linked {@link Exception}.
48 *
49 * @param owner the owner of the error (to link the messagebox to it)
50 * @param message the message
51 * @param title the title of the error message
52 * @param e the exception to log if any
53 */
54 static public void error(final Component owner, final String message, final String title, Exception e) {
55 Instance.getInstance().getTraceHandler().error(title + ": " + message);
56 if (e != null) {
57 Instance.getInstance().getTraceHandler().error(e);
58 }
59
60 SwingUtilities.invokeLater(new Runnable() {
61 @Override
62 public void run() {
63 JOptionPane.showMessageDialog(owner, message, title, JOptionPane.ERROR_MESSAGE);
64 }
65 });
66 }
67 }