blob: 30dbb9867ad95f506398e866db2937722b7f434c [file] [log] [blame]
/**
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* 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:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*/
package org.eclipse.osbp.xtext.perspective.ui.converter;
import com.google.common.base.Objects;
import com.google.inject.Inject;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainer;
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MStackElement;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.osbp.dsl.semantic.common.types.LImport;
import org.eclipse.osbp.dsl.semantic.common.types.OSBPTypesFactory;
import org.eclipse.osbp.xtext.chart.Chart;
import org.eclipse.osbp.xtext.chart.ChartDSLPackage;
import org.eclipse.osbp.xtext.dialogdsl.Dialog;
import org.eclipse.osbp.xtext.dialogdsl.DialogDSLPackage;
import org.eclipse.osbp.xtext.perspective.Perspective;
import org.eclipse.osbp.xtext.perspective.PerspectiveChart;
import org.eclipse.osbp.xtext.perspective.PerspectiveDialog;
import org.eclipse.osbp.xtext.perspective.PerspectiveDslFactory;
import org.eclipse.osbp.xtext.perspective.PerspectiveElement;
import org.eclipse.osbp.xtext.perspective.PerspectiveGrid;
import org.eclipse.osbp.xtext.perspective.PerspectiveModel;
import org.eclipse.osbp.xtext.perspective.PerspectivePackage;
import org.eclipse.osbp.xtext.perspective.PerspectivePart;
import org.eclipse.osbp.xtext.perspective.PerspectivePartStack;
import org.eclipse.osbp.xtext.perspective.PerspectiveReport;
import org.eclipse.osbp.xtext.perspective.PerspectiveSashContainer;
import org.eclipse.osbp.xtext.perspective.PerspectiveTable;
import org.eclipse.osbp.xtext.perspective.SashOrientation;
import org.eclipse.osbp.xtext.reportdsl.Report;
import org.eclipse.osbp.xtext.reportdsl.ReportDSLPackage;
import org.eclipse.osbp.xtext.table.Table;
import org.eclipse.osbp.xtext.table.TableDSLPackage;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.StringExtensions;
/**
* This class converts a given E4-Perspective to an OSBP perspective model or dsl text.
*/
@SuppressWarnings("all")
public class E4PerspectiveToDSLConverter {
private final static String OSBP_NS_URI = "osbp-NS-Uri";
private final static String OSBP_FQN = "osbp-model-fqn";
private final static String OSBP_FLAVOR = "osbp-NS-Uri-flavor";
@Inject
private IResourceDescriptions descriptions;
private PerspectiveDslFactory pFactory = PerspectiveDslFactory.eINSTANCE;
private Set<String> imports = CollectionLiterals.<String>newHashSet();
private Map<MUIElement, EObject> elements = CollectionLiterals.<MUIElement, EObject>newHashMap();
private int counter;
public PerspectiveModel toModel(final MPerspective mPerspective) {
this.counter = 0;
this.imports.clear();
final PerspectiveModel oModel = this.pFactory.createPerspectiveModel();
final PerspectivePackage oPkg = this.pFactory.createPerspectivePackage();
oPkg.setName("to.be.defined");
EList<PerspectivePackage> _packages = oModel.getPackages();
_packages.add(oPkg);
final Perspective perspective = this.convertPerspective(mPerspective);
EList<Perspective> _perspectives = oPkg.getPerspectives();
_perspectives.add(perspective);
final Consumer<String> _function = new Consumer<String>() {
public void accept(final String it) {
final LImport imp = OSBPTypesFactory.eINSTANCE.createLImport();
imp.setImportedNamespace((it + ".*"));
EList<LImport> _imports = oPkg.getImports();
_imports.add(imp);
}
};
this.imports.forEach(_function);
return oModel;
}
public Perspective convertPerspective(final MPerspective mPerspective) {
final Perspective oPerspective = this.pFactory.createPerspective();
this.register(mPerspective, oPerspective);
String _accessibilityPhrase = mPerspective.getAccessibilityPhrase();
oPerspective.setAccessibilityPhrase(_accessibilityPhrase);
String _iconURI = mPerspective.getIconURI();
oPerspective.setIconURI(_iconURI);
String _label = mPerspective.getLabel();
oPerspective.setName(_label);
List<MPartSashContainerElement> _children = mPerspective.getChildren();
final Consumer<MPartSashContainerElement> _function = new Consumer<MPartSashContainerElement>() {
public void accept(final MPartSashContainerElement it) {
final PerspectiveElement result = E4PerspectiveToDSLConverter.this.convert(it);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
EList<PerspectiveElement> _elements = oPerspective.getElements();
_elements.add(result);
}
}
};
_children.forEach(_function);
return oPerspective;
}
protected PerspectiveElement _convert(final MPartSashContainerElement element) {
return null;
}
protected PerspectiveElement _convert(final MStackElement element) {
return null;
}
protected PerspectiveElement _convert(final MPartSashContainer mElement) {
final PerspectiveSashContainer oElement = this.pFactory.createPerspectiveSashContainer();
this.register(mElement, oElement);
String _accessibilityPhrase = mElement.getAccessibilityPhrase();
oElement.setAccessibilityPhrase(_accessibilityPhrase);
String _containerData = mElement.getContainerData();
oElement.setContainerData(_containerData);
String _id = this.toId();
oElement.setElementId(_id);
boolean _isHorizontal = mElement.isHorizontal();
SashOrientation _orientation = this.toOrientation(_isHorizontal);
oElement.setOrientation(_orientation);
List<MPartSashContainerElement> _children = mElement.getChildren();
final Consumer<MPartSashContainerElement> _function = new Consumer<MPartSashContainerElement>() {
public void accept(final MPartSashContainerElement it) {
final PerspectiveElement result = E4PerspectiveToDSLConverter.this.convert(it);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
EList<PerspectiveElement> _elements = oElement.getElements();
_elements.add(result);
}
}
};
_children.forEach(_function);
return oElement;
}
public String toId() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("e");
int _plusPlus = this.counter++;
_builder.append(_plusPlus, "");
return _builder.toString();
}
protected PerspectiveElement _convert(final MPartStack mElement) {
final PerspectivePartStack oElement = this.pFactory.createPerspectivePartStack();
this.register(mElement, oElement);
String _accessibilityPhrase = mElement.getAccessibilityPhrase();
oElement.setAccessibilityPhrase(_accessibilityPhrase);
String _containerData = mElement.getContainerData();
oElement.setContainerData(_containerData);
String _id = this.toId();
oElement.setElementId(_id);
List<MStackElement> _children = mElement.getChildren();
final Consumer<MStackElement> _function = new Consumer<MStackElement>() {
public void accept(final MStackElement it) {
final PerspectiveElement result = E4PerspectiveToDSLConverter.this.convert(it);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
EList<PerspectiveElement> _elements = oElement.getElements();
_elements.add(result);
}
}
};
_children.forEach(_function);
return oElement;
}
protected PerspectiveElement _convert(final MPart mElement) {
Map<String, String> _persistedState = mElement.getPersistedState();
final String nsURI = _persistedState.get(E4PerspectiveToDSLConverter.OSBP_NS_URI);
final EPackage ePkg = EPackage.Registry.INSTANCE.getEPackage(nsURI);
boolean _equals = Objects.equal(ePkg, null);
if (_equals) {
return null;
}
final PerspectivePart oElement = this.pFactory.createPerspectivePart();
this.register(mElement, oElement);
String _accessibilityPhrase = mElement.getAccessibilityPhrase();
oElement.setAccessibilityPhrase(_accessibilityPhrase);
String _containerData = mElement.getContainerData();
oElement.setContainerData(_containerData);
String _label = mElement.getLabel();
boolean _equals_1 = Objects.equal(_label, null);
if (_equals_1) {
String _id = this.toId();
oElement.setElementId(_id);
} else {
String _label_1 = mElement.getLabel();
String _replaceAll = _label_1.replaceAll(" ", "");
String _replaceAll_1 = _replaceAll.replaceAll("(\\W)", "");
oElement.setElementId(_replaceAll_1);
}
String _iconURI = mElement.getIconURI();
oElement.setIconURI(_iconURI);
oElement.setDescription(false);
boolean _isCloseable = mElement.isCloseable();
oElement.setIsClosable(_isCloseable);
this.convertView(ePkg, mElement, oElement);
return oElement;
}
protected void _convertView(final EPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
}
protected void _convertView(final TableDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
Map<String, String> _persistedState = mPart.getPersistedState();
final String flavor = _persistedState.get(E4PerspectiveToDSLConverter.OSBP_FLAVOR);
boolean _and = false;
boolean _notEquals = (!Objects.equal(flavor, null));
if (!_notEquals) {
_and = false;
} else {
boolean _equals = flavor.equals("grid");
_and = _equals;
}
if (_and) {
final PerspectiveGrid oTable = this.pFactory.createPerspectiveGrid();
oPart.setView(oTable);
Map<String, String> _persistedState_1 = mPart.getPersistedState();
final String fqn = _persistedState_1.get(E4PerspectiveToDSLConverter.OSBP_FQN);
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(fqn);
if (_isNullOrEmpty) {
return;
}
int _lastIndexOf = fqn.lastIndexOf(".");
String _substring = fqn.substring(0, _lastIndexOf);
this.imports.add(_substring);
String[] _split = fqn.split("\\.");
QualifiedName _create = QualifiedName.create(_split);
final Iterable<IEObjectDescription> result = this.descriptions.getExportedObjects(TableDSLPackage.Literals.TABLE, _create, false);
Iterator<IEObjectDescription> _iterator = result.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = result.iterator();
final IEObjectDescription eObjectDesc = _iterator_1.next();
EObject _eObjectOrProxy = eObjectDesc.getEObjectOrProxy();
oTable.setRef(((Table) _eObjectOrProxy));
}
} else {
final PerspectiveTable oTable_1 = this.pFactory.createPerspectiveTable();
oPart.setView(oTable_1);
Map<String, String> _persistedState_2 = mPart.getPersistedState();
final String fqn_1 = _persistedState_2.get(E4PerspectiveToDSLConverter.OSBP_FQN);
boolean _isNullOrEmpty_1 = StringExtensions.isNullOrEmpty(fqn_1);
if (_isNullOrEmpty_1) {
return;
}
int _lastIndexOf_1 = fqn_1.lastIndexOf(".");
String _substring_1 = fqn_1.substring(0, _lastIndexOf_1);
this.imports.add(_substring_1);
String[] _split_1 = fqn_1.split("\\.");
QualifiedName _create_1 = QualifiedName.create(_split_1);
final Iterable<IEObjectDescription> result_1 = this.descriptions.getExportedObjects(TableDSLPackage.Literals.TABLE, _create_1, false);
Iterator<IEObjectDescription> _iterator_2 = result_1.iterator();
boolean _hasNext_1 = _iterator_2.hasNext();
if (_hasNext_1) {
Iterator<IEObjectDescription> _iterator_3 = result_1.iterator();
final IEObjectDescription eObjectDesc_1 = _iterator_3.next();
EObject _eObjectOrProxy_1 = eObjectDesc_1.getEObjectOrProxy();
oTable_1.setRef(((Table) _eObjectOrProxy_1));
}
}
}
protected void _convertView(final ChartDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
final PerspectiveChart oChart = this.pFactory.createPerspectiveChart();
oPart.setView(oChart);
Map<String, String> _persistedState = mPart.getPersistedState();
final String fqn = _persistedState.get(E4PerspectiveToDSLConverter.OSBP_FQN);
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(fqn);
if (_isNullOrEmpty) {
return;
}
int _lastIndexOf = fqn.lastIndexOf(".");
String _substring = fqn.substring(0, _lastIndexOf);
this.imports.add(_substring);
final String[] tokens = fqn.split("\\.");
int _length = tokens.length;
int _minus = (_length - 1);
String _get = tokens[_minus];
QualifiedName _create = QualifiedName.create(_get);
final Iterable<IEObjectDescription> result = this.descriptions.getExportedObjects(ChartDSLPackage.Literals.CHART, _create, false);
Iterator<IEObjectDescription> _iterator = result.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = result.iterator();
final IEObjectDescription eObjectDesc = _iterator_1.next();
EObject _eObjectOrProxy = eObjectDesc.getEObjectOrProxy();
oChart.setRef(((Chart) _eObjectOrProxy));
}
}
protected void _convertView(final ReportDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
final PerspectiveReport oReport = this.pFactory.createPerspectiveReport();
oPart.setView(oReport);
Map<String, String> _persistedState = mPart.getPersistedState();
final String fqn = _persistedState.get(E4PerspectiveToDSLConverter.OSBP_FQN);
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(fqn);
if (_isNullOrEmpty) {
return;
}
int _lastIndexOf = fqn.lastIndexOf(".");
String _substring = fqn.substring(0, _lastIndexOf);
this.imports.add(_substring);
String[] _split = fqn.split("\\.");
QualifiedName _create = QualifiedName.create(_split);
final Iterable<IEObjectDescription> result = this.descriptions.getExportedObjects(ReportDSLPackage.Literals.REPORT, _create, false);
Iterator<IEObjectDescription> _iterator = result.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = result.iterator();
final IEObjectDescription eObjectDesc = _iterator_1.next();
EObject _eObjectOrProxy = eObjectDesc.getEObjectOrProxy();
oReport.setRef(((Report) _eObjectOrProxy));
}
}
protected void _convertView(final DialogDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
final PerspectiveDialog oDialog = this.pFactory.createPerspectiveDialog();
oPart.setView(oDialog);
Map<String, String> _persistedState = mPart.getPersistedState();
final String fqn = _persistedState.get(E4PerspectiveToDSLConverter.OSBP_FQN);
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(fqn);
if (_isNullOrEmpty) {
return;
}
int _lastIndexOf = fqn.lastIndexOf(".");
String _substring = fqn.substring(0, _lastIndexOf);
this.imports.add(_substring);
String[] _split = fqn.split("\\.");
QualifiedName _create = QualifiedName.create(_split);
final Iterable<IEObjectDescription> result = this.descriptions.getExportedObjects(DialogDSLPackage.Literals.DIALOG, _create, false);
Iterator<IEObjectDescription> _iterator = result.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = result.iterator();
final IEObjectDescription eObjectDesc = _iterator_1.next();
EObject _eObjectOrProxy = eObjectDesc.getEObjectOrProxy();
oDialog.setRef(((Dialog) _eObjectOrProxy));
}
}
public SashOrientation toOrientation(final boolean isHorizontal) {
SashOrientation _xifexpression = null;
if (isHorizontal) {
return SashOrientation.HORIZONTAL;
} else {
_xifexpression = SashOrientation.VERTICAL;
}
return _xifexpression;
}
public void register(final MUIElement mElement, final EObject oElement) {
this.elements.put(mElement, oElement);
}
public PerspectiveElement convert(final MUIElement mElement) {
if (mElement instanceof MPartSashContainer) {
return _convert((MPartSashContainer)mElement);
} else if (mElement instanceof MPartStack) {
return _convert((MPartStack)mElement);
} else if (mElement instanceof MPart) {
return _convert((MPart)mElement);
} else if (mElement instanceof MPartSashContainerElement) {
return _convert((MPartSashContainerElement)mElement);
} else if (mElement instanceof MStackElement) {
return _convert((MStackElement)mElement);
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(mElement).toString());
}
}
public void convertView(final EPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
if (ePackage instanceof ChartDSLPackage) {
_convertView((ChartDSLPackage)ePackage, mPart, oPart);
return;
} else if (ePackage instanceof DialogDSLPackage) {
_convertView((DialogDSLPackage)ePackage, mPart, oPart);
return;
} else if (ePackage instanceof ReportDSLPackage) {
_convertView((ReportDSLPackage)ePackage, mPart, oPart);
return;
} else if (ePackage instanceof TableDSLPackage) {
_convertView((TableDSLPackage)ePackage, mPart, oPart);
return;
} else if (ePackage != null) {
_convertView(ePackage, mPart, oPart);
return;
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(ePackage, mPart, oPart).toString());
}
}
}