diff options
author | Tom Schindl | 2013-06-12 10:46:15 +0000 |
---|---|---|
committer | Tom Schindl | 2013-06-12 10:46:15 +0000 |
commit | b93c1ca83cfa5613c67d0d04a89c925870f73c5d (patch) | |
tree | 2b54ed5020eeb66f53885e282bc45d4e26c68d01 /org.eclipse.fx.ide.fxml.compiler | |
parent | b093c27c3f070662859123ca6c39025be4c37986 (diff) | |
download | org.eclipse.efxclipse-b93c1ca83cfa5613c67d0d04a89c925870f73c5d.tar.gz org.eclipse.efxclipse-b93c1ca83cfa5613c67d0d04a89c925870f73c5d.tar.xz org.eclipse.efxclipse-b93c1ca83cfa5613c67d0d04a89c925870f73c5d.zip |
NEW - bug 408419: Add FXML to Java Translator
Diffstat (limited to 'org.eclipse.fx.ide.fxml.compiler')
15 files changed, 301 insertions, 0 deletions
diff --git a/org.eclipse.fx.ide.fxml.compiler/.classpath b/org.eclipse.fx.ide.fxml.compiler/.classpath new file mode 100644 index 000000000..248b4c5dc --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/.classpath @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="xtend-gen"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/org.eclipse.fx.ide.fxml.compiler/.gitignore b/org.eclipse.fx.ide.fxml.compiler/.gitignore new file mode 100644 index 000000000..5e56e040e --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/org.eclipse.fx.ide.fxml.compiler/.project b/org.eclipse.fx.ide.fxml.compiler/.project new file mode 100644 index 000000000..46f4b99fd --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/.project @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.fx.ide.fxml.compiler</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.xtext.ui.shared.xtextBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + <nature>org.eclipse.xtext.ui.shared.xtextNature</nature> + </natures> +</projectDescription> diff --git a/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.core.resources.prefs b/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..99f26c020 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/<project>=UTF-8 diff --git a/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.core.runtime.prefs b/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.core.runtime.prefs new file mode 100644 index 000000000..5a0ad22d2 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.core.runtime.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +line.separator=\n diff --git a/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..f42de363a --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/org.eclipse.fx.ide.fxml.compiler/META-INF/MANIFEST.MF b/org.eclipse.fx.ide.fxml.compiler/META-INF/MANIFEST.MF new file mode 100644 index 000000000..cff906a37 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Compiler
+Bundle-SymbolicName: org.eclipse.fx.ide.fxml.compiler
+Bundle-Version: 1.0.0.qualifier
+Bundle-RequiredExecutionEnvironment: JavaSE-1.7
+Require-Bundle: org.eclipse.xtend.lib,
+ com.google.guava,
+ org.eclipse.xtext.xbase.lib,
+ org.eclipse.fx.ide.fxgraph;bundle-version="0.8.1",
+ org.eclipse.xtext.common.types;bundle-version="2.4.2"
+
diff --git a/org.eclipse.fx.ide.fxml.compiler/build.properties b/org.eclipse.fx.ide.fxml.compiler/build.properties new file mode 100644 index 000000000..34d2e4d2d --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/org.eclipse.fx.ide.fxml.compiler/sample/Sample.fxgraph b/org.eclipse.fx.ide.fxml.compiler/sample/Sample.fxgraph new file mode 100644 index 000000000..8be7588ac --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/sample/Sample.fxgraph @@ -0,0 +1,40 @@ +package a + +import javafx.scene.layout.BorderPane +import javafx.scene.control.Button +import javafx.scene.layout.HBox +import javafx.scene.control.Label +import javafx.scene.layout.VBox +import javafx.geometry.Insets + +component Sample { + BorderPane { + center : Button { + text : "Hello World" + }, + bottom : HBox { + children : [ + Label { + text : "Label 1", + minWidth : 100, + static hgrow : "ALWAYS" + }, + Label { + text : "Label 1", + static margin : Insets { + bottom : 5 + } + }, + VBox { + children : [ + Label { + text : "Label 1", + static vgrow : "SOMETIMES" + } + ] + + } + ] + } + } +} diff --git a/org.eclipse.fx.ide.fxml.compiler/sample/Sample.fxml b/org.eclipse.fx.ide.fxml.compiler/sample/Sample.fxml new file mode 100644 index 000000000..9aa358ca8 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/sample/Sample.fxml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Do not edit this file it is generated by e(fx)clipse from ../sample/Sample.fxgraph
+-->
+
+<?import java.lang.*?>
+<?import javafx.geometry.Insets?>
+<?import javafx.scene.control.Button?>
+<?import javafx.scene.control.Label?>
+<?import javafx.scene.layout.BorderPane?>
+<?import javafx.scene.layout.HBox?>
+<?import javafx.scene.layout.VBox?>
+
+<BorderPane xmlns:fx="http://javafx.com/fxml">
+
+ <center>
+ <Button text="Hello World"/>
+ </center>
+ <bottom>
+ <HBox>
+ <children>
+ <Label text="Label 1" minWidth="100" HBox.hgrow="ALWAYS"/>
+ <Label text="Label 1">
+ <HBox.margin>
+ <Insets bottom="5"/>
+ </HBox.margin>
+ </Label>
+ <VBox>
+ <children>
+ <Label text="Label 1" VBox.vgrow="SOMETIMES"/>
+ </children>
+ </VBox>
+ </children>
+ </HBox>
+ </bottom>
+</BorderPane>
diff --git a/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/FXGraphCompiler.java b/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/FXGraphCompiler.java new file mode 100644 index 000000000..728d89485 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/FXGraphCompiler.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2013 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + *******************************************************************************/ +package org.eclipse.fx.ide.fxml.compiler; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.fx.ide.fxgraph.fXGraph.Model; + +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.Provider; + +public class FXGraphCompiler { + + @Inject + private Provider<ResourceSet> resourceSetProvider; + + public static void main(String[] args) { + Injector injector = new org.eclipse.fx.ide.fxgraph.FXGraphStandaloneSetupGenerated().createInjectorAndDoEMFRegistration(); + FXGraphCompiler main = injector.getInstance(FXGraphCompiler.class); + main.compile(args[0]); + } + + protected void compile(String string) { + // load the resource + ResourceSet set = resourceSetProvider.get(); + Resource resource = set.getResource(URI.createURI(string), true); + + Model m = (Model)resource.getContents().get(0); + FXGraphJavaGenerator c = new FXGraphJavaGenerator(); + System.out.println(c.generate(m)); + } +} diff --git a/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/FXGraphJavaGenerator.xtend b/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/FXGraphJavaGenerator.xtend new file mode 100644 index 000000000..1d0a0e3f9 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/FXGraphJavaGenerator.xtend @@ -0,0 +1,99 @@ +package org.eclipse.fx.ide.fxml.compiler
+
+import org.eclipse.fx.ide.fxgraph.fXGraph.Model
+import org.eclipse.fx.ide.fxgraph.fXGraph.Element
+import org.eclipse.fx.ide.fxgraph.fXGraph.SimpleValueProperty
import org.eclipse.fx.ide.fxgraph.fXGraph.ListValueProperty +import org.eclipse.fx.ide.fxgraph.fXGraph.StaticValueProperty + +class FXGraphJavaGenerator {
+ def generate(Model model) '''
+ package «model.package.name»;
+
+ //import org.eclipse.fx.core.fxml.FXMLLoading;
+ import java.util.ResourceBundle;
+
+ «FOR i : model.imports»
+ import «i.importedNamespace»;
+ «ENDFOR»
+
+ public class «model.componentDef.name» /*implements FXMLLoading<«model.componentDef.rootNode.type.simpleName»>*/ {
+ public «model.componentDef.rootNode.type.simpleName» load(ResourceBundle bundle) {
+ «generateElementDef("root", 0, model.componentDef.rootNode)»
+ return root;
+ }
+ }
+ '''
+
+ def CharSequence generateElementDef(String name, int varCount, Element element) '''
+ «var c = varCount»
+ «element.type.simpleName» «name» = new «element.type.simpleName»();
+ «FOR p : element.properties»
+ «IF p.value instanceof SimpleValueProperty»
+ «name».set«p.name.toFirstUpper»(«(p.value as SimpleValueProperty).simpleAttributeValue»);
+ «ELSEIF p.value instanceof Element»
+ {
+ «val i = c = c +1»
+ «val varName = 'e_'+i»
+ «generateElementDef(varName,i,p.value as Element)»
+ «name».set«p.name.toFirstUpper»(«varName»);
+ «staticProperties(varName,p.value as Element)»
+ }
+ «ELSEIF p.value instanceof ListValueProperty»
+ «FOR l : (p.value as ListValueProperty).value»
+ {
+ «val i = c = c +1»
+ «val varName = 'e_'+i»
+ «IF l instanceof Element»
+ «generateElementDef(varName,i,l as Element)»
+ «name».get«p.name.toFirstUpper»().add(«varName»);
+ «staticProperties(varName,l as Element)»
+ «ENDIF»
+ }
+ «ENDFOR»
+ «ENDIF»
+ «ENDFOR»
+ '''
+
+ def staticProperties(String varname, Element element) '''
+ «FOR prop : element.staticProperties»
+ «IF prop.value instanceof SimpleValueProperty»
+ «val type = prop.type»
+ «IF (prop.value as SimpleValueProperty).stringValue != null»
+ «val enumType = ReflectionHelper.getEnumType(type, prop.name, true)»
+ «IF enumType != null»
+ «type.simpleName».set«prop.name.toFirstUpper»(«varname»,«enumType».«(prop.value as SimpleValueProperty).stringValue»);
+ «ELSE»
+ «type.simpleName».set«prop.name.toFirstUpper»(«varname»,«(prop.value as SimpleValueProperty).simpleAttributeValue»);
+ «ENDIF»
+ «ELSE»
+ «type.simpleName».set«prop.name.toFirstUpper»(«varname»,«(prop.value as SimpleValueProperty).simpleAttributeValue»);
+ «ENDIF»
+ «ENDIF»
+ «ENDFOR»
+ '''
+
+ def simpleAttributeValue(SimpleValueProperty value) {
+ if( value.stringValue != null ) {
+ return '"' + value.stringValue + '"';
+ } else if( value.booleanValue != null ) {
+ return value.booleanValue;
+ } else if( value.number != null ) {
+ if( value.negative ) {
+ return "-" + value.number;
+ } else {
+ return value.number;
+ }
+ }
+ }
+
+ def type(StaticValueProperty prop) {
+ var el = prop.eContainer
+ while( el.eContainer != null ) {
+ if( el.eContainer instanceof Element ) {
+ val e = el.eContainer as Element
+ return e.type;
+ }
+ el = el.eContainer;
+ }
+ }
+}
\ No newline at end of file diff --git a/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/ReflectionHelper.xtend b/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/ReflectionHelper.xtend new file mode 100644 index 000000000..7552098b2 --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/src/org/eclipse/fx/ide/fxml/compiler/ReflectionHelper.xtend @@ -0,0 +1,13 @@ +package org.eclipse.fx.ide.fxml.compiler + +import org.eclipse.xtext.common.types.JvmTypeReference + +class ReflectionHelper { + def static getEnumType(JvmTypeReference type, String attributeName, boolean layoutConstraint) { + val c = Class::forName(type.qualifiedName); + val methodName = "set"+attributeName.toFirstUpper; + val m = c.methods.findFirst[name == methodName && (parameterCount == 1 || (layoutConstraint && parameterCount == 2) )] + + return m?.parameterTypes.get(if (layoutConstraint) 1 else 0)?.name; + } +}
\ No newline at end of file diff --git a/org.eclipse.fx.ide.fxml.compiler/xtend-gen/.gitignore b/org.eclipse.fx.ide.fxml.compiler/xtend-gen/.gitignore new file mode 100644 index 000000000..571ee510a --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/xtend-gen/.gitignore @@ -0,0 +1 @@ +/org diff --git a/org.eclipse.fx.ide.fxml.compiler/xtend-gen/donotdelete b/org.eclipse.fx.ide.fxml.compiler/xtend-gen/donotdelete new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/org.eclipse.fx.ide.fxml.compiler/xtend-gen/donotdelete |