Commit e8b3ad75 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

* split and enhanced project wizards

parent c77fecea
Loading
Loading
Loading
Loading
+122 −0
Original line number Diff line number Diff line
package org.etsi.mts.tdl.ui.wizard
import org.eclipse.xtext.ui.wizard.template.ProjectTemplate
import com.google.inject.Inject
import org.eclipse.xtext.ui.PluginImageHelper
import org.eclipse.core.runtime.Status
import static org.eclipse.core.runtime.IStatus.*
import org.eclipse.xtext.ui.wizard.template.IProjectGenerator
import org.eclipse.xtext.ui.util.PluginProjectFactory
import org.eclipse.jdt.core.JavaCore
import org.eclipse.xtext.ui.XtextProjectHelper
import org.etsi.mts.tdl.tx.ui.internal.TxActivator
import org.eclipse.swt.graphics.Image
import java.util.List
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.Path
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.ide.IDE
import org.eclipse.swt.widgets.Display

//TODO: customise
@ProjectTemplate(label="TDLtx", icon="project_template.png", description="<p><b>TDLtx</b></p>
<p>This is a parameterized project for TDLtx. You can set a parameter to modify the content in the generated file
and a parameter to set the path the file is created in.</p><p><img href=\"project_template.png\"/></p>")
final class TDLtxProject {
    //TODO: remove after testing
	val advanced = check("Advanced:", false)
	val advancedGroup = group("Properties")
//	val name = combo("Package Name:", #["Example", "Sample", "Tutorial", "Pack"], "The name of the package", advancedGroup)
    val name = text("Main Package Name:", "Main", "The name of the main package")
	val path = text("Path:", "tdl", "The package path to place the files in", advancedGroup)
	//TODO: add other options
	val importStandard = check("Import Standard Library", true, "Import TDL standard library definitions.", advancedGroup)
	val importHTTP = check("Import HTTP Library", false, "Import TDL HTTP library definitions.", advancedGroup)
	val extendedNature = check("Add additional natures", false, "Add Eclipse Plug-in and Java natures", advancedGroup)

    @Inject
    PluginImageHelper pluginImageHelper

	override protected updateVariables() {
		name.enabled = advanced.value
		path.enabled = advanced.value
		importStandard.enabled = advanced.value
		importHTTP.enabled = advanced.value
		extendedNature.enabled = advanced.value
		if (!advanced.value) {
			name.value = "Main"
			path.value = "tdl"
			importStandard.value = true
			importHTTP.value = false
			extendedNature.value = false
		}
	}

	override protected validate() {
		//TODO: validate name as well
		if (path.value.matches('[a-z][a-z0-9_.]*(/[a-z][a-z0-9_.]*)*'))
			null
		else
			new Status(ERROR, "Wizard", "'" + path + "' is not a valid package name")
			//TODO: validate data definitions as well
	}

	override generateProjects(IProjectGenerator generator) {
		generator.generate(new PluginProjectFactory => [
			projectName = projectInfo.projectName
			location = projectInfo.locationPath
			projectNatures += #[XtextProjectHelper.NATURE_ID]
			builderIds += #[XtextProjectHelper.BUILDER_ID]
			if (extendedNature.value) {
	            projectNatures += #[JavaCore.NATURE_ID, "org.eclipse.pde.PluginNature"]
	            builderIds += #[JavaCore.BUILDER_ID]
			}
			folders += "src"
			//TODO: remove hardcoded import
			var imports = ""
			if (importStandard.value) {
	            addFile('''src/«path»/Standard.tdltx''', TemplateHelper.getLibrary("Standard"))
	            imports += "Import all from Standard\n"
			}
			if (importHTTP.value) {
				addFile('''src/«path»/HTTP.tdltx''', TemplateHelper.getLibrary("HTTP"))
				imports += "Import all from HTTP\n"
				imports += "Import all from HTTP.MessageBased\n"
			}
			addFile('''src/«path»/«name».tdltx''', '''
				/*
				 * This is an example package
				 */
				Package «name» {
				    «imports»
				    //example type
				    Type float
				    //define additional elements here
				}
			''')
		])
		//TODO: make more robust and share among other templates
		open(projectInfo.projectName+"/src/"+path+"/"+name+".tdltx")		
	}
	
