1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
3 import java.util.ArrayList;
6 import javax.swing.event.ChangeEvent;
7 import javax.swing.event.ChangeListener;
9 import net.sf.openrocket.document.Simulation;
10 import net.sf.openrocket.optimization.general.OptimizationException;
11 import net.sf.openrocket.optimization.rocketoptimization.SimulationModifier;
12 import net.sf.openrocket.unit.UnitGroup;
13 import net.sf.openrocket.util.MathUtil;
16 * An abstract implementation of the SimulationModifier interface. An implementation
17 * needs only to implement the {@link #getCurrentSIValue(Simulation)} and
18 * {@link #modify(net.sf.openrocket.document.Simulation, double)} methods.
20 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22 public abstract class AbstractSimulationModifier implements SimulationModifier {
24 private final String name;
25 private final Object relatedObject;
26 private final UnitGroup unitGroup;
28 private double minValue = 0.0;
29 private double maxValue = 1.0;
31 private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
37 * @param modifierName the name of this modifier (returned by {@link #getName()})
38 * @param relatedObject the related object (returned by {@link #getRelatedObject()})
39 * @param unitGroup the unit group (returned by {@link #getUnitGroup()})
41 public AbstractSimulationModifier(String modifierName, Object relatedObject, UnitGroup unitGroup) {
42 this.name = modifierName;
43 this.relatedObject = relatedObject;
44 this.unitGroup = unitGroup;
49 public String getName() {
54 public Object getRelatedObject() {
59 public double getCurrentScaledValue(Simulation simulation) throws OptimizationException {
60 double value = getCurrentSIValue(simulation);
61 return toScaledValue(value);
67 * Returns the scaled value (normally within [0...1]). If the min...max range is singular,
68 * this method returns 0.0, 1.0 or 0.5 depending on whether the value is less than,
69 * greater than or equal to the limit.
71 * @param value the value in SI units
72 * @return the value in scaled range (normally within [0...1])
74 protected double toScaledValue(double value) {
75 if (MathUtil.equals(minValue, maxValue)) {
83 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
88 * Returns the base value (in SI units).
90 * @param value the value in scaled range (normally within [0...1])
91 * @return the value in SI units
93 protected double toBaseValue(double value) {
94 System.out.println("value=" + value + " minValue=" + minValue + " maxValue=" + maxValue);
95 return MathUtil.map(value, 0.0, 1.0, minValue, maxValue);
101 public double getMinValue() {
106 public void setMinValue(double value) {
107 if (MathUtil.equals(minValue, value))
109 this.minValue = value;
110 if (maxValue < minValue)
116 public double getMaxValue() {
121 public void setMaxValue(double value) {
122 if (MathUtil.equals(maxValue, value))
124 this.maxValue = value;
125 if (minValue > maxValue)
131 public UnitGroup getUnitGroup() {
137 public void addChangeListener(ChangeListener listener) {
138 listeners.add(listener);
142 public void removeChangeListener(ChangeListener listener) {
143 listeners.remove(listener);
148 * Fire a change event to the listeners.
150 protected void fireChangeEvent() {
151 ChangeListener[] array = listeners.toArray(new ChangeListener[0]);
152 ChangeEvent event = new ChangeEvent(this);
153 for (ChangeListener l : array) {
154 l.stateChanged(event);