Load and save Feature Colour Settings
authoramwaterhouse <Andrew Waterhouse>
Wed, 2 Nov 2005 18:08:14 +0000 (18:08 +0000)
committeramwaterhouse <Andrew Waterhouse>
Wed, 2 Nov 2005 18:08:14 +0000 (18:08 +0000)
src/jalview/gui/FeatureSettings.java

index 28cb295..b8b623d 100755 (executable)
@@ -8,24 +8,111 @@ import java.util.*;
 import javax.swing.BorderFactory;\r
 import java.awt.event.*;\r
 import javax.swing.table.*;\r
+import java.io.*;\r
+import jalview.io.JalviewFileChooser;\r
 \r
 public class FeatureSettings extends JPanel\r
 {\r
 \r
   final FeatureRenderer fr;\r
   final AlignmentPanel ap;\r
+  final AlignViewport av;\r
   Object [][] originalData;\r
+  final JInternalFrame frame;\r
+  JScrollPane scrollPane = new JScrollPane();\r
+  JTable table;\r
 \r
   public FeatureSettings(AlignViewport av, final AlignmentPanel ap)\r
   {\r
     this.ap = ap;\r
+    this.av = av;\r
     fr = ap.seqPanel.seqCanvas.getFeatureRenderer();\r
     av.alignment.getSequences();\r
-    final JInternalFrame frame = new JInternalFrame();\r
+    frame = new JInternalFrame();\r
     frame.setContentPane(this);\r
     Desktop.addInternalFrame(frame, "Sequence Feature Settings", 400, 300);\r
 \r
+    setTableData();\r
 \r
+    final JSlider transparency = new JSlider(0, 70, 0);\r
+    transparency.addChangeListener(new ChangeListener()\r
+    {\r
+      public void stateChanged(ChangeEvent evt)\r
+      {\r
+        fr.setTransparency( (float) (100 - transparency.getValue()) / 100f);\r
+        ap.repaint();\r
+      }\r
+    });\r
+\r
+    JPanel transPanel = new JPanel(new FlowLayout());\r
+    transPanel.add(new JLabel("Transparency"));\r
+    transPanel.add(transparency);\r
+\r
+    //////////////////////////////////////////////\r
+    //We're going to need those OK cancel buttons\r
+    JPanel buttonPanel = new JPanel(new FlowLayout());\r
+    JButton button = new JButton("OK");\r
+    button.addActionListener(new ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent evt)\r
+      {\r
+        try\r
+        {\r
+          frame.setClosed(true);\r
+        }\r
+        catch (Exception exe)\r
+        {}\r
+      }\r
+    });\r
+    buttonPanel.add(button);\r
+    button = new JButton("Cancel");\r
+    button.addActionListener(new ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent evt)\r
+      {\r
+        try\r
+        {\r
+          updateFeatureRenderer(originalData);\r
+          frame.setClosed(true);\r
+        }\r
+        catch (Exception exe)\r
+        {}\r
+      }\r
+    });\r
+    buttonPanel.add(button);\r
+\r
+    button = new JButton("Load Colours");\r
+    button.addActionListener(new ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent evt)\r
+      {\r
+        load();\r
+      }\r
+    });\r
+    buttonPanel.add(button);\r
+    button = new JButton("Save Colours");\r
+    button.addActionListener(new ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent evt)\r
+      {\r
+        save();\r
+      }\r
+    });\r
+    buttonPanel.add(button);\r
+\r
+    this.setLayout(new BorderLayout());\r
+    JPanel bigPanel = new JPanel(new BorderLayout());\r
+    bigPanel.add(transPanel, BorderLayout.SOUTH);\r
+    bigPanel.add(scrollPane, BorderLayout.CENTER);\r
+\r
+    add(bigPanel, BorderLayout.CENTER);\r
+    add(buttonPanel, BorderLayout.SOUTH);\r
+\r
+\r
+  }\r
+\r
+  void setTableData()\r
+  {\r
     Vector allFeatures = new Vector();\r
     Vector features;\r
     Enumeration e;\r
@@ -77,113 +164,142 @@ public class FeatureSettings extends JPanel
          originalData[i][2] = new Boolean(fr.featuresDisplayed.contains(type));\r
       else\r
         originalData[i][2] = new Boolean(true);\r
-\r
     }\r
