1 package net.sf.openrocket.database;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Collections;
10 import net.sf.openrocket.file.preset.PresetCSVReader;
11 import net.sf.openrocket.preset.ComponentPreset;
12 import net.sf.openrocket.preset.InvalidComponentPresetException;
13 import net.sf.openrocket.preset.TypedPropertyMap;
14 import net.sf.openrocket.startup.Application;
15 import net.sf.openrocket.util.BugException;
17 public class ComponentPresetDao {
19 // List of all ComponentPresets
20 private final List<ComponentPreset> templates = new ArrayList<ComponentPreset>();
22 // Package scope constructor to control creation pattern.
23 public ComponentPresetDao() {}
25 public void initialize() throws IOException {
27 Set<String> favorites = Application.getPreferences().getComponentFavorites();
29 InputStream is = ComponentPresetDao.class.getResourceAsStream("/datafiles/bodytubepresets.csv");
31 PresetCSVReader parser = new PresetCSVReader(is);
32 List<TypedPropertyMap> list = parser.parse();
33 for( TypedPropertyMap o : list ) {
35 ComponentPreset preset = ComponentPreset.create(o);
36 if ( favorites.contains(preset.preferenceKey())) {
37 preset.setFavorite(true);
40 } catch ( InvalidComponentPresetException ex ) {
41 throw new BugException( ex );
48 public List<ComponentPreset> listAll() {
52 public void insert( ComponentPreset preset ) {
53 templates.add(preset);
56 public List<ComponentPreset> listForType( ComponentPreset.Type type ) {
58 return Collections.<ComponentPreset>emptyList();
61 List<ComponentPreset> result = new ArrayList<ComponentPreset>(templates.size()/6);
63 for( ComponentPreset preset : templates ) {
64 if ( preset.get(ComponentPreset.TYPE).equals(type) ) {
73 * Return a list of component presets based on the type.
74 * All components returned will be of Type type.
77 * @param favorite if true, only return the favorites. otherwise return all matching.
80 public List<ComponentPreset> listForType( ComponentPreset.Type type, boolean favorite ) {
83 return listForType(type);
86 List<ComponentPreset> result = new ArrayList<ComponentPreset>(templates.size()/6);
88 for( ComponentPreset preset : templates ) {
89 if ( preset.isFavorite() && preset.get(ComponentPreset.TYPE).equals(type) ) {
98 public List<ComponentPreset> listForTypes( ComponentPreset.Type ... type ) {
100 if( type == null || type.length == 0 ) {
101 return Collections.<ComponentPreset>emptyList();
104 if (type.length == 1 ) {
105 return listForType(type[0]);
108 List<ComponentPreset> result = new ArrayList<ComponentPreset>(templates.size()/6);
110 for( ComponentPreset preset : templates ) {
111 ComponentPreset.Type presetType = preset.get(ComponentPreset.TYPE);
112 typeLoop: for( int i=0; i<type.length; i++ ) {
113 if ( !presetType.equals(type) ) {
115 break typeLoop; // from inner loop.
123 public void setFavorite( ComponentPreset preset, boolean favorite ) {
124 preset.setFavorite(favorite);
125 Application.getPreferences().setComponentFavorite( preset, favorite );