JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / gui / TextColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.datamodel.SequenceGroup;
24 import jalview.util.MessageManager;
25
26 import java.awt.BorderLayout;
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31
32 import javax.swing.BorderFactory;
33 import javax.swing.JColorChooser;
34 import javax.swing.JLabel;
35 import javax.swing.JOptionPane;
36 import javax.swing.JPanel;
37 import javax.swing.JSlider;
38 import javax.swing.event.ChangeEvent;
39 import javax.swing.event.ChangeListener;
40
41 public class TextColourChooser
42 {
43   AlignmentPanel ap;
44
45   SequenceGroup sg;
46
47   public void chooseColour(AlignmentPanel ap, SequenceGroup sg)
48   {
49     this.ap = ap;
50     this.sg = sg;
51
52     int original1, original2, originalThreshold;
53     if (sg == null)
54     {
55       original1 = ap.av.getTextColour().getRGB();
56       original2 = ap.av.getTextColour2().getRGB();
57       originalThreshold = ap.av.getThresholdTextColour();
58     }
59     else
60     {
61       original1 = sg.textColour.getRGB();
62       original2 = sg.textColour2.getRGB();
63       originalThreshold = sg.thresholdTextColour;
64     }
65
66     final JSlider slider = new JSlider(0, 750, originalThreshold);
67     final JPanel col1 = new JPanel();
68     col1.setPreferredSize(new Dimension(40, 20));
69     col1.setBorder(BorderFactory.createEtchedBorder());
70     col1.setToolTipText(MessageManager.getString("label.dark_colour"));
71     col1.setBackground(new Color(original1));
72     final JPanel col2 = new JPanel();
73     col2.setPreferredSize(new Dimension(40, 20));
74     col2.setBorder(BorderFactory.createEtchedBorder());
75     col2.setToolTipText(MessageManager.getString("label.light_colour"));
76     col2.setBackground(new Color(original2));
77     final JPanel bigpanel = new JPanel(new BorderLayout());
78     JPanel panel = new JPanel();
79     bigpanel.add(panel, BorderLayout.CENTER);
80     bigpanel.add(
81             new JLabel(
82                     "<html>"
83                             + MessageManager
84                                     .getString("label.select_dark_light_set_threshold")
85                             + "</html>"), BorderLayout.NORTH);
86     panel.add(col1);
87     panel.add(slider);
88     panel.add(col2);
89
90     col1.addMouseListener(new MouseAdapter()
91     {
92       @Override
93       public void mousePressed(MouseEvent e)
94       {
95         Color col = JColorChooser.showDialog(bigpanel,
96                 MessageManager.getString("label.select_colour_for_text"),
97                 col1.getBackground());
98         if (col != null)
99         {
100           colour1Changed(col);
101           col1.setBackground(col);
102         }
103       }
104     });
105
106     col2.addMouseListener(new MouseAdapter()
107     {
108       @Override
109       public void mousePressed(MouseEvent e)
110       {
111         Color col = JColorChooser.showDialog(bigpanel,
112                 MessageManager.getString("label.select_colour_for_text"),
113                 col2.getBackground());
114         if (col != null)
115         {
116           colour2Changed(col);
117           col2.setBackground(col);
118         }
119       }
120     });
121
122     slider.addChangeListener(new ChangeListener()
123     {
124       @Override
125       public void stateChanged(ChangeEvent evt)
126       {
127         thresholdChanged(slider.getValue());
128       }
129     });
130
131     int reply = JOptionPane
132             .showInternalOptionDialog(
133                     ap,
134                     bigpanel,
135                     MessageManager
136                             .getString("label.adjunst_foreground_text_colour_threshold"),
137                     JOptionPane.OK_CANCEL_OPTION,
138                     JOptionPane.QUESTION_MESSAGE, null, null, null);
139
140     if (reply == JOptionPane.CANCEL_OPTION)
141     {
142       if (sg == null)
143       {
144         ap.av.setTextColour(new Color(original1));
145         ap.av.setTextColour2(new Color(original2));
146         ap.av.setThresholdTextColour(originalThreshold);
147       }
148       else
149       {
150         sg.textColour = new Color(original1);
151         sg.textColour2 = new Color(original2);
152         sg.thresholdTextColour = originalThreshold;
153       }
154     }
155   }
156
157   void colour1Changed(Color col)
158   {
159     if (sg == null)
160     {
161       ap.av.setTextColour(col);
162       if (ap.av.getColourAppliesToAllGroups())
163       {
164         setGroupTextColour();
165       }
166     }
167     else
168     {
169       sg.textColour = col;
170     }
171
172     ap.paintAlignment(true);
173   }
174
175   void colour2Changed(Color col)
176   {
177     if (sg == null)
178     {
179       ap.av.setTextColour2(col);
180       if (ap.av.getColourAppliesToAllGroups())
181       {
182         setGroupTextColour();
183       }
184     }
185     else
186     {
187       sg.textColour2 = col;
188     }
189
190     ap.paintAlignment(true);
191   }
192
193   void thresholdChanged(int value)
194   {
195     if (sg == null)
196     {
197       ap.av.setThresholdTextColour(value);
198       if (ap.av.getColourAppliesToAllGroups())
199       {
200         setGroupTextColour();
201       }
202     }
203     else
204     {
205       sg.thresholdTextColour = value;
206     }
207
208     ap.paintAlignment(true);
209   }
210
211   void setGroupTextColour()
212   {
213     if (ap.av.getAlignment().getGroups() == null)
214     {
215       return;
216     }
217
218     for (SequenceGroup sg : ap.av.getAlignment().getGroups())
219     {
220       sg.textColour = ap.av.getTextColour();
221       sg.textColour2 = ap.av.getTextColour2();
222       sg.thresholdTextColour = ap.av.getThresholdTextColour();
223     }
224   }
225
226 }