Refactored AnnotationColumnChoser and AnnotationColorChooser to apply the DRY principle
[jalview.git] / src / jalview / gui / AnnotationColumnChooser.java
diff --git a/src/jalview/gui/AnnotationColumnChooser.java b/src/jalview/gui/AnnotationColumnChooser.java
new file mode 100644 (file)
index 0000000..4abf49d
--- /dev/null
@@ -0,0 +1,291 @@
+package jalview.gui;
+
+import jalview.schemes.AnnotationColourGradient;
+import jalview.util.MessageManager;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JInternalFrame;
+import javax.swing.JLayeredPane;
+import javax.swing.JPanel;
+
+import net.miginfocom.swing.MigLayout;
+
+@SuppressWarnings("serial")
+public class AnnotationColumnChooser extends AnnotationRowFilter
+{
+
+  private JComboBox<String> annotations;
+
+  JButton ok = new JButton();
+
+  JButton cancel = new JButton();
+
+  JPanel jPanel1 = new JPanel();
+
+  JPanel jPanel2 = new JPanel();
+
+  BorderLayout borderLayout1 = new BorderLayout();
+
+  private JComboBox<String> threshold = new JComboBox<String>();
+
+
+  public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap)
+  {
+    super(av, ap);
+    frame = new JInternalFrame();
+    frame.setContentPane(this);
+    frame.setLayer(JLayeredPane.PALETTE_LAYER);
+    Desktop.addInternalFrame(frame, "Select By Annotation", 520, 215);
+
+    addSliderChangeListener();
+    addSliderMouseListeners();
+
+    if (av.getAlignment().getAlignmentAnnotation() == null)
+    {
+      return;
+    }
+
+    adjusting = true;
+
+    setAnnotations(new JComboBox<String>(
+            getAnnotationItems(seqAssociated.isSelected())));
+    populateThresholdComboBox(threshold);
+
+    if (av.getCurrentAnnotationColumnSelectionState() != null)
+    {
+      annotations.setSelectedIndex(av
+              .getCurrentAnnotationColumnSelectionState().getAnnotations()
+              .getSelectedIndex());
+      threshold.setSelectedIndex(av
+              .getCurrentAnnotationColumnSelectionState().getThreshold()
+              .getSelectedIndex());
+    }
+
+    try
+    {
+      jbInit();
+    } catch (Exception ex)
+    {
+    }
+    adjusting = false;
+
+    updateView();
+    frame.invalidate();
+    frame.pack();
+  }
+
+
+  public AnnotationColumnChooser()
+  {
+    try
+    {
+      jbInit();
+    } catch (Exception ex)
+    {
+      ex.printStackTrace();
+    }
+  }
+
+  private void jbInit() throws Exception
+  {
+    ok.setOpaque(false);
+    ok.setText(MessageManager.getString("action.ok"));
+    ok.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        ok_actionPerformed(e);
+      }
+    });
+    cancel.setOpaque(false);
+    cancel.setText(MessageManager.getString("action.cancel"));
+    cancel.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        cancel_actionPerformed(e);
+      }
+    });
+
+    getAnnotations().addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        annotations_actionPerformed(e);
+      }
+    });
+    getThreshold().addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        threshold_actionPerformed(e);
+      }
+    });
+    thresholdValue.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        thresholdValue_actionPerformed(e);
+      }
+    });
+    slider.setPaintLabels(false);
+    slider.setPaintTicks(true);
+    slider.setBackground(Color.white);
+    slider.setEnabled(false);
+    slider.setOpaque(false);
+    slider.setPreferredSize(new Dimension(100, 32));
+    thresholdValue.setEnabled(false);
+    thresholdValue.setColumns(7);
+    thresholdIsMin.setBackground(Color.white);
+    thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
+    thresholdIsMin.setText(MessageManager
+            .getString("label.threshold_minmax"));
+    thresholdIsMin.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent actionEvent)
+      {
+        thresholdIsMin_actionPerformed(actionEvent);
+      }
+    });
+    seqAssociated.setBackground(Color.white);
+    seqAssociated.setFont(JvSwingUtils.getLabelFont());
+    seqAssociated.setText(MessageManager
+            .getString("label.per_sequence_only"));
+    seqAssociated.addActionListener(new ActionListener()
+    {
+
+      @Override
+      public void actionPerformed(ActionEvent arg0)
+      {
+        seqAssociated_actionPerformed(arg0, annotations, seqAssociated);
+      }
+    });
+
+    this.setLayout(borderLayout1);
+    jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
+    jPanel1.setBackground(Color.white);
+    jPanel2.setBackground(Color.white);
+
+    jPanel1.add(ok);
+    jPanel1.add(cancel);
+    jPanel2.add(getAnnotations(), "grow, wrap");
+    jPanel2.add(seqAssociated, "wrap");
+    jPanel2.add(getThreshold(), "grow, wrap");
+    jPanel2.add(thresholdIsMin, "wrap");
+    jPanel2.add(slider, "grow");
+    jPanel2.add(thresholdValue, "grow");
+    this.add(jPanel1, java.awt.BorderLayout.SOUTH);
+    this.add(jPanel2, java.awt.BorderLayout.CENTER);
+    this.validate();
+  }
+
+
+  public void reset()
+  {
+    av.getColumnSelection().clear();
+  }
+
+  public void valueChanged(boolean updateAllAnnotation)
+  {
+    getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
+    updateView();
+    propagateSeqAssociatedThreshold(updateAllAnnotation,
+            getCurrentAnnotation());
+    ap.paintAlignment(false);
+  }
+
+  public JComboBox<String> getThreshold()
+  {
+    return threshold;
+  }
+
+  public void setThreshold(JComboBox<String> threshold)
+  {
+    this.threshold = threshold;
+  }
+
+  public JComboBox<String> getAnnotations()
+  {
+    return annotations;
+  }
+
+  public void setAnnotations(JComboBox<String> annotations)
+  {
+    this.annotations = annotations;
+  }
+
+  @Override
+  public void updateView()
+  {
+    changeColumnSelection();
+  }
+  void changeColumnSelection()
+  {
+    // Check if combobox is still adjusting
+    if (adjusting)
+    {
+      return;
+    }
+
+    setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
+            .getSelectedIndex()]]);
+
+
+    int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
+            .getSelectedIndex());
+
+    slider.setEnabled(true);
+    thresholdValue.setEnabled(true);
+    thresholdIsMin.setEnabled(true);
+
+    if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
+    {
+      slider.setEnabled(false);
+      thresholdValue.setEnabled(false);
+      thresholdValue.setText("");
+      thresholdIsMin.setEnabled(false);
+    }
+    else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
+            && getCurrentAnnotation().threshold == null)
+    {
+      getCurrentAnnotation()
+              .setThreshold(new jalview.datamodel.GraphLine(
+                              (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
+                      "Threshold", Color.black));
+    }
+
+    if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
+    {
+      adjusting = true;
+      float range = getCurrentAnnotation().graphMax * 1000
+              - getCurrentAnnotation().graphMin * 1000;
+
+      slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
+      slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
+      slider.setValue((int) (getCurrentAnnotation().threshold.value * 1000));
+      thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
+      slider.setMajorTickSpacing((int) (range / 10f));
+      slider.setEnabled(true);
+      thresholdValue.setEnabled(true);
+      adjusting = false;
+    }
+
+    markColumnsContaining(getCurrentAnnotation(), selectedThresholdItem);
+    av.setCurrentAnnotationColumnSelectionState(this);
+    ap.alignmentChanged();
+    ap.paintAlignment(true);
+  }
+}