Skip to content

Commit 4310216

Browse files
committed
UI/UX: Replace JFXTextArea to RichTextFx (Alert log monitoring content area)
1 parent a560ff6 commit 4310216

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

src/main/java/root/javafx/Controller/AlertLogMonitoringMenuController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,20 @@ public void initialize(URL location, ResourceBundle resources) {
134134
}
135135

136136
private void changeAlertLogListViewData(String serverID) {
137+
// AlertLog ListView
138+
String[] hightlightKeywords = tagBar.getTags().toArray(new String[0]);
139+
alertLogLV.setCellFactory(categoryList -> new AlertLogListViewCell(hightlightKeywords));
140+
137141
alertLogLV.getItems().clear();
138142
AlertLog alertLog = alertLogMonitoringResultMap.get(serverID);
139143
if (alertLog != null) {
140144
// Alert Log ListView
141145
alertLogLV.getItems().addAll(alertLog.getAlertLogs());
142-
146+
Platform.runLater(() -> {
147+
alertLogLV.scrollTo(0);
148+
alertLogLV.getSelectionModel().select(0);
149+
});
150+
143151
// Alert Log Summary
144152
alertLogSummarySP.getChildren().add(new AlertLogMonitoringSummaryAP(alertLog));
145153
} else {
@@ -178,9 +186,6 @@ private void initAlertLogMonitoringElements() {
178186
}
179187
});
180188

181-
// AlertLog ListView
182-
alertLogLV.setCellFactory(categoryList -> new AlertLogListViewCell());
183-
184189
// AlertLog Navigator
185190
navigatorTF.setTextFormatter(new NumberTextFormatter());
186191
navigatorTF.setOnKeyReleased(new EventHandler<KeyEvent>() {

src/main/java/root/javafx/CustomView/AlertLogListViewCell.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
import java.io.IOException;
44

5+
import org.fxmisc.richtext.StyleClassedTextArea;
6+
57
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
68
import javafx.fxml.FXML;
79
import javafx.fxml.FXMLLoader;
10+
import javafx.geometry.Insets;
811
import javafx.scene.control.Label;
912
import javafx.scene.control.ListCell;
10-
import javafx.scene.control.TextArea;
13+
import javafx.scene.input.ScrollEvent;
1114
import javafx.scene.layout.AnchorPane;
15+
import javafx.scene.layout.HBox;
16+
import javafx.scene.layout.Priority;
1217
import javafx.scene.paint.Paint;
1318
import javafx.scene.text.Text;
1419
import lombok.extern.slf4j.Slf4j;
@@ -28,9 +33,10 @@ public class AlertLogListViewCell extends ListCell<Log> {
2833
@FXML
2934
FontAwesomeIconView logStatusIcon;
3035
@FXML
31-
TextArea logContentTA;
36+
HBox logContentHBox;
3237

33-
private Text textHolder = new Text();
38+
public AlertLogListViewCell(String... highlightKeywords) {
39+
}
3440

3541
@Override
3642
protected void updateItem(Log logObj, boolean empty) {
@@ -50,9 +56,8 @@ protected void updateItem(Log logObj, boolean empty) {
5056
}
5157
}
5258

53-
5459
boolean isErrorLog = isErrorLog(logObj.getFullLogString());
55-
60+
5661
// logTimeStamp
5762
logTimeLabel.setText(logObj.getLogTimeStamp());
5863

@@ -63,25 +68,40 @@ protected void updateItem(Log logObj, boolean empty) {
6368
logStatusIcon.setFill(Paint.valueOf(isErrorLog ? "#d92a2a" : "#4d9c84"));
6469

6570
// logContent
66-
logContentTA.widthProperty().addListener((observable, oldValue, newValue) -> {
67-
Text text = new Text();
68-
text.setWrappingWidth(newValue.doubleValue());
69-
text.setText(logContentTA.getText());
70-
logContentTA.setPrefHeight(text.getLayoutBounds().getHeight() * 1.35);
71-
});
72-
73-
if(isErrorLog) {
74-
logContentTA.setStyle("-fx-background-color: #ffbfbf");
71+
StyleClassedTextArea codeArea = new StyleClassedTextArea();
72+
codeArea.setPadding(new Insets(2, 5, 0, 5));
73+
codeArea.setWrapText(false);
74+
codeArea.setEditable(false);
75+
codeArea.autosize();
76+
codeArea.setFocusTraversable(true);
77+
HBox.setHgrow(codeArea, Priority.ALWAYS);
78+
79+
String baseDir = System.getProperty("resourceBaseDir");
80+
codeArea.getStylesheets()
81+
.add(getClass().getResource(baseDir + "/css/alertLogListViewCell.css").toExternalForm());
82+
codeArea.getStyleClass().add("code-area");
83+
84+
codeArea.replaceText(0, 0, logObj.getFullLogString());
85+
if (isErrorLog) {
86+
codeArea.getStyleClass().add(".error");
7587
}
76-
logContentTA.setText(logObj.getFullLogString());
77-
logContentTA.setEditable(false);
78-
logContentTA.setWrapText(true);
7988

80-
textHolder.textProperty().bind(logContentTA.textProperty());
89+
// Set initial height
90+
Text text = new Text();
91+
text.setWrappingWidth(logContentHBox.widthProperty().doubleValue());
92+
text.setText(codeArea.getText());
93+
codeArea.setPrefHeight(text.getLayoutBounds().getHeight());
94+
logContentHBox.getChildren().addAll(codeArea);
8195

8296
setText(null);
8397
setGraphic(rootAP);
8498
setStyle("-fx-padding: 0");
99+
100+
// Propagate scroll event to parent listview
101+
codeArea.addEventFilter(ScrollEvent.ANY, scroll -> {
102+
codeArea.getParent().getParent().getParent().fireEvent(scroll);
103+
scroll.consume();
104+
});
85105
}
86106
}
87107

0 commit comments

Comments
 (0)