Merge pull request #1512 from dcavacec/fix-issue-1510

improve handling of spacing and links in accordion panels
This commit is contained in:
craigraw 2024-10-08 10:24:59 +02:00 committed by GitHub
commit 34bcc87468
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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,45 @@ 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
contentBox.setMinHeight(60);
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);
} }
double height = Math.max(60, numLines * 20); // Extract the URL and create a Hyperlink for it
contentBox.setPrefHeight(height); String url = matcher.group(1).replaceAll("\\[", "").replaceAll("\\]", "");
Hyperlink hyperlink = createHyperlink(url);
contentBox.getChildren().add(hyperlink);
// Update last match end
lastMatchEnd = matcher.end();
}
// Add remaining text after the last URL (if any)
if (lastMatchEnd < message.length()) {
String remainingText = message.substring(lastMatchEnd);
Label remainingLabel = createWrappedLabel(remainingText);
contentBox.getChildren().add(remainingLabel);
}
return contentBox; return contentBox;
} }
@ -178,4 +198,21 @@ 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(_ -> AppServices.get().getApplication().getHostServices().showDocument(url));
return hyperlink;
}
} }