Formatted source
[jalview.git] / src / jalview / gui / UserDefinedColours.java
index f0cf6c1..18382fb 100755 (executable)
@@ -1,39 +1,39 @@
 /*\r
-* Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
-*\r
-* This program is free software; you can redistribute it and/or\r
-* modify it under the terms of the GNU General Public License\r
-* as published by the Free Software Foundation; either version 2\r
-* of the License, or (at your option) any later version.\r
-*\r
-* This program is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with this program; if not, write to the Free Software\r
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
-*/\r
-\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ */\r
 package jalview.gui;\r
 \r
-import jalview.jbgui.GUserDefinedColours;\r
-import jalview.datamodel.*;\r
-import jalview.io.*;\r
-import javax.swing.*;\r
+import java.io.*;\r
+import java.util.*;\r
+\r
 import java.awt.*;\r
 import java.awt.event.*;\r
+import javax.swing.*;\r
 import javax.swing.event.*;\r
-import jalview.schemes.*;\r
-import java.io.*;\r
-import java.util.*;\r
 \r
+import jalview.datamodel.*;\r
+import jalview.io.*;\r
+import jalview.jbgui.*;\r
+import jalview.schemes.*;\r
 \r
-public class UserDefinedColours extends GUserDefinedColours implements ChangeListener\r
+public class UserDefinedColours\r
+    extends GUserDefinedColours implements ChangeListener\r
 {\r
-\r
   AlignmentPanel ap;\r
   SequenceGroup seqGroup;\r
   JButton selectedButton;\r
@@ -41,55 +41,66 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis
   ColourSchemeI oldColourScheme;\r
   JInternalFrame frame;\r
 \r
-\r
   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
   {\r
     super();\r
     frame = new JInternalFrame();\r
     frame.setContentPane(this);\r
-    Desktop.addInternalFrame(frame, "User Defined Colours", 450,530, false );\r
-    if(System.getProperty("os.name").startsWith("Mac"))\r
-      frame.setSize(450,560);\r
+    Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false);\r
 \r
-    if(sg!=null)\r
-        frame.setTitle( frame.getTitle()+ " ("+sg.getName()+")");\r
+    if (System.getProperty("os.name").startsWith("Mac"))\r
+    {\r
+      frame.setSize(450, 560);\r
+    }\r
+\r
+    if (sg != null)\r
+    {\r
+      frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");\r
+    }\r
 \r
     colorChooser.getSelectionModel().addChangeListener(this);\r
 \r
     this.ap = ap;\r
     seqGroup = sg;\r
 \r
+    if (seqGroup != null)\r
+    {\r
+      oldColourScheme = seqGroup.cs;\r
+    }\r
+    else\r
+    {\r
+      oldColourScheme = ap.av.getGlobalColourScheme();\r
+    }\r
 \r
-      if (seqGroup != null)\r
-        oldColourScheme = seqGroup.cs;\r
-      else\r
-        oldColourScheme = ap.av.getGlobalColourScheme();\r
-\r
-      for (int i = 0; i < 20; i++)\r
-        makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
-                   "", ResidueProperties.aa[i]);\r
+    for (int i = 0; i < 20; i++)\r
+    {\r
+      makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
+                 "", ResidueProperties.aa[i]);\r
+    }\r
 \r
-      makeButton("B", "B");\r
-      makeButton("Z", "Z");\r
-      makeButton("X", "X");\r
-      makeButton("Gap", "'.','-',' '");\r
+    makeButton("B", "B");\r
+    makeButton("Z", "Z");\r
+    makeButton("X", "X");\r
+    makeButton("Gap", "'.','-',' '");\r
 \r
-      if(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")!=null)\r
-      {\r
-        loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));\r
-      }\r
+    if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)\r
+    {\r
+      loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));\r
+    }\r
   }\r
 \r
- public void stateChanged(ChangeEvent evt)\r
- {\r
-   if(selectedButton!=null)\r
-     selectedButton.setBackground( colorChooser.getColor() );\r
- }\r
+  public void stateChanged(ChangeEvent evt)\r
+  {\r
+    if (selectedButton != null)\r
+    {\r
+      selectedButton.setBackground(colorChooser.getColor());\r
+    }\r
+  }\r
 \r
   public void colourButtonPressed(MouseEvent e)\r
   {\r
-     selectedButton = (JButton)e.getSource();\r
-     colorChooser.setColor(selectedButton.getBackground());\r
+    selectedButton = (JButton) e.getSource();\r
+    colorChooser.setColor(selectedButton.getBackground());\r
   }\r
 \r
   void makeButton(String label, String aa)\r
@@ -97,15 +108,18 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis
     final JButton button = new JButton();\r
     Color col = Color.white;\r
 \r
-\r
-    try{\r
+    try\r
+    {\r
       col = oldColourScheme.findColour(aa, -1);\r
-    }catch(Exception ex){}\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+    }\r
 \r
