blob: 022d1d17ad0b8a23d09aa9b795bae4709a8146ea [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.core.invocator.Context;
import org.eclipse.apogy.core.invocator.Environment;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
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.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
public class ContextsDefinitionComposite extends ScrolledComposite {
private DataBindingContext m_bindingContext;
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
private Environment environment;
private final ContextsListComposite contextsListComposite;
private final VariableImplementationsComposite variableImplementationsComposite;
public ContextsDefinitionComposite(Composite parent, int style, Context context, Environment environment) {
this(parent, style);
setEnvironment(environment);
}
/**
* Creates the parentComposite.
*
* @param parent
* @param style
*/
public ContextsDefinitionComposite(Composite parent, int style) {
super(parent, style);
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
ContextsDefinitionComposite.this.toolkit.dispose();
ContextsDefinitionComposite.this.contextsListComposite.dispose();
ContextsDefinitionComposite.this.variableImplementationsComposite.dispose();
if (ContextsDefinitionComposite.this.m_bindingContext != null) {
ContextsDefinitionComposite.this.m_bindingContext.dispose();
ContextsDefinitionComposite.this.m_bindingContext = null;
}
}
});
setLayout(new FillLayout());
setExpandHorizontal(true);
setExpandVertical(true);
Composite composite = new Composite(this, SWT.None);
composite.setLayout(new GridLayout(2, false));
Section sctnContexts = this.toolkit.createSection(composite,
ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
GridData gd_sctnContexts = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_sctnContexts.minimumWidth = 200;
gd_sctnContexts.widthHint = 211;
sctnContexts.setLayoutData(gd_sctnContexts);
this.toolkit.paintBordersFor(sctnContexts);
sctnContexts.setText("Contexts");
this.contextsListComposite = new ContextsListComposite(sctnContexts, SWT.NONE) {
@Override
protected void newSelection(ISelection selection) {
ContextsDefinitionComposite.this.variableImplementationsComposite
.setContext(ContextsDefinitionComposite.this.contextsListComposite.getSelectedContext());
ContextsDefinitionComposite.this.newSelection(selection);
}
};
this.toolkit.adapt(this.contextsListComposite);
this.toolkit.paintBordersFor(this.contextsListComposite);
sctnContexts.setClient(this.contextsListComposite);
Section sctnVariableImplementations = this.toolkit.createSection(composite,
ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
GridData gd_sctnVariableImplementations = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_sctnVariableImplementations.widthHint = 300;
gd_sctnVariableImplementations.minimumWidth = 300;
sctnVariableImplementations.setLayoutData(gd_sctnVariableImplementations);
this.toolkit.paintBordersFor(sctnVariableImplementations);
sctnVariableImplementations.setText("Implementation");
this.variableImplementationsComposite = new VariableImplementationsComposite(sctnVariableImplementations,
SWT.NONE) {
@Override
protected void newSelection(ISelection selection) {
ContextsDefinitionComposite.this.newSelection(selection);
}
};
this.toolkit.adapt(this.variableImplementationsComposite);
this.toolkit.paintBordersFor(this.variableImplementationsComposite);
sctnVariableImplementations.setClient(this.variableImplementationsComposite);
/**
* Perform a layout otherwise the VariableImplementation Content is not
* displayed without resize.
*/
composite.layout(true, true);
setContent(composite);
setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
// Dispose
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (ContextsDefinitionComposite.this.m_bindingContext != null)
ContextsDefinitionComposite.this.m_bindingContext.dispose();
}
});
}
/**
* This method is called when a new selection is made in the parentComposite.
*
* @param selection Reference to the selection.
*/
protected void newSelection(ISelection selection) {
}
/**
* Binds the {@link Environment} with the parentComposite.
*
* @param environment Reference to the environment.
*/
public void setEnvironment(Environment environment) {
setEnvironment(environment, true);
}
/**
* Sets the {@link Environment}.
*
* @param environment Reference to the environment.
* @param update If true then data bindings are created.
*/
private void setEnvironment(Environment environment, boolean update) {
this.environment = environment;
if (update) {
if (this.m_bindingContext != null) {
this.m_bindingContext.dispose();
this.m_bindingContext = null;
}
if (environment != null) {
this.m_bindingContext = initDataBindings();
}
}
}
/**
* Use this to prevent Window Pro Builder code analysis to fail with the complex
* data bindings code. Invokes
* {@link ContextsDefinitionComposite#initDataBindingsCustom()}.
*
* @return Reference to the data bindings context.
* @see ContextsDefinitionComposite#initDataBindingsCustom()
*/
private DataBindingContext initDataBindings() {
return initDataBindingsCustom();
}
/**
* Creates and returns the data bindings context that takes care of the
* Variables viewer and the Type Members viewer.
*
* @return Reference to the data bindings context.
*/
private DataBindingContext initDataBindingsCustom() {
DataBindingContext bindingContext = new DataBindingContext();
this.contextsListComposite.setEnvironment(this.environment);
this.variableImplementationsComposite.setContext(this.contextsListComposite.getSelectedContext());
return bindingContext;
}
}