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 currentColours = new JCheckBox();
65 protected JPanel minColour = new JPanel();
67 protected JPanel maxColour = new JPanel();
69 protected JCheckBox seqAssociated = new JCheckBox();
71 protected JCheckBox thresholdIsMin = new JCheckBox();
73 protected JSlider slider = new JSlider();
75 protected JTextField thresholdValue = new JTextField(20);
77 protected JInternalFrame frame;
79 protected JButton ok = new JButton();
81 protected JButton cancel = new JButton();
84 * enabled if the user is dragging the slider - try to keep updates to a
87 protected boolean sliderDragging = false;
89 protected JComboBox<String> threshold = new JComboBox<String>();
91 protected JComboBox<String> annotations;
94 * map from annotation to its menu item display label
95 * - so we know which item to pre-select on restore
97 private Map<AlignmentAnnotation, String> annotationLabels;
99 private AlignmentAnnotation currentAnnotation;
107 public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
111 thresholdValue.addFocusListener(new FocusAdapter()
114 public void focusLost(FocusEvent e)
116 thresholdValue_actionPerformed();
121 protected void addSliderChangeListener()
124 slider.addChangeListener(new ChangeListener()
127 public void stateChanged(ChangeEvent evt)
131 thresholdValue.setText((slider.getValue() / 1000f) + "");
132 valueChanged(!sliderDragging);
138 protected void addSliderMouseListeners()
141 slider.addMouseListener(new MouseAdapter()
144 public void mousePressed(MouseEvent e)
146 sliderDragging = true;
147 super.mousePressed(e);
151 public void mouseDragged(MouseEvent e)
153 sliderDragging = true;
154 super.mouseDragged(e);
158 public void mouseReleased(MouseEvent evt)
162 sliderDragging = false;
165 ap.paintAlignment(true);
171 * Builds and returns a list of menu items (display text) for choice of
172 * annotation. Also builds maps between annotations, their positions in the
173 * list, and their display labels in the list.
175 * @param isSeqAssociated
178 public Vector<String> getAnnotationItems(boolean isSeqAssociated)
180 annotationLabels = new HashMap<AlignmentAnnotation, String>();
182 Vector<String> list = new Vector<String>();
184 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
185 seqAssociated.setEnabled(false);
186 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
188 AlignmentAnnotation annotation = av.getAlignment()
189 .getAlignmentAnnotation()[i];
190 if (annotation.sequenceRef == null)
199 seqAssociated.setEnabled(true);
201 String label = annotation.label;
202 // add associated sequence ID if available
203 if (!isSeqAssociated && annotation.sequenceRef != null)
205 label = label + "_" + annotation.sequenceRef.getName();
208 if (!list.contains(label))
210 anmap[list.size()] = i;
212 annotationLabels.put(annotation, label);
216 if (!isSeqAssociated)
218 anmap[list.size()] = i;
219 label = label + "_" + (index++);
221 annotationLabels.put(annotation, label);
225 this.annmap = new int[list.size()];
226 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
230 protected int getSelectedThresholdItem(int indexValue)
232 int selectedThresholdItem = -1;
235 selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
237 else if (indexValue == 2)
239 selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
241 return selectedThresholdItem;
244 public void ok_actionPerformed()
248 frame.setClosed(true);
249 } catch (Exception ex)
254 public void cancel_actionPerformed()
257 ap.paintAlignment(true);
260 frame.setClosed(true);
261 } catch (Exception ex)
266 protected void thresholdCheck_actionPerformed()
271 protected void selectedAnnotationChanged()
276 protected void threshold_actionPerformed()
281 protected void thresholdValue_actionPerformed()
285 float f = Float.parseFloat(thresholdValue.getText());
286 slider.setValue((int) (f * 1000));
288 } catch (NumberFormatException ex)
293 protected void thresholdIsMin_actionPerformed()
298 protected void populateThresholdComboBox(JComboBox<String> thresh)
300 thresh.addItem(MessageManager
301 .getString("label.threshold_feature_no_threshold"));
302 thresh.addItem(MessageManager
303 .getString("label.threshold_feature_above_threshold"));
304 thresh.addItem(MessageManager
305 .getString("label.threshold_feature_below_threshold"));
309 * Rebuilds the drop-down list of annotations to choose from when the 'per
310 * sequence only' checkbox is checked or unchecked.
314 protected void seqAssociated_actionPerformed(JComboBox<String> anns)
317 String cursel = (String) anns.getSelectedItem();
318 boolean isvalid = false;
319 boolean isseqs = seqAssociated.isSelected();
320 anns.removeAllItems();
321 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
323 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
328 anns.addItem(anitem);
332 anns.setSelectedItem(cursel);
336 if (anns.getItemCount() > 0)
338 anns.setSelectedIndex(0);
346 protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
347 AlignmentAnnotation annotation)
349 if (annotation.sequenceRef == null || annotation.threshold == null)
354 float thr = annotation.threshold.value;
355 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
357 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
358 if (aa.label.equals(annotation.label)
359 && (annotation.getCalcId() == null ? aa.getCalcId() == null
360 : annotation.getCalcId().equals(aa.getCalcId())))
362 if (aa.threshold == null)
364 aa.threshold = new GraphLine(annotation.threshold);
368 aa.threshold.value = thr;
374 public AlignmentAnnotation getCurrentAnnotation()
376 return currentAnnotation;
379 protected void setCurrentAnnotation(AlignmentAnnotation currentAnnotation)
381 this.currentAnnotation = currentAnnotation;
384 protected abstract void valueChanged(boolean updateAllAnnotation);
386 protected abstract void updateView();
388 protected abstract void reset();
390 protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
392 return annotationLabels.get(ann);
395 protected void jbInit()
398 ok.setText(MessageManager.getString("action.ok"));
399 ok.addActionListener(new ActionListener()
402 public void actionPerformed(ActionEvent e)
404 ok_actionPerformed();
408 cancel.setOpaque(false);
409 cancel.setText(MessageManager.getString("action.cancel"));
410 cancel.addActionListener(new ActionListener()
413 public void actionPerformed(ActionEvent e)
415 cancel_actionPerformed();
419 annotations.addItemListener(new ItemListener()
422 public void itemStateChanged(ItemEvent e)
424 selectedAnnotationChanged();
427 annotations.setToolTipText(MessageManager
428 .getString("info.select_annotation_row"));
430 threshold.addActionListener(new ActionListener()
433 public void actionPerformed(ActionEvent e)
435 threshold_actionPerformed();
439 thresholdValue.setEnabled(false);
440 thresholdValue.setColumns(7);
441 thresholdValue.addActionListener(new ActionListener()
444 public void actionPerformed(ActionEvent e)
446 thresholdValue_actionPerformed();
450 slider.setPaintLabels(false);
451 slider.setPaintTicks(true);
452 slider.setBackground(Color.white);
453 slider.setEnabled(false);
454 slider.setOpaque(false);
455 slider.setPreferredSize(new Dimension(100, 32));
458 public JComboBox<String> getThreshold()
463 public void setThreshold(JComboBox<String> thresh)
465 this.threshold = thresh;
468 public JComboBox<String> getAnnotations()
473 public void setAnnotations(JComboBox<String> anns)
475 this.annotations = anns;