blob: 891e0e2e7ce96d2e9a5f1ae1b31e2628d957acb1 [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,
* Sebastien Gemme - initial API and implementation
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.topology.addons.dynamics.ui.composites;
import java.text.DecimalFormat;
import org.eclipse.apogy.common.databinding.converters.DoubleToStringConverter;
import org.eclipse.apogy.common.topology.addons.dynamics.ApogyCommonTopologyAddonsDynamicsPackage.Literals;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class GearRatioConstraintComposite extends Composite {
private DataBindingContext m_bindingContext;
private org.eclipse.apogy.common.topology.addons.dynamics.GearRatioConstraint gearRatioConstraint;
private final Button enabledButton;
private final Text forceText;
public GearRatioConstraintComposite(Composite parent, int style,
org.eclipse.apogy.common.topology.addons.dynamics.GearRatioConstraint newGearRatioConstraint) {
this(parent, style);
setGearRatioConstraint(newGearRatioConstraint);
}
public GearRatioConstraintComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(2, false));
new Label(this, SWT.NONE).setText("Enabled:");
this.enabledButton = new Button(this, SWT.CHECK);
this.enabledButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
new Label(this, SWT.NONE).setText("Force:");
this.forceText = new Text(this, SWT.NONE);
this.forceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
if (this.gearRatioConstraint != null) {
this.m_bindingContext = initDataBindings();
}
// Dispose
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (GearRatioConstraintComposite.this.m_bindingContext != null)
GearRatioConstraintComposite.this.m_bindingContext.dispose();
}
});
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
// Enabled
IObservableValue enabledObserveWidget = WidgetProperties.selection().observe(this.enabledButton);
IObservableValue enabledObserveValue = EMFProperties.value(Literals.ABSTRACT_CONSTRAINT__ENABLED)
.observe(this.gearRatioConstraint);
bindingContext.bindValue(enabledObserveWidget, enabledObserveValue, null, null);
// Force
IObservableValue forceObserveWidget = PojoProperties.value("text").observe(this.forceText);
IObservableValue forceObserveValue = EMFProperties.value(Literals.GEAR_RATIO_CONSTRAINT__FORCE)
.observe(this.gearRatioConstraint);
UpdateValueStrategy forceUpdateValueStrategy = new UpdateValueStrategy();
forceUpdateValueStrategy.setConverter(new DoubleToStringConverter(new DecimalFormat("0.000")));
bindingContext.bindValue(forceObserveWidget, forceObserveValue,
new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), forceUpdateValueStrategy);
return bindingContext;
}
public org.eclipse.apogy.common.topology.addons.dynamics.GearRatioConstraint getGearRatioConstraint() {
return this.gearRatioConstraint;
}
public void setGearRatioConstraint(
org.eclipse.apogy.common.topology.addons.dynamics.GearRatioConstraint newGearRatioConstraint) {
setGearRatioConstraint(newGearRatioConstraint, true);
}
public void setGearRatioConstraint(
org.eclipse.apogy.common.topology.addons.dynamics.GearRatioConstraint newGearRatioConstraint,
boolean update) {
this.gearRatioConstraint = newGearRatioConstraint;
if (update) {
if (this.m_bindingContext != null) {
this.m_bindingContext.dispose();
this.m_bindingContext = null;
}
if (this.gearRatioConstraint != null) {
this.m_bindingContext = initDataBindings();
}
}
}
}