Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
8.8 kB
2
Indexable
Never
package org.xtext.example.mydsl.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import org.xtext.example.mydsl.myDsl.UI23
import org.xtext.example.mydsl.myDsl.Form
import org.xtext.example.mydsl.myDsl.Element
import org.xtext.example.mydsl.myDsl.Component
import org.xtext.example.mydsl.myDsl.Label
import org.xtext.example.mydsl.myDsl.Layout
import org.xtext.example.mydsl.myDsl.InputText
import org.xtext.example.mydsl.myDsl.Button
import org.xtext.example.mydsl.myDsl.And
import org.xtext.example.mydsl.myDsl.Or
import org.xtext.example.mydsl.myDsl.Equal
import org.xtext.example.mydsl.myDsl.NotEqual
import org.xtext.example.mydsl.myDsl.Greater
import org.xtext.example.mydsl.myDsl.Lesser
import org.xtext.example.mydsl.myDsl.GreaterEqual
import org.xtext.example.mydsl.myDsl.LesserEqual
import org.xtext.example.mydsl.myDsl.Concat
import org.xtext.example.mydsl.myDsl.Plus
import org.xtext.example.mydsl.myDsl.Minus
import org.xtext.example.mydsl.myDsl.Multi
import org.xtext.example.mydsl.myDsl.Div
import org.xtext.example.mydsl.myDsl.Length
import org.xtext.example.mydsl.myDsl.Text
import org.xtext.example.mydsl.myDsl.Numb
import org.xtext.example.mydsl.myDsl.Expression
import org.xtext.example.mydsl.myDsl.Reference

class MyDslGenerator extends AbstractGenerator {
	String packageDir = "/user_interface"

	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		val UI23 = resource.allContents.filter(UI23).next
		fsa.generateFile("/user_interface/common/Form.java", UI23.commonF)

		packageDir = "user_interface." + UI23.name.toLowerCase

		fsa.generateFile("/user_interface/" + UI23.name + "/UserInterface.java", UI23.userInterface)

