Formatting
[jalview.git] / src / jalview / gui / TextColourChooser.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.gui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 import javax.swing.*;\r
27 import javax.swing.event.*;\r
28 \r
29 import jalview.datamodel.*;\r
30 \r
31 public class TextColourChooser\r
32 {\r
33   AlignmentPanel ap;\r
34   SequenceGroup sg;\r
35 \r
36   public void chooseColour(AlignmentPanel ap, SequenceGroup sg)\r
37   {\r
38     this.ap = ap;\r
39     this.sg = sg;\r
40 \r
41     int original1, original2, originalThreshold;\r
42     if (sg == null)\r
43     {\r
44       original1 = ap.av.textColour.getRGB();\r
45       original2 = ap.av.textColour2.getRGB();\r
46       originalThreshold = ap.av.thresholdTextColour;\r
47     }\r
48     else\r
49     {\r
50       original1 = sg.textColour.getRGB();\r
51       original2 = sg.textColour2.getRGB();\r
52       originalThreshold = sg.thresholdTextColour;\r
53     }\r
54 \r
55     final JSlider slider = new JSlider(0, 750, originalThreshold);\r
56     final JPanel col1 = new JPanel();\r
57     col1.setPreferredSize(new Dimension(40, 20));\r
58     col1.setBorder(BorderFactory.createEtchedBorder());\r
59     col1.setToolTipText("Dark Colour");\r
60     col1.setBackground(new Color(original1));\r
61     final JPanel col2 = new JPanel();\r
62     col2.setPreferredSize(new Dimension(40, 20));\r
63     col2.setBorder(BorderFactory.createEtchedBorder());\r
64     col2.setToolTipText("Light Colour");\r
65     col2.setBackground(new Color(original2));\r
66     final JPanel bigpanel = new JPanel(new BorderLayout());\r
67     JPanel panel = new JPanel();\r
68     bigpanel.add(panel, BorderLayout.CENTER);\r
69     bigpanel.add(new JLabel(\r
70         "<html><i>Select a dark and light text colour, then set the threshold to"\r
71         + "<br>switch between colours, based on background colour</i></html>"),\r
72                  BorderLayout.NORTH);\r
73     panel.add(col1);\r
74     panel.add(slider);\r
75     panel.add(col2);\r
76 \r
77     col1.addMouseListener(new MouseAdapter()\r
78     {\r
79       public void mousePressed(MouseEvent e)\r
80       {\r
81         Color col = JColorChooser.showDialog(bigpanel,\r
82                                              "Select Colour for Text",\r
83                                              col1.getBackground());\r
84         if (col != null)\r
85         {\r
86           colour1Changed(col);\r
87           col1.setBackground(col);\r
88         }\r
89       }\r
90     });\r
91 \r
92     col2.addMouseListener(new MouseAdapter()\r
93     {\r
94       public void mousePressed(MouseEvent e)\r
95       {\r
96         Color col = JColorChooser.showDialog(bigpanel,\r
97                                              "Select Colour for Text",\r
98                                              col2.getBackground());\r
99         if (col != null)\r
100         {\r
101           colour2Changed(col);\r
102           col2.setBackground(col);\r
103         }\r
104       }\r
105     });\r
106 \r
107     slider.addChangeListener(new ChangeListener()\r
108     {\r
109       public void stateChanged(ChangeEvent evt)\r
110       {\r
111         thresholdChanged(slider.getValue());\r
112       }\r
113     });\r
114 \r
115     int reply = JOptionPane.showInternalOptionDialog(\r
116         ap,\r
117         bigpanel,\r
118         "Adjust Foreground Text Colour Threshold",\r
119         JOptionPane.OK_CANCEL_OPTION,\r
120         JOptionPane.QUESTION_MESSAGE,\r
121         null,\r
122         null, null);\r
123 \r
124     if (reply == JOptionPane.CANCEL_OPTION)\r
125     {\r
126       if (sg == null)\r
127       {\r
128         ap.av.textColour = new Color(original1);\r
129         ap.av.textColour2 = new Color(original2);\r
130         ap.av.thresholdTextColour = originalThreshold;\r
131       }\r
132       else\r
133       {\r
134         sg.textColour = new Color(original1);\r
135         sg.textColour2 = new Color(original2);\r
136         sg.thresholdTextColour = originalThreshold;\r
137       }\r
138     }\r
139   }\r
140 \r
141   void colour1Changed(Color col)\r
142   {\r
143     if (sg == null)\r
144     {\r
145       ap.av.textColour = col;\r
146       if (ap.av.colourAppliesToAllGroups)\r
147       {\r
148         setGroupTextColour();\r
149       }\r
150     }\r
151     else\r
152     {\r
153       sg.textColour = col;\r
154     }\r
155 \r
156     ap.repaint();\r
157   }\r
158 \r
159   void colour2Changed(Color col)\r
160   {\r
161     if (sg == null)\r
162     {\r
163       ap.av.textColour2 = col;\r
164       if (ap.av.colourAppliesToAllGroups)\r
165       {\r
166         setGroupTextColour();\r
167       }\r
168     }\r
169     else\r
170     {\r
171       sg.textColour2 = col;\r
172     }\r
173 \r
174     ap.repaint();\r
175   }\r
176 \r
177   void thresholdChanged(int value)\r
178   {\r
179     if (sg == null)\r
180     {\r
181       ap.av.thresholdTextColour = value;\r
182       if (ap.av.colourAppliesToAllGroups)\r
183       {\r
184         setGroupTextColour();\r
185       }\r
186     }\r
187     else\r
188     {\r
189       sg.thresholdTextColour = value;\r
190     }\r
191 \r
192     ap.repaint();\r
193   }\r
194 \r
195   void setGroupTextColour()\r
196   {\r
197     if (ap.av.alignment.getGroups() == null)\r
198     {\r
199       return;\r
200     }\r
201 \r
202     Vector groups = ap.av.alignment.getGroups();\r
203 \r
204     for (int i = 0; i < groups.size(); i++)\r
205     {\r
206       SequenceGroup sg = (SequenceGroup) groups.elementAt(i);\r
207       sg.textColour = ap.av.textColour;\r
208       sg.textColour2 = ap.av.textColour2;\r
209       sg.thresholdTextColour = ap.av.thresholdTextColour;\r
210     }\r
211   }\r
212 \r
213 }\r