blob: 36723aa6741cc3f19803542be47d5ea90965b7ce [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.basic.generator
import java.util.HashSet
import org.eclipse.core.runtime.CoreException
import org.eclipse.core.runtime.IStatus
import org.eclipse.core.runtime.MultiStatus
import org.eclipse.core.runtime.Status
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.xbase.jvmmodel.JvmTypeReferenceBuilder
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import java.util.TreeSet
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import javax.inject.Inject
import org.eclipse.xtext.common.types.JvmType
/**
* <b>For the complete to-do-list support keyword information DSL editors <code>org.eclipse.osbp.xtext.basic.ui.BasicDSLUiModuleHelper</code></b>
* <hr>
* <ul>Import any required classes inside <code>{your-dsl-bundle}.generator.{your}DSLGenerator.createAppendable()</code> via <code>org.eclipse.osbp.xtext.basic.generator.BasicDslGeneratorUtils.addImportFor()</code>
* <li>add the <code>BasicDslGrammarUtils</code> extension to <code>{your-dsl-bundle}.generator.{your}DSLGenerator</code>
* <pre>
* @Inject extension BasicDslGeneratorUtils
* </pre>
* </li>
* <li>replace the sum of all calls of <code>addImportFor()</code>
* <pre>
* override createAppendable(EObject context, ImportManager importManager, GeneratorConfig config) {
* importManager.addImportFor(context.newTypeRef(typeof(ClassOne)).type)
* importManager.addImportFor(context.newTypeRef(typeof(ClassTwo)).type)
* [...etc...]
* super.createAppendable(context, importManager, config)
* }
* </pre>
* with a <b>single call</b>
* <pre>
* override createAppendable(EObject context, ImportManager importManager, GeneratorConfig config) {
* addImportFor(importManager, _typeReferenceBuilder
* , ClassOne
* , ClassTwo
* [...etc...])
* super.createAppendable(context, importManager, config)
* }
* </pre>
* </ul>
*/
class BasicDslGeneratorUtils {
@Inject extension JvmTypesBuilder
@Deprecated
def boolean addImportFor(JvmModelGenerator modelGenerator, ImportManager importManager, EObject context, Object... classesToImport) {
return addImportFor(modelGenerator, importManager, null, context, classesToImport)
}
def boolean addImportFor(JvmModelGenerator modelGenerator, ImportManager importManager, JvmTypeReferenceBuilder typeReferenceBuilder, Object... classesToImport) {
return addImportFor(modelGenerator, importManager, typeReferenceBuilder, null, classesToImport)
}
def private boolean addImportFor(JvmModelGenerator modelGenerator, ImportManager importManager, JvmTypeReferenceBuilder typeReferenceBuilder, EObject context, Object... classesToImport) {
val CODE = IStatus.ERROR
val PLUGINID = modelGenerator.class.canonicalName
val MESSAGE = " could not be found! Please check 'Imported-Package' or 'Required-Bundle' in META-INF/MANIFEST.MF of the model bundle!"
var errors = new HashSet<IStatus>
var classes = new TreeSet
for (classToImport : classesToImport) {
var className = null as String
try {
val tokens = ('''«classToImport»''').toString.split(" ")
className = tokens.get(tokens.length-1)
var jvmType = null as JvmType
if (typeReferenceBuilder != null) {
switch classToImport {
Class: {
jvmType = typeReferenceBuilder.typeRef(classToImport).type
}
String: {
jvmType = typeReferenceBuilder.typeRef(classToImport).type
}
}
}
if (context != null) {
switch classToImport {
Class: {
jvmType = context.newTypeRef(classToImport).type
}
String: {
jvmType = context.newTypeRef(classToImport).type
}
}
}
importManager.addImportFor(jvmType)
}
catch (NullPointerException npe) {
classes.add(className)
errors.add(new Status(CODE, PLUGINID, '''«classToImport» «MESSAGE»''', npe))
}
catch (Exception e) {
classes.add(className)
errors.add(new Status(CODE, PLUGINID, e.localizedMessage, e))
}
}
if (!errors.empty) {
throw new CoreException(new MultiStatus(PLUGINID, CODE, errors, '''«classes.join(",\n")» «MESSAGE»''', null))
}
return true
}
}