| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.gui.components; |
| 27 |
|
| 28 |
import javax.swing.*; |
| 29 |
import java.awt.*; |
| 30 |
import java.awt.event.ActionListener; |
| 31 |
import java.awt.event.ActionEvent; |
| 32 |
import java.io.StringWriter; |
| 33 |
import java.io.PrintWriter; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class ColorWellButton extends JButton { |
| 40 |
private JColorChooser chooser; |
| 41 |
private String colorChooserTitle; |
| 42 |
|
| 43 |
public ColorWellButton(Color color, final String colorChooserTitle) { |
| 44 |
super(); |
| 45 |
|
| 46 |
this.colorChooserTitle = colorChooserTitle; |
| 47 |
|
| 48 |
putClientProperty("JButton.buttonType", "square"); |
| 49 |
setBorderPainted(true); |
| 50 |
|
| 51 |
setIcon(new ColorWell(color)); |
| 52 |
setMargin(new Insets(10, 10, 10, 10)); |
| 53 |
addActionListener(new ActionListener() { |
| 54 |
public void actionPerformed(ActionEvent evt) { |
| 55 |
chooserButtonActionPerformed(evt); |
| 56 |
} |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
public Color getSelectedColor() { |
| 61 |
return ((ColorWell)getIcon()).color; |
| 62 |
} |
| 63 |
|
| 64 |
public void setSelectedColor(Color color) { |
| 65 |
((ColorWell)getIcon()).color = color; |
| 66 |
repaint(); |
| 67 |
} |
| 68 |
|
| 69 |
private void chooserButtonActionPerformed(java.awt.event.ActionEvent evt) { |
| 70 |
try { |
| 71 |
|
| 72 |
if (chooser == null) { |
| 73 |
chooser = new JColorChooser(); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
} |
| 84 |
Color color = chooser.showDialog(this, colorChooserTitle, ((ColorWell)getIcon()).color); |
| 85 |
if (color != null) { |
| 86 |
setSelectedColor(color); |
| 87 |
} |
| 88 |
} catch (Throwable t) { |
| 89 |
StringWriter sw = new StringWriter(); |
| 90 |
PrintWriter pw = new PrintWriter(sw); |
| 91 |
t.printStackTrace(pw); |
| 92 |
pw.close(); |
| 93 |
JOptionPane.showMessageDialog(this, "JColorChooser Failed "+sw.toString()); |
| 94 |
t.printStackTrace(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private class ColorWell implements Icon { |
| 99 |
Color color; |
| 100 |
|
| 101 |
ColorWell(Color color) { |
| 102 |
super(); |
| 103 |
this.color = color; |
| 104 |
} |
| 105 |
|
| 106 |
public int getIconWidth() { |
| 107 |
return 15; |
| 108 |
} |
| 109 |
|
| 110 |
public int getIconHeight() { |
| 111 |
return 15; |
| 112 |
} |
| 113 |
|
| 114 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
| 115 |
if (color == null) return; |
| 116 |
g.setColor(color); |
| 117 |
g.fillRect(x, y, getIconWidth(), getIconHeight()); |
| 118 |
g.setColor(color.darker()); |
| 119 |
g.drawRect(x, y, getIconWidth() - 1, getIconHeight() - 1); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
} |