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