Merge branch 'features/JAL-2360colourSchemeApplicability' into features/JAL-2371colle...
[jalview.git] / src / jalview / gui / SliderPanel.java
index e28778c..40f86b8 100755 (executable)
@@ -25,14 +25,16 @@ import jalview.jbgui.GSliderPanel;
 import jalview.schemes.CollectionColourSchemeI;
 import jalview.util.MessageManager;
 
-import java.awt.event.ActionEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+import java.beans.PropertyVetoException;
 
 import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
+import javax.swing.event.InternalFrameAdapter;
+import javax.swing.event.InternalFrameEvent;
 
 /**
  * DOCUMENT ME!
@@ -52,6 +54,18 @@ public class SliderPanel extends GSliderPanel
 
   CollectionColourSchemeI cs;
 
+  private static SliderPanel sliderPanel;
+
+  /**
+   * Returns the currently active slider panel (or null if none).
+   * 
+   * @return
+   */
+  public static SliderPanel getSliderPanel()
+  {
+    return sliderPanel;
+  }
+
   /**
    * Creates a new SliderPanel object.
    * 
@@ -116,7 +130,7 @@ public class SliderPanel extends GSliderPanel
    * 
    * @param ap
    *          DOCUMENT ME!
-   * @param cs
+   * @param ccs
    *          DOCUMENT ME!
    * @param source
    *          DOCUMENT ME!
@@ -124,21 +138,22 @@ public class SliderPanel extends GSliderPanel
    * @return DOCUMENT ME!
    */
   public static int setConservationSlider(AlignmentPanel ap,
-          CollectionColourSchemeI cs, String source)
+          CollectionColourSchemeI ccs, String source)
   {
-    SliderPanel sp = null;
+    sliderPanel = null;
 
     if (conservationSlider == null)
     {
-      sp = new SliderPanel(ap, cs.getConservationInc(), true, cs);
+      sliderPanel = new SliderPanel(ap, ccs.getConservationInc(), true, ccs);
       conservationSlider = new JInternalFrame();
-      conservationSlider.setContentPane(sp);
+      conservationSlider.setContentPane(sliderPanel);
       conservationSlider.setLayer(JLayeredPane.PALETTE_LAYER);
     }
     else
     {
-      sp = (SliderPanel) conservationSlider.getContentPane();
-      sp.cs = cs;
+      sliderPanel = (SliderPanel) conservationSlider.getContentPane();
+      sliderPanel.valueField.setText(String.valueOf(ccs.getConservationInc()));
+      sliderPanel.cs = ccs;
     }
 
     conservationSlider
@@ -148,39 +163,66 @@ public class SliderPanel extends GSliderPanel
 
     if (ap.av.getAlignment().getGroups() != null)
     {
-      sp.setAllGroupsCheckEnabled(true);
+      sliderPanel.setAllGroupsCheckEnabled(true);
     }
     else
     {
-      sp.setAllGroupsCheckEnabled(false);
+      sliderPanel.setAllGroupsCheckEnabled(false);
     }
 
-    return sp.getValue();
+    return sliderPanel.getValue();
   }
 
   /**
-   * DOCUMENT ME!
+   * Hides the PID slider panel if it is shown
    */
