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

View File

@ -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();
}

View File

@ -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);

View File

@ -82,24 +82,24 @@ public class CustomExpressionPanel extends JPanel {
//Create a file chooser
final JFileChooser fc = new JFileChooser();
if (doc.getFile() != null){
if (doc.getFile() != null) {
fc.setCurrentDirectory(doc.getFile().getParentFile());
}
fc.setFileFilter(new FileNameExtensionFilter("Openrocket file", "ork"));
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(CustomExpressionPanel.this);
if (returnVal == JFileChooser.APPROVE_OPTION){
if (returnVal == JFileChooser.APPROVE_OPTION) {
File importFile = fc.getSelectedFile();
log.info("User selected a file to import expressions from "+fc.getSelectedFile().toString());
log.info("User selected a file to import expressions from " + fc.getSelectedFile().toString());
//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()){
for (CustomExpression exp : importedDocument.getCustomExpressions()) {
doc.addCustomExpression(exp);
}
} catch (RocketLoadException e1) {
@ -128,12 +128,12 @@ public class CustomExpressionPanel extends JPanel {
/*
* Update the expressionSelectorPanel
*/
private void updateExpressions(){
private void updateExpressions() {
expressionSelectorPanel.removeAll();
int totalExpressions = doc.getCustomExpressions().size();
for (int i=0; i<totalExpressions; i++){
SingleExpression se = new SingleExpression(doc.getCustomExpressions().get(i), i != 0, i != totalExpressions-1);
for (int i = 0; i < totalExpressions; i++) {
SingleExpression se = new SingleExpression(doc.getCustomExpressions().get(i), i != 0, i != totalExpressions - 1);
expressionSelectorPanel.add(se, "wrap");
}
@ -141,7 +141,7 @@ public class CustomExpressionPanel extends JPanel {
expressionSelectorPanel.repaint();
}
private void deleteExpression(CustomExpression expression){
private void deleteExpression(CustomExpression expression) {
doc.getCustomExpressions().remove(expression);
}
@ -150,13 +150,13 @@ public class CustomExpressionPanel extends JPanel {
* @param expression
* @param move integer - +1 to move down, -1 to move up
*/
private void moveExpression(CustomExpression expression, int move){
private void moveExpression(CustomExpression expression, int move) {
List<CustomExpression> expressions = doc.getCustomExpressions();
int i = expressions.indexOf(expression);
if (i+move == expressions.size() || i+move < 0)
if (i + move == expressions.size() || i + move < 0)
return;
else
Collections.swap(expressions, i, i+move);
Collections.swap(expressions, i, i + move);
}
@ -166,10 +166,10 @@ public class CustomExpressionPanel extends JPanel {
private class SingleExpression extends JPanel {
// Convenience method to make the labels consistent
private JLabel setLabelStyle(JLabel l){
private JLabel setLabelStyle(JLabel l) {
l.setBackground(Color.WHITE);
l.setOpaque(true);
l.setBorder(BorderFactory.createRaisedBevelBorder() );
l.setBorder(BorderFactory.createRaisedBevelBorder());
l.setText(" " + l.getText() + " ");
return l;
}
@ -179,15 +179,15 @@ public class CustomExpressionPanel extends JPanel {
// name: aName symbol: a Unit: m/s
//super(new MigLayout("","[::100][:200:400][::100][:100:200][::100][:100:200]",""));
JLabel nameLabel = new JLabel( trans.get("customExpression.Name")+ " :");
JLabel name = new JLabel ( expression.getName() );
JLabel nameLabel = new JLabel(trans.get("customExpression.Name") + " :");
JLabel name = new JLabel(expression.getName());
name = setLabelStyle(name);
JLabel symbolLabel = new JLabel( trans.get("customExpression.Symbol")+ " :" );
JLabel symbol = new JLabel ( expression.getSymbol());
JLabel symbolLabel = new JLabel(trans.get("customExpression.Symbol") + " :");
JLabel symbol = new JLabel(expression.getSymbol());
symbol = setLabelStyle(symbol);
symbol.setBackground(Color.WHITE);
JLabel unitLabel = new JLabel( trans.get("customExpression.Units")+ " :");
JLabel unitLabel = new JLabel(trans.get("customExpression.Units") + " :");
UnitSelector unitSelector = new UnitSelector(expression.getType().getUnitGroup());
//JLabel unitSelector = new JLabel ( expression.getUnit() );
//unitSelector = setLabelStyle(unitSelector);
@ -196,9 +196,9 @@ public class CustomExpressionPanel extends JPanel {
JButton editButton = new JButton(Icons.EDIT);
editButton.setToolTipText(trans.get("customExpression.Units.but.ttip.Edit"));
editButton.setBorderPainted(false);
editButton.addActionListener( new ActionListener() {
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
public void actionPerformed(ActionEvent e) {
Window parent = SwingUtilities.getWindowAncestor(CustomExpressionPanel.this);
new ExpressionBuilderDialog(parent, doc, expression).setVisible(true);
updateExpressions();
@ -209,7 +209,7 @@ public class CustomExpressionPanel extends JPanel {
upButton.setToolTipText(trans.get("customExpression.Units.but.ttip.MoveUp"));
upButton.setBorderPainted(false);
upButton.setVisible(showUp);
upButton.addActionListener( new ActionListener() {
upButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveExpression(expression, -1);
@ -221,7 +221,7 @@ public class CustomExpressionPanel extends JPanel {
downButton.setToolTipText(trans.get("customExpression.Units.but.ttip.MoveDown"));
downButton.setBorderPainted(false);
downButton.setVisible(showDown);
downButton.addActionListener( new ActionListener() {
downButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveExpression(expression, 1);

View File

@ -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);
}

View File

@ -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);

View 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);