| /******************************************************************************* |
| * 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 - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| *******************************************************************************/ |
| package org.eclipse.apogy.core.environment.earth.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.earth.surface.ApogyEarthSurfaceEnvironmentPackage; |
| import org.eclipse.apogy.core.environment.earth.surface.EarthSky; |
| 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 EarthSkyMoonComposite 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 EarthSky earthSky = null; |
| |
| // Moon Displays. |
| private TypedElementSimpleUnitsComposite moonAzimuthValueLabel = null; |
| private TypedElementSimpleUnitsComposite moonElevationValueLabel = null; |
| |
| private DataBindingContext m_bindingContext; |
| |
| public EarthSkyMoonComposite(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( |
| ApogyEarthSurfaceEnvironmentPackage.Literals.EARTH_SKY__MOON_HORIZONTAL_COORDINATES, |
| ApogyCoreEnvironmentPackage.Literals.HORIZONTAL_COORDINATES__AZIMUTH), getEarthSky()); |
| 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( |
| ApogyEarthSurfaceEnvironmentPackage.Literals.EARTH_SKY__MOON_HORIZONTAL_COORDINATES, |
| ApogyCoreEnvironmentPackage.Literals.HORIZONTAL_COORDINATES__ALTITUDE), getEarthSky()); |
| |
| addDisposeListener(new DisposeListener() { |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (EarthSkyMoonComposite.this.m_bindingContext != null) |
| EarthSkyMoonComposite.this.m_bindingContext.dispose(); |
| |
| } |
| }); |
| } |
| |
| public EarthSky getEarthSky() { |
| return this.earthSky; |
| } |
| |
| public void setEarthSky(EarthSky earthSky) { |
| setEarthSky(earthSky, true); |
| } |
| |
| public void setEarthSky(EarthSky newEarthSky, boolean update) { |
| // Updates EarthSky |
| this.earthSky = 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(getEarthSky()); |
| this.moonElevationValueLabel.setInstance(getEarthSky()); |
| |
| return bindingContext; |
| } |
| } |