Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File TextColourChooser.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

20
91
13
1
311
231
24
0.26
7
13
1.85

Classes

Class Line # Actions
TextColourChooser 42 91 24
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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.gui.JalviewColourChooser.ColourChooserListener;
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    import java.util.HashMap;
33    import java.util.Map;
34   
35    import javax.swing.BorderFactory;
36    import javax.swing.JLabel;
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    Color original1, original2;
49   
50    int originalThreshold;
51   
52    Map<SequenceGroup, Color> groupColour1;
53   
54    Map<SequenceGroup, Color> groupColour2;
55   
56    Map<SequenceGroup, Integer> groupThreshold;
57   
58    /**
59    * Show a dialogue which allows the user to select two text colours and adjust
60    * a slider for the cross-over point
61    *
62    * @param alignPanel
63    * the AlignmentPanel context
64    * @param sequenceGroup
65    * the SequenceGroup context (only for group pop-menu option)
66    */
 
67  0 toggle public void chooseColour(AlignmentPanel alignPanel,
68    SequenceGroup sequenceGroup)
69    {
70  0 this.ap = alignPanel;
71  0 this.sg = sequenceGroup;
72   
73  0 saveInitialSettings();
74   
75  0 final JSlider slider = new JSlider(0, 750, originalThreshold);
76  0 final JPanel col1 = new JPanel();
77  0 col1.setPreferredSize(new Dimension(40, 20));
78  0 col1.setBorder(BorderFactory.createEtchedBorder());
79  0 col1.setToolTipText(MessageManager.getString("label.dark_colour"));
80  0 col1.setBackground(original1);
81  0 final JPanel col2 = new JPanel();
82  0 col2.setPreferredSize(new Dimension(40, 20));
83  0 col2.setBorder(BorderFactory.createEtchedBorder());
84  0 col2.setToolTipText(MessageManager.getString("label.light_colour"));
85  0 col2.setBackground(original2);
86  0 final JPanel bigpanel = new JPanel(new BorderLayout());
87  0 JPanel panel = new JPanel(new BorderLayout());
88  0 bigpanel.add(panel, BorderLayout.CENTER);
89  0 bigpanel.add(
90    new JLabel("<html>"
91    + MessageManager.getString(
92    "label.select_dark_light_set_threshold")
93    + "</html>"),
94    BorderLayout.NORTH);
95  0 panel.add(col1, BorderLayout.WEST);
96  0 panel.add(slider, BorderLayout.CENTER);
97  0 panel.add(col2, BorderLayout.EAST);
98   
99  0 col1.addMouseListener(new MouseAdapter()
100    {
 
101  0 toggle @Override
102    public void mousePressed(MouseEvent e)
103    {
104  0 String ttl = MessageManager.getString("label.select_colour_for_text");
105  0 ColourChooserListener listener = new ColourChooserListener()
106    {
 
107  0 toggle @Override
108    public void colourSelected(Color c)
109    {
110  0 colour1Changed(c);
111  0 col1.setBackground(c);
112    }
113    };
114  0 JalviewColourChooser.showColourChooser(bigpanel, ttl,
115    col1.getBackground(), listener);
116    }
117    });
118   
119  0 col2.addMouseListener(new MouseAdapter()
120    {
 
121  0 toggle @Override
122    public void mousePressed(MouseEvent e)
123    {
124  0 String ttl = MessageManager.getString("label.select_colour_for_text");
125  0 ColourChooserListener listener = new ColourChooserListener()
126    {
 
127  0 toggle @Override
128    public void colourSelected(Color c)
129    {
130  0 colour2Changed(c);
131  0 col2.setBackground(c);
132    }
133    };
134  0 JalviewColourChooser.showColourChooser(bigpanel, ttl,
135    col2.getBackground(), listener);
136    }
137    });
138   
139  0 slider.addChangeListener(new ChangeListener()
140    {
 
141  0 toggle @Override
142    public void stateChanged(ChangeEvent evt)
143    {
144  0 thresholdChanged(slider.getValue());
145    }
146    });
147   
148  0 Object[] options = new Object[] { MessageManager.getString("action.ok"),
149    MessageManager.getString("action.cancel") };
150  0 String title = MessageManager
151    .getString("label.adjust_foreground_text_colour_threshold");
152  0 Runnable action = new Runnable() // response for 1 = Cancel
153    {
 
154  0 toggle @Override
155    public void run()
156    {
157  0 restoreInitialSettings();
158    }
159    };
160  0 JvOptionPane.newOptionDialog(alignPanel).setResponseHandler(1, action)
161    .showInternalDialog(bigpanel, title,
162    JvOptionPane.YES_NO_CANCEL_OPTION,
163    JvOptionPane.PLAIN_MESSAGE, null, options,
164    MessageManager.getString("action.ok"));
165    }
166   
167    /**
168    * Restore initial settings on Cancel
169    */
 
