blob: 02ce034bfb448b522ac47f38c055c1976f20d8e0 [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 - initial API and implementation
* Regent L'Archeveque,
* Olivier L. Larouche
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.common.emf.ui.composites;
import org.eclipse.apogy.common.emf.ApogyCommonEMFFacade;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
public class EClassSelectionComposite extends Composite {
private final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
private final EClass superType;
private EClass selectedEclass;
private final ComboViewer comboViewer;;
public EClassSelectionComposite(Composite parent, int style) {
this(parent, style, null);
}
/**
*
* @param parent The composite parent.
* @param style The composite style.
* @param superType Super type for which all sub classes are to be show. Null
* shows all available EClasses.
*/
public EClassSelectionComposite(Composite parent, int style, EClass superType) {
super(parent, style);
setLayout(new FillLayout());
this.superType = superType;
this.comboViewer = createCombo(this, SWT.READ_ONLY);
this.comboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
if (event.getSelection().isEmpty()) {
EClassSelectionComposite.this.selectedEclass = null;
newEClassSelected(EClassSelectionComposite.this.selectedEclass);
EClassSelectionComposite.this.comboViewer.getCombo().setToolTipText("");
} else if (event.getSelection() instanceof IStructuredSelection) {
IStructuredSelection iStructuredSelection = (IStructuredSelection) event.getSelection();
if (iStructuredSelection.getFirstElement() instanceof EClass) {
EClassSelectionComposite.this.selectedEclass = (EClass) iStructuredSelection.getFirstElement();
EClassSelectionComposite.this.comboViewer.getCombo().setToolTipText(
EClassSelectionComposite.this.selectedEclass.getInstanceClass().getName());
newEClassSelected(EClassSelectionComposite.this.selectedEclass);
} else {
EClassSelectionComposite.this.selectedEclass = null;
EClassSelectionComposite.this.comboViewer.getCombo().setToolTipText("");
newEClassSelected(EClassSelectionComposite.this.selectedEclass);
}
}
}
});
}
public ComboViewer getComboViewer() {
return this.comboViewer;
}
public EClass getSelectedEclass() {
return this.selectedEclass;
}
public void select(EClass eClass) {
if (this.comboViewer != null && !this.comboViewer.getCombo().isDisposed()) {
if (eClass != null) {
this.comboViewer.setSelection(new StructuredSelection(eClass), true);
} else {
this.comboViewer.setSelection(new StructuredSelection(), true);
}
}
}
private ComboViewer createCombo(Composite parent, int style) {
ComboViewer comboViewer = new ComboViewer(parent, SWT.DROP_DOWN);
comboViewer.setContentProvider(ArrayContentProvider.getInstance());
comboViewer.setLabelProvider(new AdapterFactoryLabelProvider(this.adapterFactory));
// Display the combo element sorted by displayed name.
comboViewer.setComparator(new ViewerComparator() {
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
EClass eClass1 = (EClass) e1;
EClass eClass2 = (EClass) e2;
String name1 = eClass1.getName();
String name2 = eClass2.getName();
return name1.compareTo(name2);
}
});
if (this.superType != null) {
comboViewer.setInput(ApogyCommonEMFFacade.INSTANCE.getAllSubEClasses(this.superType));
} else {
comboViewer.setInput(ApogyCommonEMFFacade.INSTANCE.getAllAvailableEClasses());
}
return comboViewer;
}
protected void newEClassSelected(EClass newEclass) {
}
}