blob: 0cefcf4849564218ce3ead16a0d45fa18acafd22 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2020 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
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.core.environment.moon.surface.ui.composites;
import java.text.DecimalFormat;
import org.eclipse.apogy.common.emf.ui.emfforms.composites.TypedElementSimpleUnitsComposite;
import org.eclipse.apogy.core.environment.ApogyCoreEnvironmentPackage;
import org.eclipse.apogy.core.environment.moon.surface.ApogyMoonSurfaceEnvironmentPackage;
import org.eclipse.apogy.core.environment.moon.surface.MoonSky;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.emf.ecore.EObject;
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 MoonSkyEarthComposite extends Composite {
public static String NO_VALUE_AVAILABLE_STRING = "N/A";
public static final String AZIMUTH_FORMAT_STRING = "0.000";
public static final String ELEVATION_FORMAT_STRING = "0.000";
private static int LABEL_WIDTH = 100;
private static int VALUE_WIDTH = 75;
private static int BUTTON_WIDTH = 30;
// Earth Sky
private MoonSky moonSky = null;
// Moon Displays.
private TypedElementSimpleUnitsComposite moonAzimuthValueLabel = null;
private TypedElementSimpleUnitsComposite moonElevationValueLabel = null;
private DataBindingContext m_bindingContext;
public MoonSkyEarthComposite(Composite parent, int style) {
super(parent, style);
GridLayout layout = new GridLayout(1, true);
layout.marginHeight = 0;
layout.marginBottom = 0;
layout.marginTop = 0;
layout.marginLeft = 0;
layout.marginRight = 0;
setLayout(layout);
// Moon Azimuth
GridData gridDataAzimuth = new GridData();
gridDataAzimuth.grabExcessHorizontalSpace = false;
gridDataAzimuth.horizontalAlignment = SWT.LEFT;
this.moonAzimuthValueLabel = new TypedElementSimpleUnitsComposite(this, SWT.NONE, true, true, true,
NO_VALUE_AVAILABLE_STRING, LABEL_WIDTH, VALUE_WIDTH, BUTTON_WIDTH) {
DecimalFormat format = new DecimalFormat(AZIMUTH_FORMAT_STRING);
@Override
protected DecimalFormat getDecimalFormat() {
return this.format;
}
@Override
protected String getLabelText() {
return "Azimuth:";
}
@Override
protected boolean isFeatureEditable() {
return false;
}
@Override
protected Double resolveValue(EObject eObject, FeaturePath featurePath)
{
// Bring value between 0 and 2*Pi
Double value = super.resolveValue(eObject, featurePath);
if(value < 0.0) value += 2.0*Math.PI;
return value;
}
};
this.moonAzimuthValueLabel.setTypedElement(FeaturePath.fromList(
ApogyMoonSurfaceEnvironmentPackage.Literals.MOON_SKY__EARTH_HORIZONTAL_COORDINATES,
ApogyCoreEnvironmentPackage.Literals.HORIZONTAL_COORDINATES__AZIMUTH), getMoonSky());
this.moonAzimuthValueLabel.setLayoutData(gridDataAzimuth);
// Moon Elevation
GridData gridDataElevation = new GridData();
gridDataElevation.grabExcessHorizontalSpace = false;
gridDataElevation.horizontalAlignment = SWT.LEFT;
this.moonElevationValueLabel = new TypedElementSimpleUnitsComposite(this, SWT.NONE, true, true, true,
NO_VALUE_AVAILABLE_STRING, LABEL_WIDTH, VALUE_WIDTH, BUTTON_WIDTH) {
DecimalFormat format = new DecimalFormat(ELEVATION_FORMAT_STRING);
@Override
protected DecimalFormat getDecimalFormat() {
return this.format;
}
@Override
protected String getLabelText() {
return "Elevation:";
}
@Override
protected boolean isFeatureEditable() {
return false;
}
};
this.moonElevationValueLabel.setLayoutData(gridDataElevation);
this.moonElevationValueLabel.setTypedElement(FeaturePath.fromList(
ApogyMoonSurfaceEnvironmentPackage.Literals.MOON_SKY__EARTH_HORIZONTAL_COORDINATES,
ApogyCoreEnvironmentPackage.Literals.HORIZONTAL_COORDINATES__ALTITUDE), getMoonSky());
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (MoonSkyEarthComposite.this.m_bindingContext != null)
MoonSkyEarthComposite.this.m_bindingContext.dispose();
}
});
}
public MoonSky getMoonSky() {
return this.moonSky;
}
public void setMoonSky(MoonSky earthSky) {
setMoonSky(earthSky, true);
}
public void setMoonSky(MoonSky newEarthSky, boolean update) {
// Updates EarthSky
this.moonSky = newEarthSky;
if (update) {
if (this.m_bindingContext != null) {
this.m_bindingContext.dispose();
this.m_bindingContext = null;
}
if (newEarthSky != null) {
this.m_bindingContext = initDataBindings();
}
}
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
this.moonAzimuthValueLabel.setInstance(getMoonSky());
this.moonElevationValueLabel.setInstance(getMoonSky());
return bindingContext;
}
}