+    ////////////////////////////////////\r
+        //Now build the table\r
+        table = new JTable(new FeatureTableModel(data));\r
+        scrollPane.setViewportView(table);\r
+        table.getTableHeader().setFont(new Font("Verdana", Font.PLAIN, 12));\r
+        table.setFont(new Font("Verdana", Font.PLAIN, 12));\r
+        table.setDefaultRenderer(Color.class,\r
+                             new ColorRenderer());\r
 \r
-    final JSlider transparency = new JSlider(0,70,0);\r
-    transparency.addChangeListener(new ChangeListener()\r
-        {\r
-          public void stateChanged(ChangeEvent evt)\r
-          {\r
-            fr.setTransparency( (float)(100-transparency.getValue())/100f);\r
-            ap.repaint();\r
-          }\r
-        });\r
+        table.setDefaultEditor(Color.class,\r
+                          new ColorEditor());\r
 \r
-    JPanel transPanel = new JPanel(new FlowLayout());\r
-    transPanel.add(new JLabel("Transparency"));\r
-    transPanel.add(transparency);\r
+        table.getColumnModel().getColumn(0).setPreferredWidth(200);\r
 \r
+        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
 \r
+        table.addMouseListener(new MouseAdapter()\r
+            {\r
+              public void mousePressed(MouseEvent evt)\r
+              {\r
+                selectedRow = table.rowAtPoint(evt.getPoint());\r
+              }\r
+            });\r
 \r
-    ////////////////////////////////////\r
-    //Now build the table\r
-    final JTable table = new JTable(new FeatureTableModel(data));\r
-    JScrollPane scrollPane = new JScrollPane(table);\r
-    table.getTableHeader().setFont(new Font("Verdana", Font.PLAIN, 12));\r
-    table.setFont(new Font("Verdana", Font.PLAIN, 12));\r
-    table.setDefaultRenderer(Color.class,\r
-                         new ColorRenderer());\r
+        table.addMouseMotionListener(new MouseMotionAdapter()\r
+            {\r
+              public void mouseDragged(MouseEvent evt)\r
+              {\r
+                int newRow = table.rowAtPoint(evt.getPoint());\r
+                if(newRow!=selectedRow\r
+                   && selectedRow!=-1\r
+                   && newRow!=-1)\r
+                {\r
+                  Object[] temp = new Object[3];\r
+                  temp[0] = table.getValueAt(selectedRow, 0);\r
+                  temp[1] = table.getValueAt(selectedRow, 1);\r
+                  temp[2] = table.getValueAt(selectedRow, 2);\r
+\r
+                  table.setValueAt(table.getValueAt(newRow, 0), selectedRow, 0);\r
+                  table.setValueAt(table.getValueAt(newRow, 1), selectedRow, 1);\r
+                  table.setValueAt(table.getValueAt(newRow, 2), selectedRow, 2);\r
+\r
+                  table.setValueAt(temp[0], newRow, 0);\r
+                  table.setValueAt(temp[1], newRow, 1);\r
+                  table.setValueAt(temp[2], newRow, 2);\r
+\r
+                  selectedRow = newRow;\r
+                }\r
+              }\r
+        });\r
+\r
+  }\r
 \r
-    table.setDefaultEditor(Color.class,\r
-                      new ColorEditor());\r
+  void load()\r
+  {\r
+    JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+                 "LAST_DIRECTORY"), new String[] { "fc" },\r
+             new String[] { "Sequence Feature Colours" }, "Sequence Feature Colours");\r
+     chooser.setFileView(new jalview.io.JalviewFileView());\r
+     chooser.setDialogTitle("Load Feature Colours");\r
+     chooser.setToolTipText("Load");\r
 \r
-    table.getColumnModel().getColumn(0).setPreferredWidth(200);\r
+     int value = chooser.showOpenDialog(this);\r
 \r
