diff --git a/core/src/net/sf/openrocket/document/AttachmentFactory.java b/core/src/net/sf/openrocket/document/AttachmentFactory.java new file mode 100644 index 000000000..c6cafb36f --- /dev/null +++ b/core/src/net/sf/openrocket/document/AttachmentFactory.java @@ -0,0 +1,7 @@ +package net.sf.openrocket.document; + +public interface AttachmentFactory { + + public T getAttachment(String name); + +} diff --git a/core/src/net/sf/openrocket/document/BaseAttachmentFactory.java b/core/src/net/sf/openrocket/document/BaseAttachmentFactory.java new file mode 100644 index 000000000..077fedbc0 --- /dev/null +++ b/core/src/net/sf/openrocket/document/BaseAttachmentFactory.java @@ -0,0 +1,135 @@ +package net.sf.openrocket.document; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import net.sf.openrocket.file.FileInfo; +import net.sf.openrocket.util.FileUtils; + +public class BaseAttachmentFactory implements AttachmentFactory { + + private FileInfo fileInfo; + private boolean isZipFile = false; + + public void setBaseFile(FileInfo fileInfo) { + this.fileInfo = fileInfo; + } + + public void setIsZipFile(boolean isZipFile) { + this.isZipFile = isZipFile; + } + + public class BaseAttachment implements Attachment, Comparable { + + protected String name; + + BaseAttachment(String name) { + this.name = name; + } + + @Override + public String getName() { + return name; + } + + @Override + public InputStream getBytes() throws FileNotFoundException, IOException { + return BaseAttachmentFactory.this.getBytes(this); + } + + @Override + public int compareTo(Object o) { + if (!(o instanceof BaseAttachment)) { + return -1; + } + return this.name.compareTo(((BaseAttachment) o).name); + } + + @Override + public String toString() { + return getName(); + } + + } + + @Override + public BaseAttachment getAttachment(String name) { + return new BaseAttachment(name); + } + + /** + * This function returns an InputStream backed by a byte[] containing the decal pixels. + * If it reads in the bytes from an actual file, the underlying file is closed. + * + * @param name + * @return + * @throws FileNotFoundException + * @throws IOException + */ + private InputStream getBytes(BaseAttachment attachment) throws FileNotFoundException, IOException { + + // This is the InputStream to be returned. + InputStream rawIs = null; + + + String name = attachment.getName(); + + if (rawIs == null && isZipFile) { + rawIs = findInZipContainer(name); + } + + // Try relative to the model file directory. This is so we can support unzipped container format. + if (rawIs == null) { + if (fileInfo != null && fileInfo.getDirectory() != null) { + File decalFile = new File(fileInfo.getDirectory(), name); + rawIs = new FileInputStream(decalFile); + } + } + + if (rawIs == null) { + throw new FileNotFoundException("Unable to locate decal for name " + name); + } + + try { + byte[] bytes = FileUtils.readBytes(rawIs); + return new ByteArrayInputStream(bytes); + } finally { + rawIs.close(); + } + + } + + private ZipInputStream findInZipContainer(String name) { + ZipInputStream zis = null; + try { + zis = new ZipInputStream(fileInfo.getFileURL().openStream()); + } catch (IOException ex) { + return null; + } + try { + ZipEntry entry = zis.getNextEntry(); + while (entry != null) { + if (entry.getName().equals(name)) { + return zis; + } + entry = zis.getNextEntry(); + } + zis.close(); + return null; + } catch (IOException ioex) { + try { + zis.close(); + } catch (IOException ex) { + // why does close throw? it's maddening + } + return null; + } + } + +} diff --git a/core/src/net/sf/openrocket/document/DecalRegistry.java b/core/src/net/sf/openrocket/document/DecalRegistry.java index eb7408f67..b7510caf7 100644 --- a/core/src/net/sf/openrocket/document/DecalRegistry.java +++ b/core/src/net/sf/openrocket/document/DecalRegistry.java @@ -10,164 +10,121 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.MessageFormat; +import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; import net.sf.openrocket.appearance.DecalImage; -import net.sf.openrocket.file.FileInfo; +import net.sf.openrocket.document.BaseAttachmentFactory.BaseAttachment; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.FileUtils; -public class DecalRegistry { +public class DecalRegistry implements AttachmentFactory { private static LogHelper log = Application.getLogger(); - - private FileInfo fileInfo; - private boolean isZipFile = false; - - private Map registeredDecals = new HashMap(); - - public void setBaseFile(FileInfo fileInfo) { - this.fileInfo = fileInfo; + + private final BaseAttachmentFactory baseFactory; + + public DecalRegistry(BaseAttachmentFactory baseFactory) { + this.baseFactory = baseFactory; } - - public void setIsZipFile( boolean isZipFile ) { - this.isZipFile = isZipFile; - } - - public DecalImage getDecalImage( String decalName ) { + + private Map registeredDecals = new HashMap(); + + public DecalImage getAttachment(String decalName) { DecalImageImpl d = registeredDecals.get(decalName); - if ( d == null ) { - d = new DecalImageImpl(decalName); + if (d == null) { + BaseAttachment attachment = baseFactory.getAttachment(decalName); + d = new DecalImageImpl(attachment); registeredDecals.put(decalName, d); } return d; } - - public DecalImage getDecalImage( File file ) { - + + public DecalImage getAttachment(File file) { + // See if this file is being used already - DecalImageImpl decal = findDecalForFile( file ); - - if ( decal != null ) { + DecalImageImpl decal = findDecalForFile(file); + + if (decal != null) { return decal; } - + // It's a new file, generate a name for it. - String decalName = makeUniqueName( file.getName() ); - - decal = new DecalImageImpl( decalName ); - decal.setFileSystemLocation( file ); - + String decalName = makeUniqueName(file.getName()); + + BaseAttachment attachment = baseFactory.getAttachment(decalName); + decal = new DecalImageImpl(attachment); + decal.setFileSystemLocation(file); + registeredDecals.put(decalName, decal); return decal; - + } - - public Set getDecalList( ) { - + + public Collection getDecalList() { + Set decals = new TreeSet(); - + decals.addAll(registeredDecals.values()); - + return decals; } - - public Set getExportableDecalsList() { - - Set exportableDecals = new HashSet(); - - for( DecalImage d : registeredDecals.values() ) { - if ( isExportable(d.getName())) { - exportableDecals.add(d); - } - } - - return exportableDecals; - - } - + public class DecalImageImpl implements DecalImage, Comparable { - - private final String name; - + + private final BaseAttachment delegate; + private File fileSystemLocation; - - private DecalImageImpl( String name ) { - this.name = name; + + private DecalImageImpl(BaseAttachment delegate) { + this.delegate = delegate; } - + @Override public String getName() { - return name; + return delegate.getName(); } - + @Override public InputStream getBytes() throws FileNotFoundException, IOException { return DecalRegistry.this.getDecal(this); } - + @Override public void exportImage(File file, boolean watchForChanges) throws IOException { - this.fileSystemLocation = file; DecalRegistry.this.exportDecal(this, file); + this.fileSystemLocation = file; } - + File getFileSystemLocation() { return fileSystemLocation; } - - void setFileSystemLocation( File fileSystemLocation ) { + + void setFileSystemLocation(File fileSystemLocation) { this.fileSystemLocation = fileSystemLocation; } - + @Override public String toString() { - return name; + return delegate.toString(); } - + @Override public int compareTo(Object o) { - if ( ! (o instanceof DecalImageImpl ) ) { + if (!(o instanceof DecalImageImpl)) { return -1; } - return this.name.compareTo( ((DecalImageImpl)o).name ); + return delegate.compareTo(((DecalImageImpl) o).delegate); } - + } - - /** - * Returns true if the named decal is exportable - that is, it is currently stored in - * the zip file. - * - * @param name - * @return - */ - private boolean isExportable( String name ) { - if ( !isZipFile ) { - return false; - } - try { - InputStream is = findInZipContainer(name); - if ( is != null ) { - is.close(); - return true; - } else { - return false; - } - } catch ( IOException iex ) { - return false; - } - } - + /** * This function returns an InputStream backed by a byte[] containing the decal pixels. * If it reads in the bytes from an actual file, the underlying file is closed. @@ -177,103 +134,52 @@ public class DecalRegistry { * @throws FileNotFoundException * @throws IOException */ - private InputStream getDecal( DecalImageImpl decal ) throws FileNotFoundException, IOException { - - // This is the InputStream to be returned. - InputStream rawIs = null; - - + private InputStream getDecal(DecalImageImpl decal) throws FileNotFoundException, IOException { + // First check if the decal is located on the file system - File exportedFile= decal.getFileSystemLocation(); - if ( exportedFile != null ) { - rawIs = new FileInputStream(exportedFile); - } - - String name = decal.getName(); - - if ( rawIs == null && isZipFile ) { - rawIs = findInZipContainer(name); - } - - // Try relative to the model file directory. This is so we can support unzipped container format. - if ( rawIs == null ) { - if( fileInfo != null && fileInfo.getDirectory() != null ) { - File decalFile = new File(fileInfo.getDirectory(), name); - rawIs = new FileInputStream(decalFile); + File exportedFile = decal.getFileSystemLocation(); + if (exportedFile != null) { + InputStream rawIs = new FileInputStream(exportedFile); + try { + byte[] bytes = FileUtils.readBytes(rawIs); + return new ByteArrayInputStream(bytes); + } finally { + rawIs.close(); } + } - - if ( rawIs == null ) { - throw new FileNotFoundException( "Unable to locate decal for name " + name ); - } - - try { - byte[] bytes = FileUtils.readBytes(rawIs); - return new ByteArrayInputStream(bytes); - } - finally { - rawIs.close(); - } - + + return decal.delegate.getBytes(); + } - - private void exportDecal( DecalImageImpl decal, File selectedFile ) throws IOException { - + + private void exportDecal(DecalImageImpl decal, File selectedFile) throws IOException { + try { InputStream is = decal.getBytes(); - OutputStream os = new BufferedOutputStream( new FileOutputStream(selectedFile)); - + OutputStream os = new BufferedOutputStream(new FileOutputStream(selectedFile)); + FileUtils.copy(is, os); - + is.close(); os.close(); - - } - catch (IOException iex) { + + } catch (IOException iex) { throw new BugException(iex); } - + } - - - private ZipInputStream findInZipContainer( String name ) { - ZipInputStream zis = null; - try { - zis = new ZipInputStream(fileInfo.fileURL.openStream()); - } catch( IOException ex ) { - return null; - } - try { - ZipEntry entry = zis.getNextEntry(); - while ( entry != null ) { - if ( entry.getName().equals(name) ) { - return zis; - } - entry = zis.getNextEntry(); - } - zis.close(); - return null; - } - catch ( IOException ioex ) { - try { - zis.close(); - } catch ( IOException ex ) { - // why does close throw? it's maddening - } - return null; - } - } - - private DecalImageImpl findDecalForFile( File file ) { - - for( DecalImageImpl d : registeredDecals.values() ) { - if ( file.equals( d.getFileSystemLocation() ) ) { + + private DecalImageImpl findDecalForFile(File file) { + + for (DecalImageImpl d : registeredDecals.values()) { + if (file.equals(d.getFileSystemLocation())) { return d; } } return null; } - + /** * Regular expression for parsing file names with numerical identifiers. * For examples: @@ -298,49 +204,49 @@ public class DecalRegistry { private static final int BASE_NAME_INDEX = 1; private static final int NUMBER_INDEX = 3; private static final int EXTENSION_INDEX = 4; - - private String makeUniqueName( String name ) { - + + private String makeUniqueName(String name) { + String newName = "decals/" + name; String basename = ""; String extension = ""; Matcher nameMatcher = fileNamePattern.matcher(newName); - if ( nameMatcher.matches() ) { + if (nameMatcher.matches()) { basename = nameMatcher.group(BASE_NAME_INDEX); extension = nameMatcher.group(EXTENSION_INDEX); } - + Set counts = new TreeSet(); - - boolean needsRewrite = false; - - for ( DecalImageImpl d: registeredDecals.values() ) { - Matcher m = fileNamePattern.matcher( d.getName() ); - if ( m.matches() ) { - if ( basename.equals(m.group(BASE_NAME_INDEX)) && extension.equals(m.group(EXTENSION_INDEX))) { + + boolean needsRewrite = false; + + for (DecalImageImpl d : registeredDecals.values()) { + Matcher m = fileNamePattern.matcher(d.getName()); + if (m.matches()) { + if (basename.equals(m.group(BASE_NAME_INDEX)) && extension.equals(m.group(EXTENSION_INDEX))) { String intString = m.group(NUMBER_INDEX); - if ( intString != null ) { + if (intString != null) { Integer i = Integer.parseInt(intString); counts.add(i); } needsRewrite = true; } - } else if ( newName.equals(d.getName() ) ) { + } else if (newName.equals(d.getName())) { needsRewrite = true; } } - - if ( !needsRewrite ) { + + if (!needsRewrite) { return newName; } - + // find a missing integer; Integer newIndex = 1; - while( counts.contains(newIndex) ) { + while (counts.contains(newIndex)) { newIndex++; } - - return MessageFormat.format("{0} ({1}).{2}", basename,newIndex,extension); + + return MessageFormat.format("{0} ({1}).{2}", basename, newIndex, extension); } - + } diff --git a/core/src/net/sf/openrocket/document/OpenRocketDocument.java b/core/src/net/sf/openrocket/document/OpenRocketDocument.java index b1993c0c4..17309d2d7 100644 --- a/core/src/net/sf/openrocket/document/OpenRocketDocument.java +++ b/core/src/net/sf/openrocket/document/OpenRocketDocument.java @@ -6,20 +6,17 @@ import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; -import java.util.TreeSet; -import net.sf.openrocket.appearance.Appearance; -import net.sf.openrocket.appearance.Decal; import net.sf.openrocket.document.events.DocumentChangeEvent; import net.sf.openrocket.document.events.DocumentChangeListener; import net.sf.openrocket.document.events.SimulationChangeEvent; +import net.sf.openrocket.file.FileInfo; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.logging.TraceException; import net.sf.openrocket.rocketcomponent.ComponentChangeEvent; import net.sf.openrocket.rocketcomponent.ComponentChangeListener; import net.sf.openrocket.rocketcomponent.Configuration; import net.sf.openrocket.rocketcomponent.Rocket; -import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.simulation.FlightDataType; import net.sf.openrocket.simulation.customexpression.CustomExpression; import net.sf.openrocket.simulation.listeners.SimulationListener; @@ -61,20 +58,21 @@ public class OpenRocketDocument implements ComponentChangeListener { private final ArrayList simulations = new ArrayList(); private ArrayList customExpressions = new ArrayList(); - - private DecalRegistry decalRegistry = new DecalRegistry(); + + private BaseAttachmentFactory attachmentFactory = new BaseAttachmentFactory(); + private DecalRegistry decalRegistry = new DecalRegistry(attachmentFactory); /* * The undo/redo variables and mechanism are documented in doc/undo-redo-flow.* */ - + /** * The undo history of the rocket. Whenever a new undo position is created while the * rocket is in "dirty" state, the rocket is copied here. */ private LinkedList undoHistory = new LinkedList(); private LinkedList undoDescription = new LinkedList(); - + /** * The position in the undoHistory we are currently at. If modifications have been * made to the rocket, the rocket is in "dirty" state and this points to the previous @@ -88,7 +86,7 @@ public class OpenRocketDocument implements ComponentChangeListener { private String nextDescription = null; private String storedDescription = null; - + private ArrayList undoRedoListeners = new ArrayList(2); private File file = null; @@ -96,7 +94,7 @@ public class OpenRocketDocument implements ComponentChangeListener { private final StorageOptions storageOptions = new StorageOptions(); - + private final List listeners = new ArrayList(); @@ -115,38 +113,48 @@ public class OpenRocketDocument implements ComponentChangeListener { } - public void addCustomExpression(CustomExpression expression){ - if (customExpressions.contains(expression)){ - log.user("Could not add custom expression "+expression.getName()+" to document as document alerady has a matching expression."); + public void setBaseFile(FileInfo fileInfo) { + attachmentFactory.setBaseFile(fileInfo); + } + + + public void setIsZipFile(boolean isZipFile) { + attachmentFactory.setIsZipFile(isZipFile); + } + + + public void addCustomExpression(CustomExpression expression) { + if (customExpressions.contains(expression)) { + log.user("Could not add custom expression " + expression.getName() + " to document as document alerady has a matching expression."); } else { customExpressions.add(expression); } } - public void removeCustomExpression(CustomExpression expression){ + public void removeCustomExpression(CustomExpression expression) { customExpressions.remove(expression); } - public List getCustomExpressions(){ + public List getCustomExpressions() { return customExpressions; } /* * Returns a set of all the flight data types defined or available in any way in the rocket document */ - public Set getFlightDataTypes(){ + public Set getFlightDataTypes() { Set allTypes = new LinkedHashSet(); // built in Collections.addAll(allTypes, FlightDataType.ALL_TYPES); // custom expressions - for (CustomExpression exp : customExpressions){ + for (CustomExpression exp : customExpressions) { allTypes.add(exp.getType()); } // simulation listeners - for (Simulation sim : simulations){ + for (Simulation sim : simulations) { for (String className : sim.getSimulationListeners()) { SimulationListener l = null; try { @@ -158,7 +166,7 @@ public class OpenRocketDocument implements ComponentChangeListener { } catch (Exception e) { log.error("Could not instantiate listener: " + className); } - } + } } // imported data @@ -168,7 +176,7 @@ public class OpenRocketDocument implements ComponentChangeListener { return allTypes; } - + public Rocket getRocket() { return rocket; } @@ -181,7 +189,7 @@ public class OpenRocketDocument implements ComponentChangeListener { public DecalRegistry getDecalRegistry() { return decalRegistry; } - + public File getFile() { return file; } @@ -212,9 +220,9 @@ public class OpenRocketDocument implements ComponentChangeListener { } - - - + + + public List getSimulations() { return simulations.clone(); } @@ -320,14 +328,14 @@ public class OpenRocketDocument implements ComponentChangeListener { undoDescription.removeLast(); } - + // Add the current state to the undo history undoHistory.add(rocket.copyWithOriginalID()); undoDescription.add(null); nextDescription = description; undoPosition++; - + // Maintain maximum undo size if (undoHistory.size() > UNDO_LEVELS + UNDO_MARGIN && undoPosition > UNDO_MARGIN) { for (int i = 0; i < UNDO_MARGIN; i++) { @@ -384,7 +392,7 @@ public class OpenRocketDocument implements ComponentChangeListener { undoHistory.add(rocket.copyWithOriginalID()); undoDescription.add(null); undoPosition = 0; - + fireUndoRedoChangeEvent(); } @@ -539,7 +547,7 @@ public class OpenRocketDocument implements ComponentChangeListener { } - + /** * Return a copy of this document. The rocket is copied with original ID's, the default * motor configuration ID is maintained and the simulations are copied to the new rocket. @@ -558,14 +566,14 @@ public class OpenRocketDocument implements ComponentChangeListener { } - + /////// Listeners - public void addUndoRedoListener( UndoRedoListener listener ) { + public void addUndoRedoListener(UndoRedoListener listener) { undoRedoListeners.add(listener); } - public void removeUndoRedoListener( UndoRedoListener listener ) { + public void removeUndoRedoListener(UndoRedoListener listener) { undoRedoListeners.remove(listener); } @@ -593,6 +601,6 @@ public class OpenRocketDocument implements ComponentChangeListener { } - - + + } diff --git a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java index 0e4acb34a..d1abfaba8 100644 --- a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java +++ b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java @@ -28,7 +28,7 @@ import net.sf.openrocket.util.TextUtil; public class GeneralRocketLoader { protected final WarningSet warnings = new WarningSet(); - + private static final int READ_BYTES = 300; private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b @@ -54,7 +54,7 @@ public class GeneralRocketLoader { return doc; } catch (Exception e) { - throw new RocketLoadException("Exception loading file: " + source,e); + throw new RocketLoadException("Exception loading file: " + source, e); } finally { if (stream != null) { try { @@ -68,9 +68,9 @@ public class GeneralRocketLoader { public final OpenRocketDocument load(InputStream source, FileInfo fileInfo, MotorFinder motorFinder) throws RocketLoadException { try { - OpenRocketDocument doc = loadFromStream(source, motorFinder ); - doc.getDecalRegistry().setBaseFile(fileInfo); - return doc; + OpenRocketDocument doc = loadFromStream(source, motorFinder); + doc.setBaseFile(fileInfo); + return doc; } catch (Exception e) { throw new RocketLoadException("Exception loading stream", e); } @@ -79,7 +79,7 @@ public class GeneralRocketLoader { public final WarningSet getWarnings() { return warnings; } - + protected OpenRocketDocument loadFromStream(InputStream source, MotorFinder motorFinder) throws IOException, RocketLoadException { @@ -99,13 +99,13 @@ public class GeneralRocketLoader { throw new RocketLoadException("Unsupported or corrupt file."); } - + // Detect the appropriate loader // Check for GZIP if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) { OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source), motorFinder); - doc.getDecalRegistry().setIsZipFile(false); + doc.setIsZipFile(false); return doc; } @@ -120,7 +120,7 @@ public class GeneralRocketLoader { } if (entry.getName().matches(".*\\.[oO][rR][kK]$")) { OpenRocketDocument doc = loadFromStream(in, motorFinder); - doc.getDecalRegistry().setIsZipFile(true); + doc.setIsZipFile(true); return doc; } else if (entry.getName().matches(".*\\.[rR][kK][tT]$")) { OpenRocketDocument doc = loadFromStream(in, motorFinder); @@ -136,7 +136,7 @@ public class GeneralRocketLoader { match++; if (match == OPENROCKET_SIGNATURE.length) { OpenRocketDocument doc = loadUsing(openRocketLoader, source, motorFinder); - doc.getDecalRegistry().setIsZipFile(false); + doc.setIsZipFile(false); return doc; } } else { @@ -146,8 +146,8 @@ public class GeneralRocketLoader { byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length); if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) { - OpenRocketDocument doc = loadUsing(rocksimLoader, source, motorFinder); - doc.getDecalRegistry().setIsZipFile(false); + OpenRocketDocument doc = loadUsing(rocksimLoader, source, motorFinder); + doc.setIsZipFile(false); return doc; } throw new RocketLoadException("Unsupported or corrupt file."); diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/AppearanceHandler.java b/core/src/net/sf/openrocket/file/openrocket/importt/AppearanceHandler.java index f9202a177..8d5f2859c 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/AppearanceHandler.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/AppearanceHandler.java @@ -31,7 +31,7 @@ class AppearanceHandler extends AbstractElementHandler { throws SAXException { if ("decal".equals(element)) { String name = attributes.remove("name"); - builder.setImage(context.getOpenRocketDocument().getDecalRegistry().getDecalImage(name)); + builder.setImage(context.getOpenRocketDocument().getDecalRegistry().getAttachment(name)); double rotation = Double.parseDouble(attributes.remove("rotation")); builder.setRotation(rotation); String edgeModeName = attributes.remove("edgemode"); diff --git a/core/src/net/sf/openrocket/file/rocksim/importt/RockSimAppearanceBuilder.java b/core/src/net/sf/openrocket/file/rocksim/importt/RockSimAppearanceBuilder.java index 492fe6d38..44724a845 100644 --- a/core/src/net/sf/openrocket/file/rocksim/importt/RockSimAppearanceBuilder.java +++ b/core/src/net/sf/openrocket/file/rocksim/importt/RockSimAppearanceBuilder.java @@ -68,7 +68,7 @@ public class RockSimAppearanceBuilder extends AppearanceBuilder { //Find out how to get path of current rocksim file //so I can look in it's directory } - setImage(document.getDecalRegistry().getDecalImage(value)); + setImage(document.getDecalRegistry().getAttachment(value)); } } else if ("repeat".equals(name)) { repeat = "1".equals(value); diff --git a/core/src/net/sf/openrocket/gui/ExportDecalDialog.java b/core/src/net/sf/openrocket/gui/ExportDecalDialog.java index 52e2942cc..638e6a466 100644 --- a/core/src/net/sf/openrocket/gui/ExportDecalDialog.java +++ b/core/src/net/sf/openrocket/gui/ExportDecalDialog.java @@ -5,7 +5,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; -import java.util.Set; +import java.util.Collection; import javax.swing.JComboBox; import javax.swing.JDialog; @@ -22,67 +22,66 @@ import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; public class ExportDecalDialog extends JDialog { - + private final static Translator trans = Application.getTranslator(); private final OpenRocketDocument document; - + private JComboBox decalComboBox; - - public ExportDecalDialog(Window parent,OpenRocketDocument doc) { + + public ExportDecalDialog(Window parent, OpenRocketDocument doc) { super(parent, trans.get("ExportDecalDialog.title"), ModalityType.APPLICATION_MODAL); - + this.document = doc; - + JPanel panel = new JPanel(new MigLayout()); - + //// decal list JLabel label = new JLabel(trans.get("ExportDecalDialog.decalList.lbl")); panel.add(label); - - Set exportableDecals = document.getDecalRegistry().getExportableDecalsList(); - - decalComboBox = new JComboBox( exportableDecals.toArray( new DecalImage[0] ) ); + + Collection exportableDecals = document.getDecalRegistry().getDecalList(); + + decalComboBox = new JComboBox(exportableDecals.toArray(new DecalImage[0])); decalComboBox.setEditable(false); panel.add(decalComboBox, "growx, wrap"); - + final JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); chooser.setVisible(true); chooser.setDialogType(JFileChooser.SAVE_DIALOG); - - chooser.addActionListener( new ActionListener() { + + chooser.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); - if ( command.equals(JFileChooser.CANCEL_SELECTION) ) { + if (command.equals(JFileChooser.CANCEL_SELECTION)) { ExportDecalDialog.this.dispose(); - } else if ( command.equals(JFileChooser.APPROVE_SELECTION)) { + } else if (command.equals(JFileChooser.APPROVE_SELECTION)) { // Here we copy the bits out. - + // FIXME - confirm overwrite? DecalImage selectedDecal = (DecalImage) decalComboBox.getSelectedItem(); File selectedFile = chooser.getSelectedFile(); - - export(selectedDecal,selectedFile); + + export(selectedDecal, selectedFile); ExportDecalDialog.this.dispose(); } } }); panel.add(chooser, "span, grow"); - + this.add(panel); this.pack(); } - - private void export( DecalImage decal, File selectedFile ) { - + + private void export(DecalImage decal, File selectedFile) { + try { decal.exportImage(selectedFile, false); - } - catch (IOException iex) { + } catch (IOException iex) { throw new BugException(iex); } } - + } diff --git a/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java b/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java index ac729661f..e1730f001 100644 --- a/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java +++ b/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java @@ -71,7 +71,7 @@ public class DecalModel extends AbstractListModel implements ComboBoxModel { if (action == JFileChooser.APPROVE_OPTION) { ((SwingPreferences) Application.getPreferences()).setDefaultDirectory(fc.getCurrentDirectory()); File file = fc.getSelectedFile(); - setSelectedItem(document.getDecalRegistry().getDecalImage(file)); + setSelectedItem(document.getDecalRegistry().getAttachment(file)); } } });