JAL-2361 apply any saved changes to current colour scheme
[jalview.git] / src / jalview / gui / UserDefinedColours.java
index 9ab4327..5ec95ed 100755 (executable)
@@ -20,7 +20,6 @@
  */
 package jalview.gui;
 
-import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.datamodel.SequenceGroup;
 import jalview.io.JalviewFileChooser;
@@ -40,7 +39,6 @@ import jalview.util.MessageManager;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Insets;
-import java.awt.event.ActionEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.io.File;
@@ -82,14 +80,10 @@ public class UserDefinedColours extends GUserDefinedColours implements
 
   SequenceGroup seqGroup;
 
-  List<JButton> selectedButtons;
-
   ColourSchemeI oldColourScheme;
 
   JInternalFrame frame;
 
-  JalviewStructureDisplayI structureViewer;
-
   List<JButton> upperCaseButtons;
 
   List<JButton> lowerCaseButtons;
@@ -140,29 +134,7 @@ public class UserDefinedColours extends GUserDefinedColours implements
     showFrame();
   }
 
-  public UserDefinedColours(JalviewStructureDisplayI viewer,
-          ColourSchemeI oldcs)
-  {
-    this();
-    this.structureViewer = viewer;
-
-    colorChooser.getSelectionModel().addChangeListener(this);
-
-    oldColourScheme = oldcs;
-
-    if (oldColourScheme instanceof UserColourScheme)
-    {
-      schemeName.setText(((UserColourScheme) oldColourScheme)
-              .getSchemeName());
-    }
-
-    resetButtonPanel(false);
-
-    showFrame();
-
-  }
-
-  public UserDefinedColours()
+  UserDefinedColours()
   {
     super();
     selectedButtons = new ArrayList<JButton>();
@@ -509,8 +481,7 @@ public class UserDefinedColours extends GUserDefinedColours implements
   }
 
   /**
-   * Applies the current colour scheme to the alignment, sequence group or
-   * structure view.
+   * Applies the current colour scheme to the alignment or sequence group
    */
   @Override
   protected void applyButton_actionPerformed()
@@ -534,12 +505,14 @@ public class UserDefinedColours extends GUserDefinedColours implements
     {
       ap.alignFrame.changeColour(ucs);
     }
-    else if (structureViewer != null)
-    {
-      structureViewer.setJalviewColourScheme(ucs);
-    }
   }
 
+  /**
+   * Constructs an instance of UserColourScheme with the residue colours
+   * currently set on the buttons on the panel
+   * 
+   * @return
+   */
   UserColourScheme getSchemeFromButtons()
   {
 
@@ -595,13 +568,15 @@ public class UserDefinedColours extends GUserDefinedColours implements
   }
 
   /**
-   * DOCUMENT ME!
-   * 
-   * @param e
-   *          DOCUMENT ME!
+   * Action on clicking Load scheme button.
+   * <ul>
+   * <li>Open a file chooser to browse for files with extension .jc</li>
+   * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
+   * <li>Register the loaded colour scheme</li>
+   * </ul>
    */
   @Override
-  protected void loadbutton_actionPerformed(ActionEvent e)
+  protected void loadbutton_actionPerformed()
   {
     upperCaseButtons = new ArrayList<JButton>();
     lowerCaseButtons = new ArrayList<JButton>();
@@ -683,13 +658,20 @@ public class UserDefinedColours extends GUserDefinedColours implements
   }
 
   /**
-   * DOCUMENT ME!
-   * 
-   * @param e
-   *          DOCUMENT ME!
+   * Action on pressing the Save button.
+   * <ul>
+   * <li>Check a name has been entered</li>
+   * <li>Warn if the name already exists, remove any existing scheme of the same
+   * name if overwriting</li>
+   * <li>Do the standard file chooser thing to write with extension .jc</li>
+   * <li>If saving changes (possibly not yet applied) to the currently selected
+   * colour scheme, then apply the changes, as it is too late to back out now</li>
+   * <li>Don't apply the changes if the currently selected scheme is different,
+   * to allow a new scheme to be configured and saved but not applied</li>
+   * </ul>
    */
   @Override
-  protected void savebutton_actionPerformed(ActionEvent e)
+  protected void savebutton_actionPerformed()
   {
     String name = schemeName.getText().trim();
     if (name.length() < 1)
@@ -731,6 +713,16 @@ public class UserDefinedColours extends GUserDefinedColours implements
       File file = chooser.getSelectedFile();
       addNewColourScheme(file.getPath());
       saveToFile(file);
+
+      /*
+       * changes saved - apply to alignment if we are changing 
+       * the currently selected colour scheme
+       */
+      if (oldColourScheme != null
+              && name.equals(oldColourScheme.getSchemeName()))
+      {
+        applyButton_actionPerformed();
+      }
     }
   }
 
@@ -787,7 +779,8 @@ public class UserDefinedColours extends GUserDefinedColours implements
      * marshal to file
      */
     JalviewUserColours ucs = new JalviewUserColours();
-    ucs.setSchemeName(schemeName.getText());
+    String name = schemeName.getText();
+    ucs.setSchemeName(name);
     try
     {
       PrintWriter out = new PrintWriter(new OutputStreamWriter(
@@ -810,12 +803,11 @@ public class UserDefinedColours extends GUserDefinedColours implements
   }
 
   /**
-   * On cancel, restores the colour scheme before the dialogue was opened
-   * 
-   * @param e
+   * On cancel, restores the colour scheme that was selected before the dialogue
+   * was opened
    */
   @Override
-  protected void cancelButton_actionPerformed(ActionEvent e)
+  protected void cancelButton_actionPerformed()
   {
     if (ap != null)
     {
@@ -830,11 +822,6 @@ public class UserDefinedColours extends GUserDefinedColours implements
       ap.paintAlignment(true);
     }
 
-    if (structureViewer != null)
-    {
-      structureViewer.setJalviewColourScheme(oldColourScheme);
-    }
-
     try
     {
       frame.setClosed(true);
@@ -843,8 +830,14 @@ public class UserDefinedColours extends GUserDefinedColours implements
     }
   }
 
+  /**
+   * Action on selecting or deselecting the Case Sensitive option. When
+   * selected, separate buttons are shown for lower case residues, and the panel
+   * is resized to accommodate them. Also, the checkbox for 'apply colour to all
+   * lower case' is enabled.
+   */
   @Override
-  public void caseSensitive_actionPerformed(ActionEvent e)
+  public void caseSensitive_actionPerformed()
   {
     boolean selected = caseSensitive.isSelected();
     resetButtonPanel(selected);