| /** |
| * ******************************************************************************* |
| * Copyright (c) 2017 Timing-Architects Embedded Systems GmbH and others. |
| * 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: |
| * Timing-Architects Embedded Systems GmbH - initial API and implementation |
| * |
| * ******************************************************************************* |
| */ |
| package org.eclipse.app4mc.amalthea.generator.ui; |
| |
| import java.util.Arrays; |
| |
| import org.eclipse.app4mc.amalthea.generator.configuration.DistributionType; |
| import org.eclipse.swt.SWT; |
| 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.Event; |
| import org.eclipse.swt.widgets.Group; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Listener; |
| import org.eclipse.swt.widgets.Spinner; |
| |
| public abstract class DistributionGroup implements Listener { |
| |
| protected final Group group; |
| protected final Spinner minSpinner; |
| protected final Label minLabel; |
| protected final Spinner avgSpinner; |
| protected final Label avgLabel; |
| protected final Spinner maxSpinner; |
| protected final Label maxLabel; |
| protected final Combo distributionCombo; |
| |
| public DistributionGroup(Composite parent, String name) { |
| this.group = new Group(parent, SWT.NONE); |
| this.group.setText(name); |
| GridLayout gridLayout = new GridLayout(); |
| this.group.setLayout(gridLayout); |
| gridLayout.numColumns = 2; |
| GridData gridData = new GridData(GridData.FILL, GridData.CENTER, true, false); |
| gridData.horizontalSpan = 2; |
| this.group.setLayoutData(gridData); |
| |
| Label label = new Label(this.group, SWT.NONE); |
| label.setText("Distribution Type"); |
| label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); |
| this.distributionCombo = new Combo(this.group, SWT.READ_ONLY); |
| this.distributionCombo.setItems(Arrays.stream(DistributionType.values()).map(Enum::name).toArray(String[]::new)); |
| this.distributionCombo.select(0); |
| this.distributionCombo.addListener(SWT.Selection, this); |
| |
| this.minLabel = new Label(this.group, SWT.NONE); |
| this.minLabel.setText("Minimum"); |
| this.minLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); |
| this.minSpinner = new Spinner(this.group, SWT.NONE); |
| this.minSpinner.setMinimum(0); |
| this.minSpinner.setSelection(0); |
| |
| this.avgLabel = new Label(this.group, SWT.NONE); |
| this.avgLabel.setText("Average"); |
| this.avgLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); |
| this.avgSpinner = new Spinner(this.group, SWT.NONE); |
| this.avgSpinner.setMinimum(0); |
| this.avgSpinner.setSelection(0); |
| |
| this.maxLabel = new Label(this.group, SWT.NONE); |
| this.maxLabel.setText("Maximum"); |
| this.maxLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); |
| this.maxSpinner = new Spinner(this.group, SWT.NONE); |
| this.maxSpinner.setMinimum(0); |
| this.maxSpinner.setSelection(0); |
| |
| group.pack(); |
| } |
| |
| //TODO: guarantee that min <= avg <= max |
| // fill ui elements with values of new distribution |
| public void set(DistributionType type, int min, int avg, int max) { |
| if(null != type) { |
| this.distributionCombo.select(type.getValue()); |
| // enable/disable controls according distribution |
| this.distributionCombo.notifyListeners(SWT.Selection, new Event()); |
| this.minSpinner.setSelection(min); |
| this.avgSpinner.setSelection(avg); |
| this.maxSpinner.setSelection(max); |
| } |
| } |
| |
| @Override |
| public void handleEvent(Event event) { |
| if(SWT.Selection == event.type) { |
| if(this.distributionCombo == event.widget) { |
| switch(this.distributionCombo.getSelectionIndex()) { |
| case DistributionType.CONST_VALUE: |
| this.minSpinner.setVisible(false); |
| this.minLabel.setVisible(false); |
| this.avgSpinner.setVisible(true); |
| this.avgLabel.setVisible(true); |
| this.maxSpinner.setVisible(false); |
| this.maxLabel.setVisible(false); |
| break; |
| case DistributionType.UNIFORM_VALUE: |
| this.minSpinner.setVisible(true); |
| this.minLabel.setVisible(true); |
| this.avgSpinner.setVisible(false); |
| this.avgLabel.setVisible(false); |
| this.maxSpinner.setVisible(true); |
| this.maxLabel.setVisible(true); |
| break; |
| case DistributionType.WEIBULL_VALUE: |
| this.minSpinner.setVisible(true); |
| this.minLabel.setVisible(true); |
| this.avgSpinner.setVisible(true); |
| this.avgLabel.setVisible(true); |
| this.maxSpinner.setVisible(true); |
| this.maxLabel.setVisible(true); |
| break; |
| default: |
| this.minSpinner.setVisible(false); |
| this.minLabel.setVisible(false); |
| this.avgSpinner.setVisible(false); |
| this.avgLabel.setVisible(false); |
| this.maxSpinner.setVisible(false); |
| this.maxLabel.setVisible(false); |
| } |
| } |
| } |
| } |
| } |