recalc group conserve
[jalview.git] / src / jalview / gui / UserDefinedColours.java
index 6fed854..18382fb 100755 (executable)
@@ -1,20 +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
 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
@@ -22,48 +41,66 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis
   ColourSchemeI oldColourScheme;\r
   JInternalFrame frame;\r
 \r
-\r
-  public UserDefinedColours(JInternalFrame f, AlignmentPanel ap, SequenceGroup sg)\r
+  public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
   {\r
     super();\r
-    colorChooser.getSelectionModel().addChangeListener(this);\r
-    this.ap = ap;\r
-    frame = f;\r
-    seqGroup = sg;\r
+    frame = new JInternalFrame();\r
+    frame.setContentPane(this);\r
+    Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false);\r
 \r
+    if (System.getProperty("os.name").startsWith("Mac"))\r
+    {\r
+      frame.setSize(450, 560);\r
+    }\r
 \r
-      if (seqGroup != null)\r
-        oldColourScheme = seqGroup.cs;\r
-      else\r
-        oldColourScheme = ap.av.getGlobalColourScheme();\r
+    if (sg != null)\r
+    {\r
+      frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");\r
+    }\r
 \r
-      for (int i = 0; i < 20; i++)\r
-        makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
-                   "", ResidueProperties.aa[i]);\r
+    colorChooser.getSelectionModel().addChangeListener(this);\r
 \r
-      makeButton("B", "B");\r
-      makeButton("Z", "Z");\r
-      makeButton("X", "X");\r
-      makeButton("'.','-',' '", "-");\r
+    this.ap = ap;\r
+    seqGroup = sg;\r
 \r
-      if(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")!=null)\r
-      {\r
-        loadColour(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));\r
-      }\r
+    if (seqGroup != null)\r
+    {\r
+      oldColourScheme = seqGroup.cs;\r
+    }\r
+    else\r
+    {\r
+      oldColourScheme = ap.av.getGlobalColourScheme();\r
+    }\r
+\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
 \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
@@ -71,15 +108,18 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis
     final JButton button = new JButton();\r
     Color col = Color.white;\r
 \r
+    try\r
+    {\r
+      col = oldColourScheme.findColour(aa, -1);\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+    }\r
 \r
-    try{\r
-      col = oldColourScheme.findColour(aa, -1, null);\r
-    }catch(Exception ex){}\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
@@ -89,119 +129,210 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis
       }\r
     });\r
 \r
-    buttonPanel.add(button);\r
+    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
-    UserColourScheme ucs = new UserColourScheme();\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
-    ucs.setColourScheme( newColours );\r
 \r
-    if(seqGroup!=null)\r
+    if (seqGroup != null)\r
+    {\r
       seqGroup.cs = ucs;\r
+      ap.repaint();\r
+    }\r
     else\r
-      ap.av.setGlobalColourScheme(ucs);\r
-\r
-    ap.RefreshPanels();\r
+    {\r
+      ap.alignFrame.changeColour(ucs);\r
+    }\r
   }\r
 \r
   protected void loadbutton_actionPerformed(ActionEvent e)\r
   {\r
-    JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"));\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
-      String choice = chooser.getSelectedFile().getPath();\r
-      jalview.bin.Cache.setProperty("LAST_DIRECTORY",  choice);\r
-      loadColour(choice);\r
+      File choice = chooser.getSelectedFile();\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
+  public static UserColourScheme loadDefaultColours()\r
+  {\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
+\r
+    if (cols != null)\r
+    {\r
+      ucs = new UserColourScheme(cols);\r
+      ucs.setThreshold(0);\r
+    }\r
+\r
+    return ucs;\r
   }\r
 \r
-  void loadColour(String file)\r
+  static Color[] loadColours(String file)\r
   {\r
+    Color[] newColours = null;\r
+\r
     try\r
     {\r
-      BufferedReader in = new BufferedReader(new FileReader(file));\r
+      InputStreamReader in = new InputStreamReader(new FileInputStream(\r
+          file), "UTF-8");\r
+\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
+\r
       for (int i = 0; i < 24; i++)\r
       {\r
-        JButton button = (JButton) buttonPanel.getComponent(i);\r
-        Color c = new Color(Integer.parseInt(in.readLine()));\r
-        button.setBackground(c);\r
+        newColours[i] = new Color(Integer.parseInt(\r
+            ucs.getColour(i).getRGB(), 16));\r
       }\r
     }\r
     catch (Exception ex)\r
-    {}\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(jalview.bin.Cache.getProperty("LAST_DIRECTORY"));\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
 \r
-      try{\r
-        PrintWriter out = new PrintWriter(new FileWriter(choice));\r
-        for(int i=0; i<24; i++)\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
+\r
+        for (int i = 0; i < 24; i++)\r
         {\r
-          JButton button = (JButton)buttonPanel.getComponent(i);\r
-          out.println(button.getBackground().getRGB() + "");\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
-    UserColourScheme ucs = new UserColourScheme();\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
-          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
-    ucs.setColourScheme( 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.RefreshPanels();\r
+    ap.repaint();\r
 \r
+    try\r
+    {\r
+      frame.setClosed(true);\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+    }\r
   }\r
-\r
-\r
 }\r