Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enables Scene Builder to provide Insets editor for custom controls #594

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2022, Gluon and/or its affiliates.
* Copyright (c) 2016, 2023, Gluon and/or its affiliates.
abhinayagarwal marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -40,6 +40,7 @@
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.DurationPropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.EventHandlerPropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.ImagePropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.InsetsPropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.list.StringListPropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.paint.ColorPropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.DoublePropertyMetadata;
Expand Down Expand Up @@ -73,6 +74,7 @@
import java.util.logging.Logger;

import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
Expand Down Expand Up @@ -115,7 +117,7 @@ public ComponentClassMetadata introspect() {
}
}
exception = null;
} catch(IOException | IntrospectionException x) {
} catch (IOException | IntrospectionException x) {
exception = x;
}

Expand Down Expand Up @@ -160,7 +162,7 @@ private Object instantiate() throws IOException {
try {
fxmlLoader.setClassLoader(componentClass.getClassLoader());
result = fxmlLoader.load(new ByteArrayInputStream(fxmlBytes));
} catch(RuntimeException x) {
} catch (RuntimeException x) {
throw new IOException(x);
}

Expand Down Expand Up @@ -202,38 +204,38 @@ private PropertyMetadata makePropertyMetadata(PropertyName name,
name,
propertyType,
readWrite,
(Enum<?>)getDefaultValue(sample, propertyDescriptor.getReadMethod(), fallback),
(Enum<?>) getDefaultValue(sample, propertyDescriptor.getReadMethod(), fallback),
inspectorPath);
} else if (propertyType == Boolean.class) {
result = new BooleanPropertyMetadata(
name,
readWrite,
(Boolean)getDefaultValue(sample, propertyDescriptor.getReadMethod(), false),
(Boolean) getDefaultValue(sample, propertyDescriptor.getReadMethod(), false),
inspectorPath);
} else if (propertyType == Integer.class) {
result = new IntegerPropertyMetadata(
name,
readWrite,
(Integer)getDefaultValue(sample, propertyDescriptor.getReadMethod(), 0),
(Integer) getDefaultValue(sample, propertyDescriptor.getReadMethod(), 0),
inspectorPath);
} else if (propertyType == Double.class) {
result = new DoublePropertyMetadata(
name,
DoubleKind.COORDINATE,
readWrite,
(Double)getDefaultValue(sample, propertyDescriptor.getReadMethod(), 0.0),
(Double) getDefaultValue(sample, propertyDescriptor.getReadMethod(), 0.0),
inspectorPath);
} else if (propertyType == String.class) {
result = new StringPropertyMetadata(
name,
readWrite,
(String)getDefaultValue(sample, propertyDescriptor.getReadMethod(), null),
(String) getDefaultValue(sample, propertyDescriptor.getReadMethod(), null),
inspectorPath);
} else if (propertyType == javafx.scene.paint.Color.class) {
result = new ColorPropertyMetadata(
name,
readWrite,
(Color)getDefaultValue(sample, propertyDescriptor.getReadMethod(), null),
(Color) getDefaultValue(sample, propertyDescriptor.getReadMethod(), null),
inspectorPath);
} else if (propertyType == javafx.scene.paint.Paint.class) {
result = new PaintPropertyMetadata(
Expand All @@ -253,12 +255,15 @@ private PropertyMetadata makePropertyMetadata(PropertyName name,
readWrite,
null,
inspectorPath);
} else if (propertyType == javafx.geometry.Insets.class) {
Insets defaultValues = (Insets) getDefaultValue(sample, propertyDescriptor.getReadMethod(), Insets.EMPTY);
result = new InsetsPropertyMetadata(name, readWrite, defaultValues, inspectorPath);
} else if (propertyType == javafx.util.Duration.class) {
Duration defaultValue = (Duration)getDefaultValue(sample, propertyDescriptor.getReadMethod(), null);
Duration defaultValue = (Duration) getDefaultValue(sample, propertyDescriptor.getReadMethod(), null);
result = new DurationPropertyMetadata(
name,
readWrite,
defaultValue == null? null : new SBDuration(defaultValue),
defaultValue == null ? null : new SBDuration(defaultValue),
inspectorPath);

} else if (propertyType == javafx.event.EventHandler.class) {
Expand Down Expand Up @@ -330,7 +335,7 @@ private Object getDefaultValue(Object sample, Method readMethod, Object fallback

try {
result = readMethod.invoke(sample);
} catch(InvocationTargetException|IllegalAccessException x) {
} catch (InvocationTargetException | IllegalAccessException x) {
result = fallback;
}

Expand Down
149 changes: 149 additions & 0 deletions kit/src/test/java/com/example/app/view/CustomNodeWithInsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2023, Gluon and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* - Neither the name of Oracle Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.example.app.view;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

public class CustomNodeWithInsets extends StackPane {

private final StackPane innerPane;

private final StackPane centerPane;

/**
* A control with various nested insets to demonstrate the insets editing capability for custom controls.
*/
public CustomNodeWithInsets() {
setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
setBorder(new Border(new BorderStroke(Color.WHITE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
innerPane = new StackPane();
innerPane.setBackground(new Background(new BackgroundFill(Color.ORANGE, new CornerRadii(10), Insets.EMPTY)));
innerPane.setBorder(new Border(new BorderStroke(Color.WHITE, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(2))));
innerPane.setMinSize(100, 100);
innerPane.setMaxSize(590, 590);
innerPane.setPrefSize(590, 380);
innerPane.paddingProperty().bind(innerFrameProperty());

centerPane = new StackPane();
centerPane.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(10), Insets.EMPTY)));
centerPane.setBorder(new Border(new BorderStroke(Color.WHITE, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(2))));
centerPane.setMinSize(100, 100);
centerPane.setMaxSize(580, 570);
centerPane.setPrefSize(580, 360);

Label label = new Label("Custom Control Insets Demo");
label.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, new CornerRadii(8), new BorderWidths(4))));
label.paddingProperty().bind(textPaddingProperty());
label.setBackground(new Background(new BackgroundFill(Color.WHITE, new CornerRadii(10), Insets.EMPTY)));
label.setFont(Font.font(label.getFont().getFamily(), FontWeight.BLACK, 14));
centerPane.getChildren().add(label);
innerPane.getChildren().add(centerPane);

getChildren().add(innerPane);
setMinSize(100, 100);
setMaxSize(600, 600);
setPrefSize(598, 396);
setWidth(600);
setHeight(400);

paddingProperty().bind(outerFrameProperty());
}

private ObjectProperty<Insets> outerFrame;

public void setOuterFrame(Insets value) {
this.outerFrameProperty().setValue(value);
}

public Insets getOuterFrame() {
return this.outerFrame == null ? new Insets(90, 40, 30, 160) : outerFrameProperty().getValue();
}

public ObjectProperty<Insets> outerFrameProperty() {
if (this.outerFrame != null) {
return this.outerFrame;
}
this.outerFrame = new SimpleObjectProperty<>(new Insets(90, 40, 30, 160));
return this.outerFrame;
};

private ObjectProperty<Insets> innerFrame;

public void setInnerFrame(Insets value) {
this.innerFrameProperty().setValue(value);
}

public Insets getInnerFrame() {
return this.innerFrame == null ? new Insets(20) : innerFrameProperty().getValue();
}

public ObjectProperty<Insets> innerFrameProperty() {
if (this.innerFrame != null) {
return this.innerFrame;
}

this.innerFrame = new SimpleObjectProperty<>(new Insets(20));
return this.innerFrame;
};

private ObjectProperty<Insets> textPadding;

public void setTextPadding(Insets value) {
this.textPaddingProperty().setValue(value);
}

public Insets getTextPadding() {
return this.textPadding == null ? new Insets(15, 40, 15, 40) : textPaddingProperty().getValue();
}

public ObjectProperty<Insets> textPaddingProperty() {
if (this.textPadding != null) {
return this.textPadding;
}
this.textPadding = new SimpleObjectProperty<>(new Insets(15, 40, 15, 40));
return this.textPadding;
};
}