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 AlignmentAnnotation currentAnnotation;
99 public AnnotationRowFilter(AlignViewport viewport, final AlignmentPanel alignPanel)
102 this.ap = alignPanel;
103 thresholdValue.addFocusListener(new FocusAdapter()
106 public void focusLost(FocusEvent e)
108 thresholdValue_actionPerformed();
113 protected void addSliderChangeListener()
116 slider.addChangeListener(new ChangeListener()
119 public void stateChanged(ChangeEvent evt)
123 thresholdValue.setText((slider.getValue() / 1000f) + "");
124 valueChanged(!sliderDragging);
130 protected void addSliderMouseListeners()
133 slider.addMouseListener(new MouseAdapter()
136 public void mousePressed(MouseEvent e)
138 sliderDragging = true;
139 super.mousePressed(e);
143 public void mouseDragged(MouseEvent e)
145 sliderDragging = true;
146 super.mouseDragged(e);
150 public void mouseReleased(MouseEvent evt)
154 sliderDragging = false;
157 ap.paintAlignment(true);
163 * Builds and returns a list of menu items (display text) for choice of
164 * annotation. Also builds maps between annotations, their positions in the
165 * list, and their display labels in the list.
167 * @param isSeqAssociated
170 public Vector<String> getAnnotationItems(boolean isSeqAssociated)
172 annotationLabels = new HashMap<AlignmentAnnotation, String>();
174 Vector<String> list = new Vector<String>();
176 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
177 seqAssociated.setEnabled(false);
178 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
180 AlignmentAnnotation annotation = av.getAlignment()
181 .getAlignmentAnnotation()[i];
182 if (annotation.sequenceRef == null)
191 seqAssociated.setEnabled(true);
193 String label = annotation.label;
194 // add associated sequence ID if available
195 if (!isSeqAssociated && annotation.sequenceRef != null)
197 label = label + "_" + annotation.sequenceRef.getName();
200 if (!list.contains(label))
202 anmap[list.size()] = i;
204 annotationLabels.put(annotation, label);
208 if (!isSeqAssociated)
210 anmap[list.size()] = i;
211 label = label + "_" + (index++);
213 annotationLabels.put(annotation, label);
217 this.annmap = new int[list.size()];
218 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
222 protected int getSelectedThresholdItem(int indexValue)
224 int selectedThresholdItem = -1;
227 selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
229 else if (indexValue == 2)
231 selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
233 return selectedThresholdItem;
236 public void ok_actionPerformed()
240 frame.setClosed(true);
241 } catch (Exception ex)
246 public void cancel_actionPerformed()
249 ap.paintAlignment(true);
252 frame.setClosed(true);
253 } catch (Exception ex)
258 protected void thresholdCheck_actionPerformed()
263 protected void selectedAnnotationChanged()
268 protected void threshold_actionPerformed()
273 protected void thresholdValue_actionPerformed()
277 float f = Float.parseFloat(thresholdValue.getText());
278 slider.setValue((int) (f * 1000));
280 } catch (NumberFormatException ex)
285 protected void thresholdIsMin_actionPerformed()
290 protected void populateThresholdComboBox(JComboBox<String> thresh)
292 thresh.addItem(MessageManager
293 .getString("label.threshold_feature_no_threshold"));
294 thresh.addItem(MessageManager
295 .getString("label.threshold_feature_above_threshold"));
296 thresh.addItem(MessageManager
297 .getString("label.threshold_feature_below_threshold"));
301 * Rebuilds the drop-down list of annotations to choose from when the 'per
302 * sequence only' checkbox is checked or unchecked.
306 protected void seqAssociated_actionPerformed(JComboBox<String> anns)
309 String cursel = (String) anns.getSelectedItem();
310 boolean isvalid = false;
311 boolean isseqs = seqAssociated.isSelected();
312 anns.removeAllItems();
313 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
315 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
320 anns.addItem(anitem);
324 anns.setSelectedItem(cursel);
328 if (anns.getItemCount() > 0)
330 anns.setSelectedIndex(0);
338 protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
339 AlignmentAnnotation annotation)
341 if (annotation.sequenceRef == null || annotation.threshold == null)
346 float thr = annotation.threshold.value;
347 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
349 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
350 if (aa.label.equals(annotation.label)
351 && (annotation.getCalcId() == null ? aa.getCalcId() == null
352 : annotation.getCalcId().equals(aa.getCalcId())))
354 if (aa.threshold == null)
356 aa.threshold = new GraphLine(annotation.threshold);
360 aa.threshold.value = thr;
366 public AlignmentAnnotation getCurrentAnnotation()
368 return currentAnnotation;
371 protected void setCurrentAnnotation(AlignmentAnnotation annotation)
373 this.currentAnnotation = annotation;
376 protected abstract void valueChanged(boolean updateAllAnnotation);
378 protected abstract void updateView();
380 protected abstract void reset();
382 protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
384 return annotationLabels.get(ann);
387 protected void jbInit()
390 ok.setText(MessageManager.getString("action.ok"));
391 ok.addActionListener(new ActionListener()
394 public void actionPerformed(ActionEvent e)
396 ok_actionPerformed();
400 cancel.setOpaque(false);
401 cancel.setText(MessageManager.getString("action.cancel"));
402 cancel.addActionListener(new ActionListener()
405 public void actionPerformed(ActionEvent e)
407 cancel_actionPerformed();
411 annotations.addItemListener(new ItemListener()
414 public void itemStateChanged(ItemEvent e)
416 selectedAnnotationChanged();
419 annotations.setToolTipText(MessageManager
420 .getString("info.select_annotation_row"));
422 threshold.addActionListener(new ActionListener()
425 public void actionPerformed(ActionEvent e)
427 threshold_actionPerformed();
431 thresholdValue.setEnabled(false);
432 thresholdValue.setColumns(7);
433 thresholdValue.addActionListener(new ActionListener()
436 public void actionPerformed(ActionEvent e)
438 thresholdValue_actionPerformed();
442 slider.setPaintLabels(false);
443 slider.setPaintTicks(true);
444 slider.setBackground(Color.white);
445 slider.setEnabled(false);
446 slider.setOpaque(false);
447 slider.setPreferredSize(new Dimension(100, 32));
450 public JComboBox<String> getThreshold()
455 public void setThreshold(JComboBox<String> thresh)
457 this.threshold = thresh;
460 public JComboBox<String> getAnnotations()
465 public void setAnnotations(JComboBox<String> anns)
467 this.annotations = anns;