| /******************************************************************************* |
| * 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.topology.addons.dynamics.ApogyCommonTopologyAddonsDynamicsPackage.Literals; |
| import org.eclipse.apogy.common.topology.addons.dynamics.ConstraintState; |
| 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.FillLayout; |
| 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.Group; |
| import org.eclipse.swt.widgets.Label; |
| |
| public class PrismaticConstraintComposite extends Composite { |
| |
| private DataBindingContext m_bindingContext; |
| private org.eclipse.apogy.common.topology.addons.dynamics.PrismaticConstraint prismaticConstraint; |
| private final Button enabledButton; |
| private final ConstraintStateComposite linearConstraintStateComposite; |
| |
| private Adapter constraintStatesAdapter; |
| |
| public PrismaticConstraintComposite(Composite parent, int style, |
| org.eclipse.apogy.common.topology.addons.dynamics.PrismaticConstraint newPrismaticConstraint) { |
| this(parent, style); |
| setPrismaticConstraint(newPrismaticConstraint); |
| } |
| |
| public PrismaticConstraintComposite(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)); |
| |
| Group grpCurrentLinearState = new Group(this, SWT.NONE); |
| grpCurrentLinearState.setLayout(new FillLayout(SWT.HORIZONTAL)); |
| grpCurrentLinearState.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1)); |
| grpCurrentLinearState.setText("Current Linear State"); |
| |
| this.linearConstraintStateComposite = new ConstraintStateComposite(grpCurrentLinearState, SWT.NONE); |
| new Label(this, SWT.NONE); |
| new Label(this, SWT.NONE); |
| |
| if (this.prismaticConstraint != null) { |
| this.m_bindingContext = initDataBindings(); |
| } |
| |
| // Dispose |
| addDisposeListener(new DisposeListener() { |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (PrismaticConstraintComposite.this.m_bindingContext != null) |
| PrismaticConstraintComposite.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.prismaticConstraint); |
| |
| bindingContext.bindValue(enabledObserveWidget, enabledObserveValue, null, null); |
| |
| return bindingContext; |
| } |
| |
| public org.eclipse.apogy.common.topology.addons.dynamics.PrismaticConstraint getPrismaticConstraint() { |
| return this.prismaticConstraint; |
| } |
| |
| public void setPrismaticConstraint( |
| org.eclipse.apogy.common.topology.addons.dynamics.PrismaticConstraint newPrismaticConstraint) { |
| // Unregister listener from previous PrismaticConstraint |
| if (getPrismaticConstraint() != null) { |
| getPrismaticConstraint().eAdapters().remove(getConstraintStatesAdapter()); |
| } |
| |
| setPrismaticConstraint(newPrismaticConstraint, true); |
| |
| if (newPrismaticConstraint != null) { |
| // Register listener to the new PrismaticConstraint |
| newPrismaticConstraint.eAdapters().add(getConstraintStatesAdapter()); |
| |
| this.linearConstraintStateComposite.setConstraintState(newPrismaticConstraint.getLinearCurrentState()); |
| } else { |
| this.linearConstraintStateComposite.setConstraintState(null); |
| } |
| } |
| |
| public void setPrismaticConstraint( |
| org.eclipse.apogy.common.topology.addons.dynamics.PrismaticConstraint newPrismaticConstraint, |
| boolean update) { |
| this.prismaticConstraint = newPrismaticConstraint; |
| if (update) { |
| if (this.m_bindingContext != null) { |
| this.m_bindingContext.dispose(); |
| this.m_bindingContext = null; |
| } |
| if (this.prismaticConstraint != null) { |
| this.m_bindingContext = initDataBindings(); |
| } else { |
| this.enabledButton.setSelection(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.PRISMATIC_CONSTRAINT__LINEAR_CURRENT_STATE) { |
| PrismaticConstraintComposite.this.linearConstraintStateComposite |
| .setConstraintState((ConstraintState) msg.getNewValue(), true); |
| } |
| }; |
| }; |
| } |
| return this.constraintStatesAdapter; |
| } |
| } |