Merge branch 'develop' into spike/JAL-4047/JAL-4048_columns_in_sequenceID
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
index f6352d7..d898398 100644 (file)
  */
 package jalview.gui;
 
-import jalview.bin.Cache;
-import jalview.datamodel.SequenceGroup;
-import jalview.schemes.AnnotationColourGradient;
-import jalview.schemes.ColourSchemeI;
-import jalview.util.MessageManager;
-
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Dimension;
@@ -35,49 +29,59 @@ import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.util.Hashtable;
+import java.util.Vector;
 
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
-import javax.swing.JColorChooser;
+import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.JPanel;
 
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.GraphLine;
+import jalview.datamodel.SequenceGroup;
+import jalview.gui.JalviewColourChooser.ColourChooserListener;
+import jalview.schemes.AnnotationColourGradient;
+import jalview.schemes.ColourSchemeI;
+import jalview.util.MessageManager;
 import net.miginfocom.swing.MigLayout;
 
 @SuppressWarnings("serial")
 public class AnnotationColourChooser extends AnnotationRowFilter
 {
+  private ColourSchemeI oldcs;
 
-  ColourSchemeI oldcs;
+  private JButton defColours;
 
-  Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
-
-  /**
-   * enabled if the user is dragging the slider - try to keep updates to a
-   * minimun
-   */
+  private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
 
-  JComboBox<String> annotations;
+  private JCheckBox useOriginalColours = new JCheckBox();
 
-  JButton defColours = new JButton();
+  JPanel minColour = new JPanel();
 
-  JPanel jPanel1 = new JPanel();
+  JPanel maxColour = new JPanel();
 
-  JPanel jPanel2 = new JPanel();
+  private JCheckBox thresholdIsMin = new JCheckBox();
 
-  BorderLayout borderLayout1 = new BorderLayout();
+  protected static final int MIN_WIDTH = 500;
 
-  private JComboBox<String> threshold = new JComboBox<String>();
+  protected static final int MIN_HEIGHT = 240;
 
   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
   {
+    this(av,ap,null);
+  }
+  public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap,AnnotationColourGradient initSettings)
+  {
     super(av, ap);
     oldcs = av.getGlobalColourScheme();
     if (av.getAlignment().getGroups() != null)
     {
-      oldgroupColours = new Hashtable<SequenceGroup, ColourSchemeI>();
+      oldgroupColours = new Hashtable<>();
       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
       {
         if (sg.getColourScheme() != null)
@@ -87,12 +91,13 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       }
     }
     frame = new JInternalFrame();
+    frame.setFrameIcon(null);
     frame.setContentPane(this);
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
     Desktop.addInternalFrame(frame,
             MessageManager.getString("label.colour_by_annotation"), 520,
             215);
-
+    frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
     addSliderChangeListener();
     addSliderMouseListeners();
 
@@ -105,11 +110,28 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     setDefaultMinMax();
 
     adjusting = true;
-    if (oldcs instanceof AnnotationColourGradient)
+    if (oldcs instanceof AnnotationColourGradient && initSettings==null)
     {
-      AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
-      currentColours.setSelected(acg.isPredefinedColours()
-              || acg.getBaseColour() != null);
+      // init from oldcs
+      initialiseFrom((AnnotationColourGradient) oldcs);
+    } else {
+      // use initial colour gradient - if any..
+      initialiseFrom(initSettings);
+    }
+    
+    jbInit();
+    adjusting = false;
+
+    updateView();
+    frame.invalidate();
+    frame.pack();
+  }
+  private void initialiseFrom(AnnotationColourGradient acg)
+  {
+    if (acg!=null)
+    {
+      useOriginalColours.setSelected(
+            acg.isPredefinedColours() || acg.getBaseColour() != null);
       if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
       {
         minColour.setBackground(acg.getMinColour());
@@ -118,15 +140,21 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       seqAssociated.setSelected(acg.isSeqAssociated());
 
     }
-    annotations = new JComboBox<String>(
-            getAnnotationItems(seqAssociated.isSelected()));
+    Vector<String> annotItems = getAnnotationItems(
+            seqAssociated.isSelected());
+    annotations = new JComboBox<>(annotItems);
 
     populateThresholdComboBox(threshold);
 
-    if (oldcs instanceof AnnotationColourGradient)
+    if (acg!=null)
     {
-      AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
-      annotations.setSelectedItem(acg.getAnnotation());
+      String label = getAnnotationMenuLabel(acg.getAnnotation());
+      // TODO: workaround below shouldn't be necessary - there's a bug in getAnnotationMenuLabel!
+      if (acg.isSeqAssociated())
+      {
+        label = acg.getAnnotation().label;
+      }
+      annotations.setSelectedItem(label);
       switch (acg.getAboveThreshold())
       {
       case AnnotationColourGradient.NO_THRESHOLD:
@@ -139,40 +167,19 @@ public class AnnotationColourChooser extends AnnotationRowFilter
         getThreshold().setSelectedIndex(2);
         break;
       default:
-        throw new Error(
-                MessageManager
-                        .getString("error.implementation_error_dont_know_about_threshold_setting"));
+        throw new Error(MessageManager.getString(
+                "error.implementation_error_dont_know_about_threshold_setting"));
       }
-      thresholdIsMin.setSelected(acg.thresholdIsMinMax);
-      thresholdValue.setText("" + acg.getAnnotationThreshold());
-    }
-
-    try
-    {
-      jbInit();
-    } catch (Exception ex)
-    {
+      thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
+      thresholdValue.setText(String.valueOf(acg.getAnnotationThreshold()));
     }
-    adjusting = false;
-
-    updateView();
-    frame.invalidate();
-    frame.pack();
   }
 
-  public AnnotationColourChooser()
+  @Override
+  protected void jbInit()
   {
-    try
-    {
-      jbInit();
-    } catch (Exception ex)
-    {
-      ex.printStackTrace();
-    }
-  }
+    super.jbInit();
 
-  private void jbInit() throws Exception
-  {
     minColour.setFont(JvSwingUtils.getLabelFont());
     minColour.setBorder(BorderFactory.createEtchedBorder());
     minColour.setPreferredSize(new Dimension(40, 20));
@@ -184,7 +191,7 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       {
         if (minColour.isEnabled())
         {
-          minColour_actionPerformed();
+          showColourChooser(minColour, "label.select_colour_minimum_value");
         }
       }
     });