	private def open(String filePath) {
        val path = new Path(filePath)
		val ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path)
//        val page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		val page = PlatformUI.workbench.workbenchWindows.get(0).activePage
		Display.^default.asyncExec(new Runnable() {
			
			override run() {
				IDE.openEditor(page, ifile)
			}
			
        })
	}
	
    protected override List<Pair<String, Image>> getImages() {
        #["project_template.png".image]
    }
    private def image(String id) {
        id -> pluginImageHelper.getImage('''platform:/plugin/«TxActivator.PLUGIN_ID»/«id»''')
    }
	
}
+122 −0
Original line number Diff line number Diff line
package org.etsi.mts.tdl.ui.wizard
import org.eclipse.xtext.ui.wizard.template.ProjectTemplate
import com.google.inject.Inject
import org.eclipse.xtext.ui.PluginImageHelper
import org.eclipse.core.runtime.Status
import static org.eclipse.core.runtime.IStatus.*
import org.eclipse.xtext.ui.wizard.template.IProjectGenerator
import org.eclipse.xtext.ui.util.PluginProjectFactory
import org.eclipse.jdt.core.JavaCore
import org.eclipse.xtext.ui.XtextProjectHelper
import org.etsi.mts.tdl.tx.ui.internal.TxActivator
import org.eclipse.swt.graphics.Image
import java.util.List
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.Path
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.ide.IDE
import org.eclipse.swt.widgets.Display

//TODO: customise
@ProjectTemplate(label="TDLtx Base", icon="project_template.png", description="<p><b>TDLtx</b></p>
<p>This is a parameterized project for TDLtx. You can set a parameter to modify the content in the generated file
and a parameter to set the path the file is created in.</p><p><img href=\"project_template.png\"/></p>")
final class TDLtxProjectBase { 
    //TODO: remove after testing
	val advanced = check("Advanced:", false)
	val advancedGroup = group("Properties")
//	val name = combo("Package Name:", #["Example", "Sample", "Tutorial", "Pack"], "The name of the package", advancedGroup)
    val name = text("Main Package Name:", "Main", "The name of the main package")
	val path = text("Path:", "tdl", "The package path to place the files in", advancedGroup)
	//TODO: add other options
	val importStandard = check("Import Standard Library", true, "Import TDL standard library definitions.", advancedGroup)
	val importHTTP = check("Import HTTP Library", false, "Import TDL HTTP library definitions.", advancedGroup)
	val extendedNature = check("Add additional natures", false, "Add Eclipse Plug-in and Java natures", advancedGroup)

    @Inject
    PluginImageHelper pluginImageHelper

	override protected updateVariables() {
		name.enabled = advanced.value
		path.enabled = advanced.value
		importStandard.enabled = advanced.value
		importHTTP.enabled = advanced.value
		extendedNature.enabled = advanced.value
		if (!advanced.value) {
			name.value = "Main"
			path.value = "tdl"
			importStandard.value = true
			importHTTP.value = false
			extendedNature.value = false
		}
	}

	override protected validate() {
		//TODO: validate name as well
		if (path.value.matches('[a-z][a-z0-9_.]*(/[a-z][a-z0-9_.]*)*'))
			null
		else
			new Status(ERROR, "Wizard", "'" + path + "' is not a valid package name")
			//TODO: validate data definitions as well
	}

	override generateProjects(IProjectGenerator generator) {
		generator.generate(new PluginProjectFactory => [
			projectName = projectInfo.projectName
			location = projectInfo.locationPath
			projectNatures += #[XtextProjectHelper.NATURE_ID]
			builderIds += #[XtextProjectHelper.BUILDER_ID]
			if (extendedNature.value) {
	            projectNatures += #[JavaCore.NATURE_ID, "org.eclipse.pde.PluginNature"]
	            builderIds += #[JavaCore.BUILDER_ID]
			}
			folders += "src"
			//TODO: remove hardcoded import
			var imports = ""
			if (importStandard.value) {
	            addFile('''src/«path»/Standard.tdltx''', TemplateHelper.getLibrary("Standard"))
	            imports += "Import all from Standard\n"
			}
			if (importHTTP.value) {
				addFile('''src/«path»/HTTP.tdltx''', TemplateHelper.getLibrary("HTTP"))
				imports += "Import all from HTTP\n"
				imports += "Import all from HTTP.MessageBased\n"
			}
			addFile('''src/«path»/«name».tdltx''', '''
				/*
				 * This is an example package
				 */
				Package «name» {
				    «imports»
				    //example type
				    Type float
				    //define additional elements here
				}
			''')
		])
		//TODO: make more robust and share among other templates
		open(projectInfo.projectName+"/src/"+path+"/"+name+".tdltx")		
	}
	
