From 09a5917c98f1140162c1de36d5cdbf22e8ac9c02 Mon Sep 17 00:00:00 2001 From: plaa Date: Wed, 25 Jan 2012 21:42:20 +0000 Subject: [PATCH] document loading refactoring git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@372 180e2498-e6e9-4542-8430-84ac67f01cd8 --- core/fileformat.txt | 10 +- core/resources/l10n/messages.properties | 4 + .../net/sf/openrocket/database/Databases.java | 14 +- .../sf/openrocket/database/MotorDatabase.java | 6 +- .../openrocket/file/AbstractRocketLoader.java | 77 ++++++++ .../openrocket/file/GeneralRocketLoader.java | 65 ++++--- .../net/sf/openrocket/file/RocketLoader.java | 65 +------ .../file/openrocket/OpenRocketSaver.java | 51 +++--- .../importt/DocumentLoadingContext.java | 15 ++ .../{ => importt}/OpenRocketLoader.java | 170 +++++++++++++----- .../{ => importt}/PreferredMotorDigests.java | 4 +- .../file/rocksim/importt/RocksimLoader.java | 11 +- .../gui/scalefigure/AbstractScaleFigure.java | 13 +- 13 files changed, 324 insertions(+), 181 deletions(-) create mode 100644 core/src/net/sf/openrocket/file/AbstractRocketLoader.java create mode 100644 core/src/net/sf/openrocket/file/openrocket/importt/DocumentLoadingContext.java rename core/src/net/sf/openrocket/file/openrocket/{ => importt}/OpenRocketLoader.java (93%) rename core/src/net/sf/openrocket/file/openrocket/{ => importt}/PreferredMotorDigests.java (99%) diff --git a/core/fileformat.txt b/core/fileformat.txt index 17247f26..3ba93461 100644 --- a/core/fileformat.txt +++ b/core/fileformat.txt @@ -5,7 +5,7 @@ reference implementation. This will hopefully change in the future. The "version" attribute of the tag describes the file format version used, while the "creator" attribute MAY describe the -software version used to write the document. +software and version used to write the document. The file format version is increased every time the format is changed. The minor number is increased when changes are made that are @@ -20,11 +20,6 @@ version that supports all the necessary design features. The following file format versions exist: -0.9: Used before the first public release of Openrocket; effectively - equivalent to 1.0. Should not be used when writing documents. - OpenRocket accepts this version when reading files, but other - software need not. - 1.0: File format version of the first public release (OpenRocket 0.9.0) 1.1: Introduced with OpenRocket 0.9.4. Adds support for saving fin @@ -40,4 +35,5 @@ The following file format versions exist: parameters to the simulation conditions element. 1.4: Introduced with OpenRocket 1.1.10. Adds the launchrodvelocity and - deploymentvelocity attributes to element. \ No newline at end of file + deploymentvelocity attributes to element. The motor + digesting algorithm was changed. diff --git a/core/resources/l10n/messages.properties b/core/resources/l10n/messages.properties index e385eb94..58fd496a 100644 --- a/core/resources/l10n/messages.properties +++ b/core/resources/l10n/messages.properties @@ -1023,9 +1023,11 @@ main.menu.debug.createtestrocket = Create test rocket ! Material database ! BULK_MATERIAL Databases.materials.Acrylic = Acrylic +Databases.materials.Aluminum = Aluminum Databases.materials.Balsa = Balsa Databases.materials.Basswood = Basswood Databases.materials.Birch = Birch +Databases.materials.Brass = Brass Databases.materials.Cardboard = Cardboard Databases.materials.Carbonfiber = Carbon fiber Databases.materials.Cork = Cork @@ -1040,8 +1042,10 @@ Databases.materials.PolycarbonateLexan = Polycarbonate (Lexan) Databases.materials.Polystyrene = Polystyrene Databases.materials.PVC = PVC Databases.materials.Spruce = Spruce +Databases.materials.Steel = Steel Databases.materials.StyrofoamgenericEPS = Styrofoam (generic EPS) Databases.materials.StyrofoamBluefoamXPS = Styrofoam \"Blue foam\" (XPS) +Databases.materials.Titanium = Titanium Databases.materials.Quantumtubing = Quantum tubing Databases.materials.BlueTube = Blue tube !SURFACE_MATERIAL diff --git a/core/src/net/sf/openrocket/database/Databases.java b/core/src/net/sf/openrocket/database/Databases.java index d8a132b0..30797e31 100644 --- a/core/src/net/sf/openrocket/database/Databases.java +++ b/core/src/net/sf/openrocket/database/Databases.java @@ -18,7 +18,7 @@ public class Databases { private static final Translator trans = Application.getTranslator(); /* Static implementations of specific databases: */ - + /** * A database of bulk materials (with bulk densities). */ @@ -32,15 +32,17 @@ public class Databases { */ public static final Database LINE_MATERIAL = new Database(); - - + + static { // Add default materials BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Acrylic"), 1190, false)); + BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Aluminum"), 2700, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Balsa"), 170, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Basswood"), 500, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Birch"), 670, false)); + BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Brass"), 8600, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Cardboard"), 680, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Carbonfiber"), 1780, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Cork"), 240, false)); @@ -55,9 +57,11 @@ public class Databases { BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Polystyrene"), 1050, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.PVC"), 1390, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Spruce"), 450, false)); + BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Steel"), 7850, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.StyrofoamgenericEPS"), 20, false)); // BULK_MATERIAL.add(new Material.Bulk("Styrofoam (Blue foam, XPS)", 32, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.StyrofoamBluefoamXPS"), 32, false)); + BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Titanium"), 4500, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.Quantumtubing"), 1050, false)); BULK_MATERIAL.add(new Material.Bulk(trans.get("Databases.materials.BlueTube"), 1300, false)); @@ -93,7 +97,7 @@ public class Databases { //// Tubular nylon (25 mm, 1 in) LINE_MATERIAL.add(new Material.Line(trans.get("Databases.materials.Tubularnylon25mm"), 0.029, false)); - + // Add user-defined materials for (Material m : Application.getPreferences().getUserMaterials()) { switch (m.getType()) { @@ -198,5 +202,5 @@ public class Databases { return Material.newMaterial(type, name, density, userDefined); } - + } diff --git a/core/src/net/sf/openrocket/database/MotorDatabase.java b/core/src/net/sf/openrocket/database/MotorDatabase.java index fdde5fac..0a1e9095 100644 --- a/core/src/net/sf/openrocket/database/MotorDatabase.java +++ b/core/src/net/sf/openrocket/database/MotorDatabase.java @@ -5,7 +5,7 @@ import java.util.List; import net.sf.openrocket.motor.Motor; public interface MotorDatabase { - + /** * Return all motors in the database matching a search criteria. Any search criteria that * is null or NaN is ignored. @@ -20,5 +20,5 @@ public interface MotorDatabase { public List findMotors(Motor.Type type, String manufacturer, String designation, double diameter, double length); - -} \ No newline at end of file + +} diff --git a/core/src/net/sf/openrocket/file/AbstractRocketLoader.java b/core/src/net/sf/openrocket/file/AbstractRocketLoader.java new file mode 100644 index 00000000..0cb19110 --- /dev/null +++ b/core/src/net/sf/openrocket/file/AbstractRocketLoader.java @@ -0,0 +1,77 @@ +package net.sf.openrocket.file; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import net.sf.openrocket.aerodynamics.WarningSet; +import net.sf.openrocket.document.OpenRocketDocument; + + +public abstract class AbstractRocketLoader implements RocketLoader { + protected final WarningSet warnings = new WarningSet(); + + + /** + * Loads a rocket from the specified File object. + */ + @Override + public final OpenRocketDocument load(File source) throws RocketLoadException { + warnings.clear(); + InputStream stream = null; + + try { + + stream = new BufferedInputStream(new FileInputStream(source)); + return load(stream); + + } catch (FileNotFoundException e) { + throw new RocketLoadException("File not found: " + source); + } finally { + if (stream != null) { + try { + stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * Loads a rocket from the specified InputStream. + */ + @Override + public final OpenRocketDocument load(InputStream source) throws RocketLoadException { + warnings.clear(); + + try { + return loadFromStream(source); + } catch (RocketLoadException e) { + throw e; + } catch (IOException e) { + throw new RocketLoadException("I/O error: " + e.getMessage(), e); + } + } + + + + /** + * This method is called by the default implementations of {@link #load(File)} + * and {@link #load(InputStream)} to load the rocket. + * + * @throws RocketLoadException if an error occurs during loading. + */ + protected abstract OpenRocketDocument loadFromStream(InputStream source) throws IOException, + RocketLoadException; + + + + @Override + public final WarningSet getWarnings() { + return warnings; + } +} diff --git a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java index b6117f78..68555d10 100644 --- a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java +++ b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java @@ -1,14 +1,17 @@ package net.sf.openrocket.file; -import net.sf.openrocket.document.OpenRocketDocument; -import net.sf.openrocket.file.openrocket.OpenRocketLoader; - import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.Arrays; import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.file.openrocket.importt.OpenRocketLoader; +import net.sf.openrocket.file.rocksim.importt.RocksimLoader; /** @@ -18,24 +21,23 @@ import java.util.zip.GZIPInputStream; * * @author Sampo Niskanen */ -public class GeneralRocketLoader extends RocketLoader { - +public class GeneralRocketLoader extends AbstractRocketLoader { + private static final int READ_BYTES = 300; - private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b - private static final byte[] OPENROCKET_SIGNATURE = - ""); writeln(""); @@ -121,7 +121,7 @@ public class OpenRocketSaver extends RocketSaver { } - + @Override public long estimateFileSize(OpenRocketDocument doc, StorageOptions options) { @@ -141,14 +141,14 @@ public class OpenRocketSaver extends RocketSaver { else size += componentCount * BYTES_PER_COMPONENT_UNCOMPRESSED; - + // Size per simulation if (options.isCompressionEnabled()) size += doc.getSimulationCount() * BYTES_PER_SIMULATION_COMPRESSED; else size += doc.getSimulationCount() * BYTES_PER_SIMULATION_UNCOMPRESSED; - + // Size per flight data point int pointCount = 0; double timeSkip = options.getSimulationTimeSkip(); @@ -183,7 +183,8 @@ public class OpenRocketSaver extends RocketSaver { */ private int calculateNecessaryFileVersion(OpenRocketDocument document, StorageOptions opts) { /* - * File version 1.2 is required for: + * File version 1.4 is required for: + * - saving simulation data * - saving motor data * * File version 1.1 is required for: @@ -192,13 +193,13 @@ public class OpenRocketSaver extends RocketSaver { * * Otherwise use version 1.0. */ - - // Check if design has simulations defined (version 1.3) + + // Check if design has simulations defined (version 1.4) if (document.getSimulationCount() > 0) { - return FILE_VERSION_DIVISOR + 3; + return FILE_VERSION_DIVISOR + 4; } - // Check for motor definitions (version 1.2) + // Check for motor definitions (version 1.4) Iterator iterator = document.getRocket().iterator(); while (iterator.hasNext()) { RocketComponent c = iterator.next(); @@ -208,7 +209,7 @@ public class OpenRocketSaver extends RocketSaver { MotorMount mount = (MotorMount) c; for (String id : document.getRocket().getMotorConfigurationIDs()) { if (mount.getMotor(id) != null) { - return FILE_VERSION_DIVISOR + 2; + return FILE_VERSION_DIVISOR + 4; } } } @@ -240,7 +241,7 @@ public class OpenRocketSaver extends RocketSaver { } - + @SuppressWarnings("unchecked") private void saveComponent(RocketComponent component) throws IOException { @@ -335,12 +336,12 @@ public class OpenRocketSaver extends RocketSaver { indent--; writeln(""); - + for (String s : simulation.getSimulationListeners()) { writeElement("listener", escapeXML(s)); } - + // Write basic simulation data FlightData data = simulation.getSimulatedData(); @@ -393,7 +394,7 @@ public class OpenRocketSaver extends RocketSaver { } - + private void saveFlightDataBranch(FlightDataBranch branch, double timeSkip) throws IOException { double previousTime = -100000; @@ -462,7 +463,7 @@ public class OpenRocketSaver extends RocketSaver { } - + /* TODO: LOW: This is largely duplicated from above! */ private int countFlightDataBranchPoints(FlightDataBranch branch, double timeSkip) { int count = 0; @@ -506,7 +507,7 @@ public class OpenRocketSaver extends RocketSaver { } - + private void writeDataPointString(List> data, int index, StringBuilder sb) throws IOException { sb.setLength(0); @@ -521,7 +522,7 @@ public class OpenRocketSaver extends RocketSaver { } - + private void writeElement(String element, Object content) throws IOException { if (content == null) content = ""; @@ -529,7 +530,7 @@ public class OpenRocketSaver extends RocketSaver { } - + private void writeln(String str) throws IOException { if (str.length() == 0) { dest.write("\n"); @@ -543,8 +544,8 @@ public class OpenRocketSaver extends RocketSaver { } - - + + /** * Return the XML equivalent of an enum name. * diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/DocumentLoadingContext.java b/core/src/net/sf/openrocket/file/openrocket/importt/DocumentLoadingContext.java new file mode 100644 index 00000000..ec80db42 --- /dev/null +++ b/core/src/net/sf/openrocket/file/openrocket/importt/DocumentLoadingContext.java @@ -0,0 +1,15 @@ +package net.sf.openrocket.file.openrocket.importt; + +public class DocumentLoadingContext { + + private int fileVersion; + + public int getFileVersion() { + return fileVersion; + } + + public void setFileVersion(int fileVersion) { + this.fileVersion = fileVersion; + } + +} diff --git a/core/src/net/sf/openrocket/file/openrocket/OpenRocketLoader.java b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java similarity index 93% rename from core/src/net/sf/openrocket/file/openrocket/OpenRocketLoader.java rename to core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java index f8d6bed5..071d5710 100644 --- a/core/src/net/sf/openrocket/file/openrocket/OpenRocketLoader.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java @@ -1,4 +1,4 @@ -package net.sf.openrocket.file.openrocket; +package net.sf.openrocket.file.openrocket.importt; import java.io.IOException; import java.io.InputStream; @@ -7,6 +7,8 @@ import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import net.sf.openrocket.aerodynamics.Warning; import net.sf.openrocket.aerodynamics.WarningSet; @@ -15,8 +17,8 @@ import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.Simulation; import net.sf.openrocket.document.Simulation.Status; import net.sf.openrocket.document.StorageOptions; +import net.sf.openrocket.file.AbstractRocketLoader; import net.sf.openrocket.file.RocketLoadException; -import net.sf.openrocket.file.RocketLoader; import net.sf.openrocket.file.simplesax.ElementHandler; import net.sf.openrocket.file.simplesax.PlainTextHandler; import net.sf.openrocket.file.simplesax.SimpleSAX; @@ -90,7 +92,7 @@ import org.xml.sax.SAXException; * * @author Sampo Niskanen */ -public class OpenRocketLoader extends RocketLoader { +public class OpenRocketLoader extends AbstractRocketLoader { private static final LogHelper log = Application.getLogger(); @@ -98,11 +100,12 @@ public class OpenRocketLoader extends RocketLoader { public OpenRocketDocument loadFromStream(InputStream source) throws RocketLoadException, IOException { log.info("Loading .ork file"); + DocumentLoadingContext context = new DocumentLoadingContext(); InputSource xmlSource = new InputSource(source); - OpenRocketHandler handler = new OpenRocketHandler(); + OpenRocketHandler handler = new OpenRocketHandler(context); + - try { SimpleSAX.readXML(xmlSource, handler, warnings); } catch (SAXException e) { @@ -110,7 +113,7 @@ public class OpenRocketLoader extends RocketLoader { throw new RocketLoadException("Malformed XML in input.", e); } - + OpenRocketDocument doc = handler.getDocument(); doc.getDefaultConfiguration().setAllStages(); @@ -157,9 +160,16 @@ public class OpenRocketLoader extends RocketLoader { class DocumentConfig { /* Remember to update OpenRocketSaver as well! */ - public static final String[] SUPPORTED_VERSIONS = { "0.9", "1.0", "1.1", "1.2", "1.3" }; + public static final String[] SUPPORTED_VERSIONS = { "1.0", "1.1", "1.2", "1.3", "1.4" }; + + /** + * Divisor used in converting an integer version to the point-represented version. + * The integer version divided by this value is the major version and the remainder is + * the minor version. For example 101 corresponds to file version "1.1". + */ + public static final int FILE_VERSION_DIVISOR = 100; + - //////// Component constructors static final HashMap> constructors = new HashMap>(); static { @@ -194,7 +204,7 @@ class DocumentConfig { } } - + //////// Parameter setters /* * The keys are of the form Class:param, where Class is the class name and param @@ -399,7 +409,7 @@ class DocumentConfig { "auto", Reflection.findMethod(CenteringRing.class, "setOuterRadiusAutomatic", boolean.class))); - + // MassObject setters.put("MassObject:packedlength", new DoubleSetter( Reflection.findMethod(MassObject.class, "setLength", double.class))); @@ -526,8 +536,13 @@ class DocumentConfig { * the contents to be read by a OpenRocketContentsHandler. */ class OpenRocketHandler extends ElementHandler { + private final DocumentLoadingContext context; private OpenRocketContentHandler handler = null; + public OpenRocketHandler(DocumentLoadingContext context) { + this.context = context; + } + /** * Return the OpenRocketDocument read from the file, or null if a document * has not been read yet. @@ -551,7 +566,7 @@ class OpenRocketHandler extends ElementHandler { // Check for first call if (handler != null) { warnings.add(Warning.fromString("Multiple document elements found, ignoring later " - + "ones.")); + + "ones.")); return null; } @@ -575,10 +590,27 @@ class OpenRocketHandler extends ElementHandler { warnings.add(str); } - handler = new OpenRocketContentHandler(); + context.setFileVersion(parseVersion(docVersion)); + + handler = new OpenRocketContentHandler(context); return handler; } + + private int parseVersion(String docVersion) { + if (docVersion == null) + return 0; + + Matcher m = Pattern.compile("^([0-9]+)\\.([0-9]+)$").matcher(docVersion); + if (m.matches()) { + int major = Integer.parseInt(m.group(1)); + int minor = Integer.parseInt(m.group(2)); + return major * DocumentConfig.FILE_VERSION_DIVISOR + minor; + } else { + return 0; + } + } + @Override public void closeElement(String element, HashMap attributes, String content, WarningSet warnings) throws SAXException { @@ -587,7 +619,7 @@ class OpenRocketHandler extends ElementHandler { super.closeElement(element, attributes, content, warnings); } - + } @@ -595,13 +627,15 @@ class OpenRocketHandler extends ElementHandler { * Handles the content of the tag. */ class OpenRocketContentHandler extends ElementHandler { + private final DocumentLoadingContext context; private final OpenRocketDocument doc; private final Rocket rocket; private boolean rocketDefined = false; private boolean simulationsDefined = false; - public OpenRocketContentHandler() { + public OpenRocketContentHandler(DocumentLoadingContext context) { + this.context = context; this.rocket = new Rocket(); this.doc = new OpenRocketDocument(rocket); } @@ -625,7 +659,7 @@ class OpenRocketContentHandler extends ElementHandler { return null; } rocketDefined = true; - return new ComponentParameterHandler(rocket); + return new ComponentParameterHandler(rocket, context); } if (element.equals("simulations")) { @@ -636,7 +670,7 @@ class OpenRocketContentHandler extends ElementHandler { return null; } simulationsDefined = true; - return new SimulationsHandler(doc); + return new SimulationsHandler(doc, context); } warnings.add(Warning.fromString("Unknown element " + element + ", ignoring.")); @@ -653,10 +687,12 @@ class OpenRocketContentHandler extends ElementHandler { * contents is passed on to ComponentParameterHandler. */ class ComponentHandler extends ElementHandler { + private final DocumentLoadingContext context; private final RocketComponent parent; - public ComponentHandler(RocketComponent parent) { + public ComponentHandler(RocketComponent parent, DocumentLoadingContext context) { this.parent = parent; + this.context = context; } @Override @@ -684,7 +720,7 @@ class ComponentHandler extends ElementHandler { parent.addChild(c); - return new ComponentParameterHandler(c); + return new ComponentParameterHandler(c, context); } } @@ -695,10 +731,12 @@ class ComponentHandler extends ElementHandler { * elements. */ class ComponentParameterHandler extends ElementHandler { + private final DocumentLoadingContext context; private final RocketComponent component; - public ComponentParameterHandler(RocketComponent c) { + public ComponentParameterHandler(RocketComponent c, DocumentLoadingContext context) { this.component = c; + this.context = context; } @Override @@ -707,31 +745,31 @@ class ComponentParameterHandler extends ElementHandler { // Check for specific elements that contain other elements if (element.equals("subcomponents")) { - return new ComponentHandler(component); + return new ComponentHandler(component, context); } if (element.equals("motormount")) { if (!(component instanceof MotorMount)) { warnings.add(Warning.fromString("Illegal component defined as motor mount.")); return null; } - return new MotorMountHandler((MotorMount) component); + return new MotorMountHandler((MotorMount) component, context); } if (element.equals("finpoints")) { if (!(component instanceof FreeformFinSet)) { warnings.add(Warning.fromString("Illegal component defined for fin points.")); return null; } - return new FinSetPointHandler((FreeformFinSet) component); + return new FinSetPointHandler((FreeformFinSet) component, context); } if (element.equals("motorconfiguration")) { if (!(component instanceof Rocket)) { warnings.add(Warning.fromString("Illegal component defined for motor configuration.")); return null; } - return new MotorConfigurationHandler((Rocket) component); + return new MotorConfigurationHandler((Rocket) component, context); } - + return PlainTextHandler.INSTANCE; } @@ -774,11 +812,14 @@ class ComponentParameterHandler extends ElementHandler { * elements. */ class FinSetPointHandler extends ElementHandler { + @SuppressWarnings("unused") + private final DocumentLoadingContext context; private final FreeformFinSet finset; private final ArrayList coordinates = new ArrayList(); - public FinSetPointHandler(FreeformFinSet finset) { + public FinSetPointHandler(FreeformFinSet finset, DocumentLoadingContext context) { this.finset = finset; + this.context = context; } @Override @@ -823,11 +864,13 @@ class FinSetPointHandler extends ElementHandler { class MotorMountHandler extends ElementHandler { + private final DocumentLoadingContext context; private final MotorMount mount; private MotorHandler motorHandler; - public MotorMountHandler(MotorMount mount) { + public MotorMountHandler(MotorMount mount, DocumentLoadingContext context) { this.mount = mount; + this.context = context; mount.setMotorMount(true); } @@ -836,7 +879,7 @@ class MotorMountHandler extends ElementHandler { WarningSet warnings) { if (element.equals("motor")) { - motorHandler = new MotorHandler(); + motorHandler = new MotorHandler(context); return motorHandler; } @@ -851,7 +894,7 @@ class MotorMountHandler extends ElementHandler { } - + @Override public void closeElement(String element, HashMap attributes, String content, WarningSet warnings) throws SAXException { @@ -917,12 +960,15 @@ class MotorMountHandler extends ElementHandler { class MotorConfigurationHandler extends ElementHandler { + @SuppressWarnings("unused") + private final DocumentLoadingContext context; private final Rocket rocket; private String name = null; private boolean inNameElement = false; - public MotorConfigurationHandler(Rocket rocket) { + public MotorConfigurationHandler(Rocket rocket, DocumentLoadingContext context) { this.rocket = rocket; + this.context = context; } @Override @@ -973,6 +1019,11 @@ class MotorConfigurationHandler extends ElementHandler { class MotorHandler extends ElementHandler { + /** File version where latest digest format was introduced */ + private static final int MOTOR_DIGEST_VERSION = 104; + + @SuppressWarnings("unused") + private final DocumentLoadingContext context; private Motor.Type type = null; private String manufacturer = null; private String designation = null; @@ -981,6 +1032,11 @@ class MotorHandler extends ElementHandler { private double length = Double.NaN; private double delay = Double.NaN; + public MotorHandler(DocumentLoadingContext context) { + this.context = context; + } + + @Override public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { @@ -1105,8 +1161,10 @@ class MotorHandler extends ElementHandler { } else if (element.equals("digest")) { - // Digest - digest = content.trim(); + // Digest is used only for file versions saved using the same digest algorithm + if (context.getFileVersion() >= MOTOR_DIGEST_VERSION) { + digest = content.trim(); + } } else if (element.equals("diameter")) { @@ -1162,11 +1220,13 @@ class MotorHandler extends ElementHandler { class SimulationsHandler extends ElementHandler { + private final DocumentLoadingContext context; private final OpenRocketDocument doc; private SingleSimulationHandler handler; - public SimulationsHandler(OpenRocketDocument doc) { + public SimulationsHandler(OpenRocketDocument doc, DocumentLoadingContext context) { this.doc = doc; + this.context = context; } @Override @@ -1178,7 +1238,7 @@ class SimulationsHandler extends ElementHandler { return null; } - handler = new SingleSimulationHandler(doc); + handler = new SingleSimulationHandler(doc, context); return handler; } @@ -1189,10 +1249,11 @@ class SimulationsHandler extends ElementHandler { super.closeElement(element, attributes, content, warnings); } - + } class SingleSimulationHandler extends ElementHandler { + private final DocumentLoadingContext context; private final OpenRocketDocument doc; @@ -1203,12 +1264,13 @@ class SingleSimulationHandler extends ElementHandler { private final List listeners = new ArrayList(); - public SingleSimulationHandler(OpenRocketDocument doc) { + public SingleSimulationHandler(OpenRocketDocument doc, DocumentLoadingContext context) { this.doc = doc; + this.context = context; } - + @Override public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { @@ -1217,10 +1279,10 @@ class SingleSimulationHandler extends ElementHandler { element.equals("calculator") || element.equals("listener")) { return PlainTextHandler.INSTANCE; } else if (element.equals("conditions")) { - conditionHandler = new SimulationConditionsHandler(doc.getRocket()); + conditionHandler = new SimulationConditionsHandler(doc.getRocket(), context); return conditionHandler; } else if (element.equals("flightdata")) { - dataHandler = new FlightDataHandler(); + dataHandler = new FlightDataHandler(context); return dataHandler; } else { warnings.add("Unknown element '" + element + "', ignoring."); @@ -1286,10 +1348,12 @@ class SingleSimulationHandler extends ElementHandler { class SimulationConditionsHandler extends ElementHandler { + private final DocumentLoadingContext context; private SimulationOptions conditions; private AtmosphereHandler atmosphereHandler; - public SimulationConditionsHandler(Rocket rocket) { + public SimulationConditionsHandler(Rocket rocket, DocumentLoadingContext context) { + this.context = context; conditions = new SimulationOptions(rocket); // Set up default loading settings (which may differ from the new defaults) conditions.setGeodeticComputation(GeodeticComputationStrategy.FLAT); @@ -1303,7 +1367,7 @@ class SimulationConditionsHandler extends ElementHandler { public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { if (element.equals("atmosphere")) { - atmosphereHandler = new AtmosphereHandler(attributes.get("model")); + atmosphereHandler = new AtmosphereHandler(attributes.get("model"), context); return atmosphereHandler; } return PlainTextHandler.INSTANCE; @@ -1319,7 +1383,7 @@ class SimulationConditionsHandler extends ElementHandler { } catch (NumberFormatException ignore) { } - + if (element.equals("configid")) { if (content.equals("")) { conditions.setMotorConfigurationID(null); @@ -1396,12 +1460,15 @@ class SimulationConditionsHandler extends ElementHandler { class AtmosphereHandler extends ElementHandler { + @SuppressWarnings("unused") + private final DocumentLoadingContext context; private final String model; private double temperature = Double.NaN; private double pressure = Double.NaN; - public AtmosphereHandler(String model) { + public AtmosphereHandler(String model, DocumentLoadingContext context) { this.model = model; + this.context = context; } @Override @@ -1458,6 +1525,7 @@ class AtmosphereHandler extends ElementHandler { class FlightDataHandler extends ElementHandler { + private final DocumentLoadingContext context; private FlightDataBranchHandler dataHandler; private WarningSet warningSet = new WarningSet(); @@ -1465,6 +1533,11 @@ class FlightDataHandler extends ElementHandler { private FlightData data; + + public FlightDataHandler(DocumentLoadingContext context) { + this.context = context; + } + public FlightData getFlightData() { return data; } @@ -1482,7 +1555,7 @@ class FlightDataHandler extends ElementHandler { return null; } dataHandler = new FlightDataBranchHandler(attributes.get("name"), - attributes.get("types")); + attributes.get("types"), context); return dataHandler; } @@ -1569,15 +1642,18 @@ class FlightDataHandler extends ElementHandler { data.immute(); } - + } class FlightDataBranchHandler extends ElementHandler { + @SuppressWarnings("unused") + private final DocumentLoadingContext context; private final FlightDataType[] types; private final FlightDataBranch branch; - public FlightDataBranchHandler(String name, String typeList) { + public FlightDataBranchHandler(String name, String typeList, DocumentLoadingContext context) { + this.context = context; String[] split = typeList.split(","); types = new FlightDataType[split.length]; for (int i = 0; i < split.length; i++) { @@ -1639,7 +1715,7 @@ class FlightDataBranchHandler extends ElementHandler { // element == "datapoint" - + // Check line format String[] split = content.split(","); if (split.length != types.length) { @@ -2051,7 +2127,7 @@ class FinTabPositionSetter extends DoubleSetter { super.set(c, s, attributes, warnings); } - + } diff --git a/core/src/net/sf/openrocket/file/openrocket/PreferredMotorDigests.java b/core/src/net/sf/openrocket/file/openrocket/importt/PreferredMotorDigests.java similarity index 99% rename from core/src/net/sf/openrocket/file/openrocket/PreferredMotorDigests.java rename to core/src/net/sf/openrocket/file/openrocket/importt/PreferredMotorDigests.java index d05cccbb..ffcedf78 100644 --- a/core/src/net/sf/openrocket/file/openrocket/PreferredMotorDigests.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/PreferredMotorDigests.java @@ -1,4 +1,4 @@ -package net.sf.openrocket.file.openrocket; +package net.sf.openrocket.file.openrocket.importt; import java.util.Collections; import java.util.HashSet; @@ -17,6 +17,8 @@ import java.util.Set; */ final class PreferredMotorDigests { + // FIXME: This class is obsolete/broken after digesting algorithm changes + /** * A set containing the preferred motor digests. */ diff --git a/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java b/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java index a58a8553..6f3a2dc5 100644 --- a/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java +++ b/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java @@ -3,16 +3,17 @@ */ package net.sf.openrocket.file.rocksim.importt; +import java.io.IOException; +import java.io.InputStream; + import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.file.AbstractRocketLoader; import net.sf.openrocket.file.RocketLoadException; -import net.sf.openrocket.file.RocketLoader; import net.sf.openrocket.file.simplesax.SimpleSAX; + import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import java.io.IOException; -import java.io.InputStream; - /** * This class is the main entry point for Rocksim design file imported to OpenRocket. Currently only Rocksim v9 * file formats are supported, although it is possible that v8 formats will work for most components. @@ -28,7 +29,7 @@ import java.io.InputStream; * setMaterial * getMaterial */ -public class RocksimLoader extends RocketLoader { +public class RocksimLoader extends AbstractRocketLoader { /** * This method is called by the default implementations of {@link #load(java.io.File)} * and {@link #load(java.io.InputStream)} to load the rocket. diff --git a/core/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java b/core/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java index e22243de..5e37831c 100644 --- a/core/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java +++ b/core/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java @@ -19,7 +19,7 @@ public abstract class AbstractScaleFigure extends JPanel implements ScaleFigure private static final int DEFAULT_BORDER_PIXELS_WIDTH = 30; private static final int DEFAULT_BORDER_PIXELS_HEIGHT = 20; - + protected final double dpi; protected double scale = 1.0; @@ -41,7 +41,7 @@ public abstract class AbstractScaleFigure extends JPanel implements ScaleFigure } - + public abstract void updateFigure(); public abstract double getFigureWidth(); @@ -90,6 +90,11 @@ public abstract class AbstractScaleFigure extends JPanel implements ScaleFigure double s = Math.min(zh, zv) / dpi * 0.0254 - 0.001; + // Restrict to 100% + if (s > 1.0) { + s = 1.0; + } + setScaling(s); } @@ -124,8 +129,8 @@ public abstract class AbstractScaleFigure extends JPanel implements ScaleFigure // Copy the list before iterating to prevent concurrent modification exceptions. EventListener[] list = listeners.toArray(new EventListener[0]); for (EventListener l : list) { - if ( l instanceof StateChangeListener ) { - ((StateChangeListener)l).stateChanged(changeEvent); + if (l instanceof StateChangeListener) { + ((StateChangeListener) l).stateChanged(changeEvent); } } } -- 2.47.2