From: James Procter Date: Mon, 30 Oct 2023 11:24:07 +0000 (+0000) Subject: JAL-4315 allow feature settings to be loaded from a file or input stream without... X-Git-Tag: Release_2_11_4_0~119^2~1 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=fbd682c00e543eadb48074ac354ade57c04c20cb;p=jalview.git JAL-4315 allow feature settings to be loaded from a file or input stream without the dialog being open --- diff --git a/src/jalview/gui/FeatureSettings.java b/src/jalview/gui/FeatureSettings.java index 4d31805..3eccdf0 100644 --- a/src/jalview/gui/FeatureSettings.java +++ b/src/jalview/gui/FeatureSettings.java @@ -45,6 +45,7 @@ import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.io.Reader; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; @@ -103,6 +104,8 @@ import jalview.datamodel.features.FeatureMatcherSet; import jalview.datamodel.features.FeatureMatcherSetI; import jalview.gui.Help.HelpId; import jalview.gui.JalviewColourChooser.ColourChooserListener; +import jalview.io.DataSourceType; +import jalview.io.FileParse; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; import jalview.schemes.FeatureColour; @@ -955,6 +958,65 @@ public class FeatureSettings extends JPanel chooser.showOpenDialog(this); } + public static boolean loadFeatureSettingsFile(FeatureRenderer fr, + File file) throws Exception + { + InputStreamReader in = new InputStreamReader(new FileInputStream(file), + "UTF-8"); + return loadFeatureSettingsFile(fr, in); + } + + public static void loadFeatureSettingsFile( + FeatureRenderer featureRenderer, Object fileObject, + DataSourceType sourceType) throws Exception + { + FileParse fp = new FileParse(fileObject, sourceType); + loadFeatureSettingsFile(featureRenderer, fp.getReader()); + } + + private static boolean loadFeatureSettingsFile(FeatureRenderer fr, + Reader in) throws Exception + { + 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.getColour().size() - 1; i >= 0; i--) + { + 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.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.getFilter().size(); i++) + { + Filter filterModel = jucs.getFilter().get(i); + String featureType = filterModel.getFeatureType(); + FeatureMatcherSetI filter = jalview.project.Jalview2XML + .parseFilter(featureType, filterModel.getMatcherSet()); + if (!filter.isEmpty()) + { + fr.setFeatureFilter(featureType, filter); + } + } + return true; + } + /** * Loads feature colours and filters from XML stored in the given file * @@ -962,50 +1024,21 @@ public class FeatureSettings extends JPanel */ void load(File file) { + load(file, DataSourceType.FILE); + } + + /** + * Loads feature colours and filters from XML at a specified source + * + * @param file + * - string or file or other object that allows FileParse to be + * created + */ + void load(Object file, DataSourceType sourceType) + { try { - InputStreamReader in = new InputStreamReader( - new FileInputStream(file), "UTF-8"); - - 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.getColour().size() - 1; i >= 0; i--) - { - 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.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.getFilter().size(); i++) - { - Filter filterModel = jucs.getFilter().get(i); - String featureType = filterModel.getFeatureType(); - FeatureMatcherSetI filter = jalview.project.Jalview2XML - .parseFilter(featureType, filterModel.getMatcherSet()); - if (!filter.isEmpty()) - { - fr.setFeatureFilter(featureType, filter); - } - } - + loadFeatureSettingsFile(fr, file, sourceType); /* * update feature settings table */ @@ -1019,7 +1052,8 @@ public class FeatureSettings extends JPanel } } catch (Exception ex) { - jalview.bin.Console.outPrintln("Error loading User Colour File\n" + ex); + jalview.bin.Console + .outPrintln("Error loading User Colour File\n" + ex); } } @@ -2119,7 +2153,6 @@ public class FeatureSettings extends JPanel refreshDisplay(); } } - class FeatureIcon implements Icon { FeatureColourI gcol;