bug fixes and rocket optimization
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / modifiers / AbstractSimulationModifier.java
1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.swing.event.ChangeEvent;
7 import javax.swing.event.ChangeListener;
8
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;
14
15 /**
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.
19  * 
20  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21  */
22 public abstract class AbstractSimulationModifier implements SimulationModifier {
23         
24         private final String name;
25         private final Object relatedObject;
26         private final UnitGroup unitGroup;
27         
28         private double minValue = 0.0;
29         private double maxValue = 1.0;
30         
31         private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
32         
33         
34         /**
35          * Sole constructor.
36          * 
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()})
40          */
41         public AbstractSimulationModifier(String modifierName, Object relatedObject, UnitGroup unitGroup) {
42                 this.name = modifierName;
43                 this.relatedObject = relatedObject;
44                 this.unitGroup = unitGroup;
45         }
46         
47         
48         @Override
49         public String getName() {
50                 return name;
51         }
52         
53         @Override
54         public Object getRelatedObject() {
55                 return relatedObject;
56         }
57         
58         @Override
59         public double getCurrentScaledValue(Simulation simulation) throws OptimizationException {
60                 double value = getCurrentSIValue(simulation);
61                 return toScaledValue(value);
62         }
63         
64         
65
66         /**
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.
70          * 
71          * @param value         the value in SI units
72          * @return                      the value in scaled range (normally within [0...1])
73          */
74         protected double toScaledValue(double value) {
75                 if (MathUtil.equals(minValue, maxValue)) {
76                         if (value > maxValue)
77                                 return 1.0;
78                         if (value < minValue)
79                                 return 0.0;
80                         return 0.5;
81                 }
82                 
83                 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
84         }
85         
86         
87         /**
88          * Returns the base value (in SI units).
89          * 
90          * @param value         the value in scaled range (normally within [0...1])
91          * @return                      the value in SI units
92          */
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);
96         }
97         
98         
99
100         @Override
101         public double getMinValue() {
102                 return minValue;
103         }
104         
105         @Override
106         public void setMinValue(double value) {
107                 if (MathUtil.equals(minValue, value))
108                         return;
109                 this.minValue = value;
110                 if (maxValue < minValue)
111                         maxValue = minValue;
112                 fireChangeEvent();
113         }
114         
115         @Override
116         public double getMaxValue() {
117                 return maxValue;
118         }
119         
120         @Override
121         public void setMaxValue(double value) {
122                 if (MathUtil.equals(maxValue, value))
123                         return;
124                 this.maxValue = value;
125                 if (minValue > maxValue)
126                         minValue = maxValue;
127                 fireChangeEvent();
128         }
129         
130         @Override
131         public UnitGroup getUnitGroup() {
132                 return unitGroup;
133         }
134         
135         
136         @Override
137         public void addChangeListener(ChangeListener listener) {
138                 listeners.add(listener);
139         }
140         
141         @Override
142         public void removeChangeListener(ChangeListener listener) {
143                 listeners.remove(listener);
144         }
145         
146         
147         /**
148          * Fire a change event to the listeners.
149          */
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);
155                 }
156         }
157         
158 }