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

Access scene and window when properties are not null #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
Expand Up @@ -46,6 +46,7 @@
import com.oracle.javafx.scenebuilder.kit.selectionbar.SelectionBarController;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
Expand All @@ -71,6 +72,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Consumer;

import static com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform.IS_LINUX;
import static com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform.IS_MAC;
Expand Down Expand Up @@ -129,28 +131,15 @@ public Comparator<String> getSectionComparator() {
}
};
editorController.setLibrary(library);
sceneProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
if (getScene() != null) {
getScene().getStylesheets().add(SceneBuilderPane.class.getResource("sb.css").toExternalForm());
getScene().windowProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
if (getScene().getWindow() != null) {
editorController.setOwnerWindow((Stage) getScene().getWindow());

// custom library from module path and class path
List<Path> scan = DependenciesScanner.scan();
createCustomLibrary(scan);

getScene().windowProperty().removeListener(this);
}
}
});
sceneProperty().removeListener(this);
}
}

executeOnceWhenPropertyIsNonNull(sceneProperty(), scene -> {
scene.getStylesheets().add(SceneBuilderPane.class.getResource("sb.css").toExternalForm());
executeOnceWhenPropertyIsNonNull(scene.windowProperty(), window -> {
editorController.setOwnerWindow((Stage) window);
// custom library from module path and class path
List<Path> scan = DependenciesScanner.scan();
createCustomLibrary(scan);
});
});

SplitPane mainPane = new SplitPane(createLeftSide(), createCenter(), createRightSide());
Expand Down Expand Up @@ -332,4 +321,25 @@ private String getUserLibraryFolder() {
}
return userLibraryFolder;
}

private static <T> void executeOnceWhenPropertyIsNonNull(ObservableValue<T> p, Consumer<T> consumer) {
if (p == null) return;

T value = p.getValue();
if (value != null) {
consumer.accept(value);
} else {
final InvalidationListener listener = new InvalidationListener() {
@Override public void invalidated(Observable observable) {
T value = p.getValue();

if (value != null) {
p.removeListener(this);
consumer.accept(value);
}
}
};
p.addListener(listener);
}
}
}