-    button.setBackground(  col );\r
-    oldColours.addElement( col );\r
-    button.setText( label );\r
-    button.setForeground( col.darker().darker().darker() );\r
+    button.setBackground(col);\r
+    oldColours.addElement(col);\r
+    button.setText(label);\r
+    button.setForeground(col.darker().darker().darker());\r
     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
     button.addMouseListener(new java.awt.event.MouseAdapter()\r
     {\r
@@ -118,167 +132,207 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis
     buttonPanel.add(button, null);\r
   }\r
 \r
-\r
   protected void okButton_actionPerformed(ActionEvent e)\r
   {\r
     applyButton_actionPerformed(null);\r
-    try{\r
+\r
+    try\r
+    {\r
       frame.setClosed(true);\r
-    }catch(Exception ex){}\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+    }\r
   }\r
 \r
   protected void applyButton_actionPerformed(ActionEvent e)\r
   {\r
-    Color [] newColours = new Color[24];\r
-    for(int i=0; i<24; i++)\r
+    Color[] newColours = new Color[24];\r
+\r
+    for (int i = 0; i < 24; i++)\r
     {\r
-          JButton button = (JButton)buttonPanel.getComponent(i);\r
-          newColours[i] = button.getBackground();\r
+      JButton button = (JButton) buttonPanel.getComponent(i);\r
+      newColours[i] = button.getBackground();\r
     }\r
 \r
     UserColourScheme ucs = new UserColourScheme(newColours);\r
     ucs.setThreshold(0);\r
 \r
-    if(seqGroup!=null)\r
+    if (seqGroup != null)\r
     {\r
       seqGroup.cs = ucs;\r
       ap.repaint();\r
     }\r
     else\r
+    {\r
       ap.alignFrame.changeColour(ucs);\r
-\r
+    }\r
   }\r
 \r
   protected void loadbutton_actionPerformed(ActionEvent e)\r
   {\r
-    JalviewFileChooser chooser = new JalviewFileChooser(\r
-      jalview.bin.Cache.getProperty("LAST_DIRECTORY"),\r
-        new String[]{"jc"},\r
-        new String[]{"Jalview User Colours"},\r
-          "Jalview User Colours");\r
+    JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
+        getProperty(\r
+            "LAST_DIRECTORY"), new String[]\r
+        {"jc"},\r
+        new String[]\r
+        {"Jalview User Colours"}, "Jalview User Colours");\r
     chooser.setFileView(new jalview.io.JalviewFileView());\r
     chooser.setDialogTitle("Load colour scheme");\r
     chooser.setToolTipText("Load");\r
+\r
     int value = chooser.showOpenDialog(this);\r
+\r
     if (value == JalviewFileChooser.APPROVE_OPTION)\r
     {\r
       File choice = chooser.getSelectedFile();\r
-      jalview.bin.Cache.setProperty("LAST_DIRECTORY",  choice.getParent());\r
-      jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice.getPath());\r
+      jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
+      jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR",\r
+                                    choice.getPath());\r
+\r
       Color[] colors = loadColours(choice.getAbsolutePath());\r
+\r
       for (int i = 0; i < colors.length; i++)\r
       {\r
         JButton button = (JButton) buttonPanel.getComponent(i);\r
         button.setBackground(colors[i]);\r
       }\r
-\r
     }\r
   }\r
 \r
   public static UserColourScheme loadDefaultColours()\r
   {\r
-    if(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")!=null)\r
-      return loadDefaultColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));\r
+    if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)\r
+    {\r
+      return loadDefaultColours(jalview.bin.Cache.getProperty(\r
+          "USER_DEFINED_COLOUR"));\r
+    }\r
     else\r
+    {\r
       return null;\r
+    }\r
   }\r
 \r
   public static UserColourScheme loadDefaultColours(String file)\r
   {\r
-      UserColourScheme ucs = null;\r
-      Color cols[] = loadColours(file);\r
-      if(cols!=null)\r
-      {\r
-        ucs = new UserColourScheme(cols);\r
-        ucs.setThreshold(0);\r
-      }\r
-      return ucs;\r
+    UserColourScheme ucs = null;\r
+    Color[] cols = loadColours(file);\r
+\r
+    if (cols != null)\r
+    {\r
+      ucs = new UserColourScheme(cols);\r
+      ucs.setThreshold(0);\r
+    }\r
+\r
+    return ucs;\r
   }\r
 \r
