blob: fd353baa8a15599ada31017af1048f86786997cc [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.AbstractInitialConditions;
import org.eclipse.apogy.core.invocator.InitialConditions;
import org.eclipse.apogy.core.invocator.TypeMemberInitialConditions;
import org.eclipse.apogy.core.invocator.VariableInitialConditions;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
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;
public class InitialConditionsTreeComposite extends Composite {
private InitialConditions initialConditions;
private final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
private final TreeViewer treeViewer;
private DataBindingContext m_currentDataBindings;
public InitialConditionsTreeComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(2, false));
this.treeViewer = new TreeViewer(this);
this.treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
if (selection instanceof VariableInitialConditions) {
newVariableInitialConditionsSelected((VariableInitialConditions) selection);
} else if (selection instanceof TypeMemberInitialConditions) {
newTypeMemberInitialConditionsSelected((TypeMemberInitialConditions) selection);
}
}
});
this.treeViewer.setLabelProvider(new AdapterFactoryLabelProvider(this.adapterFactory));
this.treeViewer.setContentProvider(getContentProvider());
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
gridData.widthHint = 200;
gridData.minimumWidth = 200;
this.treeViewer.getTree().setLayoutData(gridData);
// Dispose
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (InitialConditionsTreeComposite.this.m_currentDataBindings != null)
InitialConditionsTreeComposite.this.m_currentDataBindings.dispose();
}
});
}
public InitialConditions getInitialConditions() {
return this.initialConditions;
}
public void setInitialConditions(InitialConditions initialConditions) {
if (this.m_currentDataBindings != null) {
this.m_currentDataBindings.dispose();
}
this.initialConditions = initialConditions;
if (initialConditions != null) {
this.m_currentDataBindings = initDataBindingsCustom();
this.treeViewer.setInput(initialConditions);
} else {
this.treeViewer.setInput(null);
}
refreshViewer();
}
protected void newVariableInitialConditionsSelected(VariableInitialConditions variableInitialConditions) {
}
protected void newTypeMemberInitialConditionsSelected(TypeMemberInitialConditions typeMemberInitialConditions) {
}
protected void refreshViewer() {
if (!this.treeViewer.isBusy()) {
getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
InitialConditionsTreeComposite.this.treeViewer.refresh();
}
});
}
}
protected DataBindingContext initDataBindingsCustom() {
DataBindingContext bindingContext = new DataBindingContext();
return bindingContext;
}
protected AdapterFactoryContentProvider getContentProvider() {
return new AdapterFactoryContentProvider(this.adapterFactory) {
@Override
public Object[] getElements(Object object) {
if (object instanceof InitialConditions) {
return ((InitialConditions) object).getVariableInitialConditions().toArray();
}
return null;
}
@Override
public Object[] getChildren(Object object) {
if (object instanceof InitialConditions) {
return ((InitialConditions) object).getVariableInitialConditions().toArray();
} else if (object instanceof AbstractInitialConditions) {
return ((AbstractInitialConditions) object).getTypeMembersInitialConditions().toArray();
} else {
return null;
}
}
@Override
public boolean hasChildren(Object object) {
if (object instanceof InitialConditions) {
return !((InitialConditions) object).getVariableInitialConditions().isEmpty();
} else if (object instanceof AbstractInitialConditions) {
return !((AbstractInitialConditions) object).getTypeMembersInitialConditions().isEmpty();
} else {
return false;
}
}
};
}
}