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.AlignmentAnnotation;
24 import jalview.datamodel.GraphLine;
25 import jalview.schemes.AnnotationColourGradient;
26 import jalview.util.MessageManager;
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;
40 import java.util.Vector;
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;
52 @SuppressWarnings("serial")
53 public abstract class AnnotationRowFilter extends JPanel
55 protected AlignViewport av;
57 protected AlignmentPanel ap;
59 protected int[] annmap;
61 protected boolean adjusting = false;
63 protected JCheckBox seqAssociated = new JCheckBox();
65 protected JCheckBox percentThreshold = new JCheckBox();
67 protected JSlider slider = new JSlider();
69 protected JTextField thresholdValue = new JTextField(20);
71 protected JInternalFrame frame;
73 protected JButton ok = new JButton();
75 protected JButton cancel = new JButton();
78 * enabled if the user is dragging the slider - try to keep updates to a
81 protected boolean sliderDragging = false;
83 protected JComboBox<String> threshold = new JComboBox<>();
85 protected JComboBox<String> annotations;
88 * map from annotation to its menu item display label
89 * - so we know which item to pre-select on restore
91 private Map<AlignmentAnnotation, String> annotationLabels;
93 private AlignmentAnnotation currentAnnotation;
101 public AnnotationRowFilter(AlignViewport viewport,
102 final AlignmentPanel alignPanel)
105 this.ap = alignPanel;
106 thresholdValue.addFocusListener(new FocusAdapter()
109 public void focusLost(FocusEvent e)
111 thresholdValue_actionPerformed();
116 protected void addSliderChangeListener()
119 slider.addChangeListener(new ChangeListener()
122 public void stateChanged(ChangeEvent evt)
126 setThresholdValueText();
127 valueChanged(!sliderDragging);
134 * update the text field from the threshold slider. preserves state of
135 * 'adjusting' so safe to call in init.
137 protected void setThresholdValueText()
139 boolean oldadj = adjusting;
141 if (percentThreshold.isSelected())
143 thresholdValue.setText("" + (slider.getValue() - slider.getMinimum())
144 * 100f / (slider.getMaximum() - slider.getMinimum()));
148 thresholdValue.setText((slider.getValue() / 1000f) + "");
153 protected void addSliderMouseListeners()
156 slider.addMouseListener(new MouseAdapter()
159 public void mousePressed(MouseEvent e)
161 sliderDragging = true;
162 super.mousePressed(e);
166 public void mouseDragged(MouseEvent e)
168 sliderDragging = true;
169 super.mouseDragged(e);
173 public void mouseReleased(MouseEvent evt)
177 sliderDragging = false;
185 * Builds and returns a list of menu items (display text) for choice of
186 * annotation. Also builds maps between annotations, their positions in the
187 * list, and their display labels in the list.
189 * @param isSeqAssociated
192 public Vector<String> getAnnotationItems(boolean isSeqAssociated)
194 annotationLabels = new HashMap<>();
196 Vector<String> list = new Vector<>();
198 int[] anmap = new int[av.getAlignment()
199 .getAlignmentAnnotation().length];
200 seqAssociated.setEnabled(false);
201 for (int i = 0; i < av.getAlignment()
202 .getAlignmentAnnotation().length; i++)
204 AlignmentAnnotation annotation = av.getAlignment()
205 .getAlignmentAnnotation()[i];
206 if (annotation.sequenceRef == null)
215 seqAssociated.setEnabled(true);
217 String label = annotation.label;
218 // add associated sequence ID if available
219 if (!isSeqAssociated && annotation.sequenceRef != null)
221 label = label + "_" + annotation.sequenceRef.getName();
224 if (!list.contains(label))
226 anmap[list.size()] = i;
228 annotationLabels.put(annotation, label);
232 if (!isSeqAssociated)
234 anmap[list.size()] = i;
235 label = label + "_" + (index++);
237 annotationLabels.put(annotation, label);
241 this.annmap = new int[list.size()];
242 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
246 protected int getSelectedThresholdItem(int indexValue)
248 int selectedThresholdItem = -1;
251 selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
253 else if (indexValue == 2)
255 selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
257 return selectedThresholdItem;
260 public void ok_actionPerformed()
264 frame.setClosed(true);
265 } catch (Exception ex)
270 public void cancel_actionPerformed()
273 ap.paintAlignment(true, true);
276 frame.setClosed(true);
277 } catch (Exception ex)
282 protected void thresholdCheck_actionPerformed()
287 protected void selectedAnnotationChanged()
292 protected void threshold_actionPerformed()
297 protected void thresholdValue_actionPerformed()
301 float f = Float.parseFloat(thresholdValue.getText());
302 if (percentThreshold.isSelected())
304 slider.setValue(slider.getMinimum() + ((int) ((f / 100f)
305 * (slider.getMaximum() - slider.getMinimum()))));
309 slider.setValue((int) (f * 1000));
312 } catch (NumberFormatException ex)
317 protected void percentageValue_actionPerformed()
319 setThresholdValueText();
322 protected void thresholdIsMin_actionPerformed()
327 protected void populateThresholdComboBox(JComboBox<String> thresh)
329 thresh.addItem(MessageManager
330 .getString("label.threshold_feature_no_threshold"));
331 thresh.addItem(MessageManager
332 .getString("label.threshold_feature_above_threshold"));
333 thresh.addItem(MessageManager
334 .getString("label.threshold_feature_below_threshold"));
338 * Rebuilds the drop-down list of annotations to choose from when the 'per
339 * sequence only' checkbox is checked or unchecked.
343 protected void seqAssociated_actionPerformed(JComboBox<String> anns)
346 String cursel = (String) anns.getSelectedItem();
347 boolean isvalid = false;
348 boolean isseqs = seqAssociated.isSelected();
349 anns.removeAllItems();
350 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
352 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
357 anns.addItem(anitem);
361 anns.setSelectedItem(cursel);
365 if (anns.getItemCount() > 0)
367 anns.setSelectedIndex(0);
375 protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
376 AlignmentAnnotation annotation)
378 if (annotation.sequenceRef == null || annotation.threshold == null)
383 float thr = annotation.threshold.value;
384 for (int i = 0; i < av.getAlignment()
385 .getAlignmentAnnotation().length; i++)
387 AlignmentAnnotation aa = av.getAlignment()
388 .getAlignmentAnnotation()[i];
389 if (aa.label.equals(annotation.label)
390 && (annotation.getCalcId() == null ? aa.getCalcId() == null
391 : annotation.getCalcId().equals(aa.getCalcId())))
393 if (aa.threshold == null)
395 aa.threshold = new GraphLine(annotation.threshold);
399 aa.threshold.value = thr;
405 public AlignmentAnnotation getCurrentAnnotation()
407 return currentAnnotation;
410 protected void setCurrentAnnotation(AlignmentAnnotation annotation)
412 this.currentAnnotation = annotation;
416 * update associated view model and trigger any necessary repaints.
418 * @param updateAllAnnotation
420 protected abstract void valueChanged(boolean updateAllAnnotation);
422 protected abstract void updateView();
424 protected abstract void reset();
426 protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
428 return annotationLabels.get(ann);
431 protected void jbInit()
434 ok.setText(MessageManager.getString("action.ok"));
435 ok.addActionListener(new ActionListener()
438 public void actionPerformed(ActionEvent e)
440 ok_actionPerformed();
444 cancel.setOpaque(false);
445 cancel.setText(MessageManager.getString("action.cancel"));
446 cancel.addActionListener(new ActionListener()
449 public void actionPerformed(ActionEvent e)
451 cancel_actionPerformed();
455 annotations.addItemListener(new ItemListener()
458 public void itemStateChanged(ItemEvent e)
460 selectedAnnotationChanged();
463 annotations.setToolTipText(
464 MessageManager.getString("info.select_annotation_row"));
466 threshold.addActionListener(new ActionListener()
469 public void actionPerformed(ActionEvent e)
471 threshold_actionPerformed();
475 thresholdValue.setEnabled(false);
476 thresholdValue.setColumns(7);
477 thresholdValue.addActionListener(new ActionListener()
480 public void actionPerformed(ActionEvent e)
482 thresholdValue_actionPerformed();
487 .setText(MessageManager.getString("label.as_percentage"));
488 percentThreshold.addActionListener(new ActionListener()
491 public void actionPerformed(ActionEvent e)
495 percentageValue_actionPerformed();
499 slider.setPaintLabels(false);
500 slider.setPaintTicks(true);
501 slider.setBackground(Color.white);
502 slider.setEnabled(false);
503 slider.setOpaque(false);
504 slider.setPreferredSize(new Dimension(100, 32));
507 public JComboBox<String> getThreshold()
512 public void setThreshold(JComboBox<String> thresh)
514 this.threshold = thresh;
517 public JComboBox<String> getAnnotations()
522 public void setAnnotations(JComboBox<String> anns)
524 this.annotations = anns;