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 jalview.datamodel.SequenceGroup;
24 import jalview.gui.JalviewColourChooser.ColourChooserListener;
25 import jalview.util.MessageManager;
26 import jalview.util.dialogrunner.RunResponse;
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33 import java.util.HashMap;
36 import javax.swing.BorderFactory;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 import javax.swing.JSlider;
40 import javax.swing.event.ChangeEvent;
41 import javax.swing.event.ChangeListener;
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.getString("label.select_colour_for_text");
106 ColourChooserListener listener = new ColourChooserListener()
109 public void colourSelected(Color c)
112 col1.setBackground(c);
115 JalviewColourChooser.showColourChooser(bigpanel, ttl,
116 col1.getBackground(), listener);
120 col2.addMouseListener(new MouseAdapter()
123 public void mousePressed(MouseEvent e)
125 String ttl = MessageManager.getString("label.select_colour_for_text");
126 ColourChooserListener listener = new ColourChooserListener()
129 public void colourSelected(Color c)
132 col2.setBackground(c);
135 JalviewColourChooser.showColourChooser(bigpanel, ttl,
136 col2.getBackground(), listener);
140 slider.addChangeListener(new ChangeListener()
143 public void stateChanged(ChangeEvent evt)
145 thresholdChanged(slider.getValue());
149 Object[] options = new Object[] { MessageManager.getString("action.ok"),
150 MessageManager.getString("action.cancel") };
151 String title = MessageManager
152 .getString("label.adjunst_foreground_text_colour_threshold");
153 RunResponse action = new RunResponse(JvOptionPane.NO_OPTION) // Cancel
158 restoreInitialSettings();
161 JvOptionPane.newOptionDialog(alignPanel).response(action)
162 .showInternalDialog(bigpanel, title,
163 JvOptionPane.YES_NO_CANCEL_OPTION,
164 JvOptionPane.PLAIN_MESSAGE, null, options,
165 MessageManager.getString("action.ok"));
169 * Restore initial settings on Cancel
171 protected void restoreInitialSettings()
175 ap.av.setTextColour(original1);
176 ap.av.setTextColour2(original2);
177 ap.av.setThresholdTextColour(originalThreshold);
181 sg.textColour = original1;
182 sg.textColour2 = original2;
183 sg.thresholdTextColour = originalThreshold;
187 * if 'Apply To All Groups' was in force, there will be
188 * group-specific settings to restore as well
190 for (SequenceGroup group : this.groupColour1.keySet())
192 group.textColour = groupColour1.get(group);
193 group.textColour2 = groupColour2.get(group);
194 group.thresholdTextColour = groupThreshold.get(group);
197 ap.paintAlignment(false, false);
201 * Save settings on entry, for restore on Cancel
203 protected void saveInitialSettings()
205 groupColour1 = new HashMap<>();
206 groupColour2 = new HashMap<>();
207 groupThreshold = new HashMap<>();
214 original1 = ap.av.getTextColour();
215 original2 = ap.av.getTextColour2();
216 originalThreshold = ap.av.getThresholdTextColour();
217 if (ap.av.getColourAppliesToAllGroups()
218 && ap.av.getAlignment().getGroups() != null)
221 * if applying changes to all groups, need to be able to
222 * restore group settings as well
224 for (SequenceGroup group : ap.av.getAlignment().getGroups())
226 groupColour1.put(group, group.textColour);
227 groupColour2.put(group, group.textColour2);
228 groupThreshold.put(group, group.thresholdTextColour);
235 * Sequence group scope
237 original1 = sg.textColour;
238 original2 = sg.textColour2;
239 originalThreshold = sg.thresholdTextColour;
243 void colour1Changed(Color col)
247 ap.av.setTextColour(col);
248 if (ap.av.getColourAppliesToAllGroups())
250 setGroupTextColour();
258 ap.paintAlignment(false, false);
261 void colour2Changed(Color col)
265 ap.av.setTextColour2(col);
266 if (ap.av.getColourAppliesToAllGroups())
268 setGroupTextColour();
273 sg.textColour2 = col;
276 ap.paintAlignment(false, false);
279 void thresholdChanged(int value)
283 ap.av.setThresholdTextColour(value);
284 if (ap.av.getColourAppliesToAllGroups())
286 setGroupTextColour();
291 sg.thresholdTextColour = value;
294 ap.paintAlignment(false, false);
297 void setGroupTextColour()
299 if (ap.av.getAlignment().getGroups() == null)
304 for (SequenceGroup group : ap.av.getAlignment().getGroups())
306 group.textColour = ap.av.getTextColour();
307 group.textColour2 = ap.av.getTextColour2();
308 group.thresholdTextColour = ap.av.getThresholdTextColour();