blob: 969c373abecaa4bbfdd2bf46a1bb07811cc108c9 [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,
<<<<<<< HEAD
* Olivier L. Larouche
*
=======
* Olivier L. Larouche - initial API and implementation
>>>>>>> refs/heads/eclipse_pa
* 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.EModelElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
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.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
public class EObjectDocumentationComposite extends Composite {
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
private final Text txtDocumentation;
EObject object;
/**
* Create the parentComposite.
*
* @param parent
* @param style
*/
public EObjectDocumentationComposite(Composite parent, int style) {
super(parent, style);
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
EObjectDocumentationComposite.this.toolkit.dispose();
}
});
this.toolkit.adapt(this);
this.toolkit.paintBordersFor(this);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
setLayout(gridLayout);
this.txtDocumentation = new Text(this, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
this.txtDocumentation.setEditable(false);
GridData gd_txtDocumentation = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_txtDocumentation.minimumHeight = 100;
gd_txtDocumentation.heightHint = 150;
this.txtDocumentation.setLayoutData(gd_txtDocumentation);
this.toolkit.adapt(this.txtDocumentation, true, true);
}
public void setEObject(EObject element) {
this.object = element;
updateText();
}
public EObject getEObject() {
return this.object;
}
private void updateText() {
String doc = getCustomText(this.object);
if (("".equals(doc) || doc == null) && this.object != null) {
doc = ApogyCommonEMFFacade.INSTANCE.getDocumentation(this.object.eClass());
if ("".equals(doc) || doc == null) {
ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
IItemLabelProvider provider = (IItemLabelProvider) adapterFactory.adapt(this.object,
IItemLabelProvider.class);
doc = provider.getText(this.object);
}
}
if (this.object instanceof EModelElement) {
String elementDoc = EcoreUtil.getDocumentation((EModelElement) this.object);
if (elementDoc != null) {
doc = String.format("%s%n%n%s", doc, elementDoc);
}
}
this.txtDocumentation.setText(doc);
}
protected String getCustomText(EObject object) {
return "";
}
}