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