Skip to content

Commit

Permalink
feat: added first version of visibility property
Browse files Browse the repository at this point in the history
  • Loading branch information
aldanchenko committed Jul 19, 2022
1 parent f4eefac commit 5642b63
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dlsc.preferencesfx.demo.visibility;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class VisibilityDemoAppStarter extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
Pane[] panels = new Pane[] {new VisibilityNodeExample()};

for (Pane pane : panels) {
tabPane.getTabs().add(new Tab(pane.getClass().getSimpleName().replace("Example", ""), pane));
}

Scene mainScene = new Scene(tabPane);

primaryStage.setTitle("Visibility PreferencesFX Demo");
primaryStage.setScene(mainScene);
primaryStage.setWidth(1000);
primaryStage.setHeight(700);
primaryStage.show();
primaryStage.centerOnScreen();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dlsc.preferencesfx.demo.visibility;

import com.dlsc.preferencesfx.PreferencesFx;
import com.dlsc.preferencesfx.model.Category;
import com.dlsc.preferencesfx.model.Group;
import com.dlsc.preferencesfx.model.Setting;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.layout.StackPane;

public class VisibilityNodeExample extends StackPane {

public PreferencesFx preferencesFx;

IntegerProperty brightness = new SimpleIntegerProperty(50);

BooleanProperty nightMode = new SimpleBooleanProperty(true);

public VisibilityNodeExample() {
preferencesFx = createPreferences();
getChildren().add(new VisibilityNodeView(preferencesFx, this));
}

private PreferencesFx createPreferences() {
SimpleBooleanProperty visibilityProperty = new SimpleBooleanProperty(true);
brightness.addListener((observable, oldValue, newValue) -> { visibilityProperty.set(newValue.intValue() > 50); });

return PreferencesFx.of(VisibilityNodeExample.class,
Category.of("General",
Group.of("Display",
Setting.of("Brightness", brightness),
Setting.of("Night mode", nightMode, visibilityProperty)
)
)
).persistWindowState(false).saveSettings(true).debugHistoryMode(false).buttonsVisibility(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.dlsc.preferencesfx.demo.visibility;

import com.dlsc.preferencesfx.PreferencesFx;
import com.dlsc.preferencesfx.demo.AppStarter;
import com.dlsc.preferencesfx.view.PreferencesFxView;
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.Priority;
import javafx.scene.layout.VBox;

public class VisibilityNodeView extends VBox {
private PreferencesFx preferencesFx;
private VisibilityNodeExample rootPane;

private Label brightnessLabel;
private Label nightModeLabel;

public VisibilityNodeView(PreferencesFx preferencesFx, VisibilityNodeExample rootPane) {
this.preferencesFx = preferencesFx;
this.rootPane = rootPane;

initializeParts();
layoutParts();
setupBindings();
setupListeners();
}

private void initializeParts() {
brightnessLabel = new Label();
nightModeLabel = new Label();
}

private void layoutParts() {
// VBox with values
VBox valueBox = new VBox(
brightnessLabel,
nightModeLabel
);

valueBox.setSpacing(20);
valueBox.setPadding(new Insets(20, 0, 0, 20));
Button saveSettingsButton = new Button("Save Settings");
saveSettingsButton.setOnAction(event -> preferencesFx.saveSettings());
Button discardChangesButton = new Button("Discard Changes");
discardChangesButton.setOnAction(event -> preferencesFx.discardChanges());
// VBox with descriptions
VBox descriptionBox = new VBox(
new Label("Brightness:"),
new Label("Night mode:"),
saveSettingsButton,
discardChangesButton
);
descriptionBox.setSpacing(20);
descriptionBox.setPadding(new Insets(20, 0, 0, 20));

PreferencesFxView preferencesFxView = preferencesFx.getView();
// Put everything together
BorderPane pane = new BorderPane();
HBox hBox = new HBox(descriptionBox, valueBox);
pane.setLeft(hBox);
hBox.setPadding(new Insets(0, 20, 0, 0));
pane.setCenter(preferencesFxView);
VBox.setVgrow(pane, Priority.ALWAYS);
getChildren().addAll(
pane
);

// Styling
getStyleClass().add("demo-view");
if (rootPane.nightMode.get()) {
getStylesheets().add(AppStarter.class.getResource("darkTheme.css").toExternalForm());
}
}

private void setupBindings() {
brightnessLabel.textProperty().bind(rootPane.brightness.asString().concat("%"));
nightModeLabel.textProperty().bind(rootPane.nightMode.asString());
}

private void setupListeners() {
rootPane.nightMode.addListener((observable, oldValue, newValue) -> {
if (newValue) {
getStylesheets().add(AppStarter.class.getResource("darkTheme.css").toExternalForm());
} else {
getStylesheets().remove(AppStarter.class.getResource("darkTheme.css").toExternalForm());
}
});
}
}
2 changes: 2 additions & 0 deletions preferencesfx-demo/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
exports com.dlsc.preferencesfx.demo;

opens com.dlsc.preferencesfx.demo;
exports com.dlsc.preferencesfx.demo.visibility;
opens com.dlsc.preferencesfx.demo.visibility;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dlsc.preferencesfx.formsfx.view.controls;

import com.dlsc.formsfx.model.structure.BooleanField;
import javafx.beans.property.BooleanProperty;
import javafx.scene.control.Label;
import org.controlsfx.control.ToggleSwitch;

Expand All @@ -20,6 +21,15 @@ public class ToggleControl extends SimpleControl<BooleanField, ToggleSwitch> {
*/
private Label fieldLabel;

private BooleanProperty nodeVisibilityProperty;

public ToggleControl() {
}

public ToggleControl(BooleanProperty booleanProperty) {
this.nodeVisibilityProperty = booleanProperty;
}

/**
* {@inheritDoc}
*/
Expand All @@ -34,6 +44,11 @@ public void initializeParts() {
// is necessary to offset the control to the left, because we don't use the provided label
node.setTranslateX(NEGATIVE_LABEL_INSETS);
node.setSelected(field.getValue());

if (this.nodeVisibilityProperty != null) {
node.visibleProperty().bind(this.nodeVisibilityProperty);
fieldLabel.visibleProperty().bind(this.nodeVisibilityProperty);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public static Setting of(String description, BooleanProperty property) {
);
}

public static Setting of(String description, BooleanProperty property, BooleanProperty visibilityProperty) {
return new Setting<>(
description,
Field.ofBooleanType(property)
.label(description)
.render(new ToggleControl(visibilityProperty)),
property
);
}

/**
* Constructs a setting of {@link Integer} type, which is represented by a {@link TextField}.
*
Expand Down

0 comments on commit 5642b63

Please sign in to comment.