-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from leewyatt/master
Implement Spacer And Add keyboard support to StripView
- Loading branch information
Showing
4 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SpacerApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.dlsc.gemsfx.demo; | ||
|
||
import com.dlsc.gemsfx.Spacer; | ||
import javafx.application.Application; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.CheckBox; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.VBox; | ||
import javafx.stage.Stage; | ||
|
||
public class SpacerApp extends Application { | ||
@Override | ||
public void start(Stage stage) throws Exception { | ||
VBox root = new VBox(10); | ||
root.setStyle("-fx-padding: 10px;-fx-alignment: top_center;"); | ||
|
||
Spacer spacer1 = new Spacer(); | ||
spacer1.setStyle("-fx-background-color: rgba(255,192,203,0.3);"); | ||
HBox topBox = new HBox(new Label("Hello"), spacer1, new Label("World")); | ||
|
||
Spacer spacer2 = new Spacer(); | ||
spacer2.setStyle("-fx-background-color: rgba(134,139,220,0.3);"); | ||
VBox centerBox = new VBox(new Label("Hello"), spacer2, new Label("World")); | ||
centerBox.setMinHeight(280); | ||
|
||
Spacer spacer3 = new Spacer(); | ||
spacer3.setStyle("-fx-background-color: rgba(0,255,127,0.3);"); | ||
Spacer spacer4 = new Spacer(); | ||
spacer4.setStyle("-fx-background-color: rgba(255,255,0,0.3);"); | ||
HBox bottomBox = new HBox(new Label("Hello"), spacer3, new Label("World"), spacer4, new Label("!~")); | ||
|
||
CheckBox checkBox = new CheckBox("Spacer Active"); | ||
checkBox.setSelected(true); | ||
spacer1.activeProperty().bind(checkBox.selectedProperty()); | ||
spacer2.activeProperty().bind(checkBox.selectedProperty()); | ||
spacer3.activeProperty().bind(checkBox.selectedProperty()); | ||
spacer4.activeProperty().bind(checkBox.selectedProperty()); | ||
|
||
root.getChildren().addAll(topBox, centerBox, bottomBox, checkBox); | ||
|
||
stage.setScene(new Scene(root, 380, 380)); | ||
stage.setTitle("Spacer Demo"); | ||
stage.show(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.dlsc.gemsfx; | ||
|
||
import javafx.beans.property.BooleanProperty; | ||
import javafx.css.CssMetaData; | ||
import javafx.css.Styleable; | ||
import javafx.css.StyleableBooleanProperty; | ||
import javafx.css.StyleableProperty; | ||
import javafx.css.converter.BooleanConverter; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.Priority; | ||
import javafx.scene.layout.Region; | ||
import javafx.scene.layout.VBox; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* The Spacer class extends the Region class and provides functionality | ||
* to create flexible spaces in layouts such as HBox and VBox. It is primarily | ||
* used to push adjacent nodes apart or together by filling up available space. <br/> | ||
* | ||
* The Spacer can be toggled between active and inactive states. When active, | ||
* it tries to grow as much as possible within its parent container. When | ||
* inactive, it collapses and doesn't take up any space. <br/> | ||
* | ||
* The growth direction of the Spacer (horizontal or vertical) is determined | ||
* based on its parent container. For instance, when placed inside an HBox, the | ||
* Spacer will grow horizontally. Conversely, inside a VBox, it will grow vertically. <br/> | ||
* | ||
* The active state of the Spacer can also be controlled through CSS with the | ||
* "-fx-active" property. | ||
* | ||
*/ | ||
public class Spacer extends Region { | ||
public Spacer() { | ||
this(true); | ||
} | ||
|
||
public Spacer(boolean active) { | ||
getStyleClass().add("spacer"); | ||
setActive(active); | ||
managedProperty().bind(visibleProperty()); | ||
visibleProperty().bind(activeProperty()); | ||
parentProperty().addListener((observable, oldValue, newValue) -> { | ||
if (newValue instanceof HBox) { | ||
VBox.setVgrow(this, Priority.NEVER); | ||
HBox.setHgrow(this, Priority.ALWAYS); | ||
} else if (newValue instanceof VBox) { | ||
VBox.setVgrow(this, Priority.ALWAYS); | ||
HBox.setHgrow(this, Priority.NEVER); | ||
} else { | ||
VBox.setVgrow(this, Priority.NEVER); | ||
HBox.setHgrow(this, Priority.NEVER); | ||
} | ||
}); | ||
} | ||
|
||
private final BooleanProperty active = new StyleableBooleanProperty(false) { | ||
@Override | ||
public Object getBean() { | ||
return Spacer.this; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "active"; | ||
} | ||
|
||
@Override | ||
public CssMetaData<Spacer, Boolean> getCssMetaData() { | ||
return StyleableProperties.ACTIVE; | ||
} | ||
}; | ||
|
||
public final boolean isActive() { | ||
return active.get(); | ||
} | ||
|
||
public final void setActive(boolean value) { | ||
active.set(value); | ||
} | ||
|
||
public BooleanProperty activeProperty() { | ||
return active; | ||
} | ||
|
||
private static class StyleableProperties { | ||
private static final CssMetaData<Spacer, Boolean> ACTIVE = | ||
new CssMetaData<>("-fx-active", BooleanConverter.getInstance(), false) { | ||
|
||
@Override | ||
public boolean isSettable(Spacer n) { | ||
return !n.active.isBound(); | ||
} | ||
|
||
@Override | ||
public StyleableProperty<Boolean> getStyleableProperty(Spacer n) { | ||
return (StyleableProperty<Boolean>) n.activeProperty(); | ||
} | ||
}; | ||
|
||
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES; | ||
|
||
static { | ||
List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(Region.getClassCssMetaData()); | ||
styleables.add(ACTIVE); | ||
STYLEABLES = Collections.unmodifiableList(styleables); | ||
} | ||
} | ||
|
||
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() { | ||
return StyleableProperties.STYLEABLES; | ||
} | ||
|
||
@Override | ||
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() { | ||
return getClassCssMetaData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters