Skip to content

Commit

Permalink
Merged in JPereda/jpereda-scenebuilder (pull request #54)
Browse files Browse the repository at this point in the history
Issue #131: Fix to add the required imports when using GlueElements

Approved-by: gerardo balderas <[email protected]>
Approved-by: Joeri Sykora <[email protected]>
  • Loading branch information
abhinayagarwal committed Mar 26, 2018
2 parents 543956f + cd4395d commit 1f85f2b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import com.oracle.javafx.scenebuilder.kit.fxom.sampledata.SampleDataGenerator;
import com.oracle.javafx.scenebuilder.kit.util.Deprecation;
import com.oracle.javafx.scenebuilder.kit.util.URLUtils;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.Parent;

/**
Expand All @@ -74,12 +76,14 @@ public class FXOMDocument {

private boolean hasGluonControls;

private List<Class<?>> initialDeclaredClasses;

public FXOMDocument(String fxmlText, URL location, ClassLoader classLoader, ResourceBundle resources, boolean normalize) throws IOException {
this.glue = new GlueDocument(fxmlText);
this.location = location;
this.classLoader = classLoader;
this.resources = resources;
initialDeclaredClasses = new ArrayList<>();
if (this.glue.getRootElement() != null) {
final FXOMLoader loader = new FXOMLoader(this);
loader.load(fxmlText);
Expand Down Expand Up @@ -135,6 +139,10 @@ public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
endUpdate();
}

public List<Class<?>> getInitialDeclaredClasses() {
return initialDeclaredClasses;
}

public ResourceBundle getResources() {
return resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class FXOMInstance extends FXOMObject {

private final Map<PropertyName, FXOMProperty> properties = new LinkedHashMap<>();
private Class<?> declaredClass;
private FXOMDocument fxomDocument;


FXOMInstance(
Expand All @@ -69,7 +70,8 @@ public class FXOMInstance extends FXOMObject {
|| glueElement.getTagName().equals(declaredClass.getCanonicalName());
assert sceneGraphObject != null;
assert properties != null;


this.fxomDocument = fxomDocument;
this.declaredClass = declaredClass;
for (FXOMProperty p : properties) {
this.properties.put(p.getName(), p);
Expand Down Expand Up @@ -233,6 +235,32 @@ protected void collectDeclaredClasses(Set<Class<?>> result) {
for (FXOMObject v : ((FXOMPropertyC)p).getValues()) {
v.collectDeclaredClasses(result);
}
} else if (p instanceof FXOMPropertyT) {
collectGlueElementPropertiesT(((FXOMPropertyT)p).getValueElement(), result);
}
}

}

private void collectGlueElementPropertiesT(GlueElement element, Set<Class<?>> result) {
if (element == null) {
return;
}
if (! element.getChildren().isEmpty()) {
for (GlueElement e : element.getChildren()) {
collectGlueElementPropertiesT(e, result);
}
} else {
String clazz = element.getTagName();
if (clazz != null) {
for (Class<?> c : fxomDocument.getInitialDeclaredClasses()) {
if (c.getCanonicalName().equals(clazz) || c.getSimpleName().equals(clazz)) {
if (! result.contains(c)) {
result.add(c);
}
break;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ rootClass, getSceneGraphObject(),
properties);
}

if (result instanceof FXOMInstance) {
final Class<?> declaredClassFromResult = ((FXOMInstance) result).getDeclaredClass();
if (fxomDocument.getInitialDeclaredClasses() != null &&
! fxomDocument.getInitialDeclaredClasses().contains(declaredClassFromResult)) {
fxomDocument.getInitialDeclaredClasses().add(declaredClassFromResult);
}
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public void testWithMoreWildcards() {
assertFalse("fxml import does not contain javafx.scene.control.*", imports.contains("javafx.scene.control.*"));
}

@Test
public void testWithGlueElements() {
setupTestCase(FxmlTestInfo.WITH_GLUE_ELEMENTS);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

assertTrue("fxml import contains java.lang.String", imports.contains("java.lang.String"));
assertTrue("fxml import contains javafx.scene.paint.Color", imports.contains("javafx.scene.paint.Color"));
}

@Test
public void testDuplicates() {
setupTestCase(FxmlTestInfo.DUPLICATES);
Expand Down Expand Up @@ -297,6 +310,7 @@ private enum FxmlTestInfo {
WILDCARDS_AND_STATIC_PROPERTIES("WildcardsAndStaticProperties"),
WITH_MORE_WILDCARDS("WithMoreWildcards"),
WITH_WILDCARD("WithWildcard"),
WITH_GLUE_ELEMENTS("WithGlueElements"),
PUBLIC_STATIC_IMPORT("PublicStaticImport");

private String filename;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.String?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.shape.Circle?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" text="Button">
<graphic>
<Circle radius="5">
<style>
<String fx:value="-fx-stroke: red" />
</style>
<fill>
<Color fx:value="#00ff29" />
</fill>
</Circle>
</graphic>
<style>
<String fx:value="-fx-border-color: red;" />
</style>
<textFill>
<Color fx:value="#670fff" />
</textFill>
<layoutX>
<java.lang.String fx:value="302" />
</layoutX>
<layoutY>
<String fx:value="27" />
</layoutY>
<styleClass>
<String fx:value="style1" />
</styleClass>
</Button>
</children>
</AnchorPane>

0 comments on commit 1f85f2b

Please sign in to comment.