Implement persistence of ComponentPreset favorites to preferences. Implement favorite check box in ComponentPresetChooserDialog. Added getPresetType to RocketComponent which returns the ComponentPreset.Type of the most compatible presets.

This commit is contained in:
Kevin Ruland 2012-04-04 21:29:33 +00:00
parent 26f8fdf63b
commit a5d5e3d500
11 changed files with 206 additions and 13 deletions

View File

@ -1595,4 +1595,5 @@ table.column.PartNo = Part Number
table.column.OuterDiameter = Outer Diameter table.column.OuterDiameter = Outer Diameter
table.column.InnerDiameter = Inner Diameter table.column.InnerDiameter = Inner Diameter
table.column.Length = Length table.column.Length = Length
table.column.Favorite = Favorite

View File

@ -3,43 +3,126 @@ package net.sf.openrocket.database;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import net.sf.openrocket.file.preset.PresetCSVReader; import net.sf.openrocket.file.preset.PresetCSVReader;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.InvalidComponentPresetException; import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap; import net.sf.openrocket.preset.TypedPropertyMap;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.BugException;
public class ComponentPresetDao { public class ComponentPresetDao {
// List of all ComponentPresets
private final List<ComponentPreset> templates = new ArrayList<ComponentPreset>(); private final List<ComponentPreset> templates = new ArrayList<ComponentPreset>();
// Package scope constructor to control creation pattern. // Package scope constructor to control creation pattern.
public ComponentPresetDao() {} public ComponentPresetDao() {}
public void initialize() throws IOException { public void initialize() throws IOException {
Set<String> favorites = Application.getPreferences().getComponentFavorites();
InputStream is = ComponentPresetDao.class.getResourceAsStream("/datafiles/bodytubepresets.csv"); InputStream is = ComponentPresetDao.class.getResourceAsStream("/datafiles/bodytubepresets.csv");
PresetCSVReader parser = new PresetCSVReader(is); PresetCSVReader parser = new PresetCSVReader(is);
List<TypedPropertyMap> list = parser.parse(); List<TypedPropertyMap> list = parser.parse();
for( TypedPropertyMap o : list ) { for( TypedPropertyMap o : list ) {
try { try {
ComponentPreset preset = ComponentPreset.create(o); ComponentPreset preset = ComponentPreset.create(o);
if ( favorites.contains(preset.preferenceKey())) {
preset.setFavorite(true);
}
this.insert(preset); this.insert(preset);
} catch ( InvalidComponentPresetException ex ) { } catch ( InvalidComponentPresetException ex ) {
throw new BugException( ex ); throw new BugException( ex );
} }
} }
} }
public List<ComponentPreset> listAll() { public List<ComponentPreset> listAll() {
return templates; return templates;
} }
public void insert( ComponentPreset preset ) { public void insert( ComponentPreset preset ) {
templates.add(preset); templates.add(preset);
} }
public List<ComponentPreset> listForType( ComponentPreset.Type type ) {
if ( type == null ) {
return Collections.<ComponentPreset>emptyList();
}
List<ComponentPreset> result = new ArrayList<ComponentPreset>(templates.size()/6);
for( ComponentPreset preset : templates ) {
if ( preset.get(ComponentPreset.TYPE).equals(type) ) {
result.add(preset);
}
}
return result;
}
/**
* Return a list of component presets based on the type.
* All components returned will be of Type type.
*
* @param type
* @param favorite if true, only return the favorites. otherwise return all matching.
* @return
*/
public List<ComponentPreset> listForType( ComponentPreset.Type type, boolean favorite ) {
if ( !favorite ) {
return listForType(type);
}
List<ComponentPreset> result = new ArrayList<ComponentPreset>(templates.size()/6);
for( ComponentPreset preset : templates ) {
if ( preset.isFavorite() && preset.get(ComponentPreset.TYPE).equals(type) ) {
result.add(preset);
}
}
return result;
}
public List<ComponentPreset> listForTypes( ComponentPreset.Type ... type ) {
if( type == null || type.length == 0 ) {
return Collections.<ComponentPreset>emptyList();
}
if (type.length == 1 ) {
return listForType(type[0]);
}
List<ComponentPreset> result = new ArrayList<ComponentPreset>(templates.size()/6);
for( ComponentPreset preset : templates ) {
ComponentPreset.Type presetType = preset.get(ComponentPreset.TYPE);
typeLoop: for( int i=0; i<type.length; i++ ) {
if ( !presetType.equals(type) ) {
result.add(preset);
break typeLoop; // from inner loop.
}
}
}
return result;
}
public void setFavorite( ComponentPreset preset, boolean favorite ) {
preset.setFavorite(favorite);
Application.getPreferences().setComponentFavorite( preset, favorite );
}
} }

