Skip to content

Commit

Permalink
Additional fixes for issue groovyfx-project#49 (plus some formatting)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed Aug 1, 2019
1 parent 81f10ec commit 8649290
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 100 deletions.
34 changes: 15 additions & 19 deletions src/main/groovy/groovyx/javafx/factory/CanvasFactory.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,15 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package groovyx.javafx.factory

import groovyx.javafx.canvas.CanvasOperation
import groovyx.javafx.canvas.DrawOperations
import javafx.collections.FXCollections
import javafx.scene.canvas.Canvas

/**
Expand All @@ -30,32 +26,32 @@ import javafx.scene.canvas.Canvas
*/
class CanvasFactory extends AbstractNodeFactory {
private static final String CANVAS_OPERATIONS_LIST_PROPERTY = "__canvasOperationsList"

CanvasFactory() {
super(Canvas);
super(Canvas)
}

CanvasFactory(Class<Canvas> beanClass) {
super(beanClass);
super(beanClass)
}

@Override
public void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
if (child instanceof CanvasOperation) {
def operations = builder.parentContext.get(CANVAS_OPERATIONS_LIST_PROPERTY, [])
def operations = builder.parentContext.get(CANVAS_OPERATIONS_LIST_PROPERTY, FXCollections.observableList([]))
operations << child
}else {
super.setChild(builder, parent, child);
} else {
super.setChild(builder, parent, child)
}
}


@Override
void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
def operations = builder.context.remove(CANVAS_OPERATIONS_LIST_PROPERTY)
def dop = new DrawOperations(operations: operations, canvas: node);
dop.draw();
node.userData = dop;
def dop = new DrawOperations(operations: operations, canvas: node)
dop.draw()
node.userData = dop
super.onNodeCompleted(builder, parent, node)
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/groovy/groovyx/javafx/factory/ContainerFactory.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package groovyx.javafx.factory

import javafx.collections.FXCollections
import javafx.scene.Node
import javafx.scene.NodeBuilder
import javafx.scene.Parent
Expand All @@ -27,13 +28,12 @@ import javafx.scene.layout.GridPane
*/
class ContainerFactory extends AbstractNodeFactory {
private static final String BUILDER_LIST_PROPERTY = "__builderList"

ContainerFactory(Class beanClass) {
super(beanClass);
super(beanClass)
}


public void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
if (child instanceof Node) {
if (parent instanceof BorderPane) {
parent.setCenter(child)
Expand Down Expand Up @@ -64,7 +64,7 @@ class ContainerFactory extends AbstractNodeFactory {
}
}
} else if (child instanceof NodeBuilder) {
def builderList = builder.parentContext.get(BUILDER_LIST_PROPERTY, [])
def builderList = builder.parentContext.get(BUILDER_LIST_PROPERTY, FXCollections.observableList([]))
builderList << child
} else {
super.setChild(builder, parent, child)
Expand Down
42 changes: 18 additions & 24 deletions src/main/groovy/groovyx/javafx/factory/DrawFactory.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,15 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package groovyx.javafx.factory

import groovyx.javafx.canvas.CanvasOperation
import groovyx.javafx.canvas.DrawOperations
import javafx.collections.FXCollections
import javafx.scene.canvas.Canvas

/**
Expand All @@ -30,40 +26,38 @@ import javafx.scene.canvas.Canvas
*/
class DrawFactory extends AbstractNodeFactory {
private static final String DRAW_OPERATIONS_LIST_PROPERTY = "__drawOperationsList"

DrawFactory() {
super(DrawOperations);
super(DrawOperations)
}

DrawFactory(Class<DrawOperations> beanClass) {
super(beanClass);
super(beanClass)
}

@Override
public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
DrawOperations operations = super.newInstance(builder, name, value, attributes);
if(value instanceof Canvas)
operations.canvas = value;
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
DrawOperations operations = super.newInstance(builder, name, value, attributes)
if (value instanceof Canvas)
operations.canvas = value
operations
}

@Override
public void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
if (child instanceof CanvasOperation) {
def operations = builder.parentContext.get(DRAW_OPERATIONS_LIST_PROPERTY, [])
def operations = builder.parentContext.get(DRAW_OPERATIONS_LIST_PROPERTY, FXCollections.observableList([]))
operations << child
}else {
super.setChild(builder, parent, child);
} else {
super.setChild(builder, parent, child)
}
}



@Override
void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
node.operations = builder.context.remove(DRAW_OPERATIONS_LIST_PROPERTY)
if(node.canvas != null)
if (node.canvas != null)
node.draw()
super.onNodeCompleted(builder, parent, node)
}
}