		for (form : UI23.forms) {
			fsa.generateFile("/user_interface/" + UI23.name + "/" + form.name + "Form.java", form.compileF)
		}
	}

	def commonF(UI23 UI23) '''
		package user_interface.common;
		
		import javax.swing.*;
		
		public abstract class Form {
		    public abstract boolean checkValidity();
		    public abstract JPanel createPanel();
		}
	'''

	def userInterface(UI23 UI23) '''
		package user_interface.«UI23.name»;
		
		import javax.swing.*;
		
		public class UserInterface {
		    public JFrame frame;
		    
		    public void open() {
		        frame = new JFrame("«UI23.name»");
		        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		        «UI23.forms.get(0).name»Form form = new «UI23.forms.get(0).name»Form(this);
		        frame.add(form.createPanel());
		        frame.pack();
		        frame.setVisible(true);
		    }
		}
	'''

	def compileF(Form form) '''
		package «packageDir»;
		
		import javax.swing.*;
		import java.awt.event.*;
		import user_interface.common.*;
		
		public class «form.name»Form extends Form {
		    «FOR comp : form.layout.eContents.filter(Component)» 
		    	«IF comp instanceof Element»
		    		«IF (comp.eClass.name == "InputText")»
		    			public JTextField «(comp as InputText).name»Component;
		    		«ELSEIF (comp.eClass.name == "Button")»
		    			public JButton «(comp as Button).name»Component;
		    		«ENDIF» 
		    	«ENDIF»
		    «ENDFOR»
		    
		    UserInterface parent;
		    
		    «form.name»Form(UserInterface parent) {
		        this.parent = parent;
		        
		        «FOR comp : form.layout.eContents.filter(Component)» 
		        	«IF comp instanceof Element»
		        		«IF (comp.eClass.name == "InputText")»
		        			«(comp as InputText).name»Component = new JTextField();
		        		«ELSEIF (comp.eClass.name == "Button")»
		        			«(comp as Button).name»Component = new JButton("«(comp as Button).expression.compileExp»");
		        		«ENDIF» 
		        	«ENDIF»
		        «ENDFOR»
		    }
		    
		    public JPanel createPanel() {
		        return createPanel1();
		    }
		    
		    public JPanel createPanel1() {
		        JPanel panel = new JPanel();
		        panel.setLayout(new BoxLayout(panel, BoxLayout.«IF form.layout.text.equalsIgnoreCase("row")»X_AXIS«ELSE»Y_AXIS«ENDIF»));
		        
		        «FOR comp : form.layout.eContents.filter(Component)» 
		        	«IF comp instanceof Element»
		        		«IF (comp.eClass.name == "Label")»
		        			panel.add(new JLabel("«(comp as Label).expression.compileExp»"));
		        		«ELSEIF (comp.eClass.name == "InputText")»
		        			panel.add(«(comp as InputText).name»Component);
		        		«ELSEIF (comp.eClass.name == "Button")»
		        			panel.add(«(comp as Button).name»Component);
		        		«ENDIF» 
		        	«ELSEIF comp instanceof Layout»
		        		panel.add(createPanel«form.layout.component.indexOf(comp) + 2»());
		        	«ENDIF»
		        «ENDFOR»
		        
		        return panel;
		    }
		    
		    «FOR comp : form.layout.eContents.filter(Component).filter(Layout)»
		    	public JPanel createPanel«form.layout.component.indexOf(comp) + 2»() {
		    	    JPanel panel = new JPanel();
		    	    panel.setLayout(new BoxLayout(panel, BoxLayout.«IF comp.text.equalsIgnoreCase("row")»X_AXIS«ELSE»Y_AXIS«ENDIF»));
		    	    
		    	    «FOR nestedComp : comp.component»
		    	    	«IF nestedComp instanceof Element»
		    	    		«IF (nestedComp.eClass.name == "Label")» 
		    	    			panel.add(new JLabel("«(nestedComp as Label).expression.compileExp»"));
		    	    		«ELSEIF (nestedComp.eClass.name == "InputText")» 
		    	    			panel.add(«(nestedComp as InputText).name»Component);
		    	    		«ELSEIF (nestedComp.eClass.name == "Button")» 
		    	    			panel.add(«(nestedComp as Button).name»Component);
		    	    		«ENDIF»
		    	    	«ELSEIF nestedComp instanceof Layout»
		    	    		panel.add(createPanel«comp.component.indexOf(nestedComp) + 2»());
		    	    	«ENDIF»
		    	    «ENDFOR»
		    	    
		    	    return panel;
		    	}
		    «ENDFOR»
		    
		    «IF form.layout.eContents.filter(Component).filter(Layout).size + 1 >= 2»
		    	public JPanel createPanel«form.layout.eContents.filter(Component).size() + 1»() {
		    	    JPanel panel = new JPanel();
		    	    «FOR lay : form.layout.eContents.filter(Component).filter(Layout)»
		    	    	«FOR Nestedlay : lay.component.filter(Layout)»
		    	    		panel.setLayout(new BoxLayout(panel, BoxLayout.«IF Nestedlay.text.equalsIgnoreCase("row")»X_AXIS«ELSE»Y_AXIS«ENDIF»));
		    	    		«FOR NestedElement : Nestedlay.component.filter(Element)» 
		    	    			«IF (NestedElement.eClass.name == "Label")»
		    	    				panel.add(new JLabel("«(NestedElement as Label).expression.compileExp»"));
		    	    			«ELSEIF (NestedElement.eClass.name == "InputText")»
		    	    				panel.add(«(NestedElement as InputText).name»Component);
		    	    			«ELSEIF (NestedElement.eClass.name == "Button")»
		    	    				panel.add(«(NestedElement as Button).name»Component);
		    	    			«ENDIF» 
		    	    		«ENDFOR»
		    	    	«ENDFOR»
		    	    «ENDFOR»
		    	    
		    	    return panel;
		    	}
		    «ENDIF»
		    
		    public boolean checkValidity() {
		        «FOR comp : form.layout.eContents.filter(Component)» 
		        	«IF comp instanceof Element && comp.eClass.name == "InputText"»
		        		«(comp as InputText).expression.type.toFirstUpper» «(comp as InputText).name» = this.«(comp as InputText).name»Component.getText();
		        	«ENDIF»
		        «ENDFOR»
		        
		        return true;
		    }
		}
	'''

	def static String compileExp(Expression exp) {

		switch exp {
			And: 
				'''«exp.left.compileExp» && «exp.right.compileExp»'''
			Or: 
				'''«exp.left.compileExp» || «exp.right.compileExp»'''
			Equal: 
				'''«exp.left.compileExp» == «exp.right.compileExp»'''
			NotEqual: 
				'''«exp.left.compileExp» != «exp.right.compileExp»'''
			Greater: 
				'''«exp.left.compileExp» > «exp.right.compileExp»'''
			Lesser: 
				'''«exp.left.compileExp» < «exp.right.compileExp»'''
			GreaterEqual: 
				'''«exp.left.compileExp» >=«exp.right.compileExp»'''
			LesserEqual: 
				'''«exp.left.compileExp» <= «exp.right.compileExp»'''
			Concat: 
				'''«exp.left.compileExp» &«exp.right.compileExp»'''
			Plus: 
				'''«exp.left.compileExp» +«exp.right.compileExp»'''
			Minus: 
				'''«exp.left.compileExp» -«exp.right.compileExp»'''
			Multi: 
				'''«exp.left.compileExp» *«exp.right.compileExp»'''
			Div:
				'''«exp.left.compileExp» /«exp.right.compileExp»'''
			Length:
				'''.length()'''
			Text:
				exp.text.toString
			Numb:
				exp.value.toString
			Reference:
				exp.value.toString
		}
	}
}