A Java advanced text logging pane for large output

前端 未结 1 390
慢半拍i
慢半拍i 2020-12-18 23:43

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 quit

相关标签:
1条回答
  • 2020-12-19 00:15

    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

    0 讨论(0)
提交回复
热议问题