Skip to content

Commit

Permalink
Move to JPro Platform routing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Sep 11, 2023
1 parent 11f5cd7 commit 8225c41
Show file tree
Hide file tree
Showing 24 changed files with 1,974 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ configure([project("jpro-sessions")]) {
configure([project("tree-showing"), project("jpro-auth"), project("jpro-imagemanager"), project("jpro-media"),
project("jpro-sessions"), project("htmlscrollpane"), project("freeze-detector")]) {
apply plugin: 'maven-publish'

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ OSDETECTOR_PLUGIN_VERSION = 1.7.3
JAVACPP_VERSION = 1.5.9
JAVACV_VERSION = 1.5.9
JSON_VERSION = 20230618
CONTROLSFX_VERSION = 11.1.2
MDFX_VERSION = 0.2.12
JETBRAINS_ANNOTATIONS_VERSION = 24.0.1
AUTH0_JAVAJWT_VERSION = 4.4.0
AUTH0_JWKSRSA_VERSION = 0.22.1
Expand Down
43 changes: 42 additions & 1 deletion jpro-routing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ buildscript {
}
}

configure([project(':jpro-routing:core'), project(':jpro-routing:dev'), project(':jpro-routing:core-test')]) {
configure([project(':jpro-routing:core'), project(':jpro-routing:dev'), project(':jpro-routing:core-test'),
project(':jpro-routing:example')]) {

repositories {
mavenCentral()
maven {
Expand Down Expand Up @@ -82,3 +84,42 @@ configure([project(':jpro-routing:core-test')]) {
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$JUNIT_VERSION"
}
}

configure(project(':jpro-routing:example')) {
apply plugin: 'org.openjfx.javafxplugin'
apply plugin: 'jpro-gradle-plugin'

// mainClassName = "example.scala.TestWebApplication"
// mainClassName = "example.scala.TestExtensions"
// mainClassName = "example.scala.ColorTransitionApp"
// mainClassName = "example.colors.ColorsApp"
// mainClassName = "example.popup.PopupApp"
mainClassName = "example.login.LoginApp"

application {
mainClass = "$mainClassName"
mainModule = moduleName
}

dependencies {
implementation project(':jpro-auth')
implementation project(':jpro-routing:core')
implementation project(':jpro-routing:dev')
implementation project(':jpro-routing:popup')

implementation "com.sandec:mdfx:$MDFX_VERSION"
implementation "org.controlsfx:controlsfx:$CONTROLSFX_VERSION"
implementation "fr.brouillard.oss:cssfx:$CSSFX_VERSION"
implementation "io.github.mkpaz:atlantafx-base:$ATLANTAFX_VERSION"
runtimeOnly "ch.qos.logback:logback-classic:$LOGBACK_VERSION"
}

javafx {
version = "$JAVAFX_VERSION"
modules = [ 'javafx.graphics', 'javafx.controls','javafx.swing', 'javafx.fxml', 'javafx.media', 'javafx.web' ]
}

jpro {
openingPath = "/"
}
}
88 changes: 88 additions & 0 deletions jpro-routing/example/src/main/java/example/colors/ColorsApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package example.colors;


import one.jpro.routing.*;
import one.jpro.routing.dev.DevFilter;
import one.jpro.routing.filter.container.ContainerFilter;
import example.filters.SimpleContainer;
import example.filters.SimpleHamburgerMenu;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import one.jpro.routing.Route;
import one.jpro.routing.RouteApp;
import simplefx.experimental.parts.FXFuture;

import java.util.List;
import java.util.regex.Pattern;

import static one.jpro.routing.RouteUtils.*;

public class ColorsApp extends RouteApp {

static Pattern colorPattern = Pattern.compile("/color/([0-9a-fA-F]{6})");

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

public Route createRoute() {
return Route.empty()
.and(redirect("/", "/green"))
.and(getNode("/green", (r) -> gen("Green","/red", Color.GREEN)))
.and(getNode("/red", (r) -> gen("Red", "/blue", Color.RED)))
.and(getNode("/blue", (r) -> gen("Blue", "/yellow", Color.BLUE)))
.and(getNode("/yellow", (r) -> gen("Yellow", r.resolve("/color/00ff00"), Color.YELLOW)))
.and(r -> {
var matcher = colorPattern.matcher(r.path());
if(matcher.matches()) {
var colorStr = matcher.group(1);
var color = Color.web(colorStr);
return FXFuture.unit(viewFromNode(gen("#" + colorStr, r.resolve("/red"), color)));
} else {
return FXFuture.unit(null);
}
})
.path("/colors",
Route.empty()
.and(getNode("/green", (r) -> gen("Green",r.resolve("/red"), Color.GREEN)))
.and(getNode("/red", (r) -> gen("Red", r.resolve("/green"), Color.RED)))
).filter(Filters.FullscreenFilter(true))
.filter(RouteUtils.sideTransitionFilter(1))
.filter(DevFilter.create())
.filter(ContainerFilter.create(() -> new SimpleContainer()))
.filter(ContainerFilter.create(() -> new SimpleHamburgerMenu(List.of(
new SimpleHamburgerMenu.Link("Green", "/green"),
new SimpleHamburgerMenu.Link("Red", "/red"),
new SimpleHamburgerMenu.Link("Blue", "/blue"),
new SimpleHamburgerMenu.Link("Yellow", "/yellow")
))));
}

public static Node gen(String title, String nextLink, Color color) {
StackPane result = new StackPane();
Label label = new Label(title);
label.setStyle("-fx-font-size: 36px;");
result.getChildren().add(label);
result.setStyle("-fx-background-color: " + toHexString(color) + ";");

StackPane linkArea = new StackPane();
LinkUtil.setLink(linkArea, nextLink);
result.getChildren().add(linkArea);
return result;
}
public static String toHexString(Color value) {
return "#" + (format(value.getRed()) + format(value.getGreen()) + format(value.getBlue()) + format(value.getOpacity())).toUpperCase();
}
private static String format(double v) {
String in = Integer.toHexString((int) Math.round(v * 255));
if (in.length() == 1) {
return "0" + in;
} else {
return in;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package example.filters;

import one.jpro.routing.filter.container.Container;
import one.jpro.routing.Request;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

public class SimpleContainer extends VBox implements Container {

HBox top;

public SimpleContainer() {
getStyleClass().add("simple-container");
top = new HBox();

Label topText = new Label();
// Bind the text of the request to the topText
requestProperty().addListener((observable, oldValue, newValue) -> {
if(newValue!=null) {
topText.textProperty().set(newValue.toString());
} else {
topText.setText("");
}
});
top.getChildren().add(topText);

updateChildren();
contentProperty().addListener((observable, oldValue, newValue) -> updateChildren());

}

private void updateChildren() {
getChildren().clear();
getChildren().add(top);
if(contentProperty().get()!=null) {
VBox.setVgrow(contentProperty().get(), Priority.ALWAYS);
getChildren().add(contentProperty().get());
}
}

ObjectProperty<Node> _contentProperty = new SimpleObjectProperty<>();

@Override
public ObjectProperty<Node> contentProperty() {
return _contentProperty;
}

ObjectProperty<Request> _requestProperty = new SimpleObjectProperty<>();

@Override
public ObjectProperty<Request> requestProperty() {
return _requestProperty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package example.filters;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.*;
import one.jpro.routing.LinkUtil;
import one.jpro.routing.Request;
import one.jpro.routing.filter.container.Container;

import java.util.List;

public class SimpleHamburgerMenu extends VBox implements Container {

BooleanProperty showMenu = new SimpleBooleanProperty(false);
Node topLinks;
Node expandedLinks;

HBox topBox;

StackPane contentArea;

Label burger = new Label("☰");

public SimpleHamburgerMenu(List<Link> links) {
setFillWidth(true);
getStylesheets().add(getClass().getResource("/example/filters/SimpleHamburgerMenu.css").toString());

widthProperty().addListener((observable, oldValue, newValue) -> {
updateMenuElems();
});

topLinks = createTopLinks(links);
expandedLinks = createExpandedLinks(links);

topBox = new HBox();
topBox.getStyleClass().add("top-box");

contentArea = new StackPane();
VBox.setVgrow(contentArea, Priority.ALWAYS);

showMenu.addListener((observable, oldValue, newValue) -> {
updateContentArea();
});
contentProperty().addListener((observable, oldValue, newValue) -> {
updateContentArea();
});
requestProperty().addListener((observable, oldValue, newValue) -> {
showMenu.set(false);
});

getChildren().add(topBox);
getChildren().add(contentArea);

updateContentArea();

burger.getStyleClass().add("simplehamburgermenu-burger");
burger.setOnMouseClicked(e -> {
toggleMenu();
});
}

public void toggleMenu() {
showMenu.set(!showMenu.get());
}

public void updateContentArea() {
contentArea.getChildren().clear();
if(getContent() != null) {
contentArea.getChildren().add(getContent());
}
if(showMenu.get()) {
burger.setText("X");
contentArea.getChildren().add(expandedLinks);
} else {
burger.setText("☰");
}
}
public void updateMenuElems() {
if(getWidth() > 700) {
showMenu.set(false);
topBox.getChildren().clear();
topBox.getChildren().add(topLinks);

} else {
topBox.getChildren().clear();
topBox.getChildren().add(burger);
}
}

public Node createTopLinks(List<Link> links) {
HBox linkBox = new HBox();
linkBox.getStyleClass().add("simplehamburgermenu-top-links");
for(Link link : links) {
Label label = new Label(link.text);
label.getStyleClass().add("simplehamburgermenu-top-link");
LinkUtil.setLink(label, link.link);
requestProperty().addListener((observable, oldValue, newValue) -> {
if(newValue != null) {
if(newValue.path().startsWith(link.link)) {
label.getStyleClass().add("selected");
} else {
label.getStyleClass().remove("selected");
}
}
});
linkBox.getChildren().add(label);
}
HBox.setHgrow(linkBox, Priority.ALWAYS);
return linkBox;
}

public Node createExpandedLinks(List<Link> links) {
VBox linkBox = new VBox();
boolean first = true;
linkBox.getStyleClass().add("simplehamburgermenu-expanded-links");
for(Link link : links) {
if(!first) {
linkBox.getChildren().add(new Separator());
}
first = false;

Label label = new Label(link.text);
label.getStyleClass().add("simplehamburgermenu-expanded-link");
LinkUtil.setLink(label, link.link);
requestProperty().addListener((observable, oldValue, newValue) -> {
if(newValue != null) {
if(newValue.path().startsWith(link.link)) {
label.getStyleClass().add("selected");
} else {
label.getStyleClass().remove("selected");
}
}
});
linkBox.getChildren().add(label);
}
StackPane.setAlignment(linkBox, javafx.geometry.Pos.TOP_LEFT);
linkBox.setMaxHeight(Region.USE_PREF_SIZE);
return linkBox;
}


// Link has text and link
public static class Link {
String text;
String link;
public Link(String text, String link) {
this.text = text;
this.link = link;
}
}



/* Container implementation */
ObjectProperty<Node> _contentProperty = new SimpleObjectProperty<>();

@Override
public ObjectProperty<Node> contentProperty() {
return _contentProperty;
}

ObjectProperty<Request> _requestProperty = new SimpleObjectProperty<>();

@Override
public ObjectProperty<Request> requestProperty() {
return _requestProperty;
}
}

Loading

0 comments on commit 8225c41

Please sign in to comment.