Make DecalImage no longer inherit from Attachment. It seems less

confusing since a DecalImage cannot be used in place of Attachments in
general.  Made Attachment a concrete class.
This commit is contained in:
kruland2607 2013-02-03 20:14:51 -06:00
parent 405f0db1b7
commit 4cdf03075b
7 changed files with 45 additions and 64 deletions

View File

@ -1,12 +1,18 @@
package net.sf.openrocket.appearance;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import net.sf.openrocket.document.Attachment;
import net.sf.openrocket.util.ChangeSource;
public interface DecalImage extends Attachment {
public void exportImage( File file, boolean watchForChanges ) throws IOException;
public interface DecalImage extends ChangeSource {
public String getName();
public InputStream getBytes() throws FileNotFoundException, IOException;
public void exportImage(File file, boolean watchForChanges) throws IOException;
}

View File

@ -4,12 +4,38 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import net.sf.openrocket.util.AbstractChangeSource;
import net.sf.openrocket.util.ChangeSource;
public interface Attachment extends Comparable<Attachment>, ChangeSource {
public abstract class Attachment extends AbstractChangeSource implements Comparable<Attachment>, ChangeSource {
public abstract String getName();
private final String name;
public Attachment(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public abstract InputStream getBytes() throws FileNotFoundException, IOException;
@Override
public int compareTo(Attachment o) {
return this.name.compareTo(o.name);
}
@Override
public String toString() {
return getName();
}
@Override
public void fireChangeEvent() {
super.fireChangeEvent();
}
}

View File

@ -20,7 +20,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.openrocket.appearance.DecalImage;
import net.sf.openrocket.document.attachments.BaseAttachment;
import net.sf.openrocket.document.attachments.FileSystemAttachment;
import net.sf.openrocket.gui.watcher.FileWatcher;
import net.sf.openrocket.gui.watcher.WatchEvent;
@ -28,6 +27,7 @@ import net.sf.openrocket.gui.watcher.WatchService;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.BugException;
import net.sf.openrocket.util.ChangeSource;
import net.sf.openrocket.util.FileUtils;
public class DecalRegistry {
@ -99,7 +99,7 @@ public class DecalRegistry {
return decals;
}
public class DecalImageImpl implements DecalImage, Cloneable {
public class DecalImageImpl implements DecalImage, Cloneable, Comparable<DecalImage>, ChangeSource {
private final Attachment delegate;
@ -115,7 +115,6 @@ public class DecalRegistry {
this.delegate = delegate;
}
@Override
public String getName() {
return name != null ? name : delegate.getName();
}
@ -128,7 +127,6 @@ public class DecalRegistry {
* @throws FileNotFoundException
* @throws IOException
*/
@Override
public InputStream getBytes() throws FileNotFoundException, IOException {
// First check if the decal is located on the file system
File exportedFile = getFileSystemLocation();
@ -164,7 +162,7 @@ public class DecalRegistry {
@Override
public void handleEvent(WatchEvent evt) {
((BaseAttachment) DecalImageImpl.this.delegate).fireChangeEvent();
DecalImageImpl.this.delegate.fireChangeEvent();
System.out.println(this.getFile() + " has changed");
}
@ -191,10 +189,7 @@ public class DecalRegistry {
}
@Override
public int compareTo(Attachment o) {
if (!(o instanceof DecalImageImpl)) {
return -1;
}
public int compareTo(DecalImage o) {
return getName().compareTo(o.getName());
}

View File

@ -1,45 +0,0 @@
package net.sf.openrocket.document.attachments;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import net.sf.openrocket.document.Attachment;
import net.sf.openrocket.util.AbstractChangeSource;
public abstract class BaseAttachment extends AbstractChangeSource implements Attachment {
private final String name;
public BaseAttachment(String name) {
super();
this.name = name;
}
@Override
public final String getName() {
return name;
}
@Override
public abstract InputStream getBytes() throws FileNotFoundException, IOException;
@Override
public int compareTo(Attachment o) {
if (!(o instanceof BaseAttachment)) {
return -1;
}
return this.name.compareTo(((BaseAttachment) o).name);
}
@Override
public String toString() {
return getName();
}
@Override
public void fireChangeEvent() {
super.fireChangeEvent();
}
}

View File

@ -8,7 +8,7 @@ import java.io.InputStream;
import net.sf.openrocket.document.Attachment;
public class FileSystemAttachment extends BaseAttachment implements Attachment {
public class FileSystemAttachment extends Attachment {
private final File location;

View File

@ -11,7 +11,7 @@ import java.util.zip.ZipInputStream;
import net.sf.openrocket.document.Attachment;
import net.sf.openrocket.util.FileUtils;
public class ZipFileAttachment extends BaseAttachment implements Attachment {
public class ZipFileAttachment extends Attachment {
private final URL zipFileLocation;

View File

@ -16,7 +16,6 @@ import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.appearance.DecalImage;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.file.AttachmentUtils;
import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.startup.Application;
@ -79,7 +78,7 @@ public class ExportDecalDialog extends JDialog {
private void export(DecalImage decal, File selectedFile) {
try {
AttachmentUtils.exportAttachment(decal, selectedFile);
decal.exportImage(selectedFile, false);
} catch (IOException iex) {
// FIXME - probably want a simple user dialog here since FileIO is not really a bug.
throw new BugException(iex);