2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.event.MouseAdapter;
27 import java.awt.event.MouseEvent;
28 import java.util.HashMap;
30 import java.util.concurrent.Callable;
32 import javax.swing.BorderFactory;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JSlider;
36 import javax.swing.event.ChangeEvent;
37 import javax.swing.event.ChangeListener;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.gui.JalviewColourChooser.ColourChooserListener;
41 import jalview.util.MessageManager;
43 public class TextColourChooser
49 Color original1, original2;
51 int originalThreshold;
53 Map<SequenceGroup, Color> groupColour1;
55 Map<SequenceGroup, Color> groupColour2;
57 Map<SequenceGroup, Integer> groupThreshold;
60 * Show a dialogue which allows the user to select two text colours and adjust
61 * a slider for the cross-over point
64 * the AlignmentPanel context
65 * @param sequenceGroup
66 * the SequenceGroup context (only for group pop-menu option)
68 public void chooseColour(AlignmentPanel alignPanel,
69 SequenceGroup sequenceGroup)
72 this.sg = sequenceGroup;
74 saveInitialSettings();
76 final JSlider slider = new JSlider(0, 750, originalThreshold);
77 final JPanel col1 = new JPanel();
78 col1.setPreferredSize(new Dimension(40, 20));
79 col1.setBorder(BorderFactory.createEtchedBorder());
80 col1.setToolTipText(MessageManager.getString("label.dark_colour"));
81 col1.setBackground(original1);
82 final JPanel col2 = new JPanel();
83 col2.setPreferredSize(new Dimension(40, 20));
84 col2.setBorder(BorderFactory.createEtchedBorder());
85 col2.setToolTipText(MessageManager.getString("label.light_colour"));
86 col2.setBackground(original2);
87 final JPanel bigpanel = new JPanel(new BorderLayout());
88 JPanel panel = new JPanel(new BorderLayout());
89 bigpanel.add(panel, BorderLayout.CENTER);
92 + MessageManager.getString(
93 "label.select_dark_light_set_threshold")
96 panel.add(col1, BorderLayout.WEST);
97 panel.add(slider, BorderLayout.CENTER);
98 panel.add(col2, BorderLayout.EAST);
100 col1.addMouseListener(new MouseAdapter()
103 public void mousePressed(MouseEvent e)
105 String ttl = MessageManager
106 .getString("label.select_colour_for_text");
107 ColourChooserListener listener = new ColourChooserListener()
110 public void colourSelected(Color c)
113 col1.setBackground(c);
116 JalviewColourChooser.showColourChooser(bigpanel, ttl,
117 col1.getBackground(), listener);
121 col2.addMouseListener(new MouseAdapter()
124 public void mousePressed(MouseEvent e)
126 String ttl = MessageManager
127 .getString("label.select_colour_for_text");
128 ColourChooserListener listener = new ColourChooserListener()
131 public void colourSelected(Color c)
134 col2.setBackground(c);
137 JalviewColourChooser.showColourChooser(bigpanel, ttl,
138 col2.getBackground(), listener);
142 slider.addChangeListener(new ChangeListener()
145 public void stateChanged(ChangeEvent evt)
147 thresholdChanged(slider.getValue());
151 Object[] options = new Object[] { MessageManager.getString("action.ok"),
152 MessageManager.getString("action.cancel") };
153 String title = MessageManager
154 .getString("label.adjust_foreground_text_colour_threshold");
155 Callable<Void> action = () -> // response for 1 = Cancel
157 restoreInitialSettings();
160 JvOptionPane.newOptionDialog(alignPanel.alignFrame)
161 .setResponseHandler(1, action).showInternalDialog(bigpanel,
162 title, JvOptionPane.YES_NO_CANCEL_OPTION,
163 JvOptionPane.PLAIN_MESSAGE, null, options,
164 MessageManager.getString("action.ok"));
168 * Restore initial settings on Cancel
170 protected void restoreInitialSettings()
174 ap.av.setTextColour(original1);
175 ap.av.setTextColour2(original2);
176 ap.av.setThresholdTextColour(originalThreshold);
180 sg.textColour = original1;
181 sg.textColour2 = original2;
182 sg.thresholdTextColour = originalThreshold;
186 * if 'Apply To All Groups' was in force, there will be
187 * group-specific settings to restore as well
189 for (SequenceGroup group : this.groupColour1.keySet())
191 group.textColour = groupColour1.get(group);
192 group.textColour2 = groupColour2.get(group);
193 group.thresholdTextColour = groupThreshold.get(group);
196 ap.paintAlignment(false, false);
200 * Save settings on entry, for restore on Cancel
202 protected void saveInitialSettings()
204 groupColour1 = new HashMap<>();
205 groupColour2 = new HashMap<>();
206 groupThreshold = new HashMap<>();
213 original1 = ap.av.getTextColour();
214 original2 = ap.av.getTextColour2();
215 originalThreshold = ap.av.getThresholdTextColour();
216 if (ap.av.getColourAppliesToAllGroups()
217 && ap.av.getAlignment().getGroups() != null)
220 * if applying changes to all groups, need to be able to
221 * restore group settings as well
223 for (SequenceGroup group : ap.av.getAlignment().getGroups())
225 groupColour1.put(group, group.textColour);
226 groupColour2.put(group, group.textColour2);
227 groupThreshold.put(group, group.thresholdTextColour);
234 * Sequence group scope
236 original1 = sg.textColour;
237 original2 = sg.textColour2;
238 originalThreshold = sg.thresholdTextColour;
242 void colour1Changed(Color col)
246 ap.av.setTextColour(col);
247 if (ap.av.getColourAppliesToAllGroups())
249 setGroupTextColour();
257 ap.paintAlignment(false, false);
260 void colour2Changed(Color col)
264 ap.av.setTextColour2(col);
265 if (ap.av.getColourAppliesToAllGroups())
267 setGroupTextColour();
272 sg.textColour2 = col;
275 ap.paintAlignment(false, false);
278 void thresholdChanged(int value)
282 ap.av.setThresholdTextColour(value);
283 if (ap.av.getColourAppliesToAllGroups())
285 setGroupTextColour();
290 sg.thresholdTextColour = value;
293 ap.paintAlignment(false, false);
296 void setGroupTextColour()
298 if (ap.av.getAlignment().getGroups() == null)
303 for (SequenceGroup group : ap.av.getAlignment().getGroups())
305 group.textColour = ap.av.getTextColour();
306 group.textColour2 = ap.av.getTextColour2();
307 group.thresholdTextColour = ap.av.getThresholdTextColour();