blob: 7e677d390c4799a6230a76df8c74284800d89da7 [file] [log] [blame]
/*******************************************************************************
* 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.ui.wizards;
import org.eclipse.apogy.common.emf.ApogyCommonEMFFactory;
import org.eclipse.apogy.common.emf.TreeRootNode;
import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade;
import org.eclipse.apogy.common.emf.ui.composites.EClassSelectionComposite;
import org.eclipse.apogy.common.emf.ui.wizards.NamedDescribedWizardPage;
import org.eclipse.apogy.common.topology.AggregateGroupNode;
import org.eclipse.apogy.common.topology.ApogyCommonTopologyFactory;
import org.eclipse.apogy.common.topology.bindings.ApogyCommonTopologyBindingsFactory;
import org.eclipse.apogy.common.topology.bindings.BindingsSet;
import org.eclipse.apogy.common.topology.bindings.FeatureRootsList;
import org.eclipse.apogy.core.ApogyCoreFacade;
import org.eclipse.apogy.core.ApogyCoreFactory;
import org.eclipse.apogy.core.ApogyCorePackage;
import org.eclipse.apogy.core.ApogySystem;
import org.eclipse.apogy.core.TopologyRoot;
import org.eclipse.apogy.core.invocator.ApogyCoreInvocatorPackage;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.emf.databinding.edit.EMFEditProperties;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.jface.databinding.viewers.ViewerProperties;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
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.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NewApogySystemWizard extends Wizard {
private static final Logger Logger = LoggerFactory.getLogger(NewApogySystemWizard.class);
private final ApogySystem apogySystem;
private String fileAbsolutePath;
public NewApogySystemWizard() {
super();
// Creates an empty ApogySystem.
this.apogySystem = ApogyCoreFactory.eINSTANCE.createApogySystem();
this.apogySystem.setName("<unamed>");
this.apogySystem.setDescription("None.");
TopologyRoot topologyRoot = ApogyCoreFactory.eINSTANCE.createTopologyRoot();
AggregateGroupNode rootNode = ApogyCommonTopologyFactory.eINSTANCE.createAggregateGroupNode();
rootNode.setNodeId("CHANGE_ME_TO_A_UNIQUE_ID");
rootNode.setDescription("Root node of the Apogy System Topology.");
topologyRoot.setOriginNode(rootNode);
this.apogySystem.setTopologyRoot(topologyRoot);
this.apogySystem.setAssemblyLinksList(ApogyCoreFactory.eINSTANCE.createAssemblyLinksList());
this.apogySystem.setConnectionPointsList(ApogyCoreFactory.eINSTANCE.createConnectionPointsList());
BindingsSet bindingsSet = ApogyCommonTopologyBindingsFactory.eINSTANCE.createBindingsSet();
bindingsSet.setBindingsList(ApogyCommonTopologyBindingsFactory.eINSTANCE.createBindingsList());
FeatureRootsList featureRootsList = ApogyCommonTopologyBindingsFactory.eINSTANCE.createFeatureRootsList();
featureRootsList.getFeatureRoots().add(ApogyCommonEMFFactory.eINSTANCE.createTreeRootNode());
bindingsSet.setFeatureRootsList(featureRootsList);
this.apogySystem.setBindingSet(bindingsSet);
this.apogySystem.setTypeApiAdapterClass(ApogyCorePackage.Literals.APOGY_SYSTEM_API_ADAPTER);
}
@Override
public void addPages() {
super.addPages();
// Named + Described
addPage(new NamedDescribedWizardPage(this.apogySystem, this.apogySystem));
addPage(new ApogySystemFilePathWizardPage());
addPage(new ApogySystemInterfaceClassPage());
}
@Override
public boolean canFinish() {
return (this.fileAbsolutePath != null) && (this.apogySystem.getInterfaceClass() != null);
}
@Override
public boolean performFinish() {
if (getFileAbsolutePath() != null) {
if (getApogySystem() != null) {
try {
ApogyCoreFacade.INSTANCE.saveApogySystemToFile(getApogySystem(), getFileAbsolutePath());
return true;
} catch (Exception e) {
Logger.error(e.getMessage(), e);
}
}
}
return false;
}
public ApogySystem getApogySystem() {
return this.apogySystem;
}
public String getFileAbsolutePath() {
return this.fileAbsolutePath;
}
private class ApogySystemFilePathWizardPage extends WizardPage {
private final static String WIZARD_PAGE_ID = "org.eclipse.apogy.core.ui.wizards.ApogySystemFilePathWizardPage";
private Label lblApogySystemFilePath;
private Button btnOpen;
public ApogySystemFilePathWizardPage() {
super(WIZARD_PAGE_ID);
setTitle("Apogy System File");
setDescription("Select the path to the ApogySystem file to create.");
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.None);
container.setLayout(new GridLayout(4, false));
Label fileLabel = new Label(container, SWT.NONE);
fileLabel.setText("Apogy System File :");
this.lblApogySystemFilePath = new Label(container, SWT.BORDER);
this.lblApogySystemFilePath.setText("");
GridData gd_lblApogySystemFilePath = new GridData(SWT.FILL, SWT.CENTER, true, false);
this.lblApogySystemFilePath.setLayoutData(gd_lblApogySystemFilePath);
this.btnOpen = new Button(container, SWT.PUSH);
this.btnOpen.setText("Open");
this.btnOpen.setToolTipText("Opens an existing Apogy System file.");
GridData gd_btnOpen = new GridData(SWT.CENTER, SWT.TOP, false, false);
this.btnOpen.setLayoutData(gd_btnOpen);
this.btnOpen.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
// Get file selection from user.
FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
fd.setText("Select an ApogySystem File");
String[] filterExt = { "*.ss" };
fd.setFilterExtensions(filterExt);
NewApogySystemWizard.this.fileAbsolutePath = fd.open();
if (NewApogySystemWizard.this.fileAbsolutePath != null) {
ApogySystemFilePathWizardPage.this.lblApogySystemFilePath
.setText(NewApogySystemWizard.this.fileAbsolutePath);
} else {
ApogySystemFilePathWizardPage.this.lblApogySystemFilePath.setText("");
}
validate();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
setControl(container);
validate();
}
/**
* This method is invoked to validate the content.
*/
protected void validate() {
String errorStr = null;
if (NewApogySystemWizard.this.fileAbsolutePath == null) {
errorStr = "A path must be provided.";
}
setErrorMessage(errorStr);
setPageComplete(errorStr == null);
}
}
private class ApogySystemInterfaceClassPage extends WizardPage {
// TODO : Add selection of APIAdapter
private final static String WIZARD_PAGE_ID = "org.eclipse.apogy.core.ui.wizards.ApogySystemInterfaceClassPage";
private EClassSelectionComposite comboApogySystemInterfaceClass;
private EClassSelectionComposite comboApogySystemApiAdapterClass;
private DataBindingContext m_bindingContext;
public ApogySystemInterfaceClassPage() {
super(WIZARD_PAGE_ID);
setTitle("Apogy System Interface Class");
setDescription("Select the Apogy System Interface Class.");
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.None);
container.setLayout(new GridLayout(2, false));
Label fileLabel = new Label(container, SWT.NONE);
fileLabel.setText("Interface Class :");
// Interface Class
this.comboApogySystemInterfaceClass = new EClassSelectionComposite(container, SWT.NONE);
this.comboApogySystemInterfaceClass
.setToolTipText("The interface class this Apogy System is being used for.");
GridData gd_comboApogySystemInterfaceClass = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
gd_comboApogySystemInterfaceClass.widthHint = 250;
gd_comboApogySystemInterfaceClass.minimumWidth = 250;
this.comboApogySystemInterfaceClass.setLayoutData(gd_comboApogySystemInterfaceClass);
this.comboApogySystemInterfaceClass.getComboViewer().getCombo()
.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
TreeRootNode treeRootNode = getApogySystem().getBindingSet().getFeatureRootsList()
.getFeatureRoots().get(0);
treeRootNode
.setSourceClass(ApogySystemInterfaceClassPage.this.comboApogySystemInterfaceClass
.getSelectedEclass());
validate();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
// Api Adapter Class.
Label apiAdapterLabel = new Label(container, SWT.NONE);
apiAdapterLabel.setText("API Adapter Class :");
this.comboApogySystemApiAdapterClass = new EClassSelectionComposite(container, SWT.NONE,
ApogyCorePackage.Literals.APOGY_SYSTEM_API_ADAPTER);
this.comboApogySystemApiAdapterClass.setToolTipText("The ApiAdapter to used for this system.");
GridData gd_comboApogySystemApiAdapterClass = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
gd_comboApogySystemApiAdapterClass.minimumWidth = 250;
gd_comboApogySystemApiAdapterClass.widthHint = 250;
this.comboApogySystemApiAdapterClass.setLayoutData(gd_comboApogySystemApiAdapterClass);
this.comboApogySystemApiAdapterClass.getComboViewer().getCombo()
.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
validate();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
// Initialize Bindings
this.m_bindingContext = initDataBindingsCustom();
setControl(container);
container.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
// Dispose of bindings.
if (ApogySystemInterfaceClassPage.this.m_bindingContext != null)
ApogySystemInterfaceClassPage.this.m_bindingContext.dispose();
}
});
validate();
}
/**
* This method is invoked to validate the content.
*/
protected void validate() {
String errorStr = null;
if (NewApogySystemWizard.this.apogySystem.getInterfaceClass() == null) {
errorStr = "An Interface Class must be provided.";
}
if (NewApogySystemWizard.this.apogySystem.getTypeApiAdapterClass() == null) {
errorStr = "An Api Adapter Class must be provided.";
}
setErrorMessage(errorStr);
setPageComplete(errorStr == null);
}
@SuppressWarnings("unchecked")
private DataBindingContext initDataBindingsCustom() {
DataBindingContext bindingContext = new DataBindingContext();
// Interface class.
IObservableValue<EClass> observeInterfaceClass = EMFEditProperties
.value(ApogyCommonTransactionFacade.INSTANCE.getDefaultEditingDomain(),
FeaturePath.fromList(ApogyCoreInvocatorPackage.Literals.TYPE__INTERFACE_CLASS))
.observe(getApogySystem());
IObservableValue<?> observeInterfaceClassComboViewer = ViewerProperties.singleSelection()
.observe(this.comboApogySystemInterfaceClass.getComboViewer());
bindingContext.bindValue(observeInterfaceClassComboViewer, observeInterfaceClass, new UpdateValueStrategy(),
new UpdateValueStrategy());
// Api Adapter Class
IObservableValue<EClass> observeApiAdapterClass = EMFEditProperties
.value(ApogyCommonTransactionFacade.INSTANCE.getDefaultEditingDomain(),
FeaturePath.fromList(ApogyCoreInvocatorPackage.Literals.TYPE__TYPE_API_ADAPTER_CLASS))
.observe(getApogySystem());
IObservableValue<?> observeApiAdapterClassComboViewer = ViewerProperties.singleSelection()
.observe(this.comboApogySystemApiAdapterClass.getComboViewer());
bindingContext.bindValue(observeApiAdapterClassComboViewer, observeApiAdapterClass,
new UpdateValueStrategy(), new UpdateValueStrategy());
return bindingContext;
}
}
}