blob: 068afee5da9327110be13b75166bd54abe680939 [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.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.xtext.chart.ChartDSLPackage;
import org.eclipse.osbp.xtext.dialogdsl.DialogDSLPackage;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXImportDeclaration;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXtypeFactory;
import org.eclipse.osbp.xtext.perspective.Perspective;
import org.eclipse.osbp.xtext.perspective.PerspectiveDslFactory;
import org.eclipse.osbp.xtext.perspective.PerspectiveElement;
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.PerspectiveSashContainer;
import org.eclipse.osbp.xtext.perspective.SashOrientation;
import org.eclipse.osbp.xtext.reportdsl.ReportDSLPackage;
import org.eclipse.osbp.xtext.table.TableDSLPackage;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xtype.XImportDeclaration;
import org.eclipse.xtext.xtype.XImportSection;
import org.eclipse.xtext.xtype.XtypeFactory;
/**
* 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();
oModel.setImportSection(XtypeFactory.eINSTANCE.createXImportSection());
final XImportSection importSection = oModel.getImportSection();
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 = (String it) -> {
final OXImportDeclaration imp = OXtypeFactory.eINSTANCE.createOXImportDeclaration();
imp.setImportedNamespace((it + ".*"));
imp.setWildcard(true);
EList<XImportDeclaration> _importDeclarations = importSection.getImportDeclarations();
_importDeclarations.add(imp);
};
this.imports.forEach(_function);
return oModel;
}
public Perspective convertPerspective(final MPerspective mPerspective) {
final Perspective oPerspective = this.pFactory.createPerspective();
this.register(mPerspective, oPerspective);
oPerspective.setAccessibilityPhrase(mPerspective.getAccessibilityPhrase());
oPerspective.setIconURI(mPerspective.getIconURI());
oPerspective.setName(mPerspective.getLabel());
final Consumer<MPartSashContainerElement> _function = (MPartSashContainerElement it) -> {
final PerspectiveElement result = this.convert(it);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
EList<PerspectiveElement> _elements = oPerspective.getElements();
_elements.add(result);
}
};
mPerspective.getChildren().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);
oElement.setAccessibilityPhrase(mElement.getAccessibilityPhrase());
oElement.setContainerData(mElement.getContainerData());
oElement.setElementId(this.toId());
oElement.setOrientation(this.toOrientation(mElement.isHorizontal()));
final Consumer<MPartSashContainerElement> _function = (MPartSashContainerElement it) -> {
final PerspectiveElement result = this.convert(it);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
EList<PerspectiveElement> _elements = oElement.getElements();
_elements.add(result);
}
};
mElement.getChildren().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);
oElement.setAccessibilityPhrase(mElement.getAccessibilityPhrase());
oElement.setContainerData(mElement.getContainerData());
oElement.setElementId(this.toId());
final Consumer<MStackElement> _function = (MStackElement it) -> {
final PerspectiveElement result = this.convert(it);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
EList<PerspectiveElement> _elements = oElement.getElements();
_elements.add(result);
}
};
mElement.getChildren().forEach(_function);
return oElement;
}
protected PerspectiveElement _convert(final MPart mElement) {
final String nsURI = mElement.getPersistedState().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);
oElement.setAccessibilityPhrase(mElement.getAccessibilityPhrase());
oElement.setContainerData(mElement.getContainerData());
String _label = mElement.getLabel();
boolean _equals_1 = Objects.equal(_label, null);
if (_equals_1) {
oElement.setElementId(this.toId());
} else {
oElement.setElementId(mElement.getLabel().replaceAll(" ", "").replaceAll("(\\W)", ""));
}
oElement.setIconURI(mElement.getIconURI());
oElement.setIsClosable(mElement.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) {
throw new Error("Unresolved compilation problems:"
+ "\nType mismatch: cannot convert from Table to Table"
+ "\nType mismatch: cannot convert from Table to Table");
}
protected void _convertView(final ChartDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
throw new Error("Unresolved compilation problems:"
+ "\nType mismatch: cannot convert from Chart to Chart");
}
protected void _convertView(final ReportDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
throw new Error("Unresolved compilation problems:"
+ "\nType mismatch: cannot convert from Report to Report");
}
protected void _convertView(final DialogDSLPackage ePackage, final MPart mPart, final PerspectivePart oPart) {
throw new Error("Unresolved compilation problems:"
+ "\nType mismatch: cannot convert from Dialog to Dialog");
}
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());
}
}
}