X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAnnotationRowFilter.java;h=44a16683651634b54ca39a690c364318f91c1bea;hb=fbd682c00e543eadb48074ac354ade57c04c20cb;hp=ea531f289511704c4de866d1fbeff39af3cd310a;hpb=4be2c7d1bcf79664267750cb1187079305794363;p=jalview.git diff --git a/src/jalview/gui/AnnotationRowFilter.java b/src/jalview/gui/AnnotationRowFilter.java index ea531f2..44a1668 100644 --- a/src/jalview/gui/AnnotationRowFilter.java +++ b/src/jalview/gui/AnnotationRowFilter.java @@ -20,24 +20,32 @@ */ package jalview.gui; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.GraphLine; +import jalview.schemes.AnnotationColourGradient; +import jalview.util.MessageManager; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusAdapter; +import java.awt.event.FocusEvent; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.math.BigDecimal; +import java.math.MathContext; import java.util.HashMap; import java.util.Map; import java.util.Vector; -import jalview.datamodel.AlignmentAnnotation; -import jalview.datamodel.GraphLine; -import jalview.datamodel.SequenceGroup; -import jalview.schemes.AnnotationColourGradient; -import jalview.util.MessageManager; - import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JInternalFrame; import javax.swing.JPanel; -import javax.swing.JSlider; import javax.swing.JTextField; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; @@ -45,35 +53,23 @@ import javax.swing.event.ChangeListener; @SuppressWarnings("serial") public abstract class AnnotationRowFilter extends JPanel { + private static final String TWO_DP = "%.2f"; + + private final static MathContext FOUR_SIG_FIG = new MathContext(4); + protected AlignViewport av; protected AlignmentPanel ap; protected int[] annmap; - /* - * map from annotation to its menu item display label - * - so we know which item to pre-select on restore - */ - private Map annotationLabels; - - protected boolean enableSeqAss = false; - - private AlignmentAnnotation currentAnnotation; - protected boolean adjusting = false; - protected JCheckBox currentColours = new JCheckBox(); - - protected JPanel minColour = new JPanel(); - - protected JPanel maxColour = new JPanel(); - protected JCheckBox seqAssociated = new JCheckBox(); - protected JCheckBox thresholdIsMin = new JCheckBox(); + protected JCheckBox percentThreshold = new JCheckBox(); - protected JSlider slider = new JSlider(); + protected Slider slider; protected JTextField thresholdValue = new JTextField(20); @@ -89,6 +85,41 @@ public abstract class AnnotationRowFilter extends JPanel */ protected boolean sliderDragging = false; + protected JComboBox threshold = new JComboBox<>(); + + protected JComboBox annotations; + + /* + * map from annotation to its menu item display label + * - so we know which item to pre-select on restore + */ + private Map annotationLabels; + + private AlignmentAnnotation currentAnnotation; + + /** + * Constructor + * + * @param viewport + * @param alignPanel + */ + public AnnotationRowFilter(AlignViewport viewport, + final AlignmentPanel alignPanel) + { + this.av = viewport; + this.ap = alignPanel; + this.slider = new Slider(0f, 100f, 50f); + + thresholdValue.addFocusListener(new FocusAdapter() + { + @Override + public void focusLost(FocusEvent e) + { + thresholdValue_actionPerformed(); + } + }); + } + protected void addSliderChangeListener() { @@ -99,13 +130,80 @@ public abstract class AnnotationRowFilter extends JPanel { if (!adjusting) { - thresholdValue.setText((slider.getValue() / 1000f) + ""); + setThresholdValueText(); valueChanged(!sliderDragging); } } }); } + /** + * update the text field from the threshold slider. preserves state of + * 'adjusting' so safe to call in init. + */ + protected void setThresholdValueText() + { + boolean oldadj = adjusting; + adjusting = true; + if (percentThreshold.isSelected()) + { + thresholdValue + .setText(String.format(TWO_DP, getSliderPercentageValue())); + } + else + { + /* + * round to 4 significant digits without trailing zeroes + */ + float f = getSliderValue(); + BigDecimal formatted = new BigDecimal(f).round(FOUR_SIG_FIG) + .stripTrailingZeros(); + thresholdValue.setText(formatted.toPlainString()); + } + adjusting = oldadj; + } + + /** + * Answers the value of the slider position (descaled to 'true' value) + * + * @return + */ + protected float getSliderValue() + { + return slider.getSliderValue(); + } + + /** + * Sets the slider value (scaled from the true value to the slider range) + * + * @param value + */ + protected void setSliderValue(float value) + { + slider.setSliderValue(value); + } + + /** + * Answers the value of the slider position as a percentage between minimum + * and maximum of its range + * + * @return + */ + protected float getSliderPercentageValue() + { + return slider.getSliderPercentageValue(); + } + + /** + * Sets the slider position for a given percentage value of its min-max range + * + * @param pct + */ + protected void setSliderPercentageValue(float pct) + { + slider.setSliderPercentageValue(pct); + } + protected void addSliderMouseListeners() { @@ -128,27 +226,11 @@ public abstract class AnnotationRowFilter extends JPanel @Override public void mouseReleased(MouseEvent evt) { - if (sliderDragging) - { - sliderDragging = false; - valueChanged(true); - } - ap.paintAlignment(true); + sliderDragReleased(); } }); } - public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap) - { - this.av = av; - this.ap = ap; - } - - public AnnotationRowFilter() - { - - } - /** * Builds and returns a list of menu items (display text) for choice of * annotation. Also builds maps between annotations, their positions in the @@ -159,12 +241,15 @@ public abstract class AnnotationRowFilter extends JPanel */ public Vector getAnnotationItems(boolean isSeqAssociated) { - annotationLabels = new HashMap(); + annotationLabels = new HashMap<>(); - Vector list = new Vector(); + Vector list = new Vector<>(); int index = 1; - int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length]; - for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++) + int[] anmap = new int[av.getAlignment() + .getAlignmentAnnotation().length]; + seqAssociated.setEnabled(false); + for (int i = 0; i < av.getAlignment() + .getAlignmentAnnotation().length; i++) { AlignmentAnnotation annotation = av.getAlignment() .getAlignmentAnnotation()[i]; @@ -177,7 +262,7 @@ public abstract class AnnotationRowFilter extends JPanel } else { - enableSeqAss = true; + seqAssociated.setEnabled(true); } String label = annotation.label; // add associated sequence ID if available @@ -222,11 +307,6 @@ public abstract class AnnotationRowFilter extends JPanel return selectedThresholdItem; } - public void modelChanged() - { - seqAssociated.setEnabled(enableSeqAss); - } - public void ok_actionPerformed() { try @@ -240,7 +320,7 @@ public abstract class AnnotationRowFilter extends JPanel public void cancel_actionPerformed() { reset(); - ap.paintAlignment(true); + ap.paintAlignment(true, true); try { frame.setClosed(true); @@ -249,45 +329,61 @@ public abstract class AnnotationRowFilter extends JPanel } } - public void thresholdCheck_actionPerformed() + protected void thresholdCheck_actionPerformed() { updateView(); } - public void annotations_actionPerformed() + protected void selectedAnnotationChanged() { updateView(); } - public void threshold_actionPerformed() + protected void threshold_actionPerformed() { updateView(); } - public void thresholdValue_actionPerformed() + /** + * Updates the slider position, and the display, for an update in the slider's + * text input field + */ + protected void thresholdValue_actionPerformed() { try { float f = Float.parseFloat(thresholdValue.getText()); - slider.setValue((int) (f * 1000)); + if (percentThreshold.isSelected()) + { + setSliderPercentageValue(f); + } + else + { + setSliderValue(f); + } updateView(); } catch (NumberFormatException ex) { } } - public void thresholdIsMin_actionPerformed() + protected void percentageValue_actionPerformed() + { + setThresholdValueText(); + } + + protected void thresholdIsMin_actionPerformed() { updateView(); } - protected void populateThresholdComboBox(JComboBox threshold) + protected void populateThresholdComboBox(JComboBox thresh) { - threshold.addItem(MessageManager + thresh.addItem(MessageManager .getString("label.threshold_feature_no_threshold")); - threshold.addItem(MessageManager + thresh.addItem(MessageManager .getString("label.threshold_feature_above_threshold")); - threshold.addItem(MessageManager + thresh.addItem(MessageManager .getString("label.threshold_feature_below_threshold")); } @@ -295,14 +391,15 @@ public abstract class AnnotationRowFilter extends JPanel * Rebuilds the drop-down list of annotations to choose from when the 'per * sequence only' checkbox is checked or unchecked. * - * @param annotations + * @param anns */ - protected void seqAssociated_actionPerformed(JComboBox annotations) + protected void seqAssociated_actionPerformed(JComboBox anns) { adjusting = true; - String cursel = (String) annotations.getSelectedItem(); - boolean isvalid = false, isseqs = seqAssociated.isSelected(); - annotations.removeAllItems(); + String cursel = (String) anns.getSelectedItem(); + boolean isvalid = false; + boolean isseqs = seqAssociated.isSelected(); + anns.removeAllItems(); for (String anitem : getAnnotationItems(seqAssociated.isSelected())) { if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem))) @@ -310,20 +407,22 @@ public abstract class AnnotationRowFilter extends JPanel isvalid = true; cursel = anitem; } - annotations.addItem(anitem); + anns.addItem(anitem); } - adjusting = false; if (isvalid) { - annotations.setSelectedItem(cursel); + anns.setSelectedItem(cursel); } else { - if (annotations.getItemCount() > 0) + if (anns.getItemCount() > 0) { - annotations.setSelectedIndex(0); + anns.setSelectedIndex(0); } } + adjusting = false; + + updateView(); } protected void propagateSeqAssociatedThreshold(boolean allAnnotation, @@ -335,9 +434,11 @@ public abstract class AnnotationRowFilter extends JPanel } float thr = annotation.threshold.value; - for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++) + for (int i = 0; i < av.getAlignment() + .getAlignmentAnnotation().length; i++) { - AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i]; + AlignmentAnnotation aa = av.getAlignment() + .getAlignmentAnnotation()[i]; if (aa.label.equals(annotation.label) && (annotation.getCalcId() == null ? aa.getCalcId() == null : annotation.getCalcId().equals(aa.getCalcId()))) @@ -354,81 +455,153 @@ public abstract class AnnotationRowFilter extends JPanel } } - protected boolean colorAlignmentContaining( - AlignmentAnnotation currentAnn, int selectedThresholdOption) + public AlignmentAnnotation getCurrentAnnotation() { + return currentAnnotation; + } - AnnotationColourGradient acg = null; - if (currentColours.isSelected()) - { - acg = new AnnotationColourGradient(currentAnn, - av.getGlobalColourScheme(), selectedThresholdOption); - } - else - { - acg = new AnnotationColourGradient(currentAnn, - minColour.getBackground(), maxColour.getBackground(), - selectedThresholdOption); - } - acg.setSeqAssociated(seqAssociated.isSelected()); + protected void setCurrentAnnotation(AlignmentAnnotation annotation) + { + this.currentAnnotation = annotation; + } + + /** + * update associated view model and trigger any necessary repaints. + * + * @param updateAllAnnotation + */ + protected abstract void valueChanged(boolean updateAllAnnotation); - if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f) + protected abstract void updateView(); + + protected abstract void reset(); + + protected String getAnnotationMenuLabel(AlignmentAnnotation ann) + { + return annotationLabels.get(ann); + } + + protected void jbInit() + { + ok.setOpaque(false); + ok.setText(MessageManager.getString("action.ok")); + ok.addActionListener(new ActionListener() { - acg.setPredefinedColours(true); - } + @Override + public void actionPerformed(ActionEvent e) + { + ok_actionPerformed(); + } + }); - acg.setThresholdIsMinMax(thresholdIsMin.isSelected()); + cancel.setOpaque(false); + cancel.setText(MessageManager.getString("action.cancel")); + cancel.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + cancel_actionPerformed(); + } + }); - av.setGlobalColourScheme(acg); + annotations.addItemListener(new ItemListener() + { + @Override + public void itemStateChanged(ItemEvent e) + { + selectedAnnotationChanged(); + } + }); + annotations.setToolTipText( + MessageManager.getString("info.select_annotation_row")); - if (av.getAlignment().getGroups() != null) + threshold.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) + { + threshold_actionPerformed(); + } + }); - for (SequenceGroup sg : ap.av.getAlignment().getGroups()) + thresholdValue.setEnabled(false); + thresholdValue.setColumns(7); + thresholdValue.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) { - if (sg.cs == null) - { - continue; - } + thresholdValue_actionPerformed(); + } + }); - if (currentColours.isSelected()) - { - sg.cs = new AnnotationColourGradient(currentAnn, sg.cs, - selectedThresholdOption); - ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated - .isSelected()); - } - else + percentThreshold + .setText(MessageManager.getString("label.as_percentage")); + percentThreshold.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + if (!adjusting) { - sg.cs = new AnnotationColourGradient(currentAnn, - minColour.getBackground(), maxColour.getBackground(), - selectedThresholdOption); - ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated - .isSelected()); + percentageValue_actionPerformed(); } } - } - return false; + }); + slider.setPaintLabels(false); + slider.setPaintTicks(true); + slider.setBackground(Color.white); + slider.setEnabled(false); + slider.setOpaque(false); + slider.setPreferredSize(new Dimension(100, 32)); } - public AlignmentAnnotation getCurrentAnnotation() + public JComboBox getThreshold() { - return currentAnnotation; + return threshold; } - public void setCurrentAnnotation(AlignmentAnnotation currentAnnotation) + public void setThreshold(JComboBox thresh) { - this.currentAnnotation = currentAnnotation; + this.threshold = thresh; } - public abstract void valueChanged(boolean updateAllAnnotation); + public JComboBox getAnnotations() + { + return annotations; + } - public abstract void updateView(); + public void setAnnotations(JComboBox anns) + { + this.annotations = anns; + } - public abstract void reset(); + protected void sliderDragReleased() + { + if (sliderDragging) + { + sliderDragging = false; + valueChanged(true); + } + } - protected String getAnnotationMenuLabel(AlignmentAnnotation ann) + /** + * Sets the min-max range and current value of the slider, with rescaling from + * true values to slider range as required + * + * @param min + * @param max + * @param value + */ + protected void setSliderModel(float min, float max, float value) { - return annotationLabels.get(ann); + slider.setSliderModel(min, max, value); + + /* + * tick mark every 10th position + */ + slider.setMajorTickSpacing( + (slider.getMaximum() - slider.getMinimum()) / 10); } }