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