Commit 96b9f081 authored by Nico Mack's avatar Nico Mack

Minor code cleanup and API changes

parent 5b840786
......@@ -24,6 +24,7 @@ import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.utility.EquationSystemBundle;
import lu.list.itis.dkd.tui.utility.StringUtils;
import com.google.common.base.Objects;
......@@ -32,7 +33,7 @@ import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Vector;
import java.util.ArrayList;
/**
* Class implementing numerical variables as handled by the {@link System}.
......@@ -101,6 +102,24 @@ public class NumericalVariable extends Variable<Double> {
initToDefaults();
}
// ---------------------------------------------------------------------------
public NumericalVariable(NumericalVariable original) {
super(EquationSystemBundle.NUMERIC_TYPE, original.name, original.unit);
this.displayName = original.displayName;
this.value = original.value;
this.epsilon = original.epsilon;
this.minValue = original.minValue;
this.maxValue = original.maxValue;
this.initialValue = original.initialValue;
this.normalizedValue = original.normalizedValue;
this.scale = original.scale;
this.valueRange = original.valueRange;
this.format = original.format;
this.roundingFactor = original.roundingFactor;
this.listeners = new ArrayList<>(original.listeners);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitives *
......@@ -120,7 +139,6 @@ public class NumericalVariable extends Variable<Double> {
minValue = -Double.MAX_VALUE;
maxValue = Double.MAX_VALUE;
// initialValue = Double.NaN;
valueRange = getValueRange(minValue, maxValue);
epsilon = DEFAULT_EPSILON;
scale = 1;
......@@ -287,8 +305,6 @@ public class NumericalVariable extends Variable<Double> {
// values
this.normalizedValue = this.normalizeValue(val);
// this.modified = ((newValue != null) && (!isEqual(newValue, this.value, this.epsilon)));
// this.value = newValue;
this.modified = ((newValue != null) && (!isEqual(val, this.value, this.epsilon)));
this.value = val;
if (!this.notificationSuspended() && this.modified) {
......@@ -519,16 +535,7 @@ public class NumericalVariable extends Variable<Double> {
@Override
public NumericalVariable clone() {
NumericalVariable variable = new NumericalVariable(name, unit, value);
variable.displayName = displayName;
variable.format = format;
variable.minValue = minValue;
variable.maxValue = maxValue;
variable.scale = scale;
variable.roundingFactor = roundingFactor;
variable.epsilon = epsilon;
variable.listeners = new Vector<>(listeners);
return variable;
return new NumericalVariable(this);
}
// ---------------------------------------------------------------------------
......@@ -537,14 +544,8 @@ public class NumericalVariable extends Variable<Double> {
@Override
public Double valueFromString(String stringValue) {
Double newValue = Double.valueOf(0);
try {
newValue = Double.parseDouble(stringValue);
} catch (NumberFormatException e) {
LOGGER.error("The provided value ({}) does not match the format stored by this variable instance.", stringValue, e); //$NON-NLS-1$
throw e;
}
return newValue;
Double newValue = StringUtils.getDoubleValue(stringValue);
return (newValue == null) ? Double.valueOf(0) : newValue;
}
// ---------------------------------------------------------------------------
......
......@@ -96,6 +96,12 @@ public class SpatialVariable extends Variable<Point> {
return false;
}
@Override
public void setValue(Point newValue) {
if (newValue != null) {
super.setValue(newValue.clone());
}
}
/** {@inheritDoc} */
@Override
......
......@@ -56,6 +56,10 @@ public class DisplayMarker extends InfoMarker implements InputChangeListener, Va
public DisplayMarker(DisplayMarker original) {
super(original);
this.setupDispatcher();
for (Variable<?> variable : this.variables.values()) {
this.inputChanged(new InputEvent(variable));
}
}
// ---------------------------------------------------------------------------
......@@ -65,8 +69,8 @@ public class DisplayMarker extends InfoMarker implements InputChangeListener, Va
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
/**
* iterates over all value coronas assigned to this widget and populates the dispatcher table, using
* variable names as keys and associating depending coronas as values
* iterates over all value coronas assigned to this widget and populates the dispatcher table,
* using variable names as keys and associating depending coronas as values
*/
// ---------------------------------------------------------------------------
......@@ -79,8 +83,10 @@ public class DisplayMarker extends InfoMarker implements InputChangeListener, Va
for (ValueCorona display : displays) {
Variable<?> variable = display.getVariable();
if (variable != null) {
dispatcher.put(variable.getName(), display);
variables.put(variable.getName(), variable);
String identifier = variable.getName();
dispatcher.put(identifier, display);
variables.put(identifier, variable);
variable.addListener(this);
}
}
}
......
......@@ -93,8 +93,8 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* iterates over all value coronas assigned to this widget and populates the dispatcher table, using
* variable names as keys and associating depending coronas as values
* iterates over all value coronas assigned to this widget and populates the dispatcher table,
* using variable names as keys and associating depending coronas as values
*/
// ---------------------------------------------------------------------------
......@@ -178,6 +178,9 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
List<VariableBased> displays = this.getCoronas(VariableBased.class);
for (VariableBased display : displays) {
connected.addAll(display.connectWithSystemVariables(systemVariables));
List<Variable<?>> declared = display.getDeclaredVariables();
declared.removeAll(connected);
connected.addAll(declared);
}
for (Variable<?> variable : connected) {
......@@ -185,6 +188,7 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
variable.addListener(this);
}
for (Condition<Object> condition : this.conditions) {
if (condition instanceof VariableBased) {
List<Variable<?>> parameters = ((VariableBased) condition).connectWithSystemVariables(systemVariables);
......
......@@ -49,6 +49,9 @@ public class ValueCorona extends SelectableCorona implements InformationReceiver
public ValueCorona(ValueCorona original) {
super(original);
this.variable = (original.variable != null) ? original.variable.clone() : null;
if (this.variable != null) {
this.variable.addListener(this);
}
}
// ---------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment