From c59bdd31cdd11dafa95702167fd24f50525d365c Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Wed, 9 Jan 2013 10:43:56 -0600 Subject: [PATCH] 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. --- core/src/net/sf/openrocket/file/FileInfo.java | 15 ++++-- .../openrocket/file/GeneralRocketLoader.java | 14 ++++- .../CustomExpressionPanel.java | 52 +++++++++---------- .../sf/openrocket/gui/main/BasicFrame.java | 39 ++------------ .../openrocket/gui/util/OpenFileWorker.java | 25 ++++----- .../sf/openrocket/utils/RocksimConverter.java | 2 +- 6 files changed, 62 insertions(+), 85 deletions(-) diff --git a/core/src/net/sf/openrocket/file/FileInfo.java b/core/src/net/sf/openrocket/file/FileInfo.java index 72ad6e374..fd37488db 100644 --- a/core/src/net/sf/openrocket/file/FileInfo.java +++ b/core/src/net/sf/openrocket/file/FileInfo.java @@ -5,12 +5,17 @@ import java.net.MalformedURLException; import java.net.URL; 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(); } @@ -18,11 +23,11 @@ public class FileInfo { this.fileURL = sourceURL; this.directory = null; } - + public URL getFileURL() { return fileURL; } - + public File getDirectory() { return directory; } diff --git a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java index d1abfaba8..e1658cd77 100644 --- a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java +++ b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java @@ -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(" 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); } - + /* * A JPanel which configures a single expression @@ -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); diff --git a/core/src/net/sf/openrocket/gui/main/BasicFrame.java b/core/src/net/sf/openrocket/gui/main/BasicFrame.java index 7b33bef15..ecaee7ede 100644 --- a/core/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/core/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -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 true 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 false 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); } diff --git a/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java b/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java index 98ee971bd..57026fb08 100644 --- a/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java +++ b/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java @@ -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 { 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() { @@ -55,15 +51,12 @@ public class OpenFileWorker extends SwingWorker { @Override 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,12 +68,12 @@ public class OpenFileWorker extends SwingWorker { 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); document.setSaved(true); - + return document; } finally { try { diff --git a/core/src/net/sf/openrocket/utils/RocksimConverter.java b/core/src/net/sf/openrocket/utils/RocksimConverter.java index 519a0fd5a..6a3ed6878 100644 --- a/core/src/net/sf/openrocket/utils/RocksimConverter.java +++ b/core/src/net/sf/openrocket/utils/RocksimConverter.java @@ -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);