Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 17:27:16 GMT
  2. Package jalview.gui

File SliderPanel.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

46
122
24
1
504
322
54
0.44
5.08
24
2.25

Classes

Class Line # Actions
SliderPanel 47 122 54
0.812581.2%
 

Contributing tests

This file is covered by 20 tests. .

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 java.awt.event.MouseAdapter;
24    import java.awt.event.MouseEvent;
25    import java.beans.PropertyVetoException;
26    import java.util.List;
27   
28    import javax.swing.JInternalFrame;
29    import javax.swing.JLayeredPane;
30    import javax.swing.event.ChangeEvent;
31    import javax.swing.event.ChangeListener;
32    import javax.swing.event.InternalFrameAdapter;
33    import javax.swing.event.InternalFrameEvent;
34   
35    import jalview.analysis.Conservation;
36    import jalview.datamodel.SequenceGroup;
37    import jalview.jbgui.GSliderPanel;
38    import jalview.renderer.ResidueShaderI;
39    import jalview.util.MessageManager;
40   
41    /**
42    * DOCUMENT ME!
43    *
44    * @author $author$
45    * @version $Revision$
46    */
 
47    public class SliderPanel extends GSliderPanel
48    {
49    private static final String BACKGROUND = "Background";
50   
51    static JInternalFrame conservationSlider;
52   
53    static JInternalFrame PIDSlider;
54   
55    AlignmentPanel ap;
56   
57    boolean forConservation = true;
58   
59    ResidueShaderI cs;
60   
61    /**
62    * Returns the currently displayed slider panel (or null if none).
63    *
64    * @return
65    */
 
66  19 toggle public static SliderPanel getSliderPanel()
67    {
68  19 if (conservationSlider != null && conservationSlider.isVisible())
69    {
70  10 return (SliderPanel) conservationSlider.getContentPane();
71    }
72  9 if (PIDSlider != null && PIDSlider.isVisible())
73    {
74  9 return (SliderPanel) PIDSlider.getContentPane();
75    }
76  0 return null;
77    }
78   
79    /**
80    * Creates a new SliderPanel object.
81    *
82    * @param ap
83    * DOCUMENT ME!
84    * @param value
85    * DOCUMENT ME!
86    * @param forConserve
87    * DOCUMENT ME!
88    * @param scheme
89    * DOCUMENT ME!
90    */
 
91  17 toggle public SliderPanel(final AlignmentPanel ap, int value,
92    boolean forConserve, ResidueShaderI scheme)
93    {
94  17 this.ap = ap;
95  17 this.cs = scheme;
96  17 forConservation = forConserve;
97  17 undoButton.setVisible(false);
98  17 applyButton.setVisible(false);
99   
100  17 if (forConservation)
101    {
102  7 label.setText(MessageManager.getString(
103    "label.enter_value_increase_conservation_visibility"));
104  7 slider.setMinimum(0);
105  7 slider.setMaximum(100);
106    }
107    else
108    {
109  10 label.setText(MessageManager.getString(
110    "label.enter_percentage_identity_above_which_colour_residues"));
111  10 slider.setMinimum(0);
112  10 slider.setMaximum(100);
113    }
114   
115  17 slider.addChangeListener(new ChangeListener()
116    {
 
117  17 toggle @Override
118    public void stateChanged(ChangeEvent evt)
119    {
120  17 valueField.setText(String.valueOf(slider.getValue()));
121  17 valueChanged(slider.getValue());
122    }
123    });
124   
125  17 slider.addMouseListener(new MouseAdapter()
126    {
 
127  0 toggle @Override
128    public void mouseReleased(MouseEvent evt)
129    {
130  0 ap.paintAlignment(true, true);
131    }
132    });
133   
134  17 slider.setValue(value);
135  17 valueField.setText(String.valueOf(value));
136    }
137   
138    /**
139    * Method to 'set focus' of the Conservation slider panel
140    *
141    * @param ap
142    * the panel to repaint on change of slider
143    * @param rs
144    * the colour scheme to update on change of slider
145    * @param source
146    * a text description for the panel's title
147    *
148    * @return
149    */
 
150  11 toggle public static int setConservationSlider(AlignmentPanel ap,
151    ResidueShaderI rs, String source)
152    {
153  11 SliderPanel sliderPanel = null;
154   
155  11 if (conservationSlider == null)
156    {
157  7 sliderPanel = new SliderPanel(ap, rs.getConservationInc(), true, rs);
158  7 conservationSlider = new JInternalFrame();
159  7 conservationSlider.setFrameIcon(null);
160  7 conservationSlider.setContentPane(sliderPanel);
161  7 conservationSlider.setLayer(JLayeredPane.PALETTE_LAYER);
162    }
163    else
164    {
165  4 sliderPanel = (SliderPanel) conservationSlider.getContentPane();
166  4 sliderPanel.valueField
167    .setText(String.valueOf(rs.getConservationInc()));
168  4 sliderPanel.cs = rs;
169  4 sliderPanel.ap = ap;
170  4 sliderPanel.slider.setValue(rs.getConservationInc());
171    }
172   
173  11 conservationSlider.setTitle(MessageManager.formatMessage(
174    "label.conservation_colour_increment", new String[]
175  11 { source == null ? BACKGROUND : source }));
176   
177  11 List<SequenceGroup> groups = ap.av.getAlignment().getGroups();
178  11 if (groups != null && !groups.isEmpty())
179    {
180  6 sliderPanel.setAllGroupsCheckEnabled(true);
181  6 sliderPanel.allGroupsCheck
182    .setSelected(ap.av.getColourAppliesToAllGroups());
183    }
184    else
185    {
186  5 sliderPanel.setAllGroupsCheckEnabled(false);
187    }
188   
189  11 return sliderPanel.getValue();
190    }
191   
192    /**
193    * Hides the PID slider panel if it is shown
194    */
 
195  170 toggle public static void hidePIDSlider()
196    {
197  170 if (PIDSlider != null)
198    {
199  8 try
200    {
201  8 PIDSlider.setClosed(true);
202  8 PIDSlider = null;
203    } catch (PropertyVetoException ex)
204    {
205    }
206    }
207    }
208   
209    /**
210    * Hides the conservation slider panel if it is shown
211    */
 
212  169 toggle public static void hideConservationSlider()
213    {
214  169 if (conservationSlider != null)
215    {
216  6 try
217    {
218  6 conservationSlider.setClosed(true);
219  6 conservationSlider = null;
220    } catch (PropertyVetoException ex)
221    {
222    }
223    }
224    }
225   
226    /**
227    * DOCUMENT ME!
228    */
 
229  9 toggle public static void showConservationSlider()
230    {
231  9 hidePIDSlider();
232   
233  9 if (!conservationSlider.isVisible())
234    {
235  7 Desktop.addInternalFrame(conservationSlider,
236    conservationSlider.getTitle(), true, FRAME_WIDTH,
237    FRAME_HEIGHT, false, true);
238  7 conservationSlider.addInternalFrameListener(new InternalFrameAdapter()
239    {
 
240  7 toggle @Override
241    public void internalFrameClosed(InternalFrameEvent e)
242    {
243  7 conservationSlider = null;
244    }
245    });
246  7 conservationSlider.setLayer(JLayeredPane.PALETTE_LAYER);
247    }
248    }
249   
250    /**
251    * Method to 'set focus' of the PID slider panel
252    *
253    * @param ap
254    * the panel to repaint on change of slider
255    * @param rs
256    * the colour scheme to update on change of slider
257    * @param source
258    * a text description for the panel's title
259    *
260    * @return
261    */
 
262  11 toggle public static int setPIDSliderSource(AlignmentPanel ap, ResidueShaderI rs,
263    String source)
264    {
265  11 int threshold = rs.getThreshold();
266   
267  11 SliderPanel sliderPanel = null;
268   
269  11 if (PIDSlider == null)
270    {
271  10 sliderPanel = new SliderPanel(ap, threshold, false, rs);
272  10 PIDSlider = new JInternalFrame();
273  10 PIDSlider.setFrameIcon(null);
274  10 PIDSlider.setContentPane(sliderPanel);
275  10 PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
276    }
277    else
278    {
279  1 sliderPanel = (SliderPanel) PIDSlider.getContentPane();
280  1 sliderPanel.cs = rs;
281  1 sliderPanel.ap = ap;
282  1 sliderPanel.valueField.setText(String.valueOf(rs.getThreshold()));
283  1 sliderPanel.slider.setValue(rs.getThreshold());
284    }
285   
286  11 PIDSlider.setTitle(MessageManager.formatMessage(
287    "label.percentage_identity_threshold", new String[]
288  11 { source == null ? BACKGROUND : source }));
289   
290  11 if (ap.av.getAlignment().getGroups() != null)
291    {
292  11 sliderPanel.setAllGroupsCheckEnabled(true);
293    }
294    else
295    {
296  0 sliderPanel.setAllGroupsCheckEnabled(false);
297    }
298   
299  11 return sliderPanel.getValue();
300    }
301   
302    /**
303    * DOCUMENT ME!
304    *
305    * @return
306    */
 
307  9 toggle public static JInternalFrame showPIDSlider()
308    {
309  9 hideConservationSlider();
310   
311  9 if (!PIDSlider.isVisible())
312    {
313  9 Desktop.addInternalFrame(PIDSlider, PIDSlider.getTitle(), true,
314    FRAME_WIDTH, FRAME_HEIGHT, false, true);
315  9 PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
316  9 PIDSlider.addInternalFrameListener(new InternalFrameAdapter()
317    {
 
318  9 toggle @Override
319    public void internalFrameClosed(InternalFrameEvent e)
320    {
321  9 PIDSlider = null;
322    }
323    });
324  9 PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
325    }
326  9 return PIDSlider;
327    }
328   
329    /**
330    * Updates the colour scheme with the current (identity threshold or
331    * conservation) percentage value. Also updates all groups if 'apply to all
332    * groups' is selected.
333    *
334    * @param percent
335    */
 
336  37 toggle public void valueChanged(int percent)
337    {
338  37 if (!forConservation)
339    {
340  21 ap.av.setThreshold(percent);
341    }
342  37 updateColourScheme(percent, cs, null);
343   
344  37 if (allGroupsCheck.isSelected())
345    {
346  2 List<SequenceGroup> groups = ap.av.getAlignment().getGroups();
347  2 for (SequenceGroup sg : groups)
348    {
349  2 updateColourScheme(percent, sg.getGroupColourScheme(), sg);
350    }
351    }
352   
353  37 ap.getSeqPanel().seqCanvas.repaint();
354    }
355   
356    /**
357    * Updates the colour scheme (if not null) with the current (identity
358    * threshold or conservation) percentage value
359    *
360    * @param percent
361    * @param scheme
362    * @param sg
363    */
 
364  39 toggle protected void updateColourScheme(int percent, ResidueShaderI scheme,
365    SequenceGroup sg)
366    {
367  39 if (scheme == null)
368    {
369  0 return;
370    }
371  39 if (forConservation)
372    {
373  18 if (!scheme.conservationApplied() && sg != null)
374    {
375    /*
376    * first time the colour scheme has had Conservation shading applied
377    * - compute conservation
378    */
379  0 Conservation c = new Conservation("Group", sg.getSequences(null),
380    sg.getStartRes(), sg.getEndRes());
381  0 c.calculate();
382  0 c.verdict(false, ap.av.getConsPercGaps());
383  0 sg.cs.setConservation(c);
384   
385    }
386  18 scheme.setConservationApplied(true);
387  18 scheme.setConservationInc(percent);
388    }
389    else
390    {
391  21 scheme.setThreshold(percent, ap.av.isIgnoreGapsConsensus());
392    }
393    }
394   
395    /**
396    * DOCUMENT ME!
397    *
398    * @param b
399    * DOCUMENT ME!
400    */
 
401  22 toggle public void setAllGroupsCheckEnabled(boolean b)
402    {
403  22 allGroupsCheck.setEnabled(b);
404    }
405   
406    /**
407    * DOCUMENT ME!
408    *
409    * @param e
410    * DOCUMENT ME!
411    */
 
412  0 toggle @Override
413    public void valueField_actionPerformed()
414    {
415  0 try
416    {
417  0 int i = Integer.parseInt(valueField.getText());
418  0 slider.setValue(i);
419    } catch (NumberFormatException ex)
420    {
421  0 valueField.setText(slider.getValue() + "");
422    }
423    }
424   
425    /**
426    * DOCUMENT ME!
427    *
428    * @param value
429    * DOCUMENT ME!
430    */
 
431  0 toggle public void setValue(int value)
432    {
433  0 slider.setValue(value);
434    }
435   
436    /**
437    * DOCUMENT ME!
438    *
439    * @return DOCUMENT ME!
440    */
 
441  24 toggle public int getValue()
442    {
443  24 return Integer.parseInt(valueField.getText());
444    }
445   
 
446  0 toggle @Override
447    public void slider_mouseReleased(MouseEvent e)
448    {
449  0 if (ap.overviewPanel != null)
450    {
451  0 ap.overviewPanel.updateOverviewImage();
452    }
453    }
454   
 
455  0 toggle public static int getConservationValue()
456    {
457  0 return getValue(conservationSlider);
458    }
459   
 
460  0 toggle static int getValue(JInternalFrame slider)
461    {
462  0 return slider == null ? 0
463    : ((SliderPanel) slider.getContentPane()).getValue();
464    }
465   
 
466  0 toggle public static int getPIDValue()
467    {
468  0 return getValue(PIDSlider);
469    }
470   
471    /**
472    * Answers true if the SliderPanel is for Conservation, false if it is for PID
473    * threshold
474    *
475    * @return
476    */
 
477  24 toggle public boolean isForConservation()
478    {
479  24 return forConservation;
480    }
481   
482    /**
483    * Answers the title for the slider panel; this may include 'Background' if
484    * for the alignment, or the group id if for a group
485    *
486    * @return
487    */
 
488  6 toggle public String getTitle()
489    {
490  6 String title = null;
491  6 if (isForConservation())
492    {
493  4 if (conservationSlider != null)
494    {
495  4 title = conservationSlider.getTitle();
496    }
497    }
498  2 else if (PIDSlider != null)
499    {
500  2 title = PIDSlider.getTitle();
501    }
502  6 return title;
503    }
504    }