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 JSlider slider = new JSlider();
67 protected JTextField thresholdValue = new JTextField(20);
69 protected JInternalFrame frame;
71 protected JButton ok = new JButton();
73 protected JButton cancel = new JButton();
76 * enabled if the user is dragging the slider - try to keep updates to a
79 protected boolean sliderDragging = false;
81 protected JComboBox<String> threshold = new JComboBox<String>();
83 protected JComboBox<String> annotations;
86 * map from annotation to its menu item display label
87 * - so we know which item to pre-select on restore
89 private Map<AlignmentAnnotation, String> annotationLabels;
91 private boolean enableSeqAss = false;
93 private AlignmentAnnotation currentAnnotation;
101 public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
105 thresholdValue.addFocusListener(new FocusAdapter()
108 public void focusLost(FocusEvent e)
110 thresholdValue_actionPerformed();
115 protected void addSliderChangeListener()
118 slider.addChangeListener(new ChangeListener()
121 public void stateChanged(ChangeEvent evt)
125 thresholdValue.setText((slider.getValue() / 1000f) + "");
126 valueChanged(!sliderDragging);
132 protected void addSliderMouseListeners()
135 slider.addMouseListener(new MouseAdapter()
138 public void mousePressed(MouseEvent e)
140 sliderDragging = true;
141 super.mousePressed(e);
145 public void mouseDragged(MouseEvent e)
147 sliderDragging = true;
148 super.mouseDragged(e);
152 public void mouseReleased(MouseEvent evt)
156 sliderDragging = false;
159 ap.paintAlignment(true);
165 * Builds and returns a list of menu items (display text) for choice of
166 * annotation. Also builds maps between annotations, their positions in the
167 * list, and their display labels in the list.
169 * @param isSeqAssociated
172 public Vector<String> getAnnotationItems(boolean isSeqAssociated)
174 annotationLabels = new HashMap<AlignmentAnnotation, String>();
176 Vector<String> list = new Vector<String>();
178 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
179 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
181 AlignmentAnnotation annotation = av.getAlignment()
182 .getAlignmentAnnotation()[i];
183 if (annotation.sequenceRef == null)
194 String label = annotation.label;
195 // add associated sequence ID if available
196 if (!isSeqAssociated && annotation.sequenceRef != null)
198 label = label + "_" + annotation.sequenceRef.getName();
201 if (!list.contains(label))
203 anmap[list.size()] = i;
205 annotationLabels.put(annotation, label);
209 if (!isSeqAssociated)
211 anmap[list.size()] = i;
212 label = label + "_" + (index++);
214 annotationLabels.put(annotation, label);
218 this.annmap = new int[list.size()];
219 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
223 protected int getSelectedThresholdItem(int indexValue)
225 int selectedThresholdItem = -1;
228 selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
230 else if (indexValue == 2)
232 selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
234 return selectedThresholdItem;
237 public void modelChanged()
239 seqAssociated.setEnabled(enableSeqAss);
242 public void ok_actionPerformed()
246 frame.setClosed(true);
247 } catch (Exception ex)
252 public void cancel_actionPerformed()
255 ap.paintAlignment(true);
258 frame.setClosed(true);
259 } catch (Exception ex)
264 protected void thresholdCheck_actionPerformed()
269 protected void selectedAnnotationChanged()
274 protected void threshold_actionPerformed()
279 protected void thresholdValue_actionPerformed()
283 float f = Float.parseFloat(thresholdValue.getText());
284 slider.setValue((int) (f * 1000));
286 } catch (NumberFormatException ex)
291 protected void thresholdIsMin_actionPerformed()
296 protected void populateThresholdComboBox(JComboBox<String> thresh)
298 thresh.addItem(MessageManager
299 .getString("label.threshold_feature_no_threshold"));
300 thresh.addItem(MessageManager
301 .getString("label.threshold_feature_above_threshold"));
302 thresh.addItem(MessageManager
303 .getString("label.threshold_feature_below_threshold"));
307 * Rebuilds the drop-down list of annotations to choose from when the 'per
308 * sequence only' checkbox is checked or unchecked.
312 protected void seqAssociated_actionPerformed(JComboBox<String> anns)
315 String cursel = (String) anns.getSelectedItem();
316 boolean isvalid = false, isseqs = seqAssociated.isSelected();
317 anns.removeAllItems();
318 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
320 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
325 anns.addItem(anitem);
330 anns.setSelectedItem(cursel);
334 if (anns.getItemCount() > 0)
336 anns.setSelectedIndex(0);
341 protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
342 AlignmentAnnotation annotation)
344 if (annotation.sequenceRef == null || annotation.threshold == null)
349 float thr = annotation.threshold.value;
350 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
352 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
353 if (aa.label.equals(annotation.label)
354 && (annotation.getCalcId() == null ? aa.getCalcId() == null
355 : annotation.getCalcId().equals(aa.getCalcId())))
357 if (aa.threshold == null)
359 aa.threshold = new GraphLine(annotation.threshold);
363 aa.threshold.value = thr;
369 protected AlignmentAnnotation getCurrentAnnotation()
371 return currentAnnotation;
374 protected void setCurrentAnnotation(AlignmentAnnotation currentAnnotation)
376 this.currentAnnotation = currentAnnotation;
379 protected abstract void valueChanged(boolean updateAllAnnotation);
381 protected abstract void updateView();
383 protected abstract void reset();
385 protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
387 return annotationLabels.get(ann);
390 protected void jbInit()
393 ok.setText(MessageManager.getString("action.ok"));
394 ok.addActionListener(new ActionListener()
397 public void actionPerformed(ActionEvent e)
399 ok_actionPerformed();
403 cancel.setOpaque(false);
404 cancel.setText(MessageManager.getString("action.cancel"));
405 cancel.addActionListener(new ActionListener()
408 public void actionPerformed(ActionEvent e)
410 cancel_actionPerformed();
414 annotations.addItemListener(new ItemListener()
417 public void itemStateChanged(ItemEvent e)
419 selectedAnnotationChanged();
422 annotations.setToolTipText(MessageManager
423 .getString("info.select_annotation_row"));
425 threshold.addActionListener(new ActionListener()
428 public void actionPerformed(ActionEvent e)
430 threshold_actionPerformed();
434 thresholdValue.setEnabled(false);
435 thresholdValue.setColumns(7);
436 thresholdValue.addActionListener(new ActionListener()
439 public void actionPerformed(ActionEvent e)
441 thresholdValue_actionPerformed();
445 slider.setPaintLabels(false);
446 slider.setPaintTicks(true);
447 slider.setBackground(Color.white);
448 slider.setEnabled(false);
449 slider.setOpaque(false);
450 slider.setPreferredSize(new Dimension(100, 32));
453 public JComboBox<String> getThreshold()
458 public void setThreshold(JComboBox<String> thresh)
460 this.threshold = thresh;
463 public JComboBox<String> getAnnotations()
468 public void setAnnotations(JComboBox<String> anns)
470 this.annotations = anns;