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:
parent
26f8fdf63b
commit
a5d5e3d500
@ -1595,4 +1595,5 @@ table.column.PartNo = Part Number
|
||||
table.column.OuterDiameter = Outer Diameter
|
||||
table.column.InnerDiameter = Inner Diameter
|
||||
table.column.Length = Length
|
||||
table.column.Favorite = Favorite
|
||||
|
||||
|
@ -3,43 +3,126 @@ package net.sf.openrocket.database;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import net.sf.openrocket.file.preset.PresetCSVReader;
|
||||
import net.sf.openrocket.preset.ComponentPreset;
|
||||
import net.sf.openrocket.preset.InvalidComponentPresetException;
|
||||
import net.sf.openrocket.preset.TypedPropertyMap;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
import net.sf.openrocket.util.BugException;
|
||||
|
||||
public class ComponentPresetDao {
|
||||
|
||||
// List of all ComponentPresets
|
||||
private final List<ComponentPreset> templates = new ArrayList<ComponentPreset>();
|
||||
|
||||
// Package scope constructor to control creation pattern.
|
||||
public ComponentPresetDao() {}
|
||||
|
||||
|
||||
public void initialize() throws IOException {
|
||||
|
||||
Set<String> favorites = Application.getPreferences().getComponentFavorites();
|
||||
|
||||
InputStream is = ComponentPresetDao.class.getResourceAsStream("/datafiles/bodytubepresets.csv");
|
||||
|
||||
|
||||
PresetCSVReader parser = new PresetCSVReader(is);
|
||||
List<TypedPropertyMap> list = parser.parse();
|
||||
for( TypedPropertyMap o : list ) {
|
||||
try {
|
||||
ComponentPreset preset = ComponentPreset.create(o);
|
||||
if ( favorites.contains(preset.preferenceKey())) {
|
||||
preset.setFavorite(true);
|
||||
}
|
||||
this.insert(preset);
|
||||
} catch ( InvalidComponentPresetException ex ) {
|
||||
throw new BugException( ex );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List<ComponentPreset> listAll() {
|
||||
return templates;
|
||||
}
|
||||
|
||||
|
||||
public void insert( ComponentPreset 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 );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -63,5 +63,16 @@ public abstract class Column {
|
||||
* @return the value at the specified position.
|
||||
*/
|
||||
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 ) {
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -52,5 +52,10 @@ public abstract class ColumnTableModel extends AbstractTableModel {
|
||||
}
|
||||
return columns[col].getValueAt(row);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
||||
columns[columnIndex].setValueAt(rowIndex, aValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,8 +28,7 @@ public class PresetModel extends AbstractListModel implements ComboBoxModel, Com
|
||||
private final List<ComponentPreset> presets;
|
||||
|
||||
public PresetModel(RocketComponent component) {
|
||||
// FIXME: This should load only the user's favorites, NOT all presets
|
||||
presets = Application.getComponentPresetDao().listAll();
|
||||
presets = Application.getComponentPresetDao().listForType(component.getPresetType(), true);
|
||||
this.component = component;
|
||||
previousPreset = component.getPresetComponent();
|
||||
component.addComponentChangeListener(this);
|
||||
|
@ -48,11 +48,28 @@ public class ComponentPresetChooserDialog extends JDialog {
|
||||
|
||||
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++) {
|
||||
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
|
||||
public Object getValueAt(int row) {
|
||||
if (key.getType() == Double.class && key.getUnitGroup() != null) {
|
||||
@ -70,9 +87,16 @@ public class ComponentPresetChooserDialog extends JDialog {
|
||||
public int getRowCount() {
|
||||
return ComponentPresetChooserDialog.this.presets.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCellEditable(int rowIndex, int columnIndex) {
|
||||
return columnIndex == 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
final JTable table = new JTable( tableModel );
|
||||
|
||||
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
|
||||
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
|
||||
|
@ -5,6 +5,7 @@ import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -17,6 +18,7 @@ import net.sf.openrocket.arch.SystemInfo;
|
||||
import net.sf.openrocket.document.Simulation;
|
||||
import net.sf.openrocket.logging.LogHelper;
|
||||
import net.sf.openrocket.material.Material;
|
||||
import net.sf.openrocket.preset.ComponentPreset;
|
||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
||||
import net.sf.openrocket.simulation.FlightDataType;
|
||||
import net.sf.openrocket.simulation.RK4SimulationStepper;
|
||||
@ -553,7 +555,25 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences {
|
||||
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
|
||||
|
||||
}
|
||||
|
@ -19,16 +19,37 @@ import net.sf.openrocket.util.BugException;
|
||||
*
|
||||
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||
*/
|
||||
// FIXME - Implement clone.
|
||||
public class ComponentPreset {
|
||||
|
||||
private final TypedPropertyMap properties = new TypedPropertyMap();
|
||||
|
||||
|
||||
// TODO - Implement clone.
|
||||
private boolean favorite = false;
|
||||
|
||||
public enum Type {
|
||||
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);
|
||||
@ -158,10 +179,20 @@ public class ComponentPreset {
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
public boolean isFavorite() {
|
||||
return favorite;
|
||||
}
|
||||
|
||||
public void setFavorite(boolean favorite) {
|
||||
this.favorite = favorite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return get(MANUFACTURER).toString() + " " + get(PARTNO);
|
||||
}
|
||||
|
||||
|
||||
public String preferenceKey() {
|
||||
return get(MANUFACTURER).toString() + "|" + get(PARTNO);
|
||||
}
|
||||
}
|
||||
|
@ -63,6 +63,11 @@ public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial
|
||||
|
||||
/************ Get/set component parameter methods ************/
|
||||
|
||||
@Override
|
||||
public ComponentPreset.Type getPresetType() {
|
||||
return ComponentPreset.Type.BODY_TUBE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the outer radius of the body tube.
|
||||
*
|
||||
|
@ -672,6 +672,16 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
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
|
||||
* preset values.
|
||||
|
@ -7,6 +7,7 @@ import java.util.Set;
|
||||
import net.sf.openrocket.database.Databases;
|
||||
import net.sf.openrocket.l10n.Translator;
|
||||
import net.sf.openrocket.material.Material;
|
||||
import net.sf.openrocket.preset.ComponentPreset;
|
||||
import net.sf.openrocket.rocketcomponent.BodyComponent;
|
||||
import net.sf.openrocket.rocketcomponent.FinSet;
|
||||
import net.sf.openrocket.rocketcomponent.InternalComponent;
|
||||
@ -363,6 +364,9 @@ public abstract class Preferences {
|
||||
public abstract Set<Material> getUserMaterials();
|
||||
public abstract void removeUserMaterial(Material m);
|
||||
|
||||
public abstract void setComponentFavorite( ComponentPreset preset, boolean favorite );
|
||||
public abstract Set<String> getComponentFavorites( );
|
||||
|
||||
/*
|
||||
* Map of default line styles
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user