103 changes: 52 additions & 51 deletions src/main/groovy/groovyx/javafx/factory/SceneFactory.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@ package groovyx.javafx.factory
import groovyx.javafx.event.GroovyChangeListener
import groovyx.javafx.event.GroovyEventHandler
import groovyx.javafx.event.GroovyInvalidationListener
import javafx.collections.FXCollections
import javafx.event.EventHandler
import javafx.scene.Group
import javafx.scene.Node
Expand All @@ -33,19 +34,19 @@ import org.codehaus.groovy.runtime.InvokerHelper
*/
class SceneFactory extends AbstractFXBeanFactory {
private static final String BUILDER_LIST_PROPERTY = "__builderList"
boolean syntheticRoot = false;

boolean syntheticRoot = false

SceneFactory() {
super(Scene);
super(Scene)
}

SceneFactory(Class<Scene> beanClass) {
super(beanClass);
super(beanClass)
}

public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
Scene scene;
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
Scene scene
if (checkValue(name, value)) {
scene = value
} else {
Expand All @@ -55,93 +56,93 @@ class SceneFactory extends AbstractFXBeanFactory {
def depthBuffer = attributes.remove("depthBuffer") ?: false
def antiAliasing = attributes.remove("antiAliasing") ?: SceneAntialiasing.DISABLED

if(root == null) {
if (root == null) {
root = new Group()
syntheticRoot = true
}

scene = new Scene(root, width, height, depthBuffer, antiAliasing)
}
def id = attributes.remove("id");
if(id != null)
builder.getVariables().put(id, scene);
return scene;
def id = attributes.remove("id")
if (id != null)
builder.getVariables().put(id, scene)
return scene
}

public void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
Scene scene = (Scene)parent
void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
Scene scene = (Scene) parent

// If we have a synthetic root, then the first child Node either becomes
// the root (if it's a Parent) or becomes a child of the synthetic root.
// Either way, our synthetic root is no longer synthetic.
if(syntheticRoot && child instanceof Node) {
syntheticRoot = false;
if(child instanceof Parent ) {
scene.root = child;
if (syntheticRoot && child instanceof Node) {
syntheticRoot = false
if (child instanceof Parent) {
scene.root = child
return
}
}
switch(child) {

switch (child) {
case Node:
scene.root.children.add(child)
break;
break
case String:
scene.stylesheets.add(child);
break;
scene.stylesheets.add(child)
break
case List:
scene.stylesheets.addAll(child.collect {it.toString()})
break;
scene.stylesheets.addAll(child.collect { it.toString() })
break
case GroovyEventHandler:
InvokerHelper.setProperty(scene, child.property, child);
break;
InvokerHelper.setProperty(scene, child.property, child)
break
case NodeBuilder:
def builderList = builder.parentContext.get(BUILDER_LIST_PROPERTY, [])
def builderList = builder.parentContext.get(BUILDER_LIST_PROPERTY, FXCollections.observableList([]))
builderList << child
break;
break
case GroovyChangeListener:
case GroovyInvalidationListener:
if(parent.metaClass.respondsTo(parent, child.property + "Property"))
parent."${child.property}Property"().addListener(child);
break;
if (parent.metaClass.respondsTo(parent, child.property + "Property"))
parent."${child.property}Property"().addListener(child)
break
}
}

public boolean onHandleNodeAttributes( FactoryBuilderSupport builder, Object node, Map attributes ) {
def scene = (Scene)node
boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
def scene = (Scene) node
def attr = attributes.remove("stylesheets")
if(attr) {
if(attr instanceof List)
if (attr) {
if (attr instanceof List)
scene.stylesheets.addAll(attr)
else
else
scene.stylesheets.add(attr.toString())
}
for(v in AbstractNodeFactory.nodeEvents) {
if(attributes.containsKey(v)) {
def val = attributes.remove(v);
if(val instanceof Closure) {
for (v in AbstractNodeFactory.nodeEvents) {
if (attributes.containsKey(v)) {
def val = attributes.remove(v)
if (val instanceof Closure) {
FXHelper.setPropertyOrMethod(node, v, val as EventHandler)
/*****
def handler = new GroovyEventHandler(v);
handler.setClosure((Closure)val);
def handler = new GroovyEventHandler(v)
handler.setClosure((Closure)val)
****/
}else if(val instanceof EventHandler) {
} else if(val instanceof EventHandler) {
FXHelper.setPropertyOrMethod(node, v, val)
}
}
}
return super.onHandleNodeAttributes(builder, node, attributes);
return super.onHandleNodeAttributes(builder, node, attributes)
}

@Override
void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
if (node instanceof Scene && node.root == null) {
node.root = new Group();
node.root = new Group()
}

def builderList = builder.context.remove(BUILDER_LIST_PROPERTY)
builderList?.each {
node.root.children.add(it.build());
node.root.children.add(it.build())
}
}
}
Expand Down

0 comments on commit 8649290

Please sign in to comment.