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