-    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
+     if (value == JalviewFileChooser.APPROVE_OPTION)\r
+     {\r
+       File file = chooser.getSelectedFile();\r
 \r
-    table.addMouseListener(new MouseAdapter()\r
-        {\r
-          public void mousePressed(MouseEvent evt)\r
-          {\r
-            selectedRow = table.rowAtPoint(evt.getPoint());\r
-          }\r
-        });\r
+       try\r
+       {\r
+         InputStreamReader in = new InputStreamReader(new FileInputStream(\r
+             file), "UTF-8");\r
 \r
-    table.addMouseMotionListener(new MouseMotionAdapter()\r
-        {\r
-          public void mouseDragged(MouseEvent evt)\r
-          {\r
-            int newRow = table.rowAtPoint(evt.getPoint());\r
-            if(newRow!=selectedRow\r
-               && selectedRow!=-1\r
-               && newRow!=-1)\r
-            {\r
-              Object[] temp = new Object[3];\r
-              temp[0] = table.getValueAt(selectedRow, 0);\r
-              temp[1] = table.getValueAt(selectedRow, 1);\r
-              temp[2] = table.getValueAt(selectedRow, 2);\r
+         jalview.binding.JalviewUserColours jucs = new jalview.binding.\r
+             JalviewUserColours();\r
+         jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);\r
 \r
-              table.setValueAt(table.getValueAt(newRow, 0), selectedRow, 0);\r
-              table.setValueAt(table.getValueAt(newRow, 1), selectedRow, 1);\r
-              table.setValueAt(table.getValueAt(newRow, 2), selectedRow, 2);\r
 \r
-              table.setValueAt(temp[0], newRow, 0);\r
-              table.setValueAt(temp[1], newRow, 1);\r
-              table.setValueAt(temp[2], newRow, 2);\r
+         for (int i = 0; i < jucs.getColourCount(); i++)\r
+         {\r
+           fr.setColour( jucs.getColour(i).getName(),\r
+                          new Color(Integer.parseInt( jucs.getColour(i).getRGB(), 16)));\r
+         }\r
 \r
-              selectedRow = newRow;\r
-            }\r
-          }\r
-        });\r
+         setTableData();\r
+         ap.repaint();\r
+       }\r
+       catch (Exception ex)\r
+       {\r
+         System.out.println("Error loading User ColourFile\n" + ex);\r
+       }\r
+     }\r
+  }\r
 \r
-      //////////////////////////////////////////////\r
-      //We're going to need those OK cancel buttons\r
-      JPanel buttonPanel = new JPanel(new FlowLayout());\r
-        JButton button = new JButton("OK");\r
-        button.addActionListener(new ActionListener()\r
-        {\r
-          public void actionPerformed(ActionEvent evt)\r
-          {\r
-            try {\r
-              frame.setClosed(true);\r
-            }\r
-            catch (Exception exe)\r
-            {}\r
-          }\r
-        });\r
-        buttonPanel.add(button);\r
-        button = new JButton("Cancel");\r
-        button.addActionListener(new ActionListener()\r
+  void save()\r
+  {\r
+    JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+                "LAST_DIRECTORY"), new String[] { "fc" },\r
+            new String[] { "Sequence Feature Colours" }, "Sequence Feature Colours");\r
+    chooser.setFileView(new jalview.io.JalviewFileView());\r
+    chooser.setDialogTitle("Save Feature 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.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();\r
+        ucs.setSchemeName("Sequence Features");\r
+        try\r
         {\r
-          public void actionPerformed(ActionEvent evt)\r
-          {\r
-            try{\r
-              updateFeatureRenderer(originalData);\r
-              frame.setClosed(true);\r
-            }catch(Exception exe){}\r
-          }\r
-        });\r
-        buttonPanel.add(button);\r
+            PrintWriter out = new PrintWriter(new OutputStreamWriter(\r
+                        new FileOutputStream(choice), "UTF-8"));\r
 \r
+            Enumeration e = fr.featureColours.keys();\r
+           while(e.hasMoreElements())\r
+           {\r
 \r
-        this.setLayout(new BorderLayout());\r
-        JPanel bigPanel = new JPanel(new BorderLayout());\r
-        bigPanel.add(transPanel, BorderLayout.SOUTH);\r
-        bigPanel.add(scrollPane, BorderLayout.CENTER);\r
 \r
-        add(bigPanel, BorderLayout.CENTER);\r
-        add(buttonPanel, BorderLayout.SOUTH);\r
+                jalview.binding.Colour col = new jalview.binding.Colour();\r
+                col.setName(e.nextElement().toString());\r
+                col.setRGB(jalview.util.Format.getHexString(\r
+                        fr.getColour(col.getName())));\r
+                ucs.addColour(col);\r
+            }\r
+\r
+            ucs.marshal(out);\r
+            out.close();\r
+        }\r
+        catch (Exception ex)\r
+        {\r
+            ex.printStackTrace();\r
+        }\r
+    }\r
 \r
   }\r
 \r