Debian Codegraph @

src/dr/app/gui/components/ColorWellButton.java

raw symbols SCIP
1 /*
2 * ColorWellButton.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Andrew Rambaut
37 * @version $Id$
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 // putClientProperty("Quaqua.Button.style", "colorWell");
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 // System.out.println("chooserButtonActionPerformed "+evt);
72 if (chooser == null) {
73 chooser = new JColorChooser();
74 /*
75 chooser.setSelectionModel(new DefaultColorSelectionModel() {
76 public void setSelectedColor(Color c) {
77 new Throwable().printStackTrace();
78 super.setSelectedColor(c);
79 }
80 }
81 );*/
82 //JOptionPane.showMessageDialog(this, "Made a new chooser");
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 }