170  0 toggle protected void restoreInitialSettings()
171    {
172  0 if (sg == null)
173    {
174  0 ap.av.setTextColour(original1);
175  0 ap.av.setTextColour2(original2);
176  0 ap.av.setThresholdTextColour(originalThreshold);
177    }
178    else
179    {
180  0 sg.textColour = original1;
181  0 sg.textColour2 = original2;
182  0 sg.thresholdTextColour = originalThreshold;
183    }
184   
185    /*
186    * if 'Apply To All Groups' was in force, there will be
187    * group-specific settings to restore as well
188    */
189  0 for (SequenceGroup group : this.groupColour1.keySet())
190    {
191  0 group.textColour = groupColour1.get(group);
192  0 group.textColour2 = groupColour2.get(group);
193  0 group.thresholdTextColour = groupThreshold.get(group);
194    }
195   
196  0 ap.paintAlignment(false, false);
197    }
198   
199    /**
200    * Save settings on entry, for restore on Cancel
201    */
 
202  0 toggle protected void saveInitialSettings()
203    {
204  0 groupColour1 = new HashMap<>();
205  0 groupColour2 = new HashMap<>();
206  0 groupThreshold = new HashMap<>();
207   
208  0 if (sg == null)
209    {
210    /*
211    * alignment scope
212    */
213  0 original1 = ap.av.getTextColour();
214  0 original2 = ap.av.getTextColour2();
215  0 originalThreshold = ap.av.getThresholdTextColour();
216  0 if (ap.av.getColourAppliesToAllGroups()
217    && ap.av.getAlignment().getGroups() != null)
218    {
219    /*
220    * if applying changes to all groups, need to be able to
221    * restore group settings as well
222    */
223  0 for (SequenceGroup group : ap.av.getAlignment().getGroups())
224    {
225  0 groupColour1.put(group, group.textColour);
226  0 groupColour2.put(group, group.textColour2);
227  0 groupThreshold.put(group, group.thresholdTextColour);
228    }
229    }
230    }
231    else
232    {
233    /*
234    * Sequence group scope
235    */
236  0 original1 = sg.textColour;
237  0 original2 = sg.textColour2;
238  0 originalThreshold = sg.thresholdTextColour;
239    }
240    }
241   
 
242  0 toggle void colour1Changed(Color col)
243    {
244  0 if (sg == null)
245    {
246  0 ap.av.setTextColour(col);
247  0 if (ap.av.getColourAppliesToAllGroups())
248    {
249  0 setGroupTextColour();
250    }
251    }
252    else
253    {
254  0 sg.textColour = col;
255    }
256   
257  0 ap.paintAlignment(false, false);
258    }
259   
 
260  0 toggle void colour2Changed(Color col)
261    {
262  0 if (sg == null)
263    {
264  0 ap.av.setTextColour2(col);
265  0 if (ap.av.getColourAppliesToAllGroups())
266    {
267  0 setGroupTextColour();
268    }
269    }
270    else
271    {
272  0 sg.textColour2 = col;
273    }
274   
275  0 ap.paintAlignment(false, false);
276    }
277   
 
278  0 toggle void thresholdChanged(int value)
279    {
280  0 if (sg == null)
281    {
282  0 ap.av.setThresholdTextColour(value);
283  0 if (ap.av.getColourAppliesToAllGroups())
284    {
285  0 setGroupTextColour();
286    }
287    }
288    else
289    {
290  0 sg.thresholdTextColour = value;
291    }
292   
293  0 ap.paintAlignment(false, false);
294    }
295   
 
296  0 toggle void setGroupTextColour()
297    {
298  0 if (ap.av.getAlignment().getGroups() == null)
299    {
300  0 return;
301    }
302   
303  0 for (SequenceGroup group : ap.av.getAlignment().getGroups())
304    {
305  0 group.textColour = ap.av.getTextColour();
306  0 group.textColour2 = ap.av.getTextColour2();
307  0 group.thresholdTextColour = ap.av.getThresholdTextColour();
308    }
309    }
310   
311    }