用于大输出的 Java 高级文本日志记录窗格

时间:2023-04-05
本文介绍了用于大输出的 Java 高级文本日志记录窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Mathematica 带有一个简单的 java 程序,允许检查前端和内核之间的通信.它被称为 是适合你的组件.

它使用了一个虚拟化的布局容器,因此只有在视口可见区域中的单元格才会被实际渲染.这允许延迟加载、单元回收等.ListView 使用 ObservableList 作为它的 DataStructure.与 EMF EList 类似,ObservableList自动通知 ListView 其包含的数据发生变化.

有几种工厂方法可以通过 FXCollections 甚至允许包装现有的 List(例如 RingBuffer).

如果您需要高级突出显示,RichTextFX 可能是解决方案去,因为它允许其包含的文本的详细样式.RichTextFX 也使用虚拟化布局.

<小时>

编辑#2

Tom 在他的博客中写道:http://tomsondev.bestsolution.at/2014/12/27/displaying-and-editing-large-styled-texts/

<小时>

编辑 #1 ListView 示例

JavaFX 在将模型与视图分离方面做得非常好,所以我们尽量不要混淆它,需要创建两件事:

  1. 一个数据类(模型)
  2. 该数据类(视图)的 Cell 渲染器.

首先是数据类:

公共类 LogData {私有最终字符串日志消息;私有列表<字符串>突出显示的片段 = 空;公共日志数据(字符串 pLogMessage){日志消息 = pLogMessage;}公共字符串 getLogMessage() {返回日志消息;}公共列表<字符串>getHighlightedFragments() {if (highlightedFragments == null) {做高亮();}返回突出显示的片段;}私人无效doHighlight(){列表<字符串>highlightedParts = Collections.emptyList();//TODO 词法分析器突出显示的片段 = 突出显示的部分;}}

有趣的是,突出显示是按需完成的,而不是在初始化时.或者换句话说:词法分析器仅在单元格渲染器请求数据时执行其工作.

现在是 Cell 渲染器:

ListView列表视图 = 新列表视图<>();listView.setCellFactory(cb -> new LogDataCell(){});公共类 LogDataCell 扩展 ListCell<LogData>{@覆盖protected void updateItem(LogData item, boolean empty) {super.updateItem(项目,空);如果(空 || 项目 == 空){设置文本(空);集合图形(空);}别的 {列表<字符串>片段 = item.getHighlightedFragments();如果(片段 == 空 || 片段.isEmpty()) {setText(item.getLogMessage());集合图形(空);}别的 {文本流文本流=空;//去做设置文本(空);设置图形(文本流);}}}}

这不是一个完整的示例,还有几个 TODO,但希望你能明白.

如果您想添加搜索突出显示,我在这里描述了 TableView 控件元素的类似方法:JavaFX 表格突出显示文本(标签)性能不佳

Mathematica comes with a simple java program that allows to inspect the communication between front end and the kernel. It's called LinkSnooper and in general it works quite nice. It looks like this

I think I can improve the behavior and usability of the program to some extend, but to do this, I need to reimplement some parts. One fundamental piece that I need is a text pane, which has the following properties:

  • it can receive a lot of data and it probably should use a fast ring-buffer so that the very first log-lines are removed when the data grows too much. Another possibility is that it automatically starts to write data to disk and possibly reloads it when the user scrolls up to see the first entries
  • it should be able to handle colored text. I plan to use a simple highlighter (the log-data is actually real Mathematica syntax) on each arriving line to make reading more easy
  • it doesn't need to be writable. It's OK if the text pane is read-only.

Question: Does something like this already exist? Currently, LinkSnooper uses a JTextArea underneath and before I start do write my own version, I wanted to ask whether someone has already done this.

Edit:

What I planned to do was to use some Logger framework because it seems natural to me that those libraries should be able to handle a lot of data. Additionally, they often provide interfaces to format the messages and you can define different handlers that can take care of different messages. What I was hoping for was that someone already has combined this with a neatly working text window that can handle large output.

解决方案

As Simon has pointed out I would suggest using JavaFX for this task.

If you "just" need to display large amounts of log data without advanced highlighting (sub-string range highlighting), ListView is the component for you.

It uses a virtualized layout container, so only the cells that are in the visible area of the viewport are actually rendered. This allows for lazy loading, cell recycling etc. The ListView uses an ObservableList as its DataStructure. Similar to EMF EList, the ObservableListautomatically notifies the ListView on changes in its contained data.

There are several factory methods to create an ObservableList via FXCollections even allowing to wrap an existing List (e.g. RingBuffer).

If you need the advanced highlighting, RichTextFX is probably the solution to go for as it allows detailed styling of its contained text. RichTextFX uses a virtualized layout, too.


Edit #2

Tom has written about this in his blog: http://tomsondev.bestsolution.at/2014/12/27/displaying-and-editing-large-styled-texts/


Edit #1 ListView example

JavaFX does a very good job at separating the model from the view, so we try not to mix this up and need to create two things:

  1. A data class (model)
  2. A Cell renderer for that data class (view).

First the data class:

public class LogData {

    private final String logMessage;
    private List<String> highlightedFragments = null;

    public LogData(String pLogMessage) {
        logMessage = pLogMessage;
    }

    public String getLogMessage() {
        return logMessage;
    }

    public List<String> getHighlightedFragments() {
        if (highlightedFragments == null) {
            doHighlight();
        }
        return highlightedFragments;
    }

    private void doHighlight() {
        List<String> highlightedParts = Collections.emptyList(); // TODO lexer
        highlightedFragments = highlightedParts;
    }
}

The interesting part is, that the highlighting is done on demand not on initialization. Or in other words: The lexer only performs its work, when the cell renderer requests the data.

Now the Cell renderer:

ListView<LogData> listView = new ListView<>();
listView.setCellFactory(cb -> new LogDataCell(){});

public class LogDataCell extends ListCell<LogData>
{
    @Override
    protected void updateItem(LogData item, boolean empty) {
        super.updateItem(item, empty);

        if(empty || item == null) {
            setText(null);
            setGraphic(null);
        }
        else {
            List<String> fragments = item.getHighlightedFragments();
            if(fragments == null || fragments.isEmpty()) {
                setText(item.getLogMessage());
                setGraphic(null);
            }
            else {
                TextFlow textFlow = null; //TODO
                setText(null);
                setGraphic(textFlow);
            }
        }
    }
}

This is not a fully working example, there are several TODOs left, but hopefully you get the idea.

If you want to add search highlighting, I described a similar approach for the TableView control element here: JavaFX Table with highlighted text (Labels) with poor performance

这篇关于用于大输出的 Java 高级文本日志记录窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Eclipse 中覆盖注释的错误 下一篇:如何(水平)对齐 PDFBox 中 PDTextField 的文本?

相关文章