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:
parent
8ad89448df
commit
c59bdd31cd
core/src/net/sf/openrocket
file
gui
utils
@ -9,8 +9,13 @@ public class FileInfo {
|
||||
public final URL fileURL;
|
||||
public final File directory;
|
||||
|
||||
public FileInfo(File sourceFile) throws MalformedURLException {
|
||||
this.fileURL = sourceFile.toURI().toURL();
|
||||
public FileInfo(File sourceFile) {
|
||||
URL theURL = null;
|
||||
try {
|
||||
theURL = sourceFile.toURI().toURL();
|
||||
} catch (MalformedURLException mex) {
|
||||
}
|
||||
this.fileURL = theURL;
|
||||
this.directory = sourceFile.getParentFile();
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
@ -37,9 +38,18 @@ public class GeneralRocketLoader {
|
||||
private static final byte[] ROCKSIM_SIGNATURE = TextUtil.asciiBytes("<RockSimDoc");
|
||||
|
||||
private final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
|
||||
private final FileInfo fileInfo;
|
||||
|
||||
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.
|
||||
*/
|
||||
@ -50,7 +60,7 @@ public class GeneralRocketLoader {
|
||||
try {
|
||||
|
||||
stream = new BufferedInputStream(new FileInputStream(source));
|
||||
OpenRocketDocument doc = load(stream, new FileInfo(source), motorFinder);
|
||||
OpenRocketDocument doc = load(stream, motorFinder);
|
||||
return doc;
|
||||
|
||||
} 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 {
|
||||
OpenRocketDocument doc = loadFromStream(source, motorFinder);
|
||||
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.
|
||||
|
||||
// Load expressions from selected document
|
||||
GeneralRocketLoader loader = new GeneralRocketLoader();
|
||||
GeneralRocketLoader loader = new GeneralRocketLoader(importFile);
|
||||
try {
|
||||
OpenRocketDocument importedDocument = loader.load(importFile, new DatabaseMotorFinder());
|
||||
for (CustomExpression exp : importedDocument.getCustomExpressions()) {
|
||||
|
@ -15,7 +15,6 @@ import java.awt.event.WindowEvent;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@ -62,7 +61,6 @@ import net.miginfocom.swing.MigLayout;
|
||||
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||
import net.sf.openrocket.document.OpenRocketDocument;
|
||||
import net.sf.openrocket.document.StorageOptions;
|
||||
import net.sf.openrocket.file.GeneralRocketLoader;
|
||||
import net.sf.openrocket.file.GeneralRocketSaver;
|
||||
import net.sf.openrocket.file.RocketLoadException;
|
||||
import net.sf.openrocket.gui.ExportDecalDialog;
|
||||
@ -109,11 +107,6 @@ import net.sf.openrocket.util.TestRockets;
|
||||
public class BasicFrame extends JFrame {
|
||||
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 Translator trans = Application.getTranslator();
|
||||
@ -1102,7 +1095,7 @@ public class BasicFrame extends JFrame {
|
||||
* @param parent the parent window for dialogs.
|
||||
* @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;
|
||||
// First figure out the file name from the URL
|
||||
|
||||
@ -1133,33 +1126,9 @@ public class BasicFrame extends JFrame {
|
||||
|
||||
// Open the file
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
OpenFileWorker worker = new OpenFileWorker(url);
|
||||
open(worker, displayName, parent, true);
|
||||
}
|
||||
|
||||
|
||||
@ -1172,7 +1141,7 @@ public class BasicFrame extends JFrame {
|
||||
* @return whether the file was successfully loaded and opened.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,6 @@ import javax.swing.SwingWorker;
|
||||
|
||||
import net.sf.openrocket.document.OpenRocketDocument;
|
||||
import net.sf.openrocket.file.DatabaseMotorFinder;
|
||||
import net.sf.openrocket.file.FileInfo;
|
||||
import net.sf.openrocket.file.GeneralRocketLoader;
|
||||
import net.sf.openrocket.logging.LogHelper;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
@ -30,22 +29,19 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
|
||||
|
||||
private final File file;
|
||||
private final URL jarURL;
|
||||
private final InputStream stream;
|
||||
private final GeneralRocketLoader loader;
|
||||
|
||||
public OpenFileWorker(File file, GeneralRocketLoader loader) {
|
||||
public OpenFileWorker(File file) {
|
||||
this.file = file;
|
||||
this.jarURL = null;
|
||||
this.stream = null;
|
||||
this.loader = loader;
|
||||
loader = new GeneralRocketLoader(file);
|
||||
}
|
||||
|
||||
|
||||
public OpenFileWorker(InputStream stream, URL fileURL, GeneralRocketLoader loader) {
|
||||
this.stream = stream;
|
||||
public OpenFileWorker(URL fileURL) {
|
||||
this.jarURL = fileURL;
|
||||
this.file = null;
|
||||
this.loader = loader;
|
||||
loader = new GeneralRocketLoader(fileURL);
|
||||
}
|
||||
|
||||
public GeneralRocketLoader getRocketLoader() {
|
||||
@ -56,14 +52,11 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
|
||||
protected OpenRocketDocument doInBackground() throws Exception {
|
||||
InputStream is;
|
||||
|
||||
FileInfo fileInfo = null;
|
||||
// Get the correct input stream
|
||||
if (file != null) {
|
||||
is = new FileInputStream(file);
|
||||
fileInfo = new FileInfo(file);
|
||||
} else {
|
||||
is = stream;
|
||||
fileInfo = new FileInfo(jarURL);
|
||||
is = jarURL.openStream();
|
||||
}
|
||||
|
||||
// Buffer stream unless already buffered
|
||||
@ -75,7 +68,7 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
|
||||
is = new ProgressInputStream(is);
|
||||
|
||||
try {
|
||||
OpenRocketDocument document = loader.load(is, fileInfo, new DatabaseMotorFinder());
|
||||
OpenRocketDocument document = loader.load(is, new DatabaseMotorFinder());
|
||||
|
||||
// Set document state
|
||||
document.setFile(file);
|
||||
|
@ -31,7 +31,6 @@ public class RocksimConverter {
|
||||
|
||||
setup();
|
||||
|
||||
GeneralRocketLoader loader = new GeneralRocketLoader();
|
||||
GeneralRocketSaver saver = new GeneralRocketSaver();
|
||||
|
||||
for (String inputFile : args) {
|
||||
@ -62,6 +61,7 @@ public class RocksimConverter {
|
||||
opts.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_NONE);
|
||||
opts.setExplicitlySet(true);
|
||||
|
||||
GeneralRocketLoader loader = new GeneralRocketLoader(input);
|
||||
OpenRocketDocument document = loader.load(input, new DatabaseMotorFinder());
|
||||
saver.save(output, document, opts);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user