4315e0b2c9eadcc3f7887d1c64d2b988e73d0e66
[jalview.git] / src / jalview / gui / TextColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
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 import jalview.util.MessageManager;
30
31 public class TextColourChooser
32 {
33   AlignmentPanel ap;
34
35   SequenceGroup sg;
36
37   public void chooseColour(AlignmentPanel ap, SequenceGroup sg)
38   {
39     this.ap = ap;
40     this.sg = sg;
41
42     int original1, original2, originalThreshold;
43     if (sg == null)
44     {
45       original1 = ap.av.textColour.getRGB();
46       original2 = ap.av.textColour2.getRGB();
47       originalThreshold = ap.av.thresholdTextColour;
48     }
49     else
50     {
51       original1 = sg.textColour.getRGB();
52       original2 = sg.textColour2.getRGB();
53       originalThreshold = sg.thresholdTextColour;
54     }
55
56     final JSlider slider = new JSlider(0, 750, originalThreshold);
57     final JPanel col1 = new JPanel();
58     col1.setPreferredSize(new Dimension(40, 20));
59     col1.setBorder(BorderFactory.createEtchedBorder());
60     col1.setToolTipText(MessageManager.getString("label.dark_colour"));
61     col1.setBackground(new Color(original1));
62     final JPanel col2 = new JPanel();
63     col2.setPreferredSize(new Dimension(40, 20));
64     col2.setBorder(BorderFactory.createEtchedBorder());
65     col2.setToolTipText(MessageManager.getString("label.ligth_colour"));
66     col2.setBackground(new Color(original2));
67     final JPanel bigpanel = new JPanel(new BorderLayout());
68     JPanel panel = new JPanel();
69     bigpanel.add(panel, BorderLayout.CENTER);
70     bigpanel.add(
71             new JLabel("<html>"+MessageManager.getString("label.select_dark_light_set_thereshold")+"</html>"),
72             BorderLayout.NORTH);
73     panel.add(col1);
74     panel.add(slider);
75     panel.add(col2);
76
77     col1.addMouseListener(new MouseAdapter()
78     {
79       public void mousePressed(MouseEvent e)
80       {
81         Color col = JColorChooser.showDialog(bigpanel,
82                 MessageManager.getString("label.select_colour_for_text"), 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                         MessageManager.getString("label.select_colour_for_text"), col2.getBackground());
97         if (col != null)
98         {
99           colour2Changed(col);
100           col2.setBackground(col);
101         }
102       }
103     });
104
105     slider.addChangeListener(new ChangeListener()
106     {
107       public void stateChanged(ChangeEvent evt)
108       {
109         thresholdChanged(slider.getValue());
110       }
111     });
112
113     int reply = JOptionPane.showInternalOptionDialog(ap, bigpanel,
114             MessageManager.getString("label.adjunst_foreground_text_colour_thereshold"),
115             JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
116             null, null, null);
117
118     if (reply == JOptionPane.CANCEL_OPTION)
119     {
120       if (sg == null)
121       {
122         ap.av.textColour = new Color(original1);
123         ap.av.textColour2 = new Color(original2);
124         ap.av.thresholdTextColour = originalThreshold;
125       }
126       else
127       {
128         sg.textColour = new Color(original1);
129         sg.textColour2 = new Color(original2);
130         sg.thresholdTextColour = originalThreshold;
131       }
132     }
133   }
134
135   void colour1Changed(Color col)
136   {
137     if (sg == null)
138     {
139       ap.av.textColour = col;
140       if (ap.av.getColourAppliesToAllGroups())
141       {
142         setGroupTextColour();
143       }
144     }
145     else
146     {
147       sg.textColour = col;
148     }
149
150     ap.paintAlignment(true);
151   }
152
153   void colour2Changed(Color col)
154   {
155     if (sg == null)
156     {
157       ap.av.textColour2 = col;
158       if (ap.av.getColourAppliesToAllGroups())
159       {
160         setGroupTextColour();
161       }
162     }
163     else
164     {
165       sg.textColour2 = col;
166     }
167
168     ap.paintAlignment(true);
169   }
170
171   void thresholdChanged(int value)
172   {
173     if (sg == null)
174     {
175       ap.av.thresholdTextColour = value;
176       if (ap.av.getColourAppliesToAllGroups())
177       {
178         setGroupTextColour();
179       }
180     }
181     else
182     {
183       sg.thresholdTextColour = value;
184     }
185
186     ap.paintAlignment(true);
187   }
188
189   void setGroupTextColour()
190   {
191     if (ap.av.getAlignment().getGroups() == null)
192     {
193       return;
194     }
195
196     for (SequenceGroup sg : ap.av.getAlignment().getGroups())
197     {
198       sg.textColour = ap.av.textColour;
199       sg.textColour2 = ap.av.textColour2;
200       sg.thresholdTextColour = ap.av.thresholdTextColour;
201     }
202   }
203
204 }