@@ -199,30 +206,12 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       {
         if (maxColour.isEnabled())
         {
-          maxColour_actionPerformed();
+          showColourChooser(maxColour, "label.select_colour_maximum_value");
         }
       }
     });
-    ok.setOpaque(false);
-    ok.setText(MessageManager.getString("action.ok"));
-    ok.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        ok_actionPerformed();
-      }
-    });
-    cancel.setOpaque(false);
-    cancel.setText(MessageManager.getString("action.cancel"));
-    cancel.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        cancel_actionPerformed();
-      }
-    });
+
+    defColours = new JButton();
     defColours.setOpaque(false);
     defColours.setText(MessageManager.getString("action.set_defaults"));
     defColours.setToolTipText(MessageManager
@@ -237,54 +226,22 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       }
     });
 
-    annotations.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        annotations_actionPerformed();
-      }
-    });
-    getThreshold().addActionListener(new ActionListener()
+    useOriginalColours.setFont(JvSwingUtils.getLabelFont());
+    useOriginalColours.setOpaque(false);
+    useOriginalColours.setText(
+            MessageManager.getString("label.use_original_colours"));
+    useOriginalColours.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        threshold_actionPerformed();
-      }
-    });
-    thresholdValue.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        thresholdValue_actionPerformed();
-      }
-    });
-    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);
-    currentColours.setFont(JvSwingUtils.getLabelFont());
-    currentColours.setOpaque(false);
-    currentColours.setText(MessageManager
-            .getString("label.use_original_colours"));
-    currentColours.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        currentColours_actionPerformed();
+        originalColours_actionPerformed();
       }
     });
     thresholdIsMin.setBackground(Color.white);
     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
