JAL-2630 first pass groovy colour scheme (with slight refactoring)
[jalview.git] / src / jalview / gui / UserDefinedColours.java
index b182c28..012edb6 100755 (executable)
@@ -118,7 +118,7 @@ public class UserDefinedColours extends GUserDefinedColours implements
 
     if (oldColourScheme instanceof UserColourScheme)
     {
-      schemeName.setText(((UserColourScheme) oldColourScheme).getSchemeName());
+      schemeName.setText(oldColourScheme.getSchemeName());
       if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
       {
         caseSensitive.setSelected(true);
@@ -174,7 +174,15 @@ public class UserDefinedColours extends GUserDefinedColours implements
     }
   }
 
-  void resetButtonPanel(boolean caseSensitive)
+  /**
+   * Rebuilds the panel with coloured buttons for residues. If not case
+   * sensitive colours, show 3-letter amino acid code as button text. If case
+   * sensitive, just show the single letter code, in order to make space for the
+   * additional buttons.
+   * 
+   * @param isCaseSensitive
+   */
+  void resetButtonPanel(boolean isCaseSensitive)
   {
     buttonPanel.removeAll();
 
@@ -183,23 +191,13 @@ public class UserDefinedColours extends GUserDefinedColours implements
       upperCaseButtons = new ArrayList<JButton>();
     }
 
-    JButton button;
-    String label;
     for (int i = 0; i < 20; i++)
     {
-      if (caseSensitive)
-      {
-        label = ResidueProperties.aa[i];
-      }
-      else
-      {
-        label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
-                .toString();
-      }
-
-      button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
-              i);
-
+      String label = isCaseSensitive ? ResidueProperties.aa[i]
+              : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
+                      .toString();
+      JButton button = makeButton(label, ResidueProperties.aa[i],
+              upperCaseButtons, i);
       buttonPanel.add(button);
     }
 
@@ -208,7 +206,7 @@ public class UserDefinedColours extends GUserDefinedColours implements
     buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
     buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
 
-    if (!caseSensitive)
+    if (!isCaseSensitive)
     {
       gridLayout.setRows(6);
       gridLayout.setColumns(4);
@@ -228,14 +226,14 @@ public class UserDefinedColours extends GUserDefinedColours implements
       {
         int row = i / cols + 1;
         int index = (row * cols) + i;
-        button = makeButton(ResidueProperties.aa[i].toLowerCase(),
+        JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
                 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
 
         buttonPanel.add(button, index);
       }
     }
 
-    if (caseSensitive)
+    if (isCaseSensitive)
     {
       buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
       buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
@@ -246,7 +244,7 @@ public class UserDefinedColours extends GUserDefinedColours implements
     // codes
     if (this.frame != null)
     {
-      int newWidth = caseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
+      int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
               : MY_FRAME_WIDTH;
       this.frame.setSize(newWidth, this.frame.getHeight());
     }
@@ -430,15 +428,14 @@ public class UserDefinedColours extends GUserDefinedColours implements
 
       buttons.add(button);
 
+      /*
+       * make initial button colour that of the current colour scheme,
+       * if it is a simple per-residue colouring, else white
+       */
       col = Color.white;
-      if (oldColourScheme != null)
+      if (oldColourScheme != null && oldColourScheme.isSimple())
       {
-        try
-        {
-          col = oldColourScheme.findColour(residue.charAt(0), -1, null);
-        } catch (Exception ex)
-        {
-        }
+        col = oldColourScheme.findColour(residue.charAt(0));
       }
     }
 
@@ -720,7 +717,8 @@ public class UserDefinedColours extends GUserDefinedColours implements
             Cache.getProperty(LAST_DIRECTORY), "jc",
             "Jalview User Colours");
 
-    chooser.setFileView(new JalviewFileView());
+    JalviewFileView fileView = new JalviewFileView();
+    chooser.setFileView(fileView);
     chooser.setDialogTitle(MessageManager
             .getString("label.save_colour_scheme"));
     chooser.setToolTipText(MessageManager.getString("action.save"));
@@ -729,9 +727,9 @@ public class UserDefinedColours extends GUserDefinedColours implements
 
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
-      String choice = chooser.getSelectedFile().getPath();
-      addNewColourScheme(choice);
-      saveToFile(choice);
+      File file = chooser.getSelectedFile();
+      addNewColourScheme(file.getPath());
+      saveToFile(file);
     }
   }
 
@@ -779,9 +777,9 @@ public class UserDefinedColours extends GUserDefinedColours implements
   /**
    * Saves the colour scheme to file in XML format
    * 
-   * @param filePath
+   * @param path
    */
-  protected void saveToFile(String filePath)
+  protected void saveToFile(File toFile)
   {
     /*
      * build a Java model of colour scheme as XML, and 
@@ -792,7 +790,7 @@ public class UserDefinedColours extends GUserDefinedColours implements
     try
     {
       PrintWriter out = new PrintWriter(new OutputStreamWriter(
-              new FileOutputStream(filePath), "UTF-8"));
+              new FileOutputStream(toFile), "UTF-8"));
 
       for (int i = 0; i < buttonPanel.getComponentCount(); i++)
       {