| /******************************************************************************* |
| * 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, |
| * Olivier L. Larouche - initial API and implementation |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.emf.ui.composites; |
| |
| import org.eclipse.apogy.common.emf.ApogyCommonEMFPackage; |
| import org.eclipse.apogy.common.emf.CurrentTimeSource; |
| import org.eclipse.apogy.common.emf.TimeSource; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.core.databinding.DataBindingContext; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.DisposeEvent; |
| import org.eclipse.swt.events.DisposeListener; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.events.SelectionListener; |
| 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.Label; |
| import org.eclipse.swt.widgets.Spinner; |
| |
| public class CurrentTimeSourceComposite extends AbstractTimeSourceComposite { |
| protected CurrentTimeSource currentTimeSource; |
| protected DataBindingContext bindingContext; |
| |
| protected Spinner updatePeriodSpinner; |
| protected Button resumeButton; |
| protected Button pauseButton; |
| private final Composite composite; |
| |
| public CurrentTimeSourceComposite(Composite parent, int style) { |
| this(parent, style, null); |
| } |
| |
| public CurrentTimeSourceComposite(Composite parent, int style, CurrentTimeSource currentTimeSource) { |
| super(parent, style); |
| addDisposeListener(new DisposeListener() { |
| |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (CurrentTimeSourceComposite.this.bindingContext != null) { |
| CurrentTimeSourceComposite.this.bindingContext.dispose(); |
| } |
| } |
| }); |
| |
| Composite top = new Composite(this, SWT.NONE); |
| top.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); |
| top.setLayout(new GridLayout(3, true)); |
| |
| Label updatePeriodLabel = new Label(top, SWT.None); |
| updatePeriodLabel.setText("Update period (s):"); |
| this.updatePeriodSpinner = new Spinner(top, SWT.BORDER); |
| this.updatePeriodSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); |
| this.updatePeriodSpinner.setDigits(3); |
| this.updatePeriodSpinner.setMinimum(1); |
| this.updatePeriodSpinner.setMaximum(60000); |
| this.updatePeriodSpinner.setIncrement(1); |
| this.updatePeriodSpinner.setSelection(1000); |
| new Label(top, SWT.NONE); |
| |
| this.composite = new Composite(top, SWT.NONE); |
| this.composite.setLayout(new GridLayout(2, true)); |
| this.composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); |
| |
| this.resumeButton = new Button(this.composite, SWT.PUSH); |
| this.resumeButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); |
| this.resumeButton.setText("Run"); |
| |
| this.pauseButton = new Button(this.composite, SWT.PUSH); |
| this.pauseButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); |
| this.pauseButton.setText("Pause"); |
| this.pauseButton.addSelectionListener(new SelectionListener() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| getCurrentTimeSource().pause(); |
| CurrentTimeSourceComposite.this.pauseButton.setEnabled(false); |
| CurrentTimeSourceComposite.this.resumeButton.setEnabled(true); |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }); |
| this.resumeButton.addSelectionListener(new SelectionListener() { |
| |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| getCurrentTimeSource().resume(); |
| CurrentTimeSourceComposite.this.pauseButton.setEnabled(true); |
| CurrentTimeSourceComposite.this.resumeButton.setEnabled(false); |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }); |
| new Label(top, SWT.NONE); |
| |
| this.updatePeriodSpinner.addSelectionListener(new SelectionListener() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| int selection = CurrentTimeSourceComposite.this.updatePeriodSpinner.getSelection(); |
| int digits = CurrentTimeSourceComposite.this.updatePeriodSpinner.getDigits(); |
| int period = (int) Math.round((selection / Math.pow(10, digits)) * 1000); |
| |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(getCurrentTimeSource(), |
| ApogyCommonEMFPackage.Literals.CURRENT_TIME_SOURCE__UPDATE_PERIOD, period); |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }); |
| |
| setCurrentTimeSource(currentTimeSource); |
| } |
| |
| @Override |
| public TimeSource getTimeSource() { |
| return getCurrentTimeSource(); |
| } |
| |
| @Override |
| public void activate(boolean active) { |
| if (active) { |
| this.updatePeriodSpinner.setEnabled(true); |
| this.pauseButton.setEnabled(true); |
| this.resumeButton.setEnabled(true); |
| } else { |
| this.updatePeriodSpinner.setEnabled(false); |
| this.pauseButton.setEnabled(false); |
| this.resumeButton.setEnabled(false); |
| } |
| } |
| |
| public CurrentTimeSource getCurrentTimeSource() { |
| return this.currentTimeSource; |
| } |
| |
| public void setCurrentTimeSource(CurrentTimeSource currentTimeSource) { |
| setCurrentTimeSource(currentTimeSource, true); |
| } |
| |
| public void setCurrentTimeSource(CurrentTimeSource currentTimeSource, boolean update) { |
| this.currentTimeSource = currentTimeSource; |
| |
| if (update) { |
| if (this.bindingContext != null) { |
| this.bindingContext.dispose(); |
| this.bindingContext = null; |
| } |
| |
| if (currentTimeSource != null) { |
| this.bindingContext = initDataBindings(); |
| |
| if (this.updatePeriodSpinner != null) { |
| int periodDigits = this.updatePeriodSpinner.getDigits(); |
| this.updatePeriodSpinner.setSelection( |
| (int) Math.round(Math.pow(10, periodDigits) * currentTimeSource.getUpdatePeriod() * 0.001)); |
| } |
| |
| if (currentTimeSource.isPaused()) { |
| this.pauseButton.setEnabled(false); |
| this.resumeButton.setEnabled(true); |
| } else { |
| this.pauseButton.setEnabled(true); |
| this.resumeButton.setEnabled(false); |
| } |
| } else { |
| this.pauseButton.setEnabled(false); |
| this.resumeButton.setEnabled(false); |
| } |
| } |
| |
| super.setTimeSource(currentTimeSource); |
| } |
| |
| protected DataBindingContext initDataBindings() { |
| DataBindingContext bindingContext = new DataBindingContext(); |
| |
| return bindingContext; |
| } |
| } |