blob: 2e33fcf593d373e56da631f6c533cedd2898f240 [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,
* Olivier L. Larouche - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.core.invocator.ui.composites;
import org.eclipse.apogy.common.emf.ApogyCommonEMFPackage;
import org.eclipse.apogy.core.invocator.ApogyCoreInvocatorPackage;
import org.eclipse.apogy.core.invocator.Context;
import org.eclipse.apogy.core.invocator.ContextsList;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
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;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
public class SourceDestinationContextsComposite extends Composite {
private DataBindingContext m_bindingContext;
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
private final TableViewer sourceContextViewer;
private final Table sourceContextTable;
private final Table destinationContextTable;
private ContextsList contextsList;
private final TableViewer destinationContextViewer;
/**
* Creates the parentComposite.
*
* @param parent
* @param style
*/
public SourceDestinationContextsComposite(Composite parent, int style) {
super(parent, style);
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
SourceDestinationContextsComposite.this.toolkit.dispose();
if (SourceDestinationContextsComposite.this.m_bindingContext != null) {
SourceDestinationContextsComposite.this.m_bindingContext.dispose();
SourceDestinationContextsComposite.this.m_bindingContext = null;
}
}
});
this.toolkit.adapt(this);
this.toolkit.paintBordersFor(this);
setLayout(new GridLayout(2, true));
Section sctnSourceContext = this.toolkit.createSection(this,
ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
sctnSourceContext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
this.toolkit.paintBordersFor(sctnSourceContext);
sctnSourceContext.setText("Source");
this.sourceContextViewer = new TableViewer(sctnSourceContext, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
this.sourceContextViewer.setUseHashlookup(true);
this.sourceContextTable = this.sourceContextViewer.getTable();
this.sourceContextTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
this.sourceContextTable.setLinesVisible(true);
sctnSourceContext.setClient(this.sourceContextTable);
this.sourceContextViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
sourceContextSelected();
}
});
Section sctnDestinationContext = this.toolkit.createSection(this,
ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
sctnDestinationContext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
this.toolkit.paintBordersFor(sctnDestinationContext);
sctnDestinationContext.setText("Destination");
this.destinationContextViewer = new TableViewer(sctnDestinationContext, SWT.BORDER);
this.destinationContextTable = this.destinationContextViewer.getTable();
this.destinationContextTable.setLinesVisible(true);
this.toolkit.paintBordersFor(this.destinationContextTable);
sctnDestinationContext.setClient(this.destinationContextTable);
this.destinationContextViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
destinationContextSelected();
}
});
// Dispose
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (SourceDestinationContextsComposite.this.m_bindingContext != null)
SourceDestinationContextsComposite.this.m_bindingContext.dispose();
}
});
}
/**
* Sets the {@link ContextsList} managed by the parentComposite.
*
* @param contextsList Reference to the list of contexts.
*/
public void setContextsList(ContextsList contextsList) {
setContextsList(contextsList, true);
}
/**
* Sets the {@link ContextsList} managed by the parentComposite.
*
* @param contextsList Reference to the list of contexts.
* @param update If true then data bindings are created.
*/
private void setContextsList(ContextsList contextsList, boolean update) {
this.contextsList = contextsList;
if (update) {
if (this.m_bindingContext != null) {
this.m_bindingContext.dispose();
this.m_bindingContext = null;
}
if (this.contextsList != null) {
this.m_bindingContext = initDataBindings();
}
}
}
/**
* Use this to prevent Window Pro Builder code analysis to fail with the complex
* data bindings code. Invokes
* {@link SourceDestinationContextsComposite#initDataBindingsCustom()}.
*
* @return Reference to the data bindings context.
* @see SourceDestinationContextsComposite#initDataBindingsCustom()
*/
private DataBindingContext initDataBindings() {
return initDataBindingsCustom();
}
/**
* Creates and returns the data bindings.
*
* @return Reference to the data bindings context.
*/
private DataBindingContext initDataBindingsCustom() {
DataBindingContext bindingContext = new DataBindingContext();
ViewerSupport.bind(this.sourceContextViewer,
EMFObservables.observeList(this.contextsList,
ApogyCoreInvocatorPackage.Literals.CONTEXTS_LIST__CONTEXTS),
EMFProperties.value(ApogyCommonEMFPackage.Literals.NAMED__NAME));
ViewerSupport.bind(this.destinationContextViewer,
EMFObservables.observeList(this.contextsList,
ApogyCoreInvocatorPackage.Literals.CONTEXTS_LIST__CONTEXTS),
EMFProperties.value(ApogyCommonEMFPackage.Literals.NAMED__NAME));
return bindingContext;
}
/**
* This method is invoked when a new From context is selected.
*/
protected void sourceContextSelected() {
}
/**
* This method is invoked when a new To context is selected.
*/
protected void destinationContextSelected() {
}
/**
* Returns the selected From context.
*
* @return Reference to the context.
*/
public Context getSelectedSourceContext() {
Context context = null;
if (this.sourceContextViewer.getSelection() != null
&& this.sourceContextViewer.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) this.sourceContextViewer.getSelection();
context = (Context) selection.getFirstElement();
}
return context;
}
/**
* Returns the selected To context.
*
* @return Reference to the context.
*/
public Context getSelectedDestinationContext() {
Context context = null;
if (this.destinationContextViewer.getSelection() != null
&& this.destinationContextViewer.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) this.destinationContextViewer.getSelection();
context = (Context) selection.getFirstElement();
}
return context;
}
}