04ba7bce14fbda984aea35b85c095381f5103a6e
[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 #getCurrentValue(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 = getCurrentValue(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                 return MathUtil.map(value, 0.0, 1.0, minValue, maxValue);
95         }
96         
97         
98
99         @Override
100         public double getMinValue() {
101                 return minValue;
102         }
103         
104         @Override
105         public void setMinValue(double value) {
106                 if (MathUtil.equals(minValue, value))
107                         return;
108                 this.minValue = value;
109                 if (maxValue < minValue)
110                         maxValue = minValue;
111                 fireChangeEvent();
112         }
113         
114         @Override
115         public double getMaxValue() {
116                 return maxValue;
117         }
118         
119         @Override
120         public void setMaxValue(double value) {
121                 if (MathUtil.equals(maxValue, value))
122                         return;
123                 this.maxValue = value;
124                 if (minValue > maxValue)
125                         minValue = maxValue;
126                 fireChangeEvent();
127         }
128         
129         @Override
130         public UnitGroup getUnitGroup() {
131                 return unitGroup;
132         }
133         
134         
135         @Override
136         public void addChangeListener(ChangeListener listener) {
137                 listeners.add(listener);
138         }
139         
140         @Override
141         public void removeChangeListener(ChangeListener listener) {
142                 listeners.remove(listener);
143         }
144         
145         
146         /**
147          * Fire a change event to the listeners.
148          */
149         protected void fireChangeEvent() {
150                 ChangeListener[] array = listeners.toArray(new ChangeListener[0]);
151                 ChangeEvent event = new ChangeEvent(this);
152                 for (ChangeListener l : array) {
153                         l.stateChanged(event);
154                 }
155         }
156         
157 }