blob: 5030a7e55aceb9830669e39c0ead17c6aeb969a6 [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 - initial API and implementation
* Regent L'Archeveque
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.addons.vehicle.ui.wizards;
import java.text.DecimalFormat;
import org.eclipse.apogy.addons.vehicle.ApogyAddonsVehiclePackage;
import org.eclipse.apogy.addons.vehicle.Thruster;
import org.eclipse.apogy.common.emf.ui.composites.TypedElementSimpleUnitsComposite;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.jface.wizard.WizardPage;
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.Composite;
public class ThrusterWizardPage extends WizardPage {
private final static String WIZARD_PAGE_ID = "org.eclipse.apogy.addons.vehicle.ui.wizards.ThrusterWizardPage";
public static String NO_VALUE_AVAILABLE_STRING = "N/A";
private final int LABEL_WIDTH = 200;
private final int VALUE_WIDTH = 100;
private final int BUTTON_WIDTH = 50;
private Thruster thruster;
private Adapter adapter;
private TypedElementSimpleUnitsComposite minimumThrustComposite;
private TypedElementSimpleUnitsComposite maximumThrustComposite;
private TypedElementSimpleUnitsComposite plumeAngleComposite;
public ThrusterWizardPage(Thruster thruster) {
super(WIZARD_PAGE_ID);
this.thruster = thruster;
setTitle("Thruster");
setDescription("Sets the thuster parameters.");
}
@Override
public void createControl(Composite parent) {
Composite top = new Composite(parent, SWT.None);
top.setLayout(new GridLayout(1, false));
setControl(top);
this.minimumThrustComposite = new TypedElementSimpleUnitsComposite(top, SWT.NONE, true, true, true,
NO_VALUE_AVAILABLE_STRING, this.LABEL_WIDTH, this.VALUE_WIDTH, this.BUTTON_WIDTH) {
DecimalFormat format = new DecimalFormat("0.000");
@Override
protected DecimalFormat getDecimalFormat() {
return this.format;
}
@Override
protected String getLabelText() {
return "Minimum Thrust :";
}
};
this.minimumThrustComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.minimumThrustComposite.setTypedElement(
FeaturePath.fromList(ApogyAddonsVehiclePackage.Literals.THRUSTER__MINIMUM_THRUST), getThruster());
this.maximumThrustComposite = new TypedElementSimpleUnitsComposite(top, SWT.NONE, true, true, true,
NO_VALUE_AVAILABLE_STRING, this.LABEL_WIDTH, this.VALUE_WIDTH, this.BUTTON_WIDTH) {
DecimalFormat format = new DecimalFormat("0.000");
@Override
protected DecimalFormat getDecimalFormat() {
return this.format;
}
@Override
protected String getLabelText() {
return "Maximum Thrust :";
}
};
this.maximumThrustComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.maximumThrustComposite.setTypedElement(
FeaturePath.fromList(ApogyAddonsVehiclePackage.Literals.THRUSTER__MAXIMUM_THRUST), getThruster());
this.plumeAngleComposite = new TypedElementSimpleUnitsComposite(top, SWT.NONE, true, true, true,
NO_VALUE_AVAILABLE_STRING, this.LABEL_WIDTH, this.VALUE_WIDTH, this.BUTTON_WIDTH) {
DecimalFormat format = new DecimalFormat("0.0");
@Override
protected DecimalFormat getDecimalFormat() {
return this.format;
}
@Override
protected String getLabelText() {
return "Plume Angle :";
}
};
this.plumeAngleComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.plumeAngleComposite.setTypedElement(
FeaturePath.fromList(ApogyAddonsVehiclePackage.Literals.THRUSTER__PLUME_ANGLE), getThruster());
if (this.thruster != null)
this.thruster.eAdapters().add(getAdapter());
top.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent arg0) {
if (ThrusterWizardPage.this.thruster != null)
ThrusterWizardPage.this.thruster.eAdapters().remove(getAdapter());
}
});
validate();
}
public Thruster getThruster() {
return this.thruster;
}
public void setThruster(Thruster thruster) {
this.thruster = thruster;
if (this.minimumThrustComposite != null && !this.minimumThrustComposite.isDisposed()) {
this.minimumThrustComposite.setInstance(thruster);
}
if (this.maximumThrustComposite != null && !this.maximumThrustComposite.isDisposed()) {
this.maximumThrustComposite.setInstance(thruster);
}
if (this.plumeAngleComposite != null && !this.plumeAngleComposite.isDisposed()) {
this.plumeAngleComposite.setInstance(thruster);
}
}
protected void validate() {
setErrorMessage(null);
if (this.thruster.getMinimumThrust() < 0) {
setErrorMessage("The specified minimum thrust must be greater or equal to zero !");
}
if (this.thruster.getMaximumThrust() < 0) {
setErrorMessage("The specified maximum thrust must be greater or equal to zero !");
}
if (this.thruster.getMaximumThrust() < this.thruster.getMinimumThrust()) {
setErrorMessage("The specified maximum thrust must be greater or equal than the minimum thrust !");
}
if (this.thruster.getPlumeAngle() <= 0) {
setErrorMessage("The specified plume angle must be greater or equal than zero !");
}
setPageComplete(getErrorMessage() == null);
}
protected Adapter getAdapter() {
if (this.adapter == null) {
this.adapter = new AdapterImpl() {
@Override
public void notifyChanged(Notification msg) {
if (msg.getNotifier() instanceof Thruster) {
validate();
}
}
};
}
return this.adapter;
}
}