Checkpoint commit. Added AttachmentFactory interface and
BaseAttachmentFactory. Reimplemented DecalRegistry to implement AttachmentFactory and contain a delegate BaseAttachmentFactory.
This commit is contained in:
parent
05ff94836f
commit
8ad89448df
@ -0,0 +1,7 @@
|
|||||||
|
package net.sf.openrocket.document;
|
||||||
|
|
||||||
|
public interface AttachmentFactory<T extends Attachment> {
|
||||||
|
|
||||||
|
public T getAttachment(String name);
|
||||||
|
|
||||||
|
}
|
135
core/src/net/sf/openrocket/document/BaseAttachmentFactory.java
Normal file
135
core/src/net/sf/openrocket/document/BaseAttachmentFactory.java
Normal file
@ -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<BaseAttachmentFactory.BaseAttachment> {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -10,69 +10,64 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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.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.logging.LogHelper;
|
||||||
import net.sf.openrocket.startup.Application;
|
import net.sf.openrocket.startup.Application;
|
||||||
import net.sf.openrocket.util.BugException;
|
import net.sf.openrocket.util.BugException;
|
||||||
import net.sf.openrocket.util.FileUtils;
|
import net.sf.openrocket.util.FileUtils;
|
||||||
|
|
||||||
public class DecalRegistry {
|
public class DecalRegistry implements AttachmentFactory<DecalImage> {
|
||||||
private static LogHelper log = Application.getLogger();
|
private static LogHelper log = Application.getLogger();
|
||||||
|
|
||||||
private FileInfo fileInfo;
|
private final BaseAttachmentFactory baseFactory;
|
||||||
private boolean isZipFile = false;
|
|
||||||
|
|
||||||
private Map<String,DecalImageImpl> registeredDecals = new HashMap<String,DecalImageImpl>();
|
public DecalRegistry(BaseAttachmentFactory baseFactory) {
|
||||||
|
this.baseFactory = baseFactory;
|
||||||
public void setBaseFile(FileInfo fileInfo) {
|
|
||||||
this.fileInfo = fileInfo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIsZipFile( boolean isZipFile ) {
|
private Map<String, DecalImageImpl> registeredDecals = new HashMap<String, DecalImageImpl>();
|
||||||
this.isZipFile = isZipFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DecalImage getDecalImage( String decalName ) {
|
public DecalImage getAttachment(String decalName) {
|
||||||
DecalImageImpl d = registeredDecals.get(decalName);
|
DecalImageImpl d = registeredDecals.get(decalName);
|
||||||
if ( d == null ) {
|
if (d == null) {
|
||||||
d = new DecalImageImpl(decalName);
|
BaseAttachment attachment = baseFactory.getAttachment(decalName);
|
||||||
|
d = new DecalImageImpl(attachment);
|
||||||
registeredDecals.put(decalName, d);
|
registeredDecals.put(decalName, d);
|
||||||
}
|
}
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DecalImage getDecalImage( File file ) {
|
public DecalImage getAttachment(File file) {
|
||||||
|
|
||||||
// See if this file is being used already
|
// See if this file is being used already
|
||||||
DecalImageImpl decal = findDecalForFile( file );
|
DecalImageImpl decal = findDecalForFile(file);
|
||||||
|
|
||||||
if ( decal != null ) {
|
if (decal != null) {
|
||||||
return decal;
|
return decal;
|
||||||
}
|
}
|
||||||
|
|
||||||
// It's a new file, generate a name for it.
|
// It's a new file, generate a name for it.
|
||||||
String decalName = makeUniqueName( file.getName() );
|
String decalName = makeUniqueName(file.getName());
|
||||||
|
|
||||||
decal = new DecalImageImpl( decalName );
|
BaseAttachment attachment = baseFactory.getAttachment(decalName);
|
||||||
decal.setFileSystemLocation( file );
|
decal = new DecalImageImpl(attachment);
|
||||||
|
decal.setFileSystemLocation(file);
|
||||||
|
|
||||||
registeredDecals.put(decalName, decal);
|
registeredDecals.put(decalName, decal);
|
||||||
return decal;
|
return decal;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<DecalImage> getDecalList( ) {
|
public Collection<DecalImage> getDecalList() {
|
||||||
|
|
||||||
Set<DecalImage> decals = new TreeSet<DecalImage>();
|
Set<DecalImage> decals = new TreeSet<DecalImage>();
|
||||||
|
|
||||||
@ -81,33 +76,19 @@ public class DecalRegistry {
|
|||||||
return decals;
|
return decals;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<DecalImage> getExportableDecalsList() {
|
|
||||||
|
|
||||||
Set<DecalImage> exportableDecals = new HashSet<DecalImage>();
|
|
||||||
|
|
||||||
for( DecalImage d : registeredDecals.values() ) {
|
|
||||||
if ( isExportable(d.getName())) {
|
|
||||||
exportableDecals.add(d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return exportableDecals;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DecalImageImpl implements DecalImage, Comparable {
|
public class DecalImageImpl implements DecalImage, Comparable {
|
||||||
|
|
||||||
private final String name;
|
private final BaseAttachment delegate;
|
||||||
|
|
||||||
private File fileSystemLocation;
|
private File fileSystemLocation;
|
||||||
|
|
||||||
private DecalImageImpl( String name ) {
|
private DecalImageImpl(BaseAttachment delegate) {
|
||||||
this.name = name;
|
this.delegate = delegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return delegate.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -117,57 +98,33 @@ public class DecalRegistry {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exportImage(File file, boolean watchForChanges) throws IOException {
|
public void exportImage(File file, boolean watchForChanges) throws IOException {
|
||||||
this.fileSystemLocation = file;
|
|
||||||
DecalRegistry.this.exportDecal(this, file);
|
DecalRegistry.this.exportDecal(this, file);
|
||||||
|
this.fileSystemLocation = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
File getFileSystemLocation() {
|
File getFileSystemLocation() {
|
||||||
return fileSystemLocation;
|
return fileSystemLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFileSystemLocation( File fileSystemLocation ) {
|
void setFileSystemLocation(File fileSystemLocation) {
|
||||||
this.fileSystemLocation = fileSystemLocation;
|
this.fileSystemLocation = fileSystemLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return delegate.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Object o) {
|
public int compareTo(Object o) {
|
||||||
if ( ! (o instanceof DecalImageImpl ) ) {
|
if (!(o instanceof DecalImageImpl)) {
|
||||||
return -1;
|
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.
|
* 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.
|
* If it reads in the bytes from an actual file, the underlying file is closed.
|
||||||
@ -177,97 +134,46 @@ public class DecalRegistry {
|
|||||||
* @throws FileNotFoundException
|
* @throws FileNotFoundException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private InputStream getDecal( DecalImageImpl decal ) throws FileNotFoundException, IOException {
|
private InputStream getDecal(DecalImageImpl decal) throws FileNotFoundException, IOException {
|
||||||
|
|
||||||
// This is the InputStream to be returned.
|
|
||||||
InputStream rawIs = null;
|
|
||||||
|
|
||||||
|
|
||||||
// First check if the decal is located on the file system
|
// First check if the decal is located on the file system
|
||||||
File exportedFile= decal.getFileSystemLocation();
|
File exportedFile = decal.getFileSystemLocation();
|
||||||
if ( exportedFile != null ) {
|
if (exportedFile != null) {
|
||||||
rawIs = new FileInputStream(exportedFile);
|
InputStream rawIs = new FileInputStream(exportedFile);
|
||||||
}
|
try {
|
||||||
|
byte[] bytes = FileUtils.readBytes(rawIs);
|
||||||
String name = decal.getName();
|
return new ByteArrayInputStream(bytes);
|
||||||
|
} finally {
|
||||||
if ( rawIs == null && isZipFile ) {
|
rawIs.close();
|
||||||
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 ) {
|
return decal.delegate.getBytes();
|
||||||
throw new FileNotFoundException( "Unable to locate decal for name " + name );
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
byte[] bytes = FileUtils.readBytes(rawIs);
|
|
||||||
return new ByteArrayInputStream(bytes);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
rawIs.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void exportDecal( DecalImageImpl decal, File selectedFile ) throws IOException {
|
private void exportDecal(DecalImageImpl decal, File selectedFile) throws IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
InputStream is = decal.getBytes();
|
InputStream is = decal.getBytes();
|
||||||
OutputStream os = new BufferedOutputStream( new FileOutputStream(selectedFile));
|
OutputStream os = new BufferedOutputStream(new FileOutputStream(selectedFile));
|
||||||
|
|
||||||
FileUtils.copy(is, os);
|
FileUtils.copy(is, os);
|
||||||
|
|
||||||
is.close();
|
is.close();
|
||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
}
|
} catch (IOException iex) {
|
||||||
catch (IOException iex) {
|
|
||||||
throw new BugException(iex);
|
throw new BugException(iex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DecalImageImpl findDecalForFile(File file) {
|
||||||
|
|
||||||
private ZipInputStream findInZipContainer( String name ) {
|
for (DecalImageImpl d : registeredDecals.values()) {
|
||||||
ZipInputStream zis = null;
|
if (file.equals(d.getFileSystemLocation())) {
|
||||||
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() ) ) {
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -299,13 +205,13 @@ public class DecalRegistry {
|
|||||||
private static final int NUMBER_INDEX = 3;
|
private static final int NUMBER_INDEX = 3;
|
||||||
private static final int EXTENSION_INDEX = 4;
|
private static final int EXTENSION_INDEX = 4;
|
||||||
|
|
||||||
private String makeUniqueName( String name ) {
|
private String makeUniqueName(String name) {
|
||||||
|
|
||||||
String newName = "decals/" + name;
|
String newName = "decals/" + name;
|
||||||
String basename = "";
|
String basename = "";
|
||||||
String extension = "";
|
String extension = "";
|
||||||
Matcher nameMatcher = fileNamePattern.matcher(newName);
|
Matcher nameMatcher = fileNamePattern.matcher(newName);
|
||||||
if ( nameMatcher.matches() ) {
|
if (nameMatcher.matches()) {
|
||||||
basename = nameMatcher.group(BASE_NAME_INDEX);
|
basename = nameMatcher.group(BASE_NAME_INDEX);
|
||||||
extension = nameMatcher.group(EXTENSION_INDEX);
|
extension = nameMatcher.group(EXTENSION_INDEX);
|
||||||
}
|
}
|
||||||
@ -314,33 +220,33 @@ public class DecalRegistry {
|
|||||||
|
|
||||||
boolean needsRewrite = false;
|
boolean needsRewrite = false;
|
||||||
|
|
||||||
for ( DecalImageImpl d: registeredDecals.values() ) {
|
for (DecalImageImpl d : registeredDecals.values()) {
|
||||||
Matcher m = fileNamePattern.matcher( d.getName() );
|
Matcher m = fileNamePattern.matcher(d.getName());
|
||||||
if ( m.matches() ) {
|
if (m.matches()) {
|
||||||
if ( basename.equals(m.group(BASE_NAME_INDEX)) && extension.equals(m.group(EXTENSION_INDEX))) {
|
if (basename.equals(m.group(BASE_NAME_INDEX)) && extension.equals(m.group(EXTENSION_INDEX))) {
|
||||||
String intString = m.group(NUMBER_INDEX);
|
String intString = m.group(NUMBER_INDEX);
|
||||||
if ( intString != null ) {
|
if (intString != null) {
|
||||||
Integer i = Integer.parseInt(intString);
|
Integer i = Integer.parseInt(intString);
|
||||||
counts.add(i);
|
counts.add(i);
|
||||||
}
|
}
|
||||||
needsRewrite = true;
|
needsRewrite = true;
|
||||||
}
|
}
|
||||||
} else if ( newName.equals(d.getName() ) ) {
|
} else if (newName.equals(d.getName())) {
|
||||||
needsRewrite = true;
|
needsRewrite = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !needsRewrite ) {
|
if (!needsRewrite) {
|
||||||
return newName;
|
return newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find a missing integer;
|
// find a missing integer;
|
||||||
Integer newIndex = 1;
|
Integer newIndex = 1;
|
||||||
while( counts.contains(newIndex) ) {
|
while (counts.contains(newIndex)) {
|
||||||
newIndex++;
|
newIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MessageFormat.format("{0} ({1}).{2}", basename,newIndex,extension);
|
return MessageFormat.format("{0} ({1}).{2}", basename, newIndex, extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,20 +6,17 @@ import java.util.LinkedHashSet;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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.DocumentChangeEvent;
|
||||||
import net.sf.openrocket.document.events.DocumentChangeListener;
|
import net.sf.openrocket.document.events.DocumentChangeListener;
|
||||||
import net.sf.openrocket.document.events.SimulationChangeEvent;
|
import net.sf.openrocket.document.events.SimulationChangeEvent;
|
||||||
|
import net.sf.openrocket.file.FileInfo;
|
||||||
import net.sf.openrocket.logging.LogHelper;
|
import net.sf.openrocket.logging.LogHelper;
|
||||||
import net.sf.openrocket.logging.TraceException;
|
import net.sf.openrocket.logging.TraceException;
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
|
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
|
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
|
||||||
import net.sf.openrocket.rocketcomponent.Configuration;
|
import net.sf.openrocket.rocketcomponent.Configuration;
|
||||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
import net.sf.openrocket.rocketcomponent.Rocket;
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
|
||||||
import net.sf.openrocket.simulation.FlightDataType;
|
import net.sf.openrocket.simulation.FlightDataType;
|
||||||
import net.sf.openrocket.simulation.customexpression.CustomExpression;
|
import net.sf.openrocket.simulation.customexpression.CustomExpression;
|
||||||
import net.sf.openrocket.simulation.listeners.SimulationListener;
|
import net.sf.openrocket.simulation.listeners.SimulationListener;
|
||||||
@ -62,7 +59,8 @@ public class OpenRocketDocument implements ComponentChangeListener {
|
|||||||
private final ArrayList<Simulation> simulations = new ArrayList<Simulation>();
|
private final ArrayList<Simulation> simulations = new ArrayList<Simulation>();
|
||||||
private ArrayList<CustomExpression> customExpressions = new ArrayList<CustomExpression>();
|
private ArrayList<CustomExpression> customExpressions = new ArrayList<CustomExpression>();
|
||||||
|
|
||||||
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/redo variables and mechanism are documented in doc/undo-redo-flow.*
|
||||||
@ -115,38 +113,48 @@ public class OpenRocketDocument implements ComponentChangeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void addCustomExpression(CustomExpression expression){
|
public void setBaseFile(FileInfo fileInfo) {
|
||||||
if (customExpressions.contains(expression)){
|
attachmentFactory.setBaseFile(fileInfo);
|
||||||
log.user("Could not add custom expression "+expression.getName()+" to document as document alerady has a matching expression.");
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 {
|
} else {
|
||||||
customExpressions.add(expression);
|
customExpressions.add(expression);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeCustomExpression(CustomExpression expression){
|
public void removeCustomExpression(CustomExpression expression) {
|
||||||
customExpressions.remove(expression);
|
customExpressions.remove(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CustomExpression> getCustomExpressions(){
|
public List<CustomExpression> getCustomExpressions() {
|
||||||
return customExpressions;
|
return customExpressions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns a set of all the flight data types defined or available in any way in the rocket document
|
* Returns a set of all the flight data types defined or available in any way in the rocket document
|
||||||
*/
|
*/
|
||||||
public Set<FlightDataType> getFlightDataTypes(){
|
public Set<FlightDataType> getFlightDataTypes() {
|
||||||
Set<FlightDataType> allTypes = new LinkedHashSet<FlightDataType>();
|
Set<FlightDataType> allTypes = new LinkedHashSet<FlightDataType>();
|
||||||
|
|
||||||
// built in
|
// built in
|
||||||
Collections.addAll(allTypes, FlightDataType.ALL_TYPES);
|
Collections.addAll(allTypes, FlightDataType.ALL_TYPES);
|
||||||
|
|
||||||
// custom expressions
|
// custom expressions
|
||||||
for (CustomExpression exp : customExpressions){
|
for (CustomExpression exp : customExpressions) {
|
||||||
allTypes.add(exp.getType());
|
allTypes.add(exp.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
// simulation listeners
|
// simulation listeners
|
||||||
for (Simulation sim : simulations){
|
for (Simulation sim : simulations) {
|
||||||
for (String className : sim.getSimulationListeners()) {
|
for (String className : sim.getSimulationListeners()) {
|
||||||
SimulationListener l = null;
|
SimulationListener l = null;
|
||||||
try {
|
try {
|
||||||
@ -561,11 +569,11 @@ public class OpenRocketDocument implements ComponentChangeListener {
|
|||||||
|
|
||||||
/////// Listeners
|
/////// Listeners
|
||||||
|
|
||||||
public void addUndoRedoListener( UndoRedoListener listener ) {
|
public void addUndoRedoListener(UndoRedoListener listener) {
|
||||||
undoRedoListeners.add(listener);
|
undoRedoListeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeUndoRedoListener( UndoRedoListener listener ) {
|
public void removeUndoRedoListener(UndoRedoListener listener) {
|
||||||
undoRedoListeners.remove(listener);
|
undoRedoListeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ public class GeneralRocketLoader {
|
|||||||
return doc;
|
return doc;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RocketLoadException("Exception loading file: " + source,e);
|
throw new RocketLoadException("Exception loading file: " + source, e);
|
||||||
} finally {
|
} finally {
|
||||||
if (stream != null) {
|
if (stream != null) {
|
||||||
try {
|
try {
|
||||||
@ -68,9 +68,9 @@ public class GeneralRocketLoader {
|
|||||||
|
|
||||||
public final OpenRocketDocument load(InputStream source, FileInfo fileInfo, MotorFinder motorFinder) throws RocketLoadException {
|
public final OpenRocketDocument load(InputStream source, FileInfo fileInfo, MotorFinder motorFinder) throws RocketLoadException {
|
||||||
try {
|
try {
|
||||||
OpenRocketDocument doc = loadFromStream(source, motorFinder );
|
OpenRocketDocument doc = loadFromStream(source, motorFinder);
|
||||||
doc.getDecalRegistry().setBaseFile(fileInfo);
|
doc.setBaseFile(fileInfo);
|
||||||
return doc;
|
return doc;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RocketLoadException("Exception loading stream", e);
|
throw new RocketLoadException("Exception loading stream", e);
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ public class GeneralRocketLoader {
|
|||||||
// Check for GZIP
|
// Check for GZIP
|
||||||
if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
|
if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
|
||||||
OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source), motorFinder);
|
OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source), motorFinder);
|
||||||
doc.getDecalRegistry().setIsZipFile(false);
|
doc.setIsZipFile(false);
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public class GeneralRocketLoader {
|
|||||||
}
|
}
|
||||||
if (entry.getName().matches(".*\\.[oO][rR][kK]$")) {
|
if (entry.getName().matches(".*\\.[oO][rR][kK]$")) {
|
||||||
OpenRocketDocument doc = loadFromStream(in, motorFinder);
|
OpenRocketDocument doc = loadFromStream(in, motorFinder);
|
||||||
doc.getDecalRegistry().setIsZipFile(true);
|
doc.setIsZipFile(true);
|
||||||
return doc;
|
return doc;
|
||||||
} else if (entry.getName().matches(".*\\.[rR][kK][tT]$")) {
|
} else if (entry.getName().matches(".*\\.[rR][kK][tT]$")) {
|
||||||
OpenRocketDocument doc = loadFromStream(in, motorFinder);
|
OpenRocketDocument doc = loadFromStream(in, motorFinder);
|
||||||
@ -136,7 +136,7 @@ public class GeneralRocketLoader {
|
|||||||
match++;
|
match++;
|
||||||
if (match == OPENROCKET_SIGNATURE.length) {
|
if (match == OPENROCKET_SIGNATURE.length) {
|
||||||
OpenRocketDocument doc = loadUsing(openRocketLoader, source, motorFinder);
|
OpenRocketDocument doc = loadUsing(openRocketLoader, source, motorFinder);
|
||||||
doc.getDecalRegistry().setIsZipFile(false);
|
doc.setIsZipFile(false);
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -146,8 +146,8 @@ public class GeneralRocketLoader {
|
|||||||
|
|
||||||
byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length);
|
byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length);
|
||||||
if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) {
|
if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) {
|
||||||
OpenRocketDocument doc = loadUsing(rocksimLoader, source, motorFinder);
|
OpenRocketDocument doc = loadUsing(rocksimLoader, source, motorFinder);
|
||||||
doc.getDecalRegistry().setIsZipFile(false);
|
doc.setIsZipFile(false);
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
throw new RocketLoadException("Unsupported or corrupt file.");
|
throw new RocketLoadException("Unsupported or corrupt file.");
|
||||||
|
@ -31,7 +31,7 @@ class AppearanceHandler extends AbstractElementHandler {
|
|||||||
throws SAXException {
|
throws SAXException {
|
||||||
if ("decal".equals(element)) {
|
if ("decal".equals(element)) {
|
||||||
String name = attributes.remove("name");
|
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"));
|
double rotation = Double.parseDouble(attributes.remove("rotation"));
|
||||||
builder.setRotation(rotation);
|
builder.setRotation(rotation);
|
||||||
String edgeModeName = attributes.remove("edgemode");
|
String edgeModeName = attributes.remove("edgemode");
|
||||||
|
@ -68,7 +68,7 @@ public class RockSimAppearanceBuilder extends AppearanceBuilder {
|
|||||||
//Find out how to get path of current rocksim file
|
//Find out how to get path of current rocksim file
|
||||||
//so I can look in it's directory
|
//so I can look in it's directory
|
||||||
}
|
}
|
||||||
setImage(document.getDecalRegistry().getDecalImage(value));
|
setImage(document.getDecalRegistry().getAttachment(value));
|
||||||
}
|
}
|
||||||
} else if ("repeat".equals(name)) {
|
} else if ("repeat".equals(name)) {
|
||||||
repeat = "1".equals(value);
|
repeat = "1".equals(value);
|
||||||
|
@ -5,7 +5,7 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Set;
|
import java.util.Collection;
|
||||||
|
|
||||||
import javax.swing.JComboBox;
|
import javax.swing.JComboBox;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
@ -29,7 +29,7 @@ public class ExportDecalDialog extends JDialog {
|
|||||||
|
|
||||||
private JComboBox decalComboBox;
|
private JComboBox decalComboBox;
|
||||||
|
|
||||||
public ExportDecalDialog(Window parent,OpenRocketDocument doc) {
|
public ExportDecalDialog(Window parent, OpenRocketDocument doc) {
|
||||||
super(parent, trans.get("ExportDecalDialog.title"), ModalityType.APPLICATION_MODAL);
|
super(parent, trans.get("ExportDecalDialog.title"), ModalityType.APPLICATION_MODAL);
|
||||||
|
|
||||||
this.document = doc;
|
this.document = doc;
|
||||||
@ -40,9 +40,9 @@ public class ExportDecalDialog extends JDialog {
|
|||||||
JLabel label = new JLabel(trans.get("ExportDecalDialog.decalList.lbl"));
|
JLabel label = new JLabel(trans.get("ExportDecalDialog.decalList.lbl"));
|
||||||
panel.add(label);
|
panel.add(label);
|
||||||
|
|
||||||
Set<DecalImage> exportableDecals = document.getDecalRegistry().getExportableDecalsList();
|
Collection<DecalImage> exportableDecals = document.getDecalRegistry().getDecalList();
|
||||||
|
|
||||||
decalComboBox = new JComboBox( exportableDecals.toArray( new DecalImage[0] ) );
|
decalComboBox = new JComboBox(exportableDecals.toArray(new DecalImage[0]));
|
||||||
decalComboBox.setEditable(false);
|
decalComboBox.setEditable(false);
|
||||||
panel.add(decalComboBox, "growx, wrap");
|
panel.add(decalComboBox, "growx, wrap");
|
||||||
|
|
||||||
@ -51,20 +51,20 @@ public class ExportDecalDialog extends JDialog {
|
|||||||
chooser.setVisible(true);
|
chooser.setVisible(true);
|
||||||
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
|
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
|
||||||
|
|
||||||
chooser.addActionListener( new ActionListener() {
|
chooser.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
String command = e.getActionCommand();
|
String command = e.getActionCommand();
|
||||||
if ( command.equals(JFileChooser.CANCEL_SELECTION) ) {
|
if (command.equals(JFileChooser.CANCEL_SELECTION)) {
|
||||||
ExportDecalDialog.this.dispose();
|
ExportDecalDialog.this.dispose();
|
||||||
} else if ( command.equals(JFileChooser.APPROVE_SELECTION)) {
|
} else if (command.equals(JFileChooser.APPROVE_SELECTION)) {
|
||||||
// Here we copy the bits out.
|
// Here we copy the bits out.
|
||||||
|
|
||||||
// FIXME - confirm overwrite?
|
// FIXME - confirm overwrite?
|
||||||
DecalImage selectedDecal = (DecalImage) decalComboBox.getSelectedItem();
|
DecalImage selectedDecal = (DecalImage) decalComboBox.getSelectedItem();
|
||||||
File selectedFile = chooser.getSelectedFile();
|
File selectedFile = chooser.getSelectedFile();
|
||||||
|
|
||||||
export(selectedDecal,selectedFile);
|
export(selectedDecal, selectedFile);
|
||||||
ExportDecalDialog.this.dispose();
|
ExportDecalDialog.this.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,12 +75,11 @@ public class ExportDecalDialog extends JDialog {
|
|||||||
this.pack();
|
this.pack();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void export( DecalImage decal, File selectedFile ) {
|
private void export(DecalImage decal, File selectedFile) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
decal.exportImage(selectedFile, false);
|
decal.exportImage(selectedFile, false);
|
||||||
}
|
} catch (IOException iex) {
|
||||||
catch (IOException iex) {
|
|
||||||
throw new BugException(iex);
|
throw new BugException(iex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ public class DecalModel extends AbstractListModel implements ComboBoxModel {
|
|||||||
if (action == JFileChooser.APPROVE_OPTION) {
|
if (action == JFileChooser.APPROVE_OPTION) {
|
||||||
((SwingPreferences) Application.getPreferences()).setDefaultDirectory(fc.getCurrentDirectory());
|
((SwingPreferences) Application.getPreferences()).setDefaultDirectory(fc.getCurrentDirectory());
|
||||||
File file = fc.getSelectedFile();
|
File file = fc.getSelectedFile();
|
||||||
setSelectedItem(document.getDecalRegistry().getDecalImage(file));
|
setSelectedItem(document.getDecalRegistry().getAttachment(file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user