blob: 29432c427376ab77b27d85a554ae5282d7b84995 [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,
* Sebastien Gemme - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.converters.ui.composites;
import java.util.ArrayList;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import org.eclipse.apogy.common.converters.ApogyCommonConvertersGraphsFacade;
import org.eclipse.apogy.common.converters.ConverterEdge;
import org.eclipse.apogy.common.converters.ui.composites.ConvertersUIConstants.ClassNameDisplayMode;
import org.eclipse.apogy.common.converters.ui.utils.FullyQualifiedTypesNameComparator;
import org.eclipse.apogy.common.converters.ui.utils.ShortTypesNameComparator;
import org.eclipse.swt.SWT;
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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
public class AvailableConversionComposite extends Composite {
private List inputTypesList = null;
private List outputTypesList = null;
private ClassNameDisplayMode classNameDisplayMode = ClassNameDisplayMode.SIMPLE_CLASS_NAME;
private SimpleDirectedWeightedGraph<Class<?>, ConverterEdge> convertersGraph = null;
private Map<Class<?>, java.util.List<Class<?>>> conversionsMap = null;
private java.util.List<Class<?>> inputTypesClassList = null;
public AvailableConversionComposite(Composite parent, int style) {
this(parent, style, null);
}
public AvailableConversionComposite(Composite parent, int style,
SimpleDirectedWeightedGraph<Class<?>, ConverterEdge> convertersGraph) {
super(parent, style);
this.inputTypesList = createInputTypesList();
this.outputTypesList = new List(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
this.outputTypesList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
setConvertersGraph(convertersGraph);
}
public ClassNameDisplayMode getClassNameDisplayMode() {
return this.classNameDisplayMode;
}
public void setClassNameDisplayMode(ClassNameDisplayMode classNameDisplayMode) {
this.classNameDisplayMode = classNameDisplayMode;
populateInputTypesList();
updateOutputTypesList(this.inputTypesClassList.get(this.inputTypesList.getSelectionIndex()));
}
public SimpleDirectedWeightedGraph<Class<?>, ConverterEdge> getConvertersGraph() {
return this.convertersGraph;
}
public void setConvertersGraph(SimpleDirectedWeightedGraph<Class<?>, ConverterEdge> convertersGraph) {
this.convertersGraph = convertersGraph;
this.conversionsMap = ApogyCommonConvertersGraphsFacade.INSTANCE
.getAvailableDestinationTypeMap(convertersGraph);
populateInputTypesList();
}
private List createInputTypesList() {
setLayout(new GridLayout(2, false));
Label lblInputTypes = new Label(this, SWT.NONE);
lblInputTypes.setText("Input Types");
Label lblOutputTypes = new Label(this, SWT.NONE);
lblOutputTypes.setText("Output Types");
final List inputTypesList = new List(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
inputTypesList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
inputTypesList.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
updateOutputTypesList(
AvailableConversionComposite.this.inputTypesClassList.get(inputTypesList.getSelectionIndex()));
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
return inputTypesList;
}
private void populateInputTypesList() {
this.inputTypesList.removeAll();
SortedSet<Class<?>> sortedSet = new TreeSet<Class<?>>(new ShortTypesNameComparator());
switch (this.classNameDisplayMode) {
case SIMPLE_CLASS_NAME:
sortedSet = new TreeSet<Class<?>>(new ShortTypesNameComparator());
break;
case FULLY_QUALIFIED_CLASS_NAME:
sortedSet = new TreeSet<Class<?>>(new FullyQualifiedTypesNameComparator());
break;
default:
break;
}
sortedSet.addAll(ApogyCommonConvertersGraphsFacade.INSTANCE.getAllInputTypes(this.convertersGraph));
this.inputTypesClassList = new ArrayList<Class<?>>();
this.inputTypesClassList.addAll(sortedSet);
for (Class<?> inputType : this.inputTypesClassList) {
switch (this.classNameDisplayMode) {
case SIMPLE_CLASS_NAME:
this.inputTypesList.add(inputType.getSimpleName());
break;
case FULLY_QUALIFIED_CLASS_NAME:
this.inputTypesList.add(inputType.getName());
break;
default:
break;
}
}
if (this.inputTypesClassList.size() > 0) {
this.inputTypesList.setFocus();
this.inputTypesList.setSelection(0);
this.inputTypesList.select(0);
}
}
private void updateOutputTypesList(Class<?> inputClass) {
this.outputTypesList.removeAll();
SortedSet<Class<?>> sortedSet = new TreeSet<Class<?>>(new ShortTypesNameComparator());
sortedSet.addAll(this.conversionsMap.get(inputClass));
java.util.List<Class<?>> outputTypes = new ArrayList<Class<?>>();
outputTypes.addAll(sortedSet);
for (Class<?> outputType : outputTypes) {
switch (this.classNameDisplayMode) {
case SIMPLE_CLASS_NAME:
this.outputTypesList.add(outputType.getSimpleName());
break;
case FULLY_QUALIFIED_CLASS_NAME:
this.outputTypesList.add(outputType.getName());
break;
default:
break;
}
}
}
}