1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
3 import javax.swing.event.ChangeListener;
5 import net.sf.openrocket.document.Simulation;
6 import net.sf.openrocket.optimization.rocketoptimization.SimulationModifier;
7 import net.sf.openrocket.unit.UnitGroup;
8 import net.sf.openrocket.util.BugException;
9 import net.sf.openrocket.util.MathUtil;
10 import net.sf.openrocket.util.Reflection.Method;
12 public class GenericModifier implements SimulationModifier {
14 private final String name;
15 private final Object relatedObject;
16 private final UnitGroup unitGroup;
17 private final double multiplier;
18 private final Object modifiable;
20 private final Method getter;
21 private final Method setter;
23 private double minValue;
24 private double maxValue;
30 public GenericModifier(String modifierName, Object relatedObject, UnitGroup unitGroup, double multiplier,
31 Object modifiable, String methodName) {
32 this.name = modifierName;
33 this.relatedObject = relatedObject;
34 this.unitGroup = unitGroup;
35 this.multiplier = multiplier;
36 this.modifiable = modifiable;
38 if (MathUtil.equals(multiplier, 0)) {
39 throw new IllegalArgumentException("multiplier is zero");
43 methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
44 getter = new Method(modifiable.getClass().getMethod("get" + methodName));
45 setter = new Method(modifiable.getClass().getMethod("set" + methodName, double.class));
46 } catch (SecurityException e) {
47 throw new BugException("Trying to find method get/set" + methodName + " in class " + modifiable.getClass(), e);
48 } catch (NoSuchMethodException e) {
49 throw new BugException("Trying to find method get/set" + methodName + " in class " + modifiable.getClass(), e);
55 public String getName() {
60 public Object getRelatedObject() {
65 public double getCurrentValue() {
66 return ((Double) getter.invoke(modifiable)) * multiplier;
71 public double getCurrentScaledValue() {
72 double value = getCurrentValue();
73 return toScaledValue(value);
77 public void modify(Simulation simulation, double scaledValue) {
78 double siValue = toBaseValue(scaledValue) / multiplier;
79 setter.invoke(modifiable, siValue);
84 * Returns the scaled value (normally within [0...1]).
86 private double toScaledValue(double value) {
87 if (MathUtil.equals(minValue, maxValue)) {
95 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
100 * Returns the base value (in SI units).
102 private double toBaseValue(double value) {
103 return MathUtil.map(value, 0.0, 1.0, minValue, maxValue);
109 public double getMinValue() {
114 public void setMinValue(double value) {
115 if (MathUtil.equals(minValue, value))
117 this.minValue = value;
118 if (maxValue < minValue)
124 public double getMaxValue() {
129 public void setMaxValue(double value) {
130 if (MathUtil.equals(maxValue, value))
132 this.maxValue = value;
133 if (minValue > maxValue)
139 public UnitGroup getUnitGroup() {
145 public void addChangeListener(ChangeListener listener) {
146 // TODO Auto-generated method stub
151 public void removeChangeListener(ChangeListener listener) {
152 // TODO Auto-generated method stub
157 private void fireChangeEvent() {
158 // TODO Auto-generated method stub