Jelajahi Sumber

Create custom Java-Generator as plugin project (YAKHMI-465)

benjamin.schwertfeger 14 tahun lalu
induk
melakukan
0116080d70

+ 200 - 0
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/GeneratorComposite.java

@@ -0,0 +1,200 @@
+package org.yakindu.sct.generator.genmodel.ui.wizard;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.yakindu.sct.generator.genmodel.ui.wizard.XpandGeneratorWizardPage1.KeyListenerAdapter;
+import org.yakindu.sct.generator.genmodel.ui.wizard.XpandGeneratorWizardPage1.SelectionListenerAdapter;
+
+public class GeneratorComposite extends Composite {
+
+	// (ID::)+ID
+	private static final String GENERATOR_ID_REGEX = "([a-zA-Z_][a-zA-Z0-9_]*::)+[a-zA-Z_][a-zA-Z0-9_]*"; //$NON-NLS-1$
+	private static final String GENERATOR_CLASS_REGEX = "([a-zA-Z_][a-zA-Z0-9_]*\\.)+[a-zA-Z_][a-zA-Z0-9_]*"; //$NON-NLS-1$
+
+	public static interface ValidateCallback {
+		void validate();
+
+		public static final ValidateCallback NULL = new ValidateCallback() {
+			public void validate() {
+			}
+		};
+	}
+
+	private Button cbGenerator;
+	private Group generatorGroup;
+	private Text txtGeneratorId;
+	private Text txtGeneratorName;
+	private Text txtGeneratorClass;
+	private Text txtGeneratorDesc;
+	private Button cbLibrary;
+
+	private final ValidateCallback callback;
+
+	private final KeyListenerAdapter textBoxListener = new KeyListenerAdapter() {
+		@Override
+		public void keyReleased(KeyEvent e) {
+			callback.validate();
+		}
+	};
+	private Label lblGeneratorClass;
+
+	public GeneratorComposite(Composite parent, int style,
+			ValidateCallback callback) {
+		super(parent, style);
+		if (callback != null)
+			this.callback = callback;
+		else
+			this.callback = ValidateCallback.NULL;
+
+		Composite generatorCheckboxGroup = this;
+		generatorCheckboxGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
+				true, false, 1, 1));
+		generatorCheckboxGroup.setLayout(new GridLayout(2, false));
+
+		Label lblGeneratorCheckBox = new Label(generatorCheckboxGroup, SWT.WRAP);
+		lblGeneratorCheckBox.setText("Configure for Plugin Export");
+		cbGenerator = new Button(generatorCheckboxGroup, SWT.CHECK);
+		cbGenerator.addSelectionListener(new SelectionListenerAdapter() {
+
+			public void widgetSelected(SelectionEvent e) {
+				setGroupEnabled(cbGenerator.getSelection());
+				GeneratorComposite.this.callback.validate();
+			}
+		});
+
+		generatorGroup = new Group(parent, SWT.NONE);
+		generatorGroup.setLayout(new GridLayout(2, false));
+		generatorGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
+				false, 1, 1));
+
+		Label lblGeneratorId = new Label(generatorGroup, SWT.WRAP);
+		lblGeneratorId.setText("Generator Id:");
+
+		txtGeneratorId = new Text(generatorGroup, SWT.SINGLE | SWT.BORDER);
+		txtGeneratorId.setText("custom::sctGenerator");
+		txtGeneratorId.addKeyListener(textBoxListener);
+		GridDataFactory.fillDefaults().grab(true, false)
+				.applyTo(txtGeneratorId);
+
+		Label lblGeneratorName = new Label(generatorGroup, SWT.WRAP);
+		lblGeneratorName.setText("Generator name:");
+
+		setTxtGeneratorName(new Text(generatorGroup, SWT.SINGLE | SWT.BORDER));
+		getTxtGeneratorName().addKeyListener(textBoxListener);
+		GridDataFactory.fillDefaults().grab(true, false)
+				.applyTo(getTxtGeneratorName());
+
+		lblGeneratorClass = new Label(generatorGroup, SWT.WRAP);
+		lblGeneratorClass.setText("Generator class:");
+
+		setTxtGeneratorClass(new Text(generatorGroup, SWT.SINGLE | SWT.BORDER));
+		getTxtGeneratorClass().addKeyListener(textBoxListener);
+		GridDataFactory.fillDefaults().grab(true, false)
+				.applyTo(getTxtGeneratorClass());
+
+		Label lblGeneratorDesc = new Label(generatorGroup, SWT.WRAP);
+		lblGeneratorDesc.setText("Generator description:");
+
+		setTxtGeneratorDesc(new Text(generatorGroup, SWT.SINGLE | SWT.BORDER));
+		getTxtGeneratorDesc().addKeyListener(textBoxListener);
+		GridDataFactory.fillDefaults().grab(true, false)
+				.applyTo(getTxtGeneratorDesc());
+
+		Label lblLibraryCheckBox = new Label(generatorGroup, SWT.WRAP);
+		lblLibraryCheckBox.setText("Create Feature Library");
+
+		cbLibrary = new Button(generatorGroup, SWT.CHECK);
+
+	}
+
+	public void disableGeneratorClass() {
+		lblGeneratorClass.setVisible(false);
+		txtGeneratorClass.setVisible(false);
+	}
+
+	public void setGroupEnabled(boolean enabled) {
+		generatorGroup.setEnabled(enabled);
+		txtGeneratorId.setEnabled(enabled);
+		getTxtGeneratorName().setEnabled(enabled);
+		getTxtGeneratorDesc().setEnabled(enabled);
+		getTxtGeneratorClass().setEnabled(enabled);
+		cbLibrary.setEnabled(enabled);
+	}
+
+	public boolean isGenerateLibrary() {
+		return cbLibrary.getSelection();
+	}
+
+	public boolean isGeneratePlugin() {
+		return cbGenerator.getSelection();
+	}
+
+	public Text getTxtGeneratorId() {
+		return txtGeneratorId;
+	}
+
+	public void setTxtGeneratorId(Text txtGeneratorId) {
+		this.txtGeneratorId = txtGeneratorId;
+	}
+
+	public Text getTxtGeneratorName() {
+		return txtGeneratorName;
+	}
+
+	public void setTxtGeneratorName(Text txtGeneratorName) {
+		this.txtGeneratorName = txtGeneratorName;
+	}
+
+	public Text getTxtGeneratorClass() {
+		return txtGeneratorClass;
+	}
+
+	public void setTxtGeneratorClass(Text txtGeneratorClass) {
+		this.txtGeneratorClass = txtGeneratorClass;
+	}
+
+	public Text getTxtGeneratorDesc() {
+		return txtGeneratorDesc;
+	}
+
+	public void setTxtGeneratorDesc(Text txtGeneratorDesc) {
+		this.txtGeneratorDesc = txtGeneratorDesc;
+	}
+
+	/**
+	 * 
+	 * @return the error message or null, if no error occur.
+	 */
+	public String validate() {
+		if (isGeneratePlugin()) {
+			if (getTxtGeneratorId().getText().isEmpty()) {
+				return "Generator Id must be set";
+			}
+			if (!getTxtGeneratorId().getText().matches(GENERATOR_ID_REGEX)) {
+				return "Invalid Generator Id";
+			}
+			if (getTxtGeneratorName().getText().isEmpty()) {
+				return "Generator name must be set";
+			}
+			if (txtGeneratorClass.isVisible()) {
+				if (getTxtGeneratorClass().getText().isEmpty()) {
+					return "Generator class must be set";
+				}
+				if (!getTxtGeneratorClass().getText().matches(
+							GENERATOR_CLASS_REGEX)) {
+					return "Generator class must be a full qualified java identifier";
+				}
+			}
+		}
+		return null;
+	}
+}

