Push FileInfo construction down a level by having it constructed in the

GeneralRocketLoader instead of in the OpenFileWorker.  This required a
change in the constructor args to GeneralRocketLoader.
This commit is contained in:
kruland2607 2013-01-09 10:43:56 -06:00
parent 8ad89448df
commit c59bdd31cd
6 changed files with 62 additions and 85 deletions

@ -9,8 +9,13 @@ public class FileInfo {
public final URL fileURL; public final URL fileURL;
public final File directory; public final File directory;
public FileInfo(File sourceFile) throws MalformedURLException { public FileInfo(File sourceFile) {
this.fileURL = sourceFile.toURI().toURL(); URL theURL = null;
try {
theURL = sourceFile.toURI().toURL();
} catch (MalformedURLException mex) {
}
this.fileURL = theURL;
this.directory = sourceFile.getParentFile(); this.directory = sourceFile.getParentFile();
} }

@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@ -37,9 +38,18 @@ public class GeneralRocketLoader {
private static final byte[] ROCKSIM_SIGNATURE = TextUtil.asciiBytes("<RockSimDoc"); private static final byte[] ROCKSIM_SIGNATURE = TextUtil.asciiBytes("<RockSimDoc");
private final OpenRocketLoader openRocketLoader = new OpenRocketLoader(); private final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
private final FileInfo fileInfo;
private final RocksimLoader rocksimLoader = new RocksimLoader(); private final RocksimLoader rocksimLoader = new RocksimLoader();
public GeneralRocketLoader(File file) {
this.fileInfo = new FileInfo(file);
}
public GeneralRocketLoader(URL jarURL) {
this.fileInfo = new FileInfo(jarURL);
}
/** /**
* Loads a rocket from the specified File object. * Loads a rocket from the specified File object.
*/ */
@ -50,7 +60,7 @@ public class GeneralRocketLoader {
try { try {
stream = new BufferedInputStream(new FileInputStream(source)); stream = new BufferedInputStream(new FileInputStream(source));
OpenRocketDocument doc = load(stream, new FileInfo(source), motorFinder); OpenRocketDocument doc = load(stream, motorFinder);
return doc; return doc;
} catch (Exception e) { } catch (Exception e) {
@ -66,7 +76,7 @@ public class GeneralRocketLoader {
} }
} }
public final OpenRocketDocument load(InputStream source, FileInfo fileInfo, MotorFinder motorFinder) throws RocketLoadException { public final OpenRocketDocument load(InputStream source, MotorFinder motorFinder) throws RocketLoadException {
try { try {
OpenRocketDocument doc = loadFromStream(source, motorFinder); OpenRocketDocument doc = loadFromStream(source, motorFinder);
doc.setBaseFile(fileInfo); doc.setBaseFile(fileInfo);

@ -96,7 +96,7 @@ public class CustomExpressionPanel extends JPanel {
//TODO: This should probably be somewhere else and ideally we would use an alternative minimal rocket loader. Still, it doesn't seem particularly slow this way. //TODO: This should probably be somewhere else and ideally we would use an alternative minimal rocket loader. Still, it doesn't seem particularly slow this way.
// Load expressions from selected document // Load expressions from selected document
GeneralRocketLoader loader = new GeneralRocketLoader(); GeneralRocketLoader loader = new GeneralRocketLoader(importFile);
try { try {
OpenRocketDocument importedDocument = loader.load(importFile, new DatabaseMotorFinder()); OpenRocketDocument importedDocument = loader.load(importFile, new DatabaseMotorFinder());
for (CustomExpression exp : importedDocument.getCustomExpressions()) { for (CustomExpression exp : importedDocument.getCustomExpressions()) {

@ -15,7 +15,6 @@ import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -62,7 +61,6 @@ import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.document.StorageOptions; import net.sf.openrocket.document.StorageOptions;
import net.sf.openrocket.file.GeneralRocketLoader;
import net.sf.openrocket.file.GeneralRocketSaver; import net.sf.openrocket.file.GeneralRocketSaver;
import net.sf.openrocket.file.RocketLoadException; import net.sf.openrocket.file.RocketLoadException;
import net.sf.openrocket.gui.ExportDecalDialog; import net.sf.openrocket.gui.ExportDecalDialog;
@ -109,11 +107,6 @@ import net.sf.openrocket.util.TestRockets;
public class BasicFrame extends JFrame { public class BasicFrame extends JFrame {
private static final LogHelper log = Application.getLogger(); private static final LogHelper log = Application.getLogger();
/**
* The RocketLoader instance used for loading all rocket designs.
*/
private static final GeneralRocketLoader ROCKET_LOADER = new GeneralRocketLoader();
private static final GeneralRocketSaver ROCKET_SAVER = new GeneralRocketSaver(); private static final GeneralRocketSaver ROCKET_SAVER = new GeneralRocketSaver();
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
@ -1102,7 +1095,7 @@ public class BasicFrame extends JFrame {
* @param parent the parent window for dialogs. * @param parent the parent window for dialogs.
* @return <code>true</code> if opened successfully. * @return <code>true</code> if opened successfully.
*/ */
public static boolean open(URL url, BasicFrame parent) { public static void open(URL url, BasicFrame parent) {
String displayName = null; String displayName = null;
// First figure out the file name from the URL // First figure out the file name from the URL
@ -1133,33 +1126,9 @@ public class BasicFrame extends JFrame {
// Open the file // Open the file
log.info("Opening file from url=" + url + " filename=" + displayName); log.info("Opening file from url=" + url + " filename=" + displayName);
try {
InputStream is = url.openStream();
open(is, displayName, url, parent, true);
} catch (IOException e) {
log.warn("Error opening file" + e);
JOptionPane.showMessageDialog(parent,
"An error occurred while opening the file " + displayName,
"Error loading file", JOptionPane.ERROR_MESSAGE);
}
return false; OpenFileWorker worker = new OpenFileWorker(url);
} open(worker, displayName, parent, true);
/**
* Open the specified file from an InputStream in a new design frame. If an error
* occurs, an error dialog is shown and <code>false</code> is returned.
*
* @param stream the stream to load from.
* @param displayName the file name to display in dialogs (not set to the document).
* @param parent the parent component for which a progress dialog is opened.
* @param openRocketConfigDialog if true will open the rocket configuration dialog
* @return whether the file was successfully loaded and opened.
*/
private static boolean open(InputStream stream, String displayName, URL fileURL, Window parent, boolean openRocketConfigDialog) {
OpenFileWorker worker = new OpenFileWorker(stream, fileURL, ROCKET_LOADER);
return open(worker, displayName, parent, openRocketConfigDialog);
} }
@ -1172,7 +1141,7 @@ public class BasicFrame extends JFrame {
* @return whether the file was successfully loaded and opened. * @return whether the file was successfully loaded and opened.
*/ */
public static boolean open(File file, Window parent) { public static boolean open(File file, Window parent) {
OpenFileWorker worker = new OpenFileWorker(file, ROCKET_LOADER); OpenFileWorker worker = new OpenFileWorker(file);
return open(worker, file.getName(), parent, false); return open(worker, file.getName(), parent, false);
} }

@ -13,7 +13,6 @@ import javax.swing.SwingWorker;
import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.file.DatabaseMotorFinder; import net.sf.openrocket.file.DatabaseMotorFinder;
import net.sf.openrocket.file.FileInfo;
import net.sf.openrocket.file.GeneralRocketLoader; import net.sf.openrocket.file.GeneralRocketLoader;
import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
@ -30,22 +29,19 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
private final File file; private final File file;
private final URL jarURL; private final URL jarURL;
private final InputStream stream;
private final GeneralRocketLoader loader; private final GeneralRocketLoader loader;
public OpenFileWorker(File file, GeneralRocketLoader loader) { public OpenFileWorker(File file) {
this.file = file; this.file = file;
this.jarURL = null; this.jarURL = null;
this.stream = null; loader = new GeneralRocketLoader(file);
this.loader = loader;
} }
public OpenFileWorker(InputStream stream, URL fileURL, GeneralRocketLoader loader) { public OpenFileWorker(URL fileURL) {
this.stream = stream;
this.jarURL = fileURL; this.jarURL = fileURL;
this.file = null; this.file = null;
this.loader = loader; loader = new GeneralRocketLoader(fileURL);
} }
public GeneralRocketLoader getRocketLoader() { public GeneralRocketLoader getRocketLoader() {
@ -56,14 +52,11 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
protected OpenRocketDocument doInBackground() throws Exception { protected OpenRocketDocument doInBackground() throws Exception {
InputStream is; InputStream is;
FileInfo fileInfo = null;
// Get the correct input stream // Get the correct input stream
if (file != null) { if (file != null) {
is = new FileInputStream(file); is = new FileInputStream(file);
fileInfo = new FileInfo(file);
} else { } else {
is = stream; is = jarURL.openStream();
fileInfo = new FileInfo(jarURL);
} }
// Buffer stream unless already buffered // Buffer stream unless already buffered
@ -75,7 +68,7 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
is = new ProgressInputStream(is); is = new ProgressInputStream(is);
try { try {
OpenRocketDocument document = loader.load(is, fileInfo, new DatabaseMotorFinder()); OpenRocketDocument document = loader.load(is, new DatabaseMotorFinder());
// Set document state // Set document state
document.setFile(file); document.setFile(file);

@ -31,7 +31,6 @@ public class RocksimConverter {
setup(); setup();
GeneralRocketLoader loader = new GeneralRocketLoader();
GeneralRocketSaver saver = new GeneralRocketSaver(); GeneralRocketSaver saver = new GeneralRocketSaver();
for (String inputFile : args) { for (String inputFile : args) {
@ -62,6 +61,7 @@ public class RocksimConverter {
opts.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_NONE); opts.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_NONE);
opts.setExplicitlySet(true); opts.setExplicitlySet(true);
GeneralRocketLoader loader = new GeneralRocketLoader(input);
OpenRocketDocument document = loader.load(input, new DatabaseMotorFinder()); OpenRocketDocument document = loader.load(input, new DatabaseMotorFinder());
saver.save(output, document, opts); saver.save(output, document, opts);