X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fdatabase%2FComponentPresetDatabase.java;h=bb2fae2b981957efa0323695f7f726f758b1451c;hb=4095cb0dd61a75b7b6b0bd811f8e803af5b27919;hp=0fd0d419e8d9adf620eef5c5eb501abcb227aad4;hpb=591e4f3ee829d43dbced0f281b41fafa8ea71a30;p=debian%2Fopenrocket diff --git a/core/src/net/sf/openrocket/database/ComponentPresetDatabase.java b/core/src/net/sf/openrocket/database/ComponentPresetDatabase.java index 0fd0d419..bb2fae2b 100644 --- a/core/src/net/sf/openrocket/database/ComponentPresetDatabase.java +++ b/core/src/net/sf/openrocket/database/ComponentPresetDatabase.java @@ -1,62 +1,38 @@ package net.sf.openrocket.database; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Set; -import javax.xml.bind.JAXBException; - -import net.sf.openrocket.file.Loader; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.preset.ComponentPreset; -import net.sf.openrocket.preset.InvalidComponentPresetException; -import net.sf.openrocket.preset.xml.OpenRocketComponentSaver; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.BugException; - -public class ComponentPresetDatabase extends Database implements ComponentPresetDao { - - private static final LogHelper log = Application.getLogger(); - public static class ComponentPresetLoader implements Loader { +public abstract class ComponentPresetDatabase extends Database implements ComponentPresetDao { - @Override - public Collection load(InputStream stream, String filename) { - - log.debug("Loading presets from file " + filename); + private static final LogHelper logger = Application.getLogger(); - Set favorites = Application.getPreferences().getComponentFavorites(); + private volatile boolean startedLoading = false; + private volatile boolean endedLoading = false; + private final boolean asynchronous; - try { - List presets; - presets = new OpenRocketComponentSaver().unmarshalFromOpenRocketComponent( new InputStreamReader (stream)); - for( ComponentPreset preset : presets ) { - if ( favorites.contains(preset.preferenceKey())) { - preset.setFavorite(true); - } - } - log.debug("ComponentPreset file " + filename + " contained " + presets.size() + " presets"); - return presets; - } catch (JAXBException e) { - throw new BugException("Unable to parser file: "+ filename, e); - } catch (InvalidComponentPresetException e) { - throw new BugException("Unable to parser file: "+ filename, e); - } - - } - - } + /** Set to true the first time {@link #blockUntilLoaded()} is called. */ + protected volatile boolean inUse = false; public ComponentPresetDatabase() { - super(new ComponentPresetLoader()); + super(); + this.asynchronous = false; + } + + public ComponentPresetDatabase(boolean asynchronous ) { + super(); + this.asynchronous = asynchronous; } @Override public List listAll() { + blockUntilLoaded(); return list; } @@ -67,6 +43,7 @@ public class ComponentPresetDatabase extends Database implement @Override public List listForType( ComponentPreset.Type type ) { + blockUntilLoaded(); if ( type == null ) { return Collections.emptyList(); } @@ -92,6 +69,7 @@ public class ComponentPresetDatabase extends Database implement */ @Override public List listForType( ComponentPreset.Type type, boolean favorite ) { + blockUntilLoaded(); if ( !favorite ) { return listForType(type); @@ -99,8 +77,10 @@ public class ComponentPresetDatabase extends Database implement List result = new ArrayList(list.size()/6); + Set favorites = Application.getPreferences().getComponentFavorites(type); + for( ComponentPreset preset : list ) { - if ( preset.isFavorite() && preset.get(ComponentPreset.TYPE).equals(type) ) { + if ( preset.get(ComponentPreset.TYPE).equals(type) && favorites.contains(preset.preferenceKey())) { result.add(preset); } } @@ -109,6 +89,7 @@ public class ComponentPresetDatabase extends Database implement @Override public List listForTypes( ComponentPreset.Type ... type ) { + blockUntilLoaded(); if( type == null || type.length == 0 ) { return Collections.emptyList(); @@ -135,11 +116,13 @@ public class ComponentPresetDatabase extends Database implement @Override public List listForTypes( List types ) { + blockUntilLoaded(); return listForTypes( (ComponentPreset.Type[]) types.toArray() ); } @Override public List find(String manufacturer, String partNo) { + blockUntilLoaded(); List presets = new ArrayList(); for( ComponentPreset preset : list ) { if ( preset.getManufacturer().getSimpleName().equals(manufacturer) && preset.getPartNo().equals(partNo) ) { @@ -150,10 +133,76 @@ public class ComponentPresetDatabase extends Database implement } @Override - public void setFavorite( ComponentPreset preset, boolean favorite ) { - preset.setFavorite(favorite); - Application.getPreferences().setComponentFavorite( preset, favorite ); + public void setFavorite( ComponentPreset preset, ComponentPreset.Type type, boolean favorite ) { + blockUntilLoaded(); + Application.getPreferences().setComponentFavorite( preset, type, favorite ); this.fireAddEvent(preset); } + + /** + * Used for loading the component preset database. This method will be called in a background + * thread to load the presets asynchronously. + */ + protected abstract void load(); + + /** + * Start loading the presets. + * + * @throws IllegalStateException if this method has already been called. + */ + public void startLoading() { + if (startedLoading) { + throw new IllegalStateException("Already called startLoading"); + } + startedLoading = true; + if (asynchronous) { + new LoadingThread().start(); + } else { + load(); + } + synchronized (this) { + endedLoading = true; + this.notifyAll(); + } + } + + /** + * Background thread for loading the presets. + */ + private class LoadingThread extends Thread { + + private LoadingThread() { + this.setName("PresetLoadingThread"); + this.setPriority(MIN_PRIORITY); + } + @Override + public void run() { + load(); + } + } + + /** + * Block the current thread until loading of the presets has been completed. + * + * @throws IllegalStateException if startLoading() has not been called. + */ + public void blockUntilLoaded() { + inUse = true; + if (!startedLoading) { + throw new IllegalStateException("startLoading() has not been called"); + } + if (!endedLoading) { + synchronized (this) { + while (!endedLoading) { + try { + this.wait(); + } catch (InterruptedException e) { + logger.warn("InterruptedException occurred, ignoring", e); + } + } + } + } + } + }