minor optimization. apply gpl development license
[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
34   SequenceGroup sg;
35
36   public void chooseColour(AlignmentPanel ap, SequenceGroup sg)
37   {
38     this.ap = ap;
39     this.sg = sg;
40
41     int original1, original2, originalThreshold;
42     if (sg == null)
43     {
44       original1 = ap.av.textColour.getRGB();
45       original2 = ap.av.textColour2.getRGB();
46       originalThreshold = ap.av.thresholdTextColour;
47     }
48     else
49     {
50       original1 = sg.textColour.getRGB();
51       original2 = sg.textColour2.getRGB();
52       originalThreshold = sg.thresholdTextColour;
53     }
54
55     final JSlider slider = new JSlider(0, 750, originalThreshold);
56     final JPanel col1 = new JPanel();
57     col1.setPreferredSize(new Dimension(40, 20));
58     col1.setBorder(BorderFactory.createEtchedBorder());
59     col1.setToolTipText("Dark Colour");
60     col1.setBackground(new Color(original1));
61     final JPanel col2 = new JPanel();
62     col2.setPreferredSize(new Dimension(40, 20));
63     col2.setBorder(BorderFactory.createEtchedBorder());
64     col2.setToolTipText("Light Colour");
65     col2.setBackground(new Color(original2));
66     final JPanel bigpanel = new JPanel(new BorderLayout());
67     JPanel panel = new JPanel();
68     bigpanel.add(panel, BorderLayout.CENTER);
69     bigpanel
70             .add(
71                     new JLabel(
72                             "<html><i>Select a dark and light text colour, then set the threshold to"
73                                     + "<br>switch between colours, based on background colour</i></html>"),
74                     BorderLayout.NORTH);
75     panel.add(col1);
76     panel.add(slider);
77     panel.add(col2);
78
79     col1.addMouseListener(new MouseAdapter()
80     {
81       public void mousePressed(MouseEvent e)
82       {
83         Color col = JColorChooser.showDialog(bigpanel,
84                 "Select Colour for Text", col1.getBackground());
85         if (col != null)
86         {
87           colour1Changed(col);
88           col1.setBackground(col);
89         }
90       }
91     });
92
93     col2.addMouseListener(new MouseAdapter()
94     {
95       public void mousePressed(MouseEvent e)
96       {
97         Color col = JColorChooser.showDialog(bigpanel,
98                 "Select Colour for Text", col2.getBackground());
99         if (col != null)
100         {
101           colour2Changed(col);
102           col2.setBackground(col);
103         }
104       }
105     });
106
107     slider.addChangeListener(new ChangeListener()
108     {
109       public void stateChanged(ChangeEvent evt)
110       {
111         thresholdChanged(slider.getValue());
112       }
113     });
114
115     int reply = JOptionPane.showInternalOptionDialog(ap, bigpanel,
116             "Adjust Foreground Text Colour Threshold",
117             JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
118             null, null, null);
119
120     if (reply == JOptionPane.CANCEL_OPTION)
121     {
122       if (sg == null)
123       {
124         ap.av.textColour = new Color(original1);
125         ap.av.textColour2 = new Color(original2);
126         ap.av.thresholdTextColour = originalThreshold;
127       }
128       else
129       {
130         sg.textColour = new Color(original1);
131         sg.textColour2 = new Color(original2);
132         sg.thresholdTextColour = originalThreshold;
133       }
134     }
135   }
136
137   void colour1Changed(Color col)
138   {
139     if (sg == null)
140     {
141       ap.av.textColour = col;
142       if (ap.av.colourAppliesToAllGroups)
143       {
144         setGroupTextColour();
145       }
146     }
147     else
148     {
149       sg.textColour = col;
150     }
151
152     ap.paintAlignment(true);
153   }
154
155   void colour2Changed(Color col)
156   {
157     if (sg == null)
158     {
159       ap.av.textColour2 = col;
160       if (ap.av.colourAppliesToAllGroups)
161       {
162         setGroupTextColour();
163       }
164     }
165     else
166     {
167       sg.textColour2 = col;
168     }
169
170     ap.paintAlignment(true);
171   }
172
173   void thresholdChanged(int value)
174   {
175     if (sg == null)
176     {
177       ap.av.thresholdTextColour = value;
178       if (ap.av.colourAppliesToAllGroups)
179       {
180         setGroupTextColour();
181       }
182     }
183     else
184     {
185       sg.thresholdTextColour = value;
186     }
187
188     ap.paintAlignment(true);
189   }
190
191   void setGroupTextColour()
192   {
193     if (ap.av.alignment.getGroups() == null)
194     {
195       return;
196     }
197
198     Vector groups = ap.av.alignment.getGroups();
199
200     for (int i = 0; i < groups.size(); i++)
201     {
202       SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
203       sg.textColour = ap.av.textColour;
204       sg.textColour2 = ap.av.textColour2;
205       sg.thresholdTextColour = ap.av.thresholdTextColour;
206     }
207   }
208
209 }