JAL-3853 JAL-1870 toggle to enable/disable posterior probability transparency when...
[jalview.git] / src / jalview / gui / AnnotationRowFilter.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.AlignmentAnnotation;
24 import jalview.datamodel.GraphLine;
25 import jalview.schemes.AnnotationColourGradient;
26 import jalview.util.MessageManager;
27
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.FocusAdapter;
33 import java.awt.event.FocusEvent;
34 import java.awt.event.ItemEvent;
35 import java.awt.event.ItemListener;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Vector;
41
42 import javax.swing.JButton;
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JInternalFrame;
46 import javax.swing.JPanel;
47 import javax.swing.JSlider;
48 import javax.swing.JTextField;
49 import javax.swing.event.ChangeEvent;
50 import javax.swing.event.ChangeListener;
51
52 @SuppressWarnings("serial")
53 public abstract class AnnotationRowFilter extends JPanel
54 {
55   protected AlignViewport av;
56
57   protected AlignmentPanel ap;
58
59   protected int[] annmap;
60
61   protected boolean adjusting = false;
62
63   protected JCheckBox seqAssociated = new JCheckBox();
64
65   protected JCheckBox percentThreshold = new JCheckBox();
66
67   protected JSlider slider = new JSlider();
68
69   protected JTextField thresholdValue = new JTextField(20);
70
71   protected JInternalFrame frame;
72
73   protected JButton ok = new JButton();
74
75   protected JButton cancel = new JButton();
76
77   /**
78    * enabled if the user is dragging the slider - try to keep updates to a
79    * minimun
80    */
81   protected boolean sliderDragging = false;
82
83   protected JComboBox<String> threshold = new JComboBox<>();
84
85   protected JComboBox<String> annotations;
86
87   /*
88    * map from annotation to its menu item display label
89    * - so we know which item to pre-select on restore
90    */
91   private Map<AlignmentAnnotation, String> annotationLabels;
92
93   private AlignmentAnnotation currentAnnotation;
94
95   /**
96    * Constructor
97    * 
98    * @param viewport
99    * @param alignPanel
100    */
101   public AnnotationRowFilter(AlignViewport viewport,
102           final AlignmentPanel alignPanel)
103   {
104     this.av = viewport;
105     this.ap = alignPanel;
106     thresholdValue.addFocusListener(new FocusAdapter()
107     {
108       @Override
109       public void focusLost(FocusEvent e)
110       {
111         thresholdValue_actionPerformed();
112       }
113     });
114   }
115
116   protected void addSliderChangeListener()
117   {
118
119     slider.addChangeListener(new ChangeListener()
120     {
121       @Override
122       public void stateChanged(ChangeEvent evt)
123       {
124         if (!adjusting)
125         {
126           setThresholdValueText();
127           valueChanged(!sliderDragging);
128         }
129       }
130     });
131   }
132
133   /**
134    * update the text field from the threshold slider. preserves state of
135    * 'adjusting' so safe to call in init.
136    */
137   protected void setThresholdValueText()
138   {
139     boolean oldadj = adjusting;
140     adjusting = true;
141     if (percentThreshold.isSelected())
142     {
143       thresholdValue.setText("" + (slider.getValue() - slider.getMinimum())
144               * 100f / (slider.getMaximum() - slider.getMinimum()));
145     }
146     else
147     {
148       thresholdValue.setText((slider.getValue() / 1000f) + "");
149     }
150     adjusting = oldadj;
151   }
152
153   protected void addSliderMouseListeners()
154   {
155
156     slider.addMouseListener(new MouseAdapter()
157     {
158       @Override
159       public void mousePressed(MouseEvent e)
160       {
161         sliderDragging = true;
162         super.mousePressed(e);
163       }
164
165       @Override
166       public void mouseDragged(MouseEvent e)
167       {
168         sliderDragging = true;
169         super.mouseDragged(e);
170       }
171
172       @Override
173       public void mouseReleased(MouseEvent evt)
174       {
175         sliderDragReleased();
176       }
177     });
178   }
179
180   /**
181    * Builds and returns a list of menu items (display text) for choice of
182    * annotation. Also builds maps between annotations, their positions in the
183    * list, and their display labels in the list.
184    * 
185    * @param isSeqAssociated
186    * @return
187    */
188   public Vector<String> getAnnotationItems(boolean isSeqAssociated)
189   {
190     annotationLabels = new HashMap<>();
191
192     Vector<String> list = new Vector<>();
193     int index = 1;
194     int[] anmap = new int[av.getAlignment()
195             .getAlignmentAnnotation().length];
196     seqAssociated.setEnabled(false);
197     for (int i = 0; i < av.getAlignment()
198             .getAlignmentAnnotation().length; i++)
199     {
200       AlignmentAnnotation annotation = av.getAlignment()
201               .getAlignmentAnnotation()[i];
202       if (annotation.sequenceRef == null)
203       {
204         if (isSeqAssociated)
205         {
206           continue;
207         }
208       }
209       else
210       {
211         seqAssociated.setEnabled(true);
212       }
213       String label = annotation.label;
214       // add associated sequence ID if available
215       if (!isSeqAssociated && annotation.sequenceRef != null)
216       {
217         label = label + "_" + annotation.sequenceRef.getName();
218       }
219       // make label unique
220       if (!list.contains(label))
221       {
222         anmap[list.size()] = i;
223         list.add(label);
224         annotationLabels.put(annotation, label);
225       }
226       else
227       {
228         if (!isSeqAssociated)
229         {
230           anmap[list.size()] = i;
231           label = label + "_" + (index++);
232           list.add(label);
233           annotationLabels.put(annotation, label);
234         }
235       }
236     }
237     this.annmap = new int[list.size()];
238     System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
239     return list;
240   }
241
242   protected int getSelectedThresholdItem(int indexValue)
243   {
244     int selectedThresholdItem = -1;
245     if (indexValue == 1)
246     {
247       selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
248     }
249     else if (indexValue == 2)
250     {
251       selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
252     }
253     return selectedThresholdItem;
254   }
255
256   public void ok_actionPerformed()
257   {
258     try
259     {
260       frame.setClosed(true);
261     } catch (Exception ex)
262     {
263     }
264   }
265
266   public void cancel_actionPerformed()
267   {
268     reset();
269     ap.paintAlignment(true, true);
270     try
271     {
272       frame.setClosed(true);
273     } catch (Exception ex)
274     {
275     }
276   }
277
278   protected void thresholdCheck_actionPerformed()
279   {
280     updateView();
281   }
282
283   protected void selectedAnnotationChanged()
284   {
285     updateView();
286   }
287
288   protected void threshold_actionPerformed()
289   {
290     updateView();
291   }
292
293   protected void thresholdValue_actionPerformed()
294   {
295     try
296     {
297       float f = Float.parseFloat(thresholdValue.getText());
298       if (percentThreshold.isSelected())
299       {
300         slider.setValue(slider.getMinimum() + ((int) ((f / 100f)
301                 * (slider.getMaximum() - slider.getMinimum()))));
302       }
303       else
304       {
305         slider.setValue((int) (f * 1000));
306       }
307       updateView();
308     } catch (NumberFormatException ex)
309     {
310     }
311   }
312
313   protected void percentageValue_actionPerformed()
314   {
315     setThresholdValueText();
316   }
317
318   protected void thresholdIsMin_actionPerformed()
319   {
320     updateView();
321   }
322
323   protected void populateThresholdComboBox(JComboBox<String> thresh)
324   {
325     thresh.addItem(MessageManager
326             .getString("label.threshold_feature_no_threshold"));
327     thresh.addItem(MessageManager
328             .getString("label.threshold_feature_above_threshold"));
329     thresh.addItem(MessageManager
330             .getString("label.threshold_feature_below_threshold"));
331   }
332
333   /**
334    * Rebuilds the drop-down list of annotations to choose from when the 'per
335    * sequence only' checkbox is checked or unchecked.
336    * 
337    * @param anns
338    */
339   protected void seqAssociated_actionPerformed(JComboBox<String> anns)
340   {
341     adjusting = true;
342     String cursel = (String) anns.getSelectedItem();
343     boolean isvalid = false;
344     boolean isseqs = seqAssociated.isSelected();
345     anns.removeAllItems();
346     for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
347     {
348       if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
349       {
350         isvalid = true;
351         cursel = anitem;
352       }
353       anns.addItem(anitem);
354     }
355     if (isvalid)
356     {
357       anns.setSelectedItem(cursel);
358     }
359     else
360     {
361       if (anns.getItemCount() > 0)
362       {
363         anns.setSelectedIndex(0);
364       }
365     }
366     adjusting = false;
367
368     updateView();
369   }
370   
371
372   protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
373           AlignmentAnnotation annotation)
374   {
375     if (annotation.sequenceRef == null || annotation.threshold == null)
376     {
377       return;
378     }
379
380     float thr = annotation.threshold.value;
381     for (int i = 0; i < av.getAlignment()
382             .getAlignmentAnnotation().length; i++)
383     {
384       AlignmentAnnotation aa = av.getAlignment()
385               .getAlignmentAnnotation()[i];
386       if (aa.label.equals(annotation.label)
387               && (annotation.getCalcId() == null ? aa.getCalcId() == null
388                       : annotation.getCalcId().equals(aa.getCalcId())))
389       {
390         if (aa.threshold == null)
391         {
392           aa.threshold = new GraphLine(annotation.threshold);
393         }
394         else
395         {
396           aa.threshold.value = thr;
397         }
398       }
399     }
400   }
401
402   public AlignmentAnnotation getCurrentAnnotation()
403   {
404     return currentAnnotation;
405   }
406
407   protected void setCurrentAnnotation(AlignmentAnnotation annotation)
408   {
409     this.currentAnnotation = annotation;
410   }
411
412   /**
413    * update associated view model and trigger any necessary repaints.
414    * 
415    * @param updateAllAnnotation
416    */
417   protected abstract void valueChanged(boolean updateAllAnnotation);
418
419   protected abstract void updateView();
420
421   protected abstract void reset();
422
423   protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
424   {
425     return annotationLabels.get(ann);
426   }
427
428   protected void jbInit()
429   {
430     ok.setOpaque(false);
431     ok.setText(MessageManager.getString("action.ok"));
432     ok.addActionListener(new ActionListener()
433     {
434       @Override
435       public void actionPerformed(ActionEvent e)
436       {
437         ok_actionPerformed();
438       }
439     });
440
441     cancel.setOpaque(false);
442     cancel.setText(MessageManager.getString("action.cancel"));
443     cancel.addActionListener(new ActionListener()
444     {
445       @Override
446       public void actionPerformed(ActionEvent e)
447       {
448         cancel_actionPerformed();
449       }
450     });
451
452     annotations.addItemListener(new ItemListener()
453     {
454       @Override
455       public void itemStateChanged(ItemEvent e)
456       {
457         selectedAnnotationChanged();
458       }
459     });
460     annotations.setToolTipText(
461             MessageManager.getString("info.select_annotation_row"));
462
463     threshold.addActionListener(new ActionListener()
464     {
465       @Override
466       public void actionPerformed(ActionEvent e)
467       {
468         threshold_actionPerformed();
469       }
470     });
471
472     thresholdValue.setEnabled(false);
473     thresholdValue.setColumns(7);
474     thresholdValue.addActionListener(new ActionListener()
475     {
476       @Override
477       public void actionPerformed(ActionEvent e)
478       {
479         thresholdValue_actionPerformed();
480       }
481     });
482
483     percentThreshold
484             .setText(MessageManager.getString("label.as_percentage"));
485     percentThreshold.addActionListener(new ActionListener()
486     {
487       @Override
488       public void actionPerformed(ActionEvent e)
489       {
490         if (!adjusting)
491         {
492           percentageValue_actionPerformed();
493         }
494       }
495     });
496     slider.setPaintLabels(false);
497     slider.setPaintTicks(true);
498     slider.setBackground(Color.white);
499     slider.setEnabled(false);
500     slider.setOpaque(false);
501     slider.setPreferredSize(new Dimension(100, 32));
502   }
503
504   public JComboBox<String> getThreshold()
505   {
506     return threshold;
507   }
508
509   public void setThreshold(JComboBox<String> thresh)
510   {
511     this.threshold = thresh;
512   }
513
514   public JComboBox<String> getAnnotations()
515   {
516     return annotations;
517   }
518
519   public void setAnnotations(JComboBox<String> anns)
520   {
521     this.annotations = anns;
522   }
523
524   protected void sliderDragReleased()
525   {
526     if (sliderDragging)
527     {
528       sliderDragging = false;
529       valueChanged(true);
530     }
531   }
532 }