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