View File

@ -63,5 +63,16 @@ public abstract class Column {
* @return the value at the specified position. * @return the value at the specified position.
*/ */
public abstract Object getValueAt(int row); public abstract Object getValueAt(int row);
/**
* Set a value in the table.
*
* Override if the cell is editable.
*
* @param row
* @param value
*/
public void setValueAt(int row, Object value ) {
}
} }

View File

@ -52,5 +52,10 @@ public abstract class ColumnTableModel extends AbstractTableModel {
} }
return columns[col].getValueAt(row); return columns[col].getValueAt(row);
} }
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
columns[columnIndex].setValueAt(rowIndex, aValue);
}
} }

View File

@ -28,8 +28,7 @@ public class PresetModel extends AbstractListModel implements ComboBoxModel, Com
private final List<ComponentPreset> presets; private final List<ComponentPreset> presets;
public PresetModel(RocketComponent component) { public PresetModel(RocketComponent component) {
// FIXME: This should load only the user's favorites, NOT all presets presets = Application.getComponentPresetDao().listForType(component.getPresetType(), true);
presets = Application.getComponentPresetDao().listAll();
this.component = component; this.component = component;
previousPreset = component.getPresetComponent(); previousPreset = component.getPresetComponent();
component.addComponentChangeListener(this); component.addComponentChangeListener(this);

View File

@ -48,11 +48,28 @@ public class ComponentPresetChooserDialog extends JDialog {
JPanel panel = new JPanel(new MigLayout("fill")); JPanel panel = new JPanel(new MigLayout("fill"));
final Column[] columns = new Column[columnKeys.length]; final Column[] columns = new Column[columnKeys.length+1];
columns[0] = new Column(trans.get("table.column.Favorite") ) {
@Override
public Object getValueAt(int row) {
return Boolean.valueOf(ComponentPresetChooserDialog.this.presets.get(row).isFavorite());
}
@Override
public void setValueAt(int row, Object value) {
Application.getComponentPresetDao().setFavorite(ComponentPresetChooserDialog.this.presets.get(row), (Boolean) value);
}
@Override
public Class<?> getColumnClass() {
return Boolean.class;
}
};
for (int i = 0; i < columnKeys.length; i++) { for (int i = 0; i < columnKeys.length; i++) {
final TypedKey<?> key = columnKeys[i]; final TypedKey<?> key = columnKeys[i];
columns[i] = new Column(trans.get("table.column." + columnKeys[i].getName())) { columns[i+1] = new Column(trans.get("table.column." + columnKeys[i].getName())) {
@Override @Override
public Object getValueAt(int row) { public Object getValueAt(int row) {
if (key.getType() == Double.class && key.getUnitGroup() != null) { if (key.getType() == Double.class && key.getUnitGroup() != null) {
@ -70,9 +87,16 @@ public class ComponentPresetChooserDialog extends JDialog {
public int getRowCount() { public int getRowCount() {
return ComponentPresetChooserDialog.this.presets.size(); return ComponentPresetChooserDialog.this.presets.size();
} }
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
}; };
final JTable table = new JTable( tableModel ); final JTable table = new JTable( tableModel );
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel); final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);

View File

@ -5,6 +5,7 @@ import java.awt.Dimension;
import java.awt.Point; import java.awt.Point;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -17,6 +18,7 @@ import net.sf.openrocket.arch.SystemInfo;
import net.sf.openrocket.document.Simulation; import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.material.Material; import net.sf.openrocket.material.Material;
import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.simulation.FlightDataType; import net.sf.openrocket.simulation.FlightDataType;
import net.sf.openrocket.simulation.RK4SimulationStepper; import net.sf.openrocket.simulation.RK4SimulationStepper;
@ -553,7 +555,25 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences {
return materials; return materials;
} }
public void setComponentFavorite( ComponentPreset preset, boolean favorite ) {
Preferences prefs = PREFNODE.node("favoritePresets");
if ( favorite ) {
prefs.putBoolean(preset.preferenceKey(), true);
} else {
prefs.remove(preset.preferenceKey());
}
}
public Set<String> getComponentFavorites( ) {
Preferences prefs = PREFNODE.node("favoritePresets");
Set<String> collection = new HashSet<String>();
try {
collection.addAll( Arrays.asList(prefs.keys()));
} catch ( BackingStoreException bex ) {
}
return collection;
}
//// Helper methods //// Helper methods
} }

View File

@ -19,16 +19,37 @@ import net.sf.openrocket.util.BugException;
* *
* @author Sampo Niskanen <sampo.niskanen@iki.fi> * @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/ */
// FIXME - Implement clone.
public class ComponentPreset { public class ComponentPreset {
private final TypedPropertyMap properties = new TypedPropertyMap(); private final TypedPropertyMap properties = new TypedPropertyMap();
private boolean favorite = false;
// TODO - Implement clone.
public enum Type { public enum Type {
BODY_TUBE, BODY_TUBE,
NOSE_CONE NOSE_CONE;
Type[] compatibleTypes;
Type () {
compatibleTypes = new Type[1];
compatibleTypes[0] = this;
}
Type( Type ... t ) {
compatibleTypes = new Type[t.length+1];
compatibleTypes[0] = this;
for( int i=0; i<t.length; i++ ) {
compatibleTypes[i+1] = t[i];
}
}
public Type[] getCompatibleTypes() {
return compatibleTypes;
}
} }
public final static TypedKey<Manufacturer> MANUFACTURER = new TypedKey<Manufacturer>("Manufacturer", Manufacturer.class); public final static TypedKey<Manufacturer> MANUFACTURER = new TypedKey<Manufacturer>("Manufacturer", Manufacturer.class);
@ -158,10 +179,20 @@ public class ComponentPreset {
return (T) value; return (T) value;
} }
public boolean isFavorite() {
return favorite;
}
public void setFavorite(boolean favorite) {
this.favorite = favorite;
}
@Override @Override
public String toString() { public String toString() {
return get(MANUFACTURER).toString() + " " + get(PARTNO); return get(MANUFACTURER).toString() + " " + get(PARTNO);
} }
public String preferenceKey() {
return get(MANUFACTURER).toString() + "|" + get(PARTNO);
}
} }

View File

@ -63,6 +63,11 @@ public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial
/************ Get/set component parameter methods ************/ /************ Get/set component parameter methods ************/
@Override
public ComponentPreset.Type getPresetType() {
return ComponentPreset.Type.BODY_TUBE;
}
/** /**
* Return the outer radius of the body tube. * Return the outer radius of the body tube.
* *

View File

@ -672,6 +672,16 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
return presetComponent; return presetComponent;
} }
/**
* Return the most compatible preset type for this component.
* This method should be overridden by components which have presets
*
* @return the most compatible ComponentPreset.Type or <code>null</code> if no presets are compatible.
*/
public ComponentPreset.Type getPresetType() {
return null;
}
/** /**
* Set the preset component this component is based upon and load all of the * Set the preset component this component is based upon and load all of the
* preset values. * preset values.

View File

@ -7,6 +7,7 @@ import java.util.Set;
import net.sf.openrocket.database.Databases; import net.sf.openrocket.database.Databases;
import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.material.Material; import net.sf.openrocket.material.Material;
import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.rocketcomponent.BodyComponent; import net.sf.openrocket.rocketcomponent.BodyComponent;
import net.sf.openrocket.rocketcomponent.FinSet; import net.sf.openrocket.rocketcomponent.FinSet;
import net.sf.openrocket.rocketcomponent.InternalComponent; import net.sf.openrocket.rocketcomponent.InternalComponent;
@ -363,6 +364,9 @@ public abstract class Preferences {
public abstract Set<Material> getUserMaterials(); public abstract Set<Material> getUserMaterials();
public abstract void removeUserMaterial(Material m); public abstract void removeUserMaterial(Material m);
public abstract void setComponentFavorite( ComponentPreset preset, boolean favorite );
public abstract Set<String> getComponentFavorites( );
/* /*
* Map of default line styles * Map of default line styles
*/ */