	private def open(String filePath) {
        val path = new Path(filePath)
		val ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path)
//        val page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		val page = PlatformUI.workbench.workbenchWindows.get(0).activePage
		Display.^default.asyncExec(new Runnable() {
			
			override run() {
				IDE.openEditor(page, ifile)
			}
			
        })
	}
	
    protected override List<Pair<String, Image>> getImages() {
        #["project_template.png".image]
    }
    private def image(String id) {
        id -> pluginImageHelper.getImage('''platform:/plugin/«TxActivator.PLUGIN_ID»/«id»''')
    }
	
}
+1 −283
Original line number Diff line number Diff line
@@ -3,32 +3,8 @@
 */
package org.etsi.mts.tdl.ui.wizard


import org.eclipse.core.runtime.Status
import org.eclipse.jdt.core.JavaCore
import org.eclipse.xtext.ui.XtextProjectHelper
import org.eclipse.xtext.ui.util.PluginProjectFactory
import org.eclipse.xtext.ui.wizard.template.IProjectGenerator
import org.eclipse.xtext.ui.wizard.template.IProjectTemplateProvider
import org.eclipse.xtext.ui.wizard.template.ProjectTemplate

import static org.eclipse.core.runtime.IStatus.*
import org.eclipse.xtext.ui.PluginImageHelper
import com.google.inject.Inject
import java.util.List
import org.eclipse.swt.graphics.Image
import org.etsi.mts.tdl.tx.ui.internal.TxActivator
import org.etsi.mts.tdl.openapi2tdl.next.ConverterNext
import org.eclipse.ui.preferences.ScopedPreferenceStore
import org.eclipse.core.runtime.preferences.InstanceScope
import org.etsi.mts.tdl.ui.wizard.TemplateHelper.Libraries
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.resources.IFile
import org.eclipse.core.runtime.Path
import org.eclipse.core.runtime.IPath
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.ide.IDE
import org.eclipse.swt.widgets.Display
import org.eclipse.xtext.ui.wizard.template.IProjectTemplateProvider