-    thresholdIsMin.setText(MessageManager
-            .getString("label.threshold_minmax"));
+    thresholdIsMin
+            .setText(MessageManager.getString("label.threshold_minmax"));
     thresholdIsMin.addActionListener(new ActionListener()
     {
       @Override
@@ -295,8 +252,8 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     });
     seqAssociated.setBackground(Color.white);
     seqAssociated.setFont(JvSwingUtils.getLabelFont());
-    seqAssociated.setText(MessageManager
-            .getString("label.per_sequence_only"));
+    seqAssociated
+            .setText(MessageManager.getString("label.per_sequence_only"));
     seqAssociated.addActionListener(new ActionListener()
     {
 
@@ -307,7 +264,9 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       }
     });
 
-    this.setLayout(borderLayout1);
+    this.setLayout(new BorderLayout());
+    JPanel jPanel1 = new JPanel();
+    JPanel jPanel2 = new JPanel();
     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
     jPanel1.setBackground(Color.white);
     jPanel2.setBackground(Color.white);
@@ -316,7 +275,7 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     jPanel1.add(cancel);
     jPanel2.add(annotations, "grow, wrap");
     jPanel2.add(seqAssociated);
-    jPanel2.add(currentColours);
+    jPanel2.add(useOriginalColours);
     JPanel colpanel = new JPanel(new FlowLayout());
     colpanel.setBackground(Color.white);
     colpanel.add(minColour);
@@ -340,42 +299,33 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   private void setDefaultMinMax()
   {
-    minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
-            Color.orange));
-    maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
-            Color.red));
+    minColour.setBackground(
+            Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
+    maxColour.setBackground(
+            Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
   }
 
-  public void minColour_actionPerformed()
+  protected void showColourChooser(JPanel colourPanel, String titleKey)
   {
-    Color col = JColorChooser.showDialog(this,
-            MessageManager.getString("label.select_colour_minimum_value"),
-            minColour.getBackground());
-    if (col != null)
+    String ttl = MessageManager.getString(titleKey);
+    ColourChooserListener listener = new ColourChooserListener()
     {
-      minColour.setBackground(col);
-    }
-    minColour.repaint();
-    updateView();
-  }
-
-  public void maxColour_actionPerformed()
-  {
-    Color col = JColorChooser.showDialog(this,
-            MessageManager.getString("label.select_colour_maximum_value"),
-            maxColour.getBackground());
-    if (col != null)
-    {
-      maxColour.setBackground(col);
-    }
-    maxColour.repaint();
-    updateView();
+      @Override
+      public void colourSelected(Color c)
+      {
+        colourPanel.setBackground(c);
+        colourPanel.repaint();
+        updateView();
+      }
+    };
+    JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
+            colourPanel.getBackground(), listener);
   }
 
   @Override
   public void reset()
   {
-    av.setGlobalColourScheme(oldcs);
+    this.ap.alignFrame.changeColour(oldcs);
     if (av.getAlignment().getGroups() != null)
     {
 
@@ -391,36 +341,28 @@ public class AnnotationColourChooser extends AnnotationRowFilter
   {
     if (slider.isEnabled())
     {
-      if (currentColours.isSelected()
-              && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
+      if (useOriginalColours.isSelected() && !(av
+              .getGlobalColourScheme() instanceof AnnotationColourGradient))
       {
         updateView();
       }
-      getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
+      getCurrentAnnotation().threshold.value = getSliderValue();
       propagateSeqAssociatedThreshold(updateAllAnnotation,
               getCurrentAnnotation());
-      ap.paintAlignment(false);
+      ap.paintAlignment(false, false);
     }
   }
 
-  public JComboBox<String> getThreshold()
-  {
-    return threshold;
-  }
-
-  public void setThreshold(JComboBox<String> threshold)
-  {
-    this.threshold = threshold;
-  }
-
-  public void currentColours_actionPerformed()
+  public void originalColours_actionPerformed()
   {
-    if (currentColours.isSelected())
+    boolean selected = useOriginalColours.isSelected();
+    if (selected)
     {
       reset();
     }
-    maxColour.setEnabled(!currentColours.isSelected());
-    minColour.setEnabled(!currentColours.isSelected());
+    maxColour.setEnabled(!selected);
+    minColour.setEnabled(!selected);
+    thresholdIsMin.setEnabled(!selected);
     updateView();
   }
 
@@ -433,16 +375,20 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       return;
     }
 
-    setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
-            .getSelectedIndex()]]);
+    int selIndex = annotations
+            .getSelectedIndex();
+    int annIndex = annmap[selIndex];
+    setCurrentAnnotation(
+            av.getAlignment().getAlignmentAnnotation()[annIndex]);
 
