blob: 2c7795b2c60a68ad98e49b745916747898923275 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pierre Allard,
* Regent L'Archeveque,
* Olivier L. Larouche - initial API and implementation
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.emf.ui.descriptors;
import javax.measure.unit.Unit;
import org.eclipse.apogy.common.emf.ApogyCommonEMFFacade;
import org.eclipse.apogy.common.emf.ui.Activator;
import org.eclipse.apogy.common.emf.ui.ApogyCommonEMFUIFacade;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AbstractUnitItemPropertyDescriptor extends ItemPropertyDescriptor {
private static final Logger Logger = LoggerFactory.getLogger(AbstractUnitItemPropertyDescriptor.class);
private IPropertyChangeListener preferencesListener = null;
private Unit<?> nativeUnit = null;
private Unit<?> displayUnit = null;
private Boolean unitsAreDefined = null;
private Boolean unitsAreValid = null;
public AbstractUnitItemPropertyDescriptor(AdapterFactory adapterFactory, ResourceLocator resourceLocator,
String displayName, String description, EStructuralFeature feature, boolean isSettable, boolean multiLine,
boolean sortChoices, Object staticImage, String category, String[] filterFlags) {
super(adapterFactory, resourceLocator, displayName, description, feature, isSettable, multiLine, sortChoices,
staticImage, category, filterFlags);
// Register a listener on the Preferences to respond to changes in display
// units.
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(getPreferencesListener());
}
@Override
public void setPropertyValue(Object object, Object value) {
Object nativeValue = value;
// If native and display units are defined.
if (areUnitsValid()) {
if (value instanceof Number) {
try {
double displayValue = ((Number) value).doubleValue();
double tempNativeValue = getDisplayUnit().getConverterTo(getNativeUnit()).convert(displayValue);
if (nativeValue instanceof Byte) {
nativeValue = ((Number) tempNativeValue).byteValue();
} else if (nativeValue instanceof Short) {
nativeValue = ((Number) tempNativeValue).shortValue();
} else if (nativeValue instanceof Integer) {
nativeValue = ((Number) tempNativeValue).intValue();
} else if (nativeValue instanceof Long) {
nativeValue = ((Number) tempNativeValue).longValue();
} else if (nativeValue instanceof Float) {
nativeValue = ((Number) tempNativeValue).floatValue();
} else if (nativeValue instanceof Double) {
nativeValue = ((Number) tempNativeValue).doubleValue();
}
} catch (Throwable t) {
String featureID = this.feature.getEContainingClass().getInstanceClassName() + "."
+ this.feature.getName();
Logger.error(featureID + " : Failed to convert from display units to native units.", t);
}
}
}
super.setPropertyValue(object, nativeValue);
}
@Override
public String getDisplayName(Object object) {
String displayName = super.getDisplayName(object);
if (areUnitsValid()) {
displayName = displayName + " (" + getDisplayUnit().toString() + ")";
}
return displayName;
}
public boolean areUnitsDefined() {
if (this.unitsAreDefined == null) {
this.unitsAreDefined = new Boolean(getNativeUnit() != null && getDisplayUnit() != null);
}
return this.unitsAreDefined.booleanValue();
}
public boolean areUnitsValid() {
if (this.unitsAreValid == null) {
this.unitsAreValid = Boolean.FALSE;
if (areUnitsDefined()) {
try {
getNativeUnit().getConverterTo(getDisplayUnit());
this.unitsAreValid = true;
} catch (Throwable e) {
String featureID = this.feature.getEContainingClass().getInstanceClassName() + "."
+ this.feature.getName();
Logger.error("Invalid Units for feature <" + featureID + "> : Display Unit <"
+ getDisplayUnit().toString() + "> is not compatible with Native Unit <"
+ getNativeUnit().toString() + ">.", e);
}
}
}
return this.unitsAreValid.booleanValue();
}
@Override
protected Object getValue(EObject object, EStructuralFeature feature) {
// If native and display units are defined.
if (areUnitsValid()) {
try {
// Return the value converted to the display unit.
Object value = super.getValue(object, feature);
if (value instanceof Number) {
double nativeValue = ((Number) value).doubleValue();
double displayValue = getNativeUnit().getConverterTo(getDisplayUnit()).convert(nativeValue);
return Double.toString(displayValue);
}
} catch (Throwable t) {
String featureID = feature.getEContainingClass().getInstanceClassName() + "." + feature.getName();
Logger.error(featureID + " : Failed to convert from native units to display units !", t);
}
}
return super.getValue(object, feature);
}
protected Unit<?> getDisplayUnit() {
if (this.displayUnit == null) {
this.displayUnit = ApogyCommonEMFUIFacade.INSTANCE.getDisplayUnits(this.feature);
}
return this.displayUnit;
}
protected Unit<?> getNativeUnit() {
if (this.nativeUnit == null) {
this.nativeUnit = ApogyCommonEMFFacade.INSTANCE.getEngineeringUnits(this.feature);
}
return this.nativeUnit;
}
private IPropertyChangeListener getPreferencesListener() {
if (this.preferencesListener == null) {
this.preferencesListener = new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
// Reset attributes to trigger resolving of display units.
AbstractUnitItemPropertyDescriptor.this.displayUnit = null;
AbstractUnitItemPropertyDescriptor.this.unitsAreDefined = null;
AbstractUnitItemPropertyDescriptor.this.unitsAreValid = null;
}
};
}
return this.preferencesListener;
}
}