-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to
JPro Platform
routing examples
- Loading branch information
Showing
24 changed files
with
1,974 additions
and
1 deletion.
There are no files selected for viewing
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
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
88 changes: 88 additions & 0 deletions
88
jpro-routing/example/src/main/java/example/colors/ColorsApp.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,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; | ||
} | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
jpro-routing/example/src/main/java/example/filters/SimpleContainer.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,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; | ||
} | ||
} |
174 changes: 174 additions & 0 deletions
174
jpro-routing/example/src/main/java/example/filters/SimpleHamburgerMenu.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,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; | ||
} | ||
} | ||
|
Oops, something went wrong.