From 9cd96dd3e43ae32814fa8c62df274ff9224bfdbc Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 18 Sep 2018 17:43:03 +0100 Subject: [PATCH] JAL-3063 Save/Load user defined colours using JAXB --- src/jalview/gui/UserDefinedColours.java | 16 +++++++++++---- src/jalview/schemes/ColourSchemeLoader.java | 29 ++++++++++++++++----------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 3290500..b1f6d1b 100755 --- a/src/jalview/gui/UserDefinedColours.java +++ b/src/jalview/gui/UserDefinedColours.java @@ -24,8 +24,6 @@ import jalview.bin.Cache; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; import jalview.jbgui.GUserDefinedColours; -import jalview.schemabinding.version2.Colour; -import jalview.schemabinding.version2.JalviewUserColours; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeLoader; import jalview.schemes.ColourSchemes; @@ -34,6 +32,9 @@ import jalview.schemes.UserColourScheme; import jalview.util.ColorUtils; import jalview.util.Format; import jalview.util.MessageManager; +import jalview.xml.binding.jalview.JalviewUserColours; +import jalview.xml.binding.jalview.JalviewUserColours.Colour; +import jalview.xml.binding.jalview.ObjectFactory; import java.awt.Color; import java.awt.Font; @@ -51,6 +52,8 @@ import javax.swing.JButton; import javax.swing.JInternalFrame; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; /** * This panel allows the user to assign colours to Amino Acid residue codes, and @@ -858,9 +861,14 @@ public class UserDefinedColours extends GUserDefinedColours Colour col = new Colour(); col.setName(button.getText()); col.setRGB(Format.getHexString(button.getBackground())); - ucs.addColour(col); + ucs.getColour().add(col); } - ucs.marshal(out); + JAXBContext jaxbContext = JAXBContext + .newInstance(JalviewUserColours.class); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + jaxbMarshaller.marshal( + new ObjectFactory().createJalviewUserColours(ucs), out); + // ucs.marshal(out); out.close(); } catch (Exception ex) { diff --git a/src/jalview/schemes/ColourSchemeLoader.java b/src/jalview/schemes/ColourSchemeLoader.java index 9f08688..481f809 100644 --- a/src/jalview/schemes/ColourSchemeLoader.java +++ b/src/jalview/schemes/ColourSchemeLoader.java @@ -20,14 +20,17 @@ */ package jalview.schemes; -import jalview.binding.JalviewUserColours; +import jalview.xml.binding.jalview.JalviewUserColours; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; -import org.exolab.castor.xml.Unmarshaller; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; public class ColourSchemeLoader { @@ -51,12 +54,14 @@ public class ColourSchemeLoader InputStreamReader in = new InputStreamReader( new FileInputStream(file), "UTF-8"); - jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours(); - - org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller( - jucs); - jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar - .unmarshal(in); + JAXBContext jc = JAXBContext + .newInstance("jalview.xml.binding.jalview"); + javax.xml.bind.Unmarshaller um = jc.createUnmarshaller(); + XMLStreamReader streamReader = XMLInputFactory.newInstance() + .createXMLStreamReader(in); + JAXBElement jbe = um.unmarshal(streamReader, + JalviewUserColours.class); + JalviewUserColours jucs = jbe.getValue(); /* * non-case-sensitive colours are for 20 amino acid codes, @@ -69,9 +74,9 @@ public class ColourSchemeLoader String name; int index; - for (int i = 0; i < jucs.getColourCount(); i++) + for (int i = 0; i < jucs.getColour().size(); i++) { - name = jucs.getColour(i).getName(); + name = jucs.getColour().get(i).getName(); if (ResidueProperties.aa3Hash.containsKey(name)) { index = ResidueProperties.aa3Hash.get(name).intValue(); @@ -86,7 +91,7 @@ public class ColourSchemeLoader } Color color = new Color( - Integer.parseInt(jucs.getColour(i).getRGB(), 16)); + Integer.parseInt(jucs.getColour().get(i).getRGB(), 16)); if (name.toLowerCase().equals(name)) { caseSensitive = true; @@ -117,7 +122,7 @@ public class ColourSchemeLoader jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours(); - jucs = JalviewUserColours.unmarshal(in); + jucs = jalview.binding.JalviewUserColours.unmarshal(in); newColours = new Color[jucs.getColourCount()]; -- 1.7.10.2