| /******************************************************************************* |
| * 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.ui.composites; |
| |
| import java.text.SimpleDateFormat; |
| |
| import org.eclipse.apogy.common.databinding.converters.DateToStringConverter; |
| import org.eclipse.apogy.common.emf.ApogyCommonEMFPackage; |
| import org.eclipse.apogy.common.emf.TimeSource; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.emf.ui.composites.AbstractTimeSourceComposite; |
| import org.eclipse.apogy.core.environment.ApogyCoreEnvironmentPackage; |
| import org.eclipse.apogy.core.environment.ApogyEnvironment; |
| import org.eclipse.core.databinding.DataBindingContext; |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.beans.PojoProperties; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| 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.EMFProperties; |
| import org.eclipse.emf.databinding.FeaturePath; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.events.SelectionListener; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Combo; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Label; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class ApogyEnvironmentTimeSourceComposite extends Composite { |
| private static final Logger Logger = LoggerFactory.getLogger(ApogyEnvironmentTimeSourceComposite.class); |
| |
| protected DataBindingContext bindingContext; |
| private Adapter activeTimeSourceAdapter = null; |
| protected ApogyEnvironment apogyEnvironment; |
| |
| protected ApogyEnvironmentTimeSourcesListComposite timeSourcesListComposite; |
| protected Label currentTimeValueLabel; |
| protected Combo activeTimeSourceCombo; |
| |
| public ApogyEnvironmentTimeSourceComposite(Composite parent, int style) { |
| this(parent, style, null); |
| } |
| |
| public ApogyEnvironmentTimeSourceComposite(Composite parent, int style, ApogyEnvironment apogyEnvironment) { |
| super(parent, style); |
| setApogyEnvironment(apogyEnvironment); |
| setLayout(new FillLayout()); |
| |
| Composite top = new Composite(this, SWT.BORDER); |
| top.setLayout(new GridLayout(1, false)); |
| |
| // Adds the current time displays |
| Composite currentTimeComposite = new Composite(top, SWT.NONE); |
| GridData currentTimeCompositeGridData = new GridData(SWT.FILL, SWT.TOP, true, false); |
| currentTimeComposite.setLayoutData(currentTimeCompositeGridData); |
| currentTimeComposite.setLayout(new GridLayout(2, true)); |
| |
| Label currentTimeLabel = new Label(currentTimeComposite, SWT.NONE); |
| currentTimeLabel.setText("Current Environment Time : "); |
| |
| this.currentTimeValueLabel = new Label(currentTimeComposite, SWT.NONE); |
| this.currentTimeValueLabel.setText("?"); |
| GridData currentTimeValueLabelGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); |
| currentTimeValueLabelGridData.widthHint = 250; |
| currentTimeValueLabelGridData.minimumWidth = 250; |
| this.currentTimeValueLabel.setLayoutData(currentTimeValueLabelGridData); |
| |
| Label activeTimeSourceLabel = new Label(currentTimeComposite, SWT.NONE); |
| activeTimeSourceLabel.setText("Active Time Source :"); |
| |
| this.activeTimeSourceCombo = createActiveTimeSourceCombo(currentTimeComposite); |
| |
| GridData activeTimeSourceComboGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); |
| activeTimeSourceComboGridData.widthHint = 250; |
| activeTimeSourceComboGridData.minimumWidth = 250; |
| this.activeTimeSourceCombo.setLayoutData(activeTimeSourceComboGridData); |
| |
| // Creates and populate the TabFolder. |
| this.timeSourcesListComposite = new ApogyEnvironmentTimeSourcesListComposite(top, SWT.BORDER); |
| GridData timeSourcesListCompositeGridData = new GridData(SWT.FILL, SWT.FILL, false, false); |
| timeSourcesListCompositeGridData.minimumWidth = 800; |
| timeSourcesListCompositeGridData.widthHint = 800; |
| timeSourcesListCompositeGridData.heightHint = 600; |
| timeSourcesListCompositeGridData.minimumHeight = 600; |
| this.timeSourcesListComposite.setLayoutData(timeSourcesListCompositeGridData); |
| } |
| |
| public ApogyEnvironment getApogyEnvironment() { |
| return this.apogyEnvironment; |
| } |
| |
| public void setApogyEnvironment(ApogyEnvironment apogyEnvironment) { |
| setApogyEnvironment(apogyEnvironment, true); |
| } |
| |
| public void setApogyEnvironment(ApogyEnvironment apogyEnvironment, boolean update) { |
| if (this.apogyEnvironment != null) { |
| apogyEnvironment.eAdapters().remove(getActiveTimeSourceAdapter()); |
| } |
| |
| this.apogyEnvironment = apogyEnvironment; |
| |
| if (apogyEnvironment != null) { |
| apogyEnvironment.eAdapters().add(getActiveTimeSourceAdapter()); |
| |
| // Sets the list of time source. |
| if (this.timeSourcesListComposite != null) |
| this.timeSourcesListComposite.setTimeSourcesList(apogyEnvironment.getTimeSourcesList()); |
| } |
| |
| if (update) { |
| if (this.timeSourcesListComposite != null) { |
| // Clears Active Time Source Combo |
| populateActiveTimeSourceCombo(this.activeTimeSourceCombo); |
| |
| // Clear items. |
| this.timeSourcesListComposite.setTimeSourcesList(null); |
| |
| // Dispose of previous bindings. |
| if (this.bindingContext != null) { |
| this.bindingContext.dispose(); |
| this.bindingContext = null; |
| } |
| |
| if (apogyEnvironment != null) { |
| // Clears Active Time Source Combo |
| populateActiveTimeSourceCombo(this.activeTimeSourceCombo); |
| |
| // Sets the TimeSourceList. |
| this.timeSourcesListComposite.setTimeSourcesList(apogyEnvironment.getTimeSourcesList()); |
| |
| setActiveTimeSource(apogyEnvironment.getActiveTimeSource()); |
| this.bindingContext = initDataBindings(); |
| } |
| } |
| } |
| } |
| |
| protected Combo createActiveTimeSourceCombo(Composite parent) { |
| final Combo combo = new Combo(parent, SWT.READ_ONLY); |
| |
| // Populate the Combo. |
| populateActiveTimeSourceCombo(combo); |
| |
| combo.addSelectionListener(new SelectionListener() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| try { |
| int index = combo.getSelectionIndex(); |
| |
| if (getApogyEnvironment() != null && getApogyEnvironment().getTimeSourcesList() != null) { |
| TimeSource timeSource = getApogyEnvironment().getTimeSourcesList().getTimeSources().get(index); |
| |
| // Sets the Environment active Time source in a Transaction friendly way. |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(getApogyEnvironment(), |
| ApogyCoreEnvironmentPackage.Literals.APOGY_ENVIRONMENT__ACTIVE_TIME_SOURCE, timeSource); |
| } |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }); |
| |
| if (getApogyEnvironment() != null && getApogyEnvironment().getActiveTimeSource() != null) { |
| combo.select(getApogyEnvironment().getTimeSourcesList().getTimeSources() |
| .indexOf(getApogyEnvironment().getActiveTimeSource())); |
| } |
| |
| return combo; |
| } |
| |
| protected void populateActiveTimeSourceCombo(Combo combo) { |
| if (getApogyEnvironment() != null) { |
| int size = getApogyEnvironment().getTimeSourcesList().getTimeSources().size(); |
| String[] items = new String[size]; |
| |
| for (int i = 0; i < size; i++) { |
| TimeSource timeSource = getApogyEnvironment().getTimeSourcesList().getTimeSources().get(i); |
| String timeSourceText = getTimeSourceText(timeSource); |
| items[i] = timeSourceText; |
| } |
| |
| combo.setItems(items); |
| } |
| } |
| |
| protected String getTimeSourceText(TimeSource timeSource) { |
| String text = timeSource.eClass().getName(); |
| return text; |
| } |
| |
| protected void setActiveTimeSource(TimeSource activeTimeSource) { |
| this.timeSourcesListComposite.setSelectedTimeSourceTab(activeTimeSource); |
| |
| if (this.activeTimeSourceCombo != null) { |
| if (activeTimeSource != null) { |
| this.activeTimeSourceCombo |
| .select(getApogyEnvironment().getTimeSourcesList().getTimeSources().indexOf(activeTimeSource)); |
| } else { |
| this.activeTimeSourceCombo.select(-1); |
| } |
| |
| } |
| } |
| |
| @SuppressWarnings("unchecked") |
| protected DataBindingContext initDataBindings() { |
| DataBindingContext bindingContext = new DataBindingContext(); |
| |
| // Bind ApogyEnvironment current time to currentTimeLabel. |
| IObservableValue<?> currentTimeLabelValue = PojoProperties.value("text").observe(this.currentTimeValueLabel); |
| IObservableValue<?> currentTimeObserveValue = EMFProperties |
| .value(FeaturePath.fromList(ApogyCommonEMFPackage.Literals.TIMED__TIME)).observe(getApogyEnvironment()); |
| UpdateValueStrategy currentTimeValueStrategy = new UpdateValueStrategy(); |
| currentTimeValueStrategy.setConverter( |
| new DateToStringConverter(new SimpleDateFormat(AbstractTimeSourceComposite.DATE_FORMAT_STRING))); |
| bindingContext.bindValue(currentTimeLabelValue, currentTimeObserveValue, |
| new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), currentTimeValueStrategy); |
| |
| return bindingContext; |
| } |
| |
| protected Adapter getActiveTimeSourceAdapter() { |
| if (this.activeTimeSourceAdapter == null) { |
| this.activeTimeSourceAdapter = new AdapterImpl() { |
| @Override |
| public void notifyChanged(Notification msg) { |
| if (msg.getNotifier() instanceof ApogyEnvironment) { |
| int featureId = msg.getFeatureID(ApogyEnvironment.class); |
| |
| if (featureId == ApogyCoreEnvironmentPackage.APOGY_ENVIRONMENT__ACTIVE_TIME_SOURCE) { |
| TimeSource newActiveTimeSource = (TimeSource) msg.getNewValue(); |
| setActiveTimeSource(newActiveTimeSource); |
| } |
| } |
| } |
| }; |
| } |
| |
| return this.activeTimeSourceAdapter; |
| } |
| } |