From: gmungoc Date: Tue, 18 Sep 2018 15:58:25 +0000 (+0100) Subject: JAL-3063 Save/Load user colour scheme using JAXB X-Git-Tag: Release_2_11_1_0~78^2~26 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=0c805692348a8a45c279ee5f4702c7bd5a393c5e;p=jalview.git JAL-3063 Save/Load user colour scheme using JAXB --- diff --git a/src/jalview/gui/FeatureSettings.java b/src/jalview/gui/FeatureSettings.java index 78c1cac..90d7c35 100644 --- a/src/jalview/gui/FeatureSettings.java +++ b/src/jalview/gui/FeatureSettings.java @@ -30,13 +30,14 @@ import jalview.datamodel.features.FeatureMatcherSetI; import jalview.gui.Help.HelpId; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; -import jalview.schemabinding.version2.Filter; -import jalview.schemabinding.version2.JalviewUserColours; -import jalview.schemabinding.version2.MatcherSet; import jalview.schemes.FeatureColour; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean; +import jalview.xml.binding.jalview.JalviewUserColours; +import jalview.xml.binding.jalview.JalviewUserColours.Colour; +import jalview.xml.binding.jalview.JalviewUserColours.Filter; +import jalview.xml.binding.jalview.ObjectFactory; import java.awt.BorderLayout; import java.awt.Color; @@ -98,6 +99,11 @@ import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.Marshaller; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; public class FeatureSettings extends JPanel implements FeatureSettingsControllerI @@ -874,30 +880,39 @@ public class FeatureSettings extends JPanel InputStreamReader in = new InputStreamReader( new FileInputStream(file), "UTF-8"); - JalviewUserColours jucs = JalviewUserColours.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(); + + // JalviewUserColours jucs = JalviewUserColours.unmarshal(in); /* * load feature colours */ - for (int i = jucs.getColourCount() - 1; i >= 0; i--) + for (int i = jucs.getColour().size() - 1; i >= 0; i--) { - jalview.schemabinding.version2.Colour newcol = jucs.getColour(i); - FeatureColourI colour = Jalview2XML.unmarshalColour(newcol); + Colour newcol = jucs.getColour().get(i); + FeatureColourI colour = jalview.project.Jalview2XML + .parseColour(newcol); fr.setColour(newcol.getName(), colour); - fr.setOrder(newcol.getName(), i / (float) jucs.getColourCount()); + fr.setOrder(newcol.getName(), i / (float) jucs.getColour().size()); } /* * load feature filters; loaded filters will replace any that are * currently defined, other defined filters are left unchanged */ - for (int i = 0; i < jucs.getFilterCount(); i++) + for (int i = 0; i < jucs.getFilter().size(); i++) { - jalview.schemabinding.version2.Filter filterModel = jucs - .getFilter(i); + Filter filterModel = jucs.getFilter().get(i); String featureType = filterModel.getFeatureType(); - FeatureMatcherSetI filter = Jalview2XML.unmarshalFilter(featureType, - filterModel.getMatcherSet()); + FeatureMatcherSetI filter = jalview.project.Jalview2XML + .parseFilter(featureType, filterModel.getMatcherSet()); if (!filter.isEmpty()) { fr.setFeatureFilter(featureType, filter); @@ -979,9 +994,9 @@ public class FeatureSettings extends JPanel for (String featureType : sortedTypes) { FeatureColourI fcol = fr.getFeatureStyle(featureType); - jalview.schemabinding.version2.Colour col = Jalview2XML.marshalColour( - featureType, fcol); - ucs.addColour(col); + Colour col = jalview.project.Jalview2XML.marshalColour(featureType, + fcol); + ucs.getColour().add(col); } /* @@ -994,16 +1009,26 @@ public class FeatureSettings extends JPanel { Iterator iterator = filter.getMatchers().iterator(); FeatureMatcherI firstMatcher = iterator.next(); - MatcherSet ms = Jalview2XML.marshalFilter(firstMatcher, iterator, + jalview.xml.binding.jalview.FeatureMatcherSet ms = jalview.project.Jalview2XML + .marshalFilter(firstMatcher, iterator, filter.isAnded()); Filter filterModel = new Filter(); filterModel.setFeatureType(featureType); filterModel.setMatcherSet(ms); - ucs.addFilter(filterModel); + ucs.getFilter().add(filterModel); } } + JAXBContext jaxbContext = JAXBContext + .newInstance(JalviewUserColours.class); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + jaxbMarshaller.marshal( + new ObjectFactory().createJalviewUserColours(ucs), out); + + // jaxbMarshaller.marshal(object, pout); + // marshaller.marshal(object); + out.flush(); - ucs.marshal(out); + // ucs.marshal(out); out.close(); } catch (Exception ex) { diff --git a/src/jalview/project/Jalview2XML.java b/src/jalview/project/Jalview2XML.java index 5106e45..c4ad897 100644 --- a/src/jalview/project/Jalview2XML.java +++ b/src/jalview/project/Jalview2XML.java @@ -4815,7 +4815,7 @@ public class Jalview2XML if (filters != null) { FeatureMatcherSetI filter = Jalview2XML - .unmarshalFilter(featureType, filters); + .parseFilter(featureType, filters); if (!filter.isEmpty()) { fr.setFeatureFilter(featureType, filter); @@ -5873,7 +5873,7 @@ public class Jalview2XML * @param fcol * @return */ - protected static Colour marshalColour( + public static Colour marshalColour( String featureType, FeatureColourI fcol) { Colour col = new Colour(); @@ -5930,7 +5930,7 @@ public class Jalview2XML * @param and * if true, conditions are and-ed, else or-ed */ - protected static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter( + public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter( FeatureMatcherI firstMatcher, Iterator filters, boolean and) { @@ -5996,14 +5996,14 @@ public class Jalview2XML * @param matcherSetModel * @return */ - protected static FeatureMatcherSetI unmarshalFilter( + public static FeatureMatcherSetI parseFilter( String featureType, jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel) { FeatureMatcherSetI result = new FeatureMatcherSet(); try { - unmarshalFilterConditions(result, matcherSetModel, true); + parseFilterConditions(result, matcherSetModel, true); } catch (IllegalStateException e) { // mixing AND and OR conditions perhaps @@ -6027,7 +6027,7 @@ public class Jalview2XML * @throws IllegalStateException * if AND and OR conditions are mixed */ - protected static void unmarshalFilterConditions( + protected static void parseFilterConditions( FeatureMatcherSetI matcherSet, jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel, boolean and) @@ -6080,13 +6080,12 @@ public class Jalview2XML * compound condition */ List matchers = matcherSetModel - .getCompoundMatcher() - .getMatcherSet(); + .getCompoundMatcher().getMatcherSet(); boolean anded = matcherSetModel.getCompoundMatcher().isAnd(); if (matchers.size() == 2) { - unmarshalFilterConditions(matcherSet, matchers.get(0), anded); - unmarshalFilterConditions(matcherSet, matchers.get(1), anded); + parseFilterConditions(matcherSet, matchers.get(0), anded); + parseFilterConditions(matcherSet, matchers.get(1), anded); } else { @@ -6101,7 +6100,7 @@ public class Jalview2XML * @param colourModel * @return */ - protected static FeatureColourI unmarshalColour(Colour colourModel) + public static FeatureColourI parseColour(Colour colourModel) { FeatureColourI colour = null;