Fix #1510: Handle spacing and links in content box messages.

This commit is contained in:
David Cavaceci 2024-10-02 11:30:06 -05:00
parent e50fe4c68c
commit bf457620db

View file

@ -14,8 +14,8 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority; import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import java.util.Arrays; import java.util.regex.Matcher;
import java.util.OptionalDouble; import java.util.regex.Pattern;
public class TitledDescriptionPane extends TitledPane { public class TitledDescriptionPane extends TitledPane {
private Label mainLabel; private Label mainLabel;
@ -127,25 +127,44 @@ public class TitledDescriptionPane extends TitledPane {
} }
protected Node getContentBox(String message) { protected Node getContentBox(String message) {
Label details = new Label(message); // Create the VBox to hold text and Hyperlink components
details.setWrapText(true); VBox contentBox = new VBox();
HBox contentBox = new HBox();
contentBox.setAlignment(Pos.TOP_LEFT); contentBox.setAlignment(Pos.TOP_LEFT);
contentBox.getChildren().add(details);
contentBox.setPadding(new Insets(10, 30, 10, 30)); contentBox.setPadding(new Insets(10, 30, 10, 30));
contentBox.setPrefWidth(400); // Set preferred width for wrapping
double width = TextUtils.computeTextWidth(details.getFont(), message, 0.0D); // Define the regex pattern to match URLs
double numLines = Math.max(1, Math.ceil(width / 400d)); String urlPattern = "(\\[https?://\\S+])";
Pattern pattern = Pattern.compile(urlPattern);
Matcher matcher = pattern.matcher(message);
//Handle long words like txids // StringBuilder to track the non-URL text
OptionalDouble maxWordLength = Arrays.stream(message.split(" ")).mapToDouble(word -> TextUtils.computeTextWidth(details.getFont(), message, 0.0D)).max(); int lastMatchEnd = 0;
if(maxWordLength.isPresent() && maxWordLength.getAsDouble() > 300.0) {
numLines += 1.0; // Iterate through the matches and build the components
while (matcher.find()) {
// Add the text before the URL as a normal Label
if (matcher.start() > lastMatchEnd) {
String nonUrlText = message.substring(lastMatchEnd, matcher.start());
Label textLabel = createWrappedLabel(nonUrlText);
contentBox.getChildren().add(textLabel);
}
// Extract the URL and create a Hyperlink for it
String url = matcher.group(1).replaceAll("\\[", "").replaceAll("\\]", "");
Hyperlink hyperlink = createHyperlink(url);
contentBox.getChildren().add(hyperlink);
// Update last match end
lastMatchEnd = matcher.end();
} }
double height = Math.max(60, numLines * 20); // Add remaining text after the last URL (if any)
contentBox.setPrefHeight(height); if (lastMatchEnd < message.length()) {
String remainingText = message.substring(lastMatchEnd);
Label remainingLabel = createWrappedLabel(remainingText);
contentBox.getChildren().add(remainingLabel);
}
return contentBox; return contentBox;
} }
@ -178,4 +197,27 @@ public class TitledDescriptionPane extends TitledPane {
return account; return account;
} }
// Helper method to create a wrapped Label with a specified maxWidth
private Label createWrappedLabel(String text) {
Label label = new Label(text);
label.setWrapText(true);
label.setMaxWidth(400);
return label;
}
// Helper method to create a Hyperlink
private Hyperlink createHyperlink(String url) {
Hyperlink hyperlink = new Hyperlink(url);
hyperlink.setMaxWidth(400); // Set maximum width for wrapping
hyperlink.setWrapText(true); // Ensure text wrapping in the hyperlink
hyperlink.setOnAction(e -> {
try {
java.awt.Desktop.getDesktop().browse(new java.net.URI(url));
} catch (Exception ex) {
// TODO: handle failure to launch link
}
});
return hyperlink;
}
} }