Skip to content

Commit

Permalink
replace TextArea with WebView for summary display
Browse files Browse the repository at this point in the history
  • Loading branch information
mulla028 committed Dec 9, 2024
1 parent 834e51e commit b5199d4
Showing 1 changed file with 28 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;

import org.jabref.logic.ai.summarization.Summary;
import org.jabref.logic.layout.format.MarkdownFormatter;
import org.jabref.logic.util.WebViewStore;

import com.airhacks.afterburner.views.ViewLoader;

public class SummaryShowingComponent extends VBox {
@FXML private TextArea summaryTextArea;
@FXML private Text summaryInfoText;
@FXML private CheckBox markdownCheckbox;
@FXML private StackPane contentStackPane;

private WebView markdownWebView;

private WebView contentWebView;
private final Summary summary;
private final Runnable regenerateCallback;
private final MarkdownFormatter markdownFormatter = new MarkdownFormatter();
Expand All @@ -42,58 +38,47 @@ public SummaryShowingComponent(Summary summary, Runnable regenerateCallback) {

@FXML
private void initialize() {
if (!contentStackPane.getChildren().contains(summaryTextArea)) {
contentStackPane.getChildren().add(summaryTextArea);
}
initializeWebView();
updateContent(false); // Start in plain text mode
updateInfoText();
}

summaryTextArea.setText(summary.content());
private void initializeWebView() {
contentWebView = WebViewStore.get();
contentWebView.setMinHeight(100);
contentWebView.setPrefHeight(300);

// Apply styles directly to the WebView
contentWebView.setStyle("-fx-font-family: Sans;");

VBox.setVgrow(contentWebView, Priority.ALWAYS);
getChildren().addFirst(contentWebView);
}

private void updateContent(boolean isMarkdown) {
String content = summary.content();
if (isMarkdown) {
contentWebView.getEngine().loadContent(markdownFormatter.format(content));
} else {
contentWebView.getEngine().loadContent("<pre>" + content + "</pre>");
}
}

private void updateInfoText() {
String newInfo = summaryInfoText
.getText()
.replaceAll("%0", formatTimestamp(summary.timestamp()))
.replaceAll("%1", summary.aiProvider().getLabel() + " " + summary.model());

summaryInfoText.setText(newInfo);
}

private static String formatTimestamp(LocalDateTime timestamp) {
return timestamp.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.getDefault()));
}

private void initializeMarkdownWebView() {
if (markdownWebView == null) {
markdownWebView = new WebView();
VBox.setVgrow(markdownWebView, Priority.ALWAYS);

markdownWebView.prefWidthProperty().bind(summaryTextArea.widthProperty());
markdownWebView.prefHeightProperty().bind(summaryTextArea.heightProperty());

markdownWebView.setVisible(false);
markdownWebView.setManaged(false);
contentStackPane.getChildren().add(markdownWebView);
}
}

@FXML
private void onMarkdownToggle() {
initializeMarkdownWebView();

if (markdownCheckbox.isSelected()) {
String htmlContent = markdownFormatter.format(summaryTextArea.getText());
markdownWebView.getEngine().loadContent(htmlContent);

markdownWebView.setVisible(true);
markdownWebView.setManaged(true);

summaryTextArea.setVisible(false);
summaryTextArea.setManaged(false);
} else {
summaryTextArea.setVisible(true);
summaryTextArea.setManaged(true);

markdownWebView.setVisible(false);
markdownWebView.setManaged(false);
}
updateContent(markdownCheckbox.isSelected());
}

@FXML
Expand Down

0 comments on commit b5199d4

Please sign in to comment.