-  public static void showConservationSlider()
+  public static void hidePIDSlider()
   {
-    try
+    if (PIDSlider != null)
     {
-      PIDSlider.setClosed(true);
-      PIDSlider = null;
-    } catch (Exception ex)
+      try
+      {
+        PIDSlider.setClosed(true);
+        PIDSlider = null;
+      } catch (PropertyVetoException ex)
+      {
+      }
+    }
+  }
+
+  /**
+   * Hides the conservation slider panel if it is shown
+   */
+  public static void hideConservationSlider()
+  {
+    if (conservationSlider != null)
     {
+      try
+      {
+        conservationSlider.setClosed(true);
+        conservationSlider = null;
+      } catch (PropertyVetoException ex)
+      {
+      }
     }
+  }
+
+  /**
+   * DOCUMENT ME!
+   */
+  public static void showConservationSlider()
+  {
+    hidePIDSlider();
 
     if (!conservationSlider.isVisible())
     {
       Desktop.addInternalFrame(conservationSlider,
               conservationSlider.getTitle(), 420, 90, false);
       conservationSlider
-              .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
+              .addInternalFrameListener(new InternalFrameAdapter()
               {
                 @Override
-                public void internalFrameClosed(
-                        javax.swing.event.InternalFrameEvent e)
+                public void internalFrameClosed(InternalFrameEvent e)
                 {
                   conservationSlider = null;
                 }
@@ -194,7 +236,7 @@ public class SliderPanel extends GSliderPanel
    * 
    * @param ap
    *          DOCUMENT ME!
-   * @param collectionColourScheme
+   * @param ccs
    *          DOCUMENT ME!
    * @param source
    *          DOCUMENT ME!
@@ -202,24 +244,22 @@ public class SliderPanel extends GSliderPanel
    * @return DOCUMENT ME!
    */
   public static int setPIDSliderSource(AlignmentPanel ap,
-          CollectionColourSchemeI collectionColourScheme,
-          String source)
+          CollectionColourSchemeI ccs, String source)
   {
-    SliderPanel pid = null;
-
-    int threshold = collectionColourScheme.getThreshold();
+    int threshold = ccs.getThreshold();
 
     if (PIDSlider == null)
     {
-      pid = new SliderPanel(ap, threshold, false, collectionColourScheme);
+      sliderPanel = new SliderPanel(ap, threshold, false, ccs);
       PIDSlider = new JInternalFrame();
-      PIDSlider.setContentPane(pid);
+      PIDSlider.setContentPane(sliderPanel);
       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
     }
     else
     {
-      pid = (SliderPanel) PIDSlider.getContentPane();
-      pid.cs = collectionColourScheme;
+      sliderPanel = (SliderPanel) PIDSlider.getContentPane();
+      sliderPanel.cs = ccs;
+      sliderPanel.valueField.setText(String.valueOf(ccs.getThreshold()));
     }
 
     PIDSlider
@@ -229,46 +269,41 @@ public class SliderPanel extends GSliderPanel
 
     if (ap.av.getAlignment().getGroups() != null)
     {
-      pid.setAllGroupsCheckEnabled(true);
+      sliderPanel.setAllGroupsCheckEnabled(true);
     }
     else
     {
-      pid.setAllGroupsCheckEnabled(false);
+      sliderPanel.setAllGroupsCheckEnabled(false);
     }
 
-    return pid.getValue();
+    return sliderPanel.getValue();
   }
 
   /**
    * DOCUMENT ME!
+   * 
+   * @return
    */
-  public static void showPIDSlider()
+  public static JInternalFrame showPIDSlider()
   {
-    try
-    {
-      conservationSlider.setClosed(true);
-      conservationSlider = null;
-    } catch (Exception ex)
-    {
-    }
+    hideConservationSlider();
 
     if (!PIDSlider.isVisible())
     {
       Desktop.addInternalFrame(PIDSlider, PIDSlider.getTitle(), 420, 90,
               false);
       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
-      PIDSlider
-              .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
-              {
-                @Override
-                public void internalFrameClosed(
-                        javax.swing.event.InternalFrameEvent e)
-                {
-                  PIDSlider = null;
-                }
-              });
+      PIDSlider.addInternalFrameListener(new InternalFrameAdapter()
+      {
+        @Override
+        public void internalFrameClosed(InternalFrameEvent e)
+        {
+          PIDSlider = null;
+        }
+      });
       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
     }
+    return PIDSlider;
   }
 
   /**
@@ -338,7 +373,7 @@ public class SliderPanel extends GSliderPanel
    *          DOCUMENT ME!
    */
   @Override
-  public void valueField_actionPerformed(ActionEvent e)
+  public void valueField_actionPerformed()
   {
     try
     {
@@ -396,4 +431,37 @@ public class SliderPanel extends GSliderPanel
     return getValue(PIDSlider);
   }
 
+  /**
+   * Answers true if the SliderPanel is for Conservation, false if it is for PID
+   * threshold
+   * 
+   * @return
+   */
+  public boolean isForConservation()
+  {
+    return forConservation;
+  }
+
+  /**
+   * Answers the title for the slider panel; this may include 'Background' if
+   * for the alignment, or the group id if for a group
+   * 
+   * @return
+   */
+  public String getTitle()
+  {
+    String title = null;
+    if (isForConservation())
+    {
+      if (conservationSlider != null)
+      {
+        title = conservationSlider.getTitle();
+      }
+    }
+    else if (PIDSlider != null)
+    {
+      title = PIDSlider.getTitle();
+    }
+    return title;
+  }
 }