Commit b2abc6df authored by Kai-Holger Brassel's avatar Kai-Holger Brassel
Browse files

First public version as of Verena Weilers Dissertation

parent 2c40014f
No related merge requests found
Showing with 546 additions and 0 deletions
+546 -0
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="lib" path="indriya-2.0.2.jar" sourcepath="indriya-2.0.2-sources.jar"/>
<!-- classpathentry exported="true" kind="lib" path="javax.inject-1.jar"/-->
<classpathentry exported="true" kind="lib" path="unit-api-2.0.jar" sourcepath="unit-api-2.0-sources.jar"/>
<classpathentry exported="true" kind="lib" path="uom-lib-common-2.0.jar" sourcepath="uom-lib-common-2.0-sources.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/de.hftstuttgart.units.libs/uom-lib-common-2.0-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
*.jar
!indriya*.jar
!javax.inject*.jar
!unit-api*.jar
!uom-lib-common*.jar
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>de.hftstuttgart.indriya.osgiplugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: de.hftstuttgart.units.libs
Bundle-SymbolicName: de.hftstuttgart.indriya.osgiplugin
Bundle-Version: 1.0.0
Bundle-ClassPath: indriya-2.0.2-javadoc.jar,
indriya-2.0.2-sources.jar,
indriya-2.0.2.jar,
unit-api-2.0-javadoc.jar,
unit-api-2.0-sources.jar,
unit-api-2.0.jar,
uom-lib-common-2.0-javadoc.jar,
uom-lib-common-2.0-sources.jar,
uom-lib-common-2.0.jar
Export-Package: javax.measure,
javax.measure.format,
javax.measure.quantity,
javax.measure.spi,
tech.units.indriya,
tech.units.indriya.format,
tech.units.indriya.function,
tech.units.indriya.internal.format,
tech.units.indriya.internal.format.l10n,
tech.units.indriya.internal.function.calc,
tech.units.indriya.internal.function.radix,
tech.units.indriya.internal.function.simplify,
tech.units.indriya.quantity,
tech.units.indriya.quantity.time,
tech.units.indriya.spi,
tech.units.indriya.unit,
tech.uom.lib.common.function,
tech.uom.lib.common.util
Automatic-Module-Name: de.hftstuttgart.units.libs
bin.includes = META-INF/,\
indriya-2.0.2.jar,\
javax.inject-1.jar,\
unit-api-2.0.jar,\
uom-lib-common-2.0.jar
File added
File added
File added
File added
File added
File added
File added
File added
File added
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>de.hftstuttgart.units.ui.renderer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: QuantityControl
Bundle-SymbolicName: de.hftstuttgart.units.ui.renderer;singleton:=true
Bundle-Version: 1.0.0
Bundle-Vendor: Eclipse Modeling Project
Export-Package: de.hftstuttgart.units.ui.renderer;version="1.24.0"
Require-Bundle: org.eclipse.emf.ecp.edit.swt;bundle-version="1.23.0",
org.eclipse.emf.ecp.ui.view.swt;bundle-version="1.23.0",
org.eclipse.emf.ecp.view.core.swt;bundle-version="1.23.0",
org.eclipse.emf.ecp.view.template.model;bundle-version="1.23.0",
org.eclipse.emf.edit;bundle-version="[2.8.0,3.0.0)",
org.eclipse.core.runtime;bundle-version="[3.8.0,4.0.0)",
org.eclipse.emfforms.core.services.editsupport;bundle-version="1.23.0",
org.eclipse.emf.databinding;bundle-version="[1.3.0,2.0.0)",
org.eclipse.emfforms.swt.core.di;bundle-version="1.23.0",
de.hftstuttgart.energycomponents;bundle-version="1.0.0",
de.hftstuttgart.units;bundle-version="1.0.0",
de.hftstuttgart.indriya.osgiplugin;bundle-version="1.0.0",
org.eclipse.emfforms.localization,
org.eclipse.jface
Service-Component: OSGI-INF/quantityRendererService.xml
Bundle-ActivationPolicy: lazy
Import-Package: javax.inject;version="1.0.0",
org.eclipse.emfforms.spi.common.report;version="1.23.0",
org.eclipse.jface.layout;version="0.0.0",
org.eclipse.swt;version="0.0.0",
org.eclipse.swt.widgets;version="0.0.0"
Automatic-Module-Name: de.hftstuttgart.units.ui.eclipse.quantityrenderer
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="de.hftstuttgart.units.ui.rendererservice">
<implementation class="de.hftstuttgart.units.ui.renderer.QuantityControlRendererService"/>
<service>
<provide interface="org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService"/>
</service>
<reference bind="setEMFFormsDatabinding" cardinality="1..1" interface="org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding" name="EMFFormsDatabinding" policy="static"/>
<reference bind="setReportService" cardinality="1..1" interface="org.eclipse.emfforms.spi.common.report.ReportService" name="ReportService" policy="static"/>
</scr:component>
output.. = bin/
bin.includes = META-INF/,\
.,\
OSGI-INF/
source.. = src/
package de.hftstuttgart.units.ui.renderer;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import javax.inject.Inject;
import javax.measure.Unit;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecp.edit.internal.swt.controls.NumericalHelper;
import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
import org.eclipse.emf.ecp.view.internal.core.swt.Activator;
import org.eclipse.emf.ecp.view.internal.core.swt.MessageKeys;
import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
import org.eclipse.emf.ecp.view.spi.core.swt.renderer.TextControlSWTRenderer;
import org.eclipse.emf.ecp.view.spi.model.VControl;
import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleChangeListener;
import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
import org.eclipse.emfforms.spi.common.report.ReportService;
import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
import org.eclipse.jface.dialogs.IDialogLabelKeys;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import de.hftstuttgart.energycomponents.EnCompPackage;
import de.hftstuttgart.units.NullableQuantity;
public class QuantityControlRenderer extends TextControlSWTRenderer {
private final EMFFormsLocalizationService localizationService;
private final EMFFormsLocaleProvider localeProvider;
private EMFFormsLocaleChangeListener emfFormsLocaleChangeListener;
private Unit<?> unit;
/**
* Default constructor.
*
* @param vElement the view model element to be rendered
* @param viewContext the view context
* @param reportService The {@link ReportService}
* @param emfFormsDatabinding The {@link EMFFormsDatabinding}
* @param emfFormsLabelProvider The {@link EMFFormsLabelProvider}
* @param vtViewTemplateProvider The {@link VTViewTemplateProvider}
* @param emfFormsEditSupport The {@link EMFFormsEditSupport}
* @param localizationService The {@link EMFFormsLocalizationService}
* @param localeProvider The {@link EMFFormsLocaleProvider}
*/
@Inject
// CHECKSTYLE.OFF: ParameterNumber
public QuantityControlRenderer(VControl vElement, ViewModelContext viewContext, ReportService reportService,
EMFFormsDatabinding emfFormsDatabinding, EMFFormsLabelProvider emfFormsLabelProvider,
VTViewTemplateProvider vtViewTemplateProvider, EMFFormsEditSupport emfFormsEditSupport,
EMFFormsLocalizationService localizationService, EMFFormsLocaleProvider localeProvider) {
// CHECKSTYLE.ON: ParameterNumber
super(vElement, viewContext, reportService, emfFormsDatabinding, emfFormsLabelProvider, vtViewTemplateProvider,
emfFormsEditSupport);
this.localizationService = localizationService;
this.localeProvider = localeProvider;
}
@Override
protected Control createSWTControl(Composite parent) {
final VFeaturePathDomainModelReference featureRef = (VFeaturePathDomainModelReference) getVElement()
.getDomainModelReference();
final String defaultString = featureRef.getDomainModelEFeature().getDefaultValueLiteral();
final NullableQuantity defaultValue = NullableQuantity.create(defaultString);
unit = defaultValue.getUnit();
final Composite composite = new Composite(parent, SWT.NONE);
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.BEGINNING).applyTo(composite);
final Control control = super.createSWTControl(composite);
final Label label = new Label(composite, SWT.PUSH);
label.setText(unit.toString());
return control;
}
@Override
protected int getDefaultAlignment() {
return SWT.RIGHT;
}
/*
* (non-Javadoc)
* @see org.eclipse.emf.ecp.edit.internal.swt.controls.AbstractTextControl#getTextVariantID()
*/
@Override
protected String getTextVariantID() {
return "org_eclipse_emf_ecp_control_numerical"; //$NON-NLS-1$
}
@Override
protected String getTextMessage() {
return ""; //$NON-NLS-1$
}
@Override
protected Object convert(Text text, EDataType attributeType, String value) throws DatabindingFailedException {
final EStructuralFeature eStructuralFeature = (EStructuralFeature) getModelValue().getValueType();
final NumericalTargetToModelUpdateStrategy converter = new NumericalTargetToModelUpdateStrategy(
eStructuralFeature, getModelValue(), getDataBindingContext(), text);
return converter.convert(value);
}
@Override
protected Binding[] createBindings(final Control control) throws DatabindingFailedException {
final EStructuralFeature structuralFeature = (EStructuralFeature) getModelValue().getValueType();
final UpdateValueStrategy<?, ?> targetToModelStrategy = withPreSetValidation(
new NumericalTargetToModelUpdateStrategy(structuralFeature, getModelValue(), getDataBindingContext(),
(Text) Composite.class.cast(control).getChildren()[0]));
final NumericalModelToTargetUpdateStrategy modelToTargetStrategy = new NumericalModelToTargetUpdateStrategy(
getInstanceClass(structuralFeature), false);
final Binding binding = bindValue(control, getModelValue(), getDataBindingContext(),
targetToModelStrategy, modelToTargetStrategy);
final Binding tooltipBinding = createTooltipBinding(control, getModelValue(), getDataBindingContext(),
targetToModelStrategy,
new NumericalModelToTargetUpdateStrategy(getInstanceClass(structuralFeature), true));
emfFormsLocaleChangeListener = new EMFFormsLocaleChangeListener() {
/**
* {@inheritDoc}
*
* @see org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleChangeListener#notifyLocaleChange()
*/
@Override
public void notifyLocaleChange() {
((Text) control).setMessage(getTextMessage());
binding.updateModelToTarget();
}
};
localeProvider.addEMFFormsLocaleChangeListener(emfFormsLocaleChangeListener);
return new Binding[] { binding, tooltipBinding };
}
private Class<?> getInstanceClass(EStructuralFeature feature) {
if (feature.getEType() == EnCompPackage.eINSTANCE.getQuantityDouble()) {
return Double.class;
} else if (feature.getEType() == EnCompPackage.eINSTANCE.getQuantityLong()) {
return Long.class;
}
assert false;
return null;
}
@Override
protected String getTextFromTextField(Text text, EDataType attributeType) {
return text.getText().isBlank() ? null : super.getTextFromTextField(text, attributeType);
}
/**
* Converts the numerical value from the model to the target. Locale settings are respected,
* i.e. formatting is performed according to the current locale.
*/
private class NumericalModelToTargetUpdateStrategy extends ModelToTargetUpdateStrategy {
private final Class<?> instanceClass;
NumericalModelToTargetUpdateStrategy(Class<?> instanceClass, boolean tooltip) {
super(tooltip);
this.instanceClass = instanceClass;
}
@Override
public Object convertValue(Object value) {
if (value == null) {
return ""; //$NON-NLS-1$
}
return ((NullableQuantity) value).getNumber()
.map(n -> NumericalHelper.setupFormat(localeProvider.getLocale(), instanceClass).format(n))
.orElse(""); //$NON-NLS-1$
}
}
/**
* More specific target to model update strategy that convert the string
* in the text field to a number. If the string is a invalid number,
* for instance because of the current locale, the value is reset to
* the last valid value found in the mode.
*/
private class NumericalTargetToModelUpdateStrategy extends TargetToModelUpdateStrategy {
private final Text text;
private final IObservableValue<?> modelValue;
private final EStructuralFeature eStructuralFeature;
private final DataBindingContext dataBindingContext;
NumericalTargetToModelUpdateStrategy(EStructuralFeature eStructuralFeature,
IObservableValue<?> modelValue, DataBindingContext dataBindingContext, Text text) {
super(eStructuralFeature.isUnsettable());
this.eStructuralFeature = eStructuralFeature;
this.modelValue = modelValue;
this.dataBindingContext = dataBindingContext;
this.text = text;
}
@Override
protected Object convertValue(final Object value) {
final DecimalFormat format = NumericalHelper.setupFormat(
localeProvider.getLocale(), getInstanceClass(eStructuralFeature));
try {
Number number = null;
if (value == null) {
number = NumericalHelper.getDefaultValue(getInstanceClass(eStructuralFeature));
} else {
final ParsePosition pp = new ParsePosition(0);
number = format.parse((String) value, pp);
if (pp.getErrorIndex() != -1 || pp.getIndex() != ((String) value).length()) {
return getOldValue(value);
}
if (NumericalHelper.isInteger(getInstanceClass(eStructuralFeature))) {
boolean maxValue = false;
boolean minValue = false;
final Class<?> instanceClass = getInstanceClass(eStructuralFeature);
try {
if (number.doubleValue() >= getInstanceMaxValue(instanceClass)) {
maxValue = true;
} else if (number.doubleValue() <= getInstanceMinValue(instanceClass)) {
minValue = true;
}
} catch (final IllegalArgumentException ex) {
Activator.logException(ex);
} catch (final SecurityException ex) {
Activator.logException(ex);
} catch (final IllegalAccessException ex) {
Activator.logException(ex);
} catch (final NoSuchFieldException ex) {
Activator.logException(ex);
}
if (maxValue || minValue) {
return numberToQuantity(number);
}
}
}
String formatedNumber = ""; //$NON-NLS-1$
if (number != null) {
formatedNumber = format.format(number);
}
if (formatedNumber.length() == 0) {
return null;
}
return numberToQuantity(format.parse(formatedNumber));
} catch (final ParseException ex) {
return getOldValue(value);
}
}
private Object numberToQuantity(Number number) {
return NullableQuantity.create(number, unit);
}
private double getInstanceMinValue(Class<?> instanceClass)
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
if (Integer.class.isAssignableFrom(instanceClass)
|| Integer.class.getField("TYPE").get(null).equals(instanceClass)) { //$NON-NLS-1$
return Integer.MIN_VALUE;
}
if (Long.class.isAssignableFrom(instanceClass)
|| Long.class.getField("TYPE").get(null).equals(instanceClass)) { //$NON-NLS-1$
return Long.MIN_VALUE;
}
if (Short.class.isAssignableFrom(instanceClass)
|| Short.class.getField("TYPE").get(null).equals(instanceClass)) { //$NON-NLS-1$
return Short.MIN_VALUE;
}
return Double.NaN;
}
private double getInstanceMaxValue(Class<?> instanceClass)
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
if (Integer.class.isAssignableFrom(instanceClass)
|| Integer.class.getField("TYPE").get(null).equals(instanceClass)) { //$NON-NLS-1$
return Integer.MAX_VALUE;
}
if (Long.class.isAssignableFrom(instanceClass)
|| Long.class.getField("TYPE").get(null).equals(instanceClass)) { //$NON-NLS-1$
return Long.MAX_VALUE;
}
if (Short.class.isAssignableFrom(instanceClass)
|| Short.class.getField("TYPE").get(null).equals(instanceClass)) { //$NON-NLS-1$
return Short.MAX_VALUE;
}
return Double.NaN;
}
@Override
protected IStatus doSet(IObservableValue observableValue, Object value) {
final IStatus status = super.doSet(observableValue, value);
// update targets after a model change triggered by the target to model databinding
dataBindingContext.updateTargets();
return status;
}
private Object getOldValue(final Object value) {
if (eStructuralFeature.getDefaultValue() == null && value == null || value.equals("")) { //$NON-NLS-1$
return null;
}
final Object result = modelValue.getValue();
final MessageDialog messageDialog = new MessageDialog(text.getShell(),
localizationService.getString(getClass(), MessageKeys.NumericalControl_InvalidNumber), null,
localizationService.getString(getClass(), MessageKeys.NumericalControl_InvalidNumberWillBeUnset),
MessageDialog.ERROR,
new String[] { JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) }, 0);
new ECPDialogExecutor(messageDialog) {
@Override
public void handleResult(int codeResult) {
}
}.execute();
dataBindingContext.updateTargets();
if (eStructuralFeature.isUnsettable() && result == null) {
// showUnsetLabel();
return SetCommand.UNSET_VALUE;
}
return result;
}
}
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.ecp.view.spi.core.swt.renderer.TextControlSWTRenderer#getUnsetText()
*/
@Override
protected String getUnsetText() {
return localizationService.getString(getClass(), MessageKeys.NumericalControl_NoNumberClickToSetNumber);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTRenderer#dispose()
*/
@Override
protected void dispose() {
super.dispose();
localeProvider.removeEMFFormsLocaleChangeListener(emfFormsLocaleChangeListener);
}
}
Supports Markdown
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