Redo unit conversion

This commit is contained in:
Bane Stojkovic
2026-02-20 22:31:40 +01:00
parent 303e59303b
commit 03af702933
2 changed files with 26 additions and 22 deletions
@@ -69,30 +69,9 @@ public class PropertyWrapper implements Cloneable {
InvocationTargetException, SecurityException, NoSuchMethodException {
this.ownerObject = object;
Object value = getGetter().invoke(object);
value = convertUnitsIfNeeded(value);
this.value = value;
}
private static Object convertUnitsIfNeeded(Object value) {
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_UNIT_CONVERSION_KEY, true)
&& value instanceof Size sizeValue) {
SizeUnit preferredUnit;
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.METRIC_KEY, true)) {
preferredUnit = SizeUnit.mm;
}
else {
preferredUnit = SizeUnit.in;
}
if (preferredUnit.getType() != sizeValue.getUnit().getType()) {
SizeUnit targetUnit = SizeUnit.ConversionMapping.get(sizeValue.getUnit());
if (targetUnit != null) {
value = sizeValue.convertToUnits(targetUnit);
}
}
}
return value;
}
// public void readUniqueFrom(IDIYComponent component)
// throws IllegalArgumentException, IllegalAccessException,
// InvocationTargetException {
@@ -35,6 +35,10 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import javax.swing.JComboBox;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.IPlugInPort;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.swingframework.DoubleTextField;
import org.diylc.common.PropertyWrapper;
@@ -55,7 +59,8 @@ public class MeasureEditor extends Container {
@SuppressWarnings("unchecked")
public MeasureEditor(final PropertyWrapper property) {
setLayout(new BorderLayout());
final AbstractMeasure<?> measure = ((AbstractMeasure<?>) property.getValue());
AbstractMeasure<?> measure = ((AbstractMeasure<?>) property.getValue());
measure = convertUnitsIfNeeded(measure);
valueField = new DoubleTextField(measure == null ? null : measure.getValue());
oldBg = valueField.getBackground();
valueField.addPropertyChangeListener(DoubleTextField.VALUE_PROPERTY, new PropertyChangeListener() {
@@ -122,6 +127,26 @@ public class MeasureEditor extends Container {
}
}
private AbstractMeasure<?> convertUnitsIfNeeded(AbstractMeasure<?> value) {
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_UNIT_CONVERSION_KEY, true)
&& value instanceof Size sizeValue) {
SizeUnit preferredUnit;
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.METRIC_KEY, true)) {
preferredUnit = SizeUnit.mm;
}
else {
preferredUnit = SizeUnit.in;
}
if (preferredUnit.getType() != sizeValue.getUnit().getType()) {
SizeUnit targetUnit = SizeUnit.ConversionMapping.get(sizeValue.getUnit());
if (targetUnit != null) {
value = sizeValue.convertToUnits(targetUnit);
}
}
}
return value;
}
@Override
public void requestFocus() {
this.valueField.requestFocus();