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 java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.util.HashMap;
27 import java.util.Vector;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.GraphLine;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.schemes.AnnotationColourGradient;
33 import jalview.util.MessageManager;
35 import javax.swing.JButton;
36 import javax.swing.JCheckBox;
37 import javax.swing.JComboBox;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JPanel;
40 import javax.swing.JSlider;
41 import javax.swing.JTextField;
42 import javax.swing.event.ChangeEvent;
43 import javax.swing.event.ChangeListener;
45 @SuppressWarnings("serial")
46 public abstract class AnnotationRowFilter extends JPanel
48 protected AlignViewport av;
50 protected AlignmentPanel ap;
52 protected int[] annmap;
55 * map from annotation to its menu item display label
56 * - so we know which item to pre-select on restore
58 private Map<AlignmentAnnotation, String> annotationLabels;
60 protected boolean enableSeqAss = false;
62 private AlignmentAnnotation currentAnnotation;
64 protected boolean adjusting = false;
66 protected JCheckBox currentColours = new JCheckBox();
68 protected JPanel minColour = new JPanel();
70 protected JPanel maxColour = new JPanel();
72 protected JCheckBox seqAssociated = new JCheckBox();
74 protected JCheckBox thresholdIsMin = new JCheckBox();
76 protected JSlider slider = new JSlider();
78 protected JTextField thresholdValue = new JTextField(20);
80 protected JInternalFrame frame;
82 protected JButton ok = new JButton();
84 protected JButton cancel = new JButton();
87 * enabled if the user is dragging the slider - try to keep updates to a
90 protected boolean sliderDragging = false;
92 protected void addSliderChangeListener()
95 slider.addChangeListener(new ChangeListener()
98 public void stateChanged(ChangeEvent evt)
102 thresholdValue.setText((slider.getValue() / 1000f) + "");
103 valueChanged(!sliderDragging);
109 protected void addSliderMouseListeners()
112 slider.addMouseListener(new MouseAdapter()
115 public void mousePressed(MouseEvent e)
117 sliderDragging = true;
118 super.mousePressed(e);
122 public void mouseDragged(MouseEvent e)
124 sliderDragging = true;
125 super.mouseDragged(e);
129 public void mouseReleased(MouseEvent evt)
133 sliderDragging = false;
136 ap.paintAlignment(true);
141 public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
147 public AnnotationRowFilter()
153 * Builds and returns a list of menu items (display text) for choice of
154 * annotation. Also builds maps between annotations, their positions in the
155 * list, and their display labels in the list.
157 * @param isSeqAssociated
160 public Vector<String> getAnnotationItems(boolean isSeqAssociated)
162 annotationLabels = new HashMap<AlignmentAnnotation, String>();
164 Vector<String> list = new Vector<String>();
166 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
167 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
169 AlignmentAnnotation annotation = av.getAlignment()
170 .getAlignmentAnnotation()[i];
171 if (annotation.sequenceRef == null)
182 String label = annotation.label;
183 // add associated sequence ID if available
184 if (!isSeqAssociated && annotation.sequenceRef != null)
186 label = label + "_" + annotation.sequenceRef.getName();
189 if (!list.contains(label))
191 anmap[list.size()] = i;
193 annotationLabels.put(annotation, label);
197 if (!isSeqAssociated)
199 anmap[list.size()] = i;
200 label = label + "_" + (index++);
202 annotationLabels.put(annotation, label);
206 this.annmap = new int[list.size()];
207 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
211 protected int getSelectedThresholdItem(int indexValue)
213 int selectedThresholdItem = -1;
216 selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
218 else if (indexValue == 2)
220 selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
222 return selectedThresholdItem;
225 public void modelChanged()
227 seqAssociated.setEnabled(enableSeqAss);
230 public void ok_actionPerformed()
234 frame.setClosed(true);
235 } catch (Exception ex)
240 public void cancel_actionPerformed()
243 ap.paintAlignment(true);
246 frame.setClosed(true);
247 } catch (Exception ex)
252 public void thresholdCheck_actionPerformed()
257 public void annotations_actionPerformed()
262 public void threshold_actionPerformed()
267 public void thresholdValue_actionPerformed()
271 float f = Float.parseFloat(thresholdValue.getText());
272 slider.setValue((int) (f * 1000));
274 } catch (NumberFormatException ex)
279 public void thresholdIsMin_actionPerformed()
284 protected void populateThresholdComboBox(JComboBox<String> threshold)
286 threshold.addItem(MessageManager
287 .getString("label.threshold_feature_no_threshold"));
288 threshold.addItem(MessageManager
289 .getString("label.threshold_feature_above_threshold"));
290 threshold.addItem(MessageManager
291 .getString("label.threshold_feature_below_threshold"));
295 * Rebuilds the drop-down list of annotations to choose from when the 'per
296 * sequence only' checkbox is checked or unchecked.
300 protected void seqAssociated_actionPerformed(JComboBox<String> annotations)
303 String cursel = (String) annotations.getSelectedItem();
304 boolean isvalid = false, isseqs = seqAssociated.isSelected();
305 annotations.removeAllItems();
306 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
308 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
313 annotations.addItem(anitem);
318 annotations.setSelectedItem(cursel);
322 if (annotations.getItemCount() > 0)
324 annotations.setSelectedIndex(0);
329 protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
330 AlignmentAnnotation annotation)
332 if (annotation.sequenceRef == null || annotation.threshold == null)
337 float thr = annotation.threshold.value;
338 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
340 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
341 if (aa.label.equals(annotation.label)
342 && (annotation.getCalcId() == null ? aa.getCalcId() == null
343 : annotation.getCalcId().equals(aa.getCalcId())))
345 if (aa.threshold == null)
347 aa.threshold = new GraphLine(annotation.threshold);
351 aa.threshold.value = thr;
357 protected boolean colorAlignmentContaining(
358 AlignmentAnnotation currentAnn, int selectedThresholdOption)
361 AnnotationColourGradient acg = null;
362 if (currentColours.isSelected())
364 acg = new AnnotationColourGradient(currentAnn,
365 av.getGlobalColourScheme(), selectedThresholdOption);
369 acg = new AnnotationColourGradient(currentAnn,
370 minColour.getBackground(), maxColour.getBackground(),
371 selectedThresholdOption);
373 acg.setSeqAssociated(seqAssociated.isSelected());
375 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
377 acg.setPredefinedColours(true);
380 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
382 av.setGlobalColourScheme(acg);
384 if (av.getAlignment().getGroups() != null)
387 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
394 if (currentColours.isSelected())
396 sg.cs = new AnnotationColourGradient(currentAnn, sg.cs,
397 selectedThresholdOption);
398 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
403 sg.cs = new AnnotationColourGradient(currentAnn,
404 minColour.getBackground(), maxColour.getBackground(),
405 selectedThresholdOption);
406 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
414 public AlignmentAnnotation getCurrentAnnotation()
416 return currentAnnotation;
419 public void setCurrentAnnotation(AlignmentAnnotation currentAnnotation)
421 this.currentAnnotation = currentAnnotation;
424 public abstract void valueChanged(boolean updateAllAnnotation);
426 public abstract void updateView();
428 public abstract void reset();
430 protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
432 return annotationLabels.get(ann);