blob: eb39c7486d807d99d32bc6deaf86dc52e894b4a8 [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 org.eclipse.apogy.common.math.ui.composites.Tuple3dComposite;
import org.eclipse.apogy.common.topology.addons.dynamics.ApogyCommonTopologyAddonsDynamicsPackage.Literals;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
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;
public class RPROConstraintComposite extends Composite {
private DataBindingContext m_bindingContext;
private org.eclipse.apogy.common.topology.addons.dynamics.RPROConstraint rPROConstraint;
private final Button enabledButton;
private final Tuple3dComposite linearStrengthComposite;
private final Tuple3dComposite angularStrengthComposite;
private Adapter constraintStatesAdapter;
public RPROConstraintComposite(Composite parent, int style,
org.eclipse.apogy.common.topology.addons.dynamics.RPROConstraint newRPROConstraint) {
this(parent, style);
setRPROConstraint(newRPROConstraint);
}
public RPROConstraintComposite(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("Angular Strength:");
this.angularStrengthComposite = new Tuple3dComposite(this, SWT.NONE);
this.angularStrengthComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
new Label(this, SWT.NONE).setText("Linear Strength:");
this.linearStrengthComposite = new Tuple3dComposite(this, SWT.NONE);
this.linearStrengthComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
if (this.rPROConstraint != null) {
this.m_bindingContext = initDataBindings();
}
// Dispose
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (RPROConstraintComposite.this.m_bindingContext != null)
RPROConstraintComposite.this.m_bindingContext.dispose();
}
});
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
IObservableValue enabledObserveWidget = WidgetProperties.selection().observe(this.enabledButton);
IObservableValue enabledObserveValue = EMFProperties.value(Literals.ABSTRACT_CONSTRAINT__ENABLED)
.observe(this.rPROConstraint);
bindingContext.bindValue(enabledObserveWidget, enabledObserveValue, null, null);
return bindingContext;
}
public org.eclipse.apogy.common.topology.addons.dynamics.RPROConstraint getRPROConstraint() {
return this.rPROConstraint;
}
public void setRPROConstraint(org.eclipse.apogy.common.topology.addons.dynamics.RPROConstraint newRPROConstraint) {
// Unregister listener from previous CylindricalConstraint
if (getRPROConstraint() != null) {
getRPROConstraint().eAdapters().remove(getConstraintStatesAdapter());
}
setRPROConstraint(newRPROConstraint, true);
if (newRPROConstraint != null) {
// Register listener to the new CylindricalConstraint
newRPROConstraint.eAdapters().add(getConstraintStatesAdapter());
this.linearStrengthComposite.setTuple3d(newRPROConstraint.getLinearStrength());
this.angularStrengthComposite.setTuple3d(newRPROConstraint.getAngularStrength());
} else {
this.linearStrengthComposite.setTuple3d(null);
this.angularStrengthComposite.setTuple3d(null);
}
}
public void setRPROConstraint(org.eclipse.apogy.common.topology.addons.dynamics.RPROConstraint newRPROConstraint,
boolean update) {
this.rPROConstraint = newRPROConstraint;
if (update) {
if (this.m_bindingContext != null) {
this.m_bindingContext.dispose();
this.m_bindingContext = null;
}
if (this.rPROConstraint != null) {
this.m_bindingContext = initDataBindings();
} else {
this.enabledButton.setEnabled(false);
}
}
}
private Adapter getConstraintStatesAdapter() {
if (this.constraintStatesAdapter == null) {
this.constraintStatesAdapter = new AdapterImpl() {
@Override
public void notifyChanged(org.eclipse.emf.common.notify.Notification msg) {
// If the angular current state has been changed.
if (msg.getFeature() == Literals.RPRO_CONSTRAINT__ANGULAR_STRENGTH) {
RPROConstraintComposite.this.angularStrengthComposite
.setTuple3d((org.eclipse.apogy.common.math.Tuple3d) msg.getNewValue());
}
// If the linear current state has been changed.
else if (msg.getFeature() == Literals.RPRO_CONSTRAINT__LINEAR_STRENGTH) {
RPROConstraintComposite.this.linearStrengthComposite
.setTuple3d((org.eclipse.apogy.common.math.Tuple3d) msg.getNewValue());
}
};
};
}
return this.constraintStatesAdapter;
}
}