-  static Color [] loadColours(String file)\r
+  static Color[] loadColours(String file)\r
   {\r
-    Color [] newColours=null;\r
+    Color[] newColours = null;\r
+\r
     try\r
     {\r
-      InputStreamReader in = new InputStreamReader(\r
-          new FileInputStream(file), "UTF-8");\r
+      InputStreamReader in = new InputStreamReader(new FileInputStream(\r
+          file), "UTF-8");\r
 \r
-      jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();\r
-      ucs = (jalview.binding.JalviewUserColours)ucs.unmarshal(in);\r
+      jalview.binding.JalviewUserColours ucs = new jalview.binding.\r
+          JalviewUserColours();\r
+      ucs = (jalview.binding.JalviewUserColours) ucs.unmarshal(in);\r
 \r
       newColours = new Color[ucs.getColourCount()];\r
-      for (int i = 0; i < 24; i++)\r
-        newColours[i] = new Color(Integer.parseInt(ucs.getColour(i).getRGB(), 16));\r
 \r
+      for (int i = 0; i < 24; i++)\r
+      {\r
+        newColours[i] = new Color(Integer.parseInt(\r
+            ucs.getColour(i).getRGB(), 16));\r
+      }\r
     }\r
     catch (Exception ex)\r
-    {System.out.println("Error loading UserColourFile "+file);}\r
+    {\r
+      System.out.println("Error loading UserColourFile " + file);\r
+    }\r
 \r
     return newColours;\r
   }\r
 \r
   protected void savebutton_actionPerformed(ActionEvent e)\r
   {\r
-    JalviewFileChooser chooser = new JalviewFileChooser(\r
-    jalview.bin.Cache.getProperty("LAST_DIRECTORY"),\r
-    new String[]{"jc"},\r
-    new String[]{"Jalview User Colours"},\r
-      "Jalview User Colours");\r
+    JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
+        getProperty(\r
+            "LAST_DIRECTORY"), new String[]\r
+        {"jc"},\r
+        new String[]\r
+        {"Jalview User Colours"}, "Jalview User Colours");\r
 \r
     chooser.setFileView(new jalview.io.JalviewFileView());\r
     chooser.setDialogTitle("Save colour scheme");\r
     chooser.setToolTipText("Save");\r
+\r
     int value = chooser.showSaveDialog(this);\r
+\r
     if (value == JalviewFileChooser.APPROVE_OPTION)\r
     {\r
       String choice = chooser.getSelectedFile().getPath();\r
       jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice);\r
-      jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();\r
 \r
-      try{\r
+      jalview.binding.JalviewUserColours ucs = new jalview.binding.\r
+          JalviewUserColours();\r
+\r
+      try\r
+      {\r
         PrintWriter out = new PrintWriter(new OutputStreamWriter(\r
-                                     new FileOutputStream(choice), "UTF-8"));\r
-        for(int i=0; i<24; i++)\r
+            new FileOutputStream(choice), "UTF-8"));\r
+\r
+        for (int i = 0; i < 24; i++)\r
         {\r
-         JButton button = (JButton)buttonPanel.getComponent(i);\r
-         jalview.binding.Colour col = new jalview.binding.Colour();\r
-         col.setName(button.getText());\r
-         col.setRGB( jalview.util.Format.getHexString( button.getBackground() ));\r
-         ucs.addColour(col);\r
+          JButton button = (JButton) buttonPanel.getComponent(i);\r
+          jalview.binding.Colour col = new jalview.binding.Colour();\r
+          col.setName(button.getText());\r
+          col.setRGB(jalview.util.Format.getHexString(\r
+              button.getBackground()));\r
+          ucs.addColour(col);\r
         }\r
 \r
         ucs.marshal(out);\r
         out.close();\r
       }\r
-      catch(Exception ex)\r
-      {ex.printStackTrace();}\r
+      catch (Exception ex)\r
+      {\r
+        ex.printStackTrace();\r
+      }\r
     }\r
-\r
   }\r
 \r
   protected void cancelButton_actionPerformed(ActionEvent e)\r
   {\r
+    Color[] newColours = new Color[24];\r
 \r
-    Color [] newColours = new Color[24];\r
-    for(int i=0; i<24; i++)\r
+    for (int i = 0; i < 24; i++)\r
     {\r
-          newColours[i] = (Color)oldColours.elementAt(i);\r
-          buttonPanel.getComponent(i).setBackground(newColours[i]);\r
+      newColours[i] = (Color) oldColours.elementAt(i);\r
+      buttonPanel.getComponent(i).setBackground(newColours[i]);\r
     }\r
 \r
-    UserColourScheme ucs = new UserColourScheme( newColours );\r
+    UserColourScheme ucs = new UserColourScheme(newColours);\r
 \r
     if (seqGroup != null)\r
+    {\r
       seqGroup.cs = ucs;\r
+    }\r
     else\r
+    {\r
       ap.av.setGlobalColourScheme(ucs);\r
+    }\r
 \r
     ap.repaint();\r
 \r
-    try{\r
+    try\r
+    {\r
       frame.setClosed(true);\r
-    }catch(Exception ex){}\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+    }\r
   }\r
-\r
-\r
 }\r