/**
 * Create a list with all project templates to be shown in the template new project wizard.
@@ -56,261 +32,3 @@ class TDLtxProjectTemplateProvider implements IProjectTemplateProvider {
		]
	}
}

//TODO: customise
@ProjectTemplate(label="TDLtx Base", icon="project_template.png", description="<p><b>TDLtx</b></p>
<p>This is a parameterized project for TDLtx. You can set a parameter to modify the content in the generated file
and a parameter to set the path the file is created in.</p><p><img href=\"project_template.png\"/></p>")
final class TDLtxProjectBase {
    //TODO: remove after testing
	val advanced = check("Advanced:", false)
	val advancedGroup = group("Properties")
	val name = combo("Package Name:", #["Example", "Sample", "Tutorial", "Pack"], "The name of the package", advancedGroup)
	val path = text("Path:", "tdl", "The package path to place the files in", advancedGroup)

    @Inject
    PluginImageHelper pluginImageHelper

	override protected updateVariables() {
		name.enabled = advanced.value
		path.enabled = advanced.value
		if (!advanced.value) {
			name.value = "Example"
			path.value = "tdl"
		}
	}

	override protected validate() {
		if (path.value.matches('[a-z][a-z0-9_.]*(/[a-z][a-z0-9_.]*)*'))
			null
		else
			new Status(ERROR, "Wizard", "'" + path + "' is not a valid package name")
			//TODO: validate data definitions as well
	}

	override generateProjects(IProjectGenerator generator) {
		generator.generate(new PluginProjectFactory => [
			projectName = projectInfo.projectName
			location = projectInfo.locationPath
			//projectNatures += #[JavaCore.NATURE_ID, "org.eclipse.pde.PluginNature", XtextProjectHelper.NATURE_ID]
			//builderIds += #[JavaCore.BUILDER_ID, XtextProjectHelper.BUILDER_ID]
			projectNatures += #[XtextProjectHelper.NATURE_ID]
			builderIds += #[XtextProjectHelper.BUILDER_ID]
			folders += "src"
			//TODO: remove hardcoded import
            addFile('''src/«path»/Wizardry.tdltx''', TemplateContent.Example)
			addFile('''src/«path»/Model.tdltx''', '''
				/*
				 * This is an example package
				 */
				Package «name» {
				    Import all from Wizardry
				    Type float
				}
			''')
		])
		
	}
	
    protected override List<Pair<String, Image>> getImages() {
        #["project_template.png".image]
    }
    private def image(String id) {
        id -> pluginImageHelper.getImage('''platform:/plugin/«TxActivator.PLUGIN_ID»/«id»''')
    }
	
}

//TODO: customise
@ProjectTemplate(label="TDLtx", icon="project_template.png", description="<p><b>TDLtx</b></p>
<p>This is a parameterized project for TDLtx. You can set a parameter to modify the content in the generated file
and a parameter to set the path the file is created in.</p><p><img href=\"project_template.png\"/></p>")
final class TDLtxProject {
    //TODO: remove after testing
	val advanced = check("Advanced:", false)
	val advancedGroup = group("Properties")
//	val name = combo("Package Name:", #["Example", "Sample", "Tutorial", "Pack"], "The name of the package", advancedGroup)
    val name = text("Main Package Name:", "Main", "The name of the main package")
	val path = text("Path:", "tdl", "The package path to place the files in", advancedGroup)
	//TODO: add other options
	val importStandard = check("Import Standard Library", true, "Import TDL standard library definitions.", advancedGroup)
	val importHTTP = check("Import HTTP Library", false, "Import TDL HTTP library definitions.", advancedGroup)

    @Inject
    PluginImageHelper pluginImageHelper

	override protected updateVariables() {
		name.enabled = advanced.value
		path.enabled = advanced.value
		importStandard.enabled = advanced.value
		importHTTP.enabled = advanced.value
		if (!advanced.value) {
			name.value = "Main"
			path.value = "tdl"
			importStandard.value = true
			importHTTP.value = false
		}
	}

	override protected validate() {
		//TODO: validate name as well
		if (path.value.matches('[a-z][a-z0-9_.]*(/[a-z][a-z0-9_.]*)*'))
			null
		else
			new Status(ERROR, "Wizard", "'" + path + "' is not a valid package name")
			//TODO: validate data definitions as well
	}

	override generateProjects(IProjectGenerator generator) {
		generator.generate(new PluginProjectFactory => [
			projectName = projectInfo.projectName
			location = projectInfo.locationPath
			//projectNatures += #[JavaCore.NATURE_ID, "org.eclipse.pde.PluginNature", XtextProjectHelper.NATURE_ID]
			//builderIds += #[JavaCore.BUILDER_ID, XtextProjectHelper.BUILDER_ID]
			projectNatures += #[XtextProjectHelper.NATURE_ID]
			builderIds += #[XtextProjectHelper.BUILDER_ID]
			folders += "src"
			//TODO: remove hardcoded import
			var imports = ""
			if (importStandard.value) {
	            addFile('''src/«path»/Standard.tdltx''', TemplateHelper.getLibrary("Standard"))
	            imports += "Import all from Standard\n"
			}
			if (importHTTP.value) {
				addFile('''src/«path»/HTTP.tdltx''', TemplateHelper.getLibrary("HTTP"))
				imports += "Import all from HTTP\n"
				imports += "Import all from HTTP.MessageBased\n"
			}
			addFile('''src/«path»/«name».tdltx''', '''
				/*
				 * This is an example package
				 */
				Package «name» {
				    «imports»
				    //example type
				    Type float
				    //define additional elements here
				}
			''')
		])
		//TODO: make more robust and share among other templates
		open(projectInfo.projectName+"/src/"+path+"/"+name+".tdltx")		
	}
	
	private def open(String filePath) {
        val path = new Path(filePath)
		val ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path)
//        val page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		val page = PlatformUI.workbench.workbenchWindows.get(0).activePage
		Display.^default.asyncExec(new Runnable() {
			
			override run() {
				IDE.openEditor(page, ifile)
			}
			
        })
	}
	
    protected override List<Pair<String, Image>> getImages() {
        #["project_template.png".image]
    }
    private def image(String id) {
        id -> pluginImageHelper.getImage('''platform:/plugin/«TxActivator.PLUGIN_ID»/«id»''')
    }
	
}


//TODO: customise further, reuse?
@ProjectTemplate(label="TDLtx with OpenAPI", icon="project_template.png", description="<p><b>TDLtx with OpenAPI</b></p>
<p>This is a parameterized project for TDLtx with the option to import data definitions from OpenAPI. 
You can set a parameter to modify the content in the generated file
and a parameter to set the path the file is created in.</p><p><img href=\"project_template.png\"/></p>")
final class TDLtxProjectWithOpenAPI {
    //TODO: more modern way?
    //TODO: differentiate for project vs file wizard
	val s = new ScopedPreferenceStore(new InstanceScope(), "org.etsi.mts.tdl.tx.ui.template.data.openapi")
    val lastUsed = "LAST_USED"
    //TODO: file prompt?
    //TODO: remove after testing
    val advanced = check("Advanced:", true)
    val advancedGroup = group("Properties")
    val name = text("Main Package Name:", "Main", "The name of the main package")
//    val name = combo("Package Name:", #["Example", "Sample", "Tutorial", "Pack"], "The name of the package", advancedGroup)
    val path = text("Path:", "tdl", "The package path to place the files in", advancedGroup)
    val dataDefinitionsPath = text("Data DefinitionsPath:", s.getString(lastUsed), "The data definitions path to import from", advancedGroup)
    val sourceMapping = text("Source mapping tag:", "SOURCE_MAPPING", "The source mapping tag to be used for the data mappings", advancedGroup)
    val targetMapping = text("Target mapping tag:", "TARGET_MAPPING", "The target mapping tag to be used for the data mappings", advancedGroup)

    @Inject
    PluginImageHelper pluginImageHelper

    override protected updateVariables() {
        name.enabled = advanced.value
        path.enabled = advanced.value
        dataDefinitionsPath.enabled = advanced.value
        sourceMapping.enabled = advanced.value
        targetMapping.enabled = advanced.value
        
        if (!advanced.value) {
            name.value = "Main"
            path.value = "tdl"
            dataDefinitionsPath.value = s.getString(lastUsed)
	        sourceMapping.value = "SOURCE_MAPPING"
	        targetMapping.value = "TARGET_MAPPING"
        }
    }

    override protected validate() {
        if (path.value.matches('[a-z][a-z0-9_.]*(/[a-z][a-z0-9_.]*)*'))
            null
        else
            new Status(ERROR, "Wizard", "'" + path + "' is not a valid package name")
            //TODO: validate data definitions as well
    }

    override generateProjects(IProjectGenerator generator) {
        generator.generate(new PluginProjectFactory => [
            projectName = projectInfo.projectName
            location = projectInfo.locationPath
            //projectNatures += #[JavaCore.NATURE_ID, "org.eclipse.pde.PluginNature", XtextProjectHelper.NATURE_ID]
            //builderIds += #[JavaCore.BUILDER_ID, XtextProjectHelper.BUILDER_ID]
            projectNatures += #[XtextProjectHelper.NATURE_ID]
            builderIds += #[XtextProjectHelper.BUILDER_ID]
            folders += "src"
            addRequiredBundles(List.of("org.etsi.mts.tdl.tx.ui"))
            
//            addFile('''src/«path»/Standard.tdltx''', TemplateContent.Standard)
            //TODO: move libraries to a separate bundle
            addFile('''src/«path»/Standard.tdltx''', TemplateHelper.getLibrary("Standard"))
            //TODO: remove hardcoded import
            //TODO: make target package name configurable? i.e. based on name
            addFile('''src/«path»/imported.tdltx''', 
            	ConverterNext.processToString(dataDefinitionsPath.value, 
					"src/"+path.value+"/imported.tdltx", 
					sourceMapping.value, 
					targetMapping.value
			))
            addFile('''src/«path»/Model.tdltx''', '''
                /*
                 * This is an example package
                 */
                Package «name» {
                    Import all from Standard
                    Import all from generated_from_ExampleAPI_yaml
                    Type simple
                }
            ''')
             
        ])
        
    }
    
    protected override List<Pair<String, Image>> getImages() {
        #["project_template.png".image]
    }
    private def image(String id) {
        id -> pluginImageHelper.getImage('''platform:/plugin/«TxActivator.PLUGIN_ID»/«id»''')
    }
    
}
+155 −0

File added.

Preview size limit exceeded, changes collapsed.