+ 7 - 3
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/GeneratorProjectTemplate.xtend

@@ -74,8 +74,10 @@ class GeneratorProjectTemplate {
 		project.getFile('META-INF/MANIFEST.MF').write(data.manifest)
 		if (data.pluginExport) {
 			project.getFile('plugin.xml').write(data.plugin)
-			project.getFile('src/'+data.generatorClass.javaFilename)
-				.write(data.xpandGenerator)
+			if (data.generatorType == GeneratorType::Xpand) {
+				project.getFile('src/'+data.generatorClass.javaFilename)
+					.write(data.xpandGenerator)
+			}
 			if (data.typeLibrary) {
 				project.createFolder('library')
 				project.getFile('library/FeatureTypeLibrary.xmi')
@@ -533,7 +535,9 @@ class GeneratorProjectTemplate {
 	'''
 	
 	def buildProperties(ProjectData data) '''
-		source.. = src
+		source.. = src«IF data.generatorType == GeneratorType::Xtend»,\
+			xtend-gen
+		«ENDIF»
 		«IF data.pluginExport»
 			bin.includes = META-INF/,.,plugin.xml
 		«ELSE»

+ 26 - 0
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/WorkspaceGeneratorWizardPage1.java

@@ -23,6 +23,7 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
+import org.yakindu.sct.generator.genmodel.ui.wizard.GeneratorComposite.ValidateCallback;
 /**
  * 
  * @author holger willebrandt - Initial contribution and API
@@ -41,6 +42,7 @@ public class WorkspaceGeneratorWizardPage1 extends IProjectWizardPage {
 			validate();
 		}
 	};
+	private GeneratorComposite generatorComposite;
 
 	protected WorkspaceGeneratorWizardPage1(String pageName) {
 		super(pageName);
@@ -81,6 +83,15 @@ public class WorkspaceGeneratorWizardPage1 extends IProjectWizardPage {
 
 		cbXtend = new Button(generatorCheckboxGroup, SWT.CHECK);
 
+		ValidateCallback callback = new ValidateCallback() {
+			public void validate() {
+				WorkspaceGeneratorWizardPage1.this.validate();
+			}
+		};
+		generatorComposite = new GeneratorComposite(parent, SWT.NONE, callback);
+		generatorComposite.disableGeneratorClass();
+
+		generatorComposite.setGroupEnabled(false);
 
 		setPageComplete(false);
 	}
@@ -123,6 +134,12 @@ public class WorkspaceGeneratorWizardPage1 extends IProjectWizardPage {
 			setErrorMessage("Generator class must be a full qualified java identifier");
 			return false;
 		}
+		String message = generatorComposite.validate();
+		if (message != null) {
+			setErrorMessage(message);
+			return false;
+		}
+
 		return true;
 	}
 
@@ -133,6 +150,15 @@ public class WorkspaceGeneratorWizardPage1 extends IProjectWizardPage {
 		data.generatorClass = txtGeneratorClass.getText().trim();
 		data.generatorType = cbXtend.getSelection() ? GeneratorType.Xtend
 				: GeneratorType.Java;
+		data.generatorDescription = generatorComposite.getTxtGeneratorDesc()
+				.getText().trim();
+		data.generatorId = generatorComposite.getTxtGeneratorId().getText()
+				.trim();
+		data.generatorName = generatorComposite.getTxtGeneratorName().getText()
+				.trim();
+		data.pluginExport = generatorComposite.isGeneratePlugin();
+		data.typeLibrary = generatorComposite.isGenerateLibrary();
+		
 		return data;
 	}
 

+ 23 - 107
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/XpandGeneratorWizardPage1.java

@@ -1,4 +1,3 @@
-
 /**
  * Copyright (c) 2011 committers of YAKINDU and others.
  * All rights reserved. This program and the accompanying materials
@@ -20,29 +19,19 @@ import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
+import org.yakindu.sct.generator.genmodel.ui.wizard.GeneratorComposite.ValidateCallback;
+
 /**
  * 
  * @author holger willebrandt - Initial contribution and API
  */
 public class XpandGeneratorWizardPage1 extends IProjectWizardPage {
-	// (ID::)+ID
-	private static final String GENERATOR_ID_REGEX = "([a-zA-Z_][a-zA-Z0-9_]*::)+[a-zA-Z_][a-zA-Z0-9_]*"; //$NON-NLS-1$
-	private static final String GENERATOR_CLASS_REGEX = "([a-zA-Z_][a-zA-Z0-9_]*\\.)+[a-zA-Z_][a-zA-Z0-9_]*"; //$NON-NLS-1$
 	// project will be an OSGI bundle, so there are some name restrictions
 	private static final String PROJECT_REGEX = "[A-Za-z0-9\\._]+"; //$NON-NLS-1$
-	private Group generatorGroup;
-	private Text txtGeneratorDesc;
-	private Text txtGeneratorId;
-	private Button cbGenerator;
 	private Text txtProjectName;
-	private Button cbLibrary;
-	private Text txtGeneratorName;
-	private Text txtGeneratorClass;
 	private final KeyListenerAdapter textBoxListener = new KeyListenerAdapter() {
 
 		@Override
@@ -50,6 +39,7 @@ public class XpandGeneratorWizardPage1 extends IProjectWizardPage {
 			validate();
 		}
 	};
+	private GeneratorComposite generatorComposite;
 
 	protected XpandGeneratorWizardPage1(String pageName) {
 		super(pageName);
@@ -72,66 +62,14 @@ public class XpandGeneratorWizardPage1 extends IProjectWizardPage {
 		GridDataFactory.fillDefaults().grab(true, false)
 				.applyTo(txtProjectName);
 
-		Composite generatorCheckboxGroup = new Composite(parent, SWT.NULL);
-		generatorCheckboxGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
-				true, false, 1, 1));
-		generatorCheckboxGroup.setLayout(new GridLayout(2, false));
-
-		Label lblGeneratorCheckBox = new Label(generatorCheckboxGroup, SWT.WRAP);
-		lblGeneratorCheckBox.setText("Configure for Plugin Export");
-		cbGenerator = new Button(generatorCheckboxGroup, SWT.CHECK);
-		cbGenerator.addSelectionListener(new SelectionListenerAdapter() {
-
-			public void widgetSelected(SelectionEvent e) {
-				setGeneratorGroupEnabled(cbGenerator.getSelection());
-				validate();
+		ValidateCallback callback = new ValidateCallback() {
+			public void validate() {
+				XpandGeneratorWizardPage1.this.validate();
 			}
-		});
-
-		generatorGroup = new Group(parent, SWT.NONE);
-		generatorGroup.setLayout(new GridLayout(2, false));
-		generatorGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
-				false, 1, 1));
-
-		Label lblGeneratorId = new Label(generatorGroup, SWT.WRAP);
-		lblGeneratorId.setText("Generator Id:");
-
-		txtGeneratorId = new Text(generatorGroup, SWT.SINGLE | SWT.BORDER);
-		txtGeneratorId.setText("custom::xpand");
-		txtGeneratorId.addKeyListener(textBoxListener);
-		GridDataFactory.fillDefaults().grab(true, false)
-				.applyTo(txtGeneratorId);
-
-		Label lblGeneratorName = new Label(generatorGroup, SWT.WRAP);
-		lblGeneratorName.setText("Generator name:");
-
-		txtGeneratorName = new Text(generatorGroup, SWT.SINGLE | SWT.BORDER);
-		txtGeneratorName.addKeyListener(textBoxListener);
-		GridDataFactory.fillDefaults().grab(true, false)
-				.applyTo(txtGeneratorName);
-
-		Label lblGeneratorClass = new Label(generatorGroup, SWT.WRAP);
-		lblGeneratorClass.setText("Generator class:");
-
-		txtGeneratorClass = new Text(generatorGroup, SWT.SINGLE | SWT.BORDER);
-		txtGeneratorClass.addKeyListener(textBoxListener);
-		GridDataFactory.fillDefaults().grab(true, false)
-				.applyTo(txtGeneratorClass);
-
-		Label lblGeneratorDesc = new Label(generatorGroup, SWT.WRAP);
-		lblGeneratorDesc.setText("Generator description:");
-
-		txtGeneratorDesc = new Text(generatorGroup, SWT.SINGLE | SWT.BORDER);
-		txtGeneratorDesc.addKeyListener(textBoxListener);
-		GridDataFactory.fillDefaults().grab(true, false)
-				.applyTo(txtGeneratorDesc);
+		};
+		generatorComposite = new GeneratorComposite(parent, SWT.NONE, callback);
 
-		Label lblLibraryCheckBox = new Label(generatorGroup, SWT.WRAP);
-		lblLibraryCheckBox.setText("Create Feature Library");
-
-		cbLibrary = new Button(generatorGroup, SWT.CHECK);
-
-		setGeneratorGroupEnabled(false);
+		generatorComposite.setGroupEnabled(false);
 		setPageComplete(false);
 	}
 
@@ -165,51 +103,29 @@ public class XpandGeneratorWizardPage1 extends IProjectWizardPage {
 	}
 
 	private boolean checkGeneratorOptions() {
-		if (generatorGroup.isEnabled()) {
-			if (txtGeneratorId.getText().isEmpty()) {
-				setErrorMessage("Generator Id must be set");
-				return false;
-			}
-			if (!txtGeneratorId.getText().matches(GENERATOR_ID_REGEX)) {
-				setErrorMessage("Invalid Generator Id");
-				return false;
-			}
-			if (txtGeneratorName.getText().isEmpty()) {
-				setErrorMessage("Generator name must be set");
-				return false;
-			}
-			if (txtGeneratorClass.getText().isEmpty()) {
-				setErrorMessage("Generator class must be set");
-				return false;
-			}
-			if (!txtGeneratorClass.getText().matches(GENERATOR_CLASS_REGEX)) {
-				setErrorMessage("Generator class must be a full qualified java identifier");
-				return false;
-			}
+		String message = generatorComposite.validate();
+		if (message != null) {
+			setErrorMessage(message);
+			return false;
 		}
 		return true;
 	}
 
-	protected void setGeneratorGroupEnabled(boolean enabled) {
-		generatorGroup.setEnabled(enabled);
-		txtGeneratorId.setEnabled(enabled);
-		txtGeneratorName.setEnabled(enabled);
-		txtGeneratorDesc.setEnabled(enabled);
-		txtGeneratorClass.setEnabled(enabled);
-		cbLibrary.setEnabled(enabled);
-	}
-
 	@Override
 	public ProjectData getProjectData() {
 		ProjectData data = new ProjectData();
 		data.generatorType = GeneratorType.Xpand;
 		data.projectName = txtProjectName.getText().trim();
-		data.generatorClass = txtGeneratorClass.getText().trim();
-		data.generatorDescription = txtGeneratorDesc.getText().trim();
-		data.generatorId = txtGeneratorId.getText().trim();
-		data.generatorName = txtGeneratorName.getText().trim();
-		data.pluginExport = cbGenerator.getSelection();
-		data.typeLibrary = cbLibrary.getSelection();
+		data.generatorClass = generatorComposite.getTxtGeneratorClass()
+				.getText().trim();
+		data.generatorDescription = generatorComposite.getTxtGeneratorDesc()
+				.getText().trim();
+		data.generatorId = generatorComposite.getTxtGeneratorId().getText()
+				.trim();
+		data.generatorName = generatorComposite.getTxtGeneratorName().getText()
+				.trim();
+		data.pluginExport = generatorComposite.isGeneratePlugin();
+		data.typeLibrary = generatorComposite.isGenerateLibrary();
 		return data;
 	}
 

+ 23 - 9
plugins/org.yakindu.sct.generator.genmodel.ui/xtend-gen/org/yakindu/sct/generator/genmodel/ui/wizard/GeneratorProjectTemplate.java

@@ -95,12 +95,16 @@ public class GeneratorProjectTemplate {
           IFile _file_5 = project.getFile("plugin.xml");
           StringConcatenation _plugin = this.plugin(data);
           this.write(_file_5, _plugin);
-          String _generatorClass = data.getGeneratorClass();
-          String _javaFilename = this.javaFilename(_generatorClass);
-          String _operator_plus = StringExtensions.operator_plus("src/", _javaFilename);
-          IFile _file_6 = project.getFile(_operator_plus);
-          StringConcatenation _xpandGenerator = this.xpandGenerator(data);
-          this.write(_file_6, _xpandGenerator);
+          GeneratorType _generatorType_2 = data.getGeneratorType();
+          boolean _operator_equals_1 = ObjectExtensions.operator_equals(_generatorType_2, GeneratorType.Xpand);
+          if (_operator_equals_1) {
+            String _generatorClass = data.getGeneratorClass();
+            String _javaFilename = this.javaFilename(_generatorClass);
+            String _operator_plus = StringExtensions.operator_plus("src/", _javaFilename);
+            IFile _file_6 = project.getFile(_operator_plus);
+            StringConcatenation _xpandGenerator = this.xpandGenerator(data);
+            this.write(_file_6, _xpandGenerator);
+          }
           boolean _isTypeLibrary = data.isTypeLibrary();
           if (_isTypeLibrary) {
             {
@@ -130,8 +134,8 @@ public class GeneratorProjectTemplate {
       IFile _file_11 = project.getFile(".project");
       StringConcatenation _projectFile = this.projectFile(data);
       this.write(_file_11, _projectFile);
-      GeneratorType _generatorType_2 = data.getGeneratorType();
-      final GeneratorType __valOfSwitchOver = _generatorType_2;
+      GeneratorType _generatorType_3 = data.getGeneratorType();
+      final GeneratorType __valOfSwitchOver = _generatorType_3;
       boolean matched = false;
       if (!matched) {
         if (org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_equals(__valOfSwitchOver,GeneratorType.Xpand)) {
@@ -1284,7 +1288,17 @@ public class GeneratorProjectTemplate {
   public StringConcatenation buildProperties(final ProjectData data) {
     StringConcatenation _builder = new StringConcatenation();
     _builder.append("source.. = src");
-    _builder.newLine();
+    {
+      GeneratorType _generatorType = data.getGeneratorType();
+      boolean _operator_equals = ObjectExtensions.operator_equals(_generatorType, GeneratorType.Xtend);
+      if (_operator_equals) {
+        _builder.append(",\\");
+        _builder.newLineIfNotEmpty();
+        _builder.append("\t");
+        _builder.append("xtend-gen");
+        _builder.newLine();
+      }
+    }
     {
       boolean _isPluginExport = data.isPluginExport();
       if (_isPluginExport) {