-    int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
-            .getSelectedIndex());
+    int selectedThresholdItem = getSelectedThresholdItem(
+            getThreshold().getSelectedIndex());
 
     slider.setEnabled(true);
     thresholdValue.setEnabled(true);
-    thresholdIsMin.setEnabled(true);
+    thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
 
+    final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
     {
       slider.setEnabled(false);
@@ -451,36 +397,104 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       thresholdIsMin.setEnabled(false);
     }
     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
-            && getCurrentAnnotation().threshold == null)
+            && currentAnnotation.threshold == null)
     {
-      getCurrentAnnotation()
-              .setThreshold(
-                      new jalview.datamodel.GraphLine(
-                              (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
-                              "Threshold", Color.black));
+      currentAnnotation.setThreshold(new GraphLine(
+              (currentAnnotation.graphMax - currentAnnotation.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));
+      setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
+              currentAnnotation.threshold.value);
       slider.setEnabled(true);
+
+      setThresholdValueText();
       thresholdValue.setEnabled(true);
       adjusting = false;
     }
-    colorAlignmContaining(getCurrentAnnotation(), selectedThresholdItem);
+    colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
 
     ap.alignmentChanged();
-    // ensure all associated views (overviews, structures, etc) are notified of
-    // updated colours.
-    ap.paintAlignment(true);
   }
 
+  protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
+          int selectedThresholdOption)
+  {
+
+    AnnotationColourGradient acg = null;
+    if (useOriginalColours.isSelected())
+    {
+      acg = new AnnotationColourGradient(currentAnn,
+              av.getGlobalColourScheme(), selectedThresholdOption);
+    }
+    else
+    {
+      acg = new AnnotationColourGradient(currentAnn,
+              minColour.getBackground(), maxColour.getBackground(),
+              selectedThresholdOption);
+    }
+    acg.setSeqAssociated(seqAssociated.isSelected());
+
+    if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
+    {
+      acg.setPredefinedColours(true);
+    }
+
+    acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
+
+    this.ap.alignFrame.changeColour(acg);
+
+    if (av.getAlignment().getGroups() != null)
+    {
+
+      for (SequenceGroup sg : ap.av.getAlignment().getGroups())
+      {
+        if (sg.cs == null)
+        {
+          continue;
+        }
+        sg.setColourScheme(acg.getInstance(av, sg));
+      }
+    }
+  }
+
+  @Override
+  protected void sliderDragReleased()
+  {
+    super.sliderDragReleased();
+    ap.paintAlignment(true, true);
+  }
+
+  /**
+   * construct and display a colourchooser for a given annotation row
+   * 
+   * @param av
+   * @param ap
+   * @param alignmentAnnotation
+   * @param perseq - when true, enable per-sequence if alignment annotation is per sequence 
+   */
+  public static void displayFor(AlignViewport av, AlignmentPanel ap,
+          AlignmentAnnotation alignmentAnnotation, boolean perSeq)
+  {
+    ColourSchemeI global = av.getGlobalColourScheme();
+    AnnotationColourGradient newCS = new AnnotationColourGradient(alignmentAnnotation, global, alignmentAnnotation.threshold!=null ? AnnotationColourGradient.ABOVE_THRESHOLD:AnnotationColourGradient.NO_THRESHOLD);
+    if (alignmentAnnotation.sequenceRef!=null)
+    {
+      newCS.setSeqAssociated(perSeq);
+    }
+    for (int i=0;i<alignmentAnnotation.annotations.length;i++)
+    {
+      Annotation ann = alignmentAnnotation.annotations[i];
+      if (ann!=null && ann.colour!=null && !ann.colour.equals(Color.white))
+      {
+        newCS.setPredefinedColours(true);
+        break;
+      }
+    }
+    AnnotationColourChooser achooser = new AnnotationColourChooser(av,ap,newCS);
+  }
 }