a demo about using JBHtmlPane component to report error conv is null

Answered

public class RefactorChatWindow implements DumbAware, ToolWindowFactory 

@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {

JFrame frame = new JFrame("CodeContentCellHighlightComponent Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        JPanel parentComponent = new JPanel(new BorderLayout());
        parentComponent.setPreferredSize(new Dimension(800, 600));
        String codeContent = "\npublic class QuickSort {\n    public static void main(String[] args) {\n        System.out.println(\"Hello World!\");\n    }\n}";
        String language = "java";
        CodeContentCellHighlightComponent highlightComponent = new CodeContentCellHighlightComponent(project, codeContent, language, parentComponent, 800, true);
        parentComponent.add(highlightComponent, BorderLayout.CENTER);
        frame.getContentPane().add(parentComponent);
        frame.setVisible(true);

CodeContentCellHighlightComponent

    private final Project project;
    private final ToolbarPane toolbarPane;
    private final JComponent parentComponent;
    Dimension parentComponentSize;
    private final CodeJBHtmlPane editorTextField
    private JScrollPane jbScrollPane;
    private String lang;
    @Getter
    private String content;

    public CodeContentCellHighlightComponent(Project project, String content, String language, JComponent parentComponent, int limitCodeBlockHeight, boolean isOwner) {
        super(new BorderLayout());
        this.project = project;
        this.lang = language;
        this.content = content;

        this.parentComponent = parentComponent;
        this.parentComponentSize = parentComponent.getSize();
        // 创建核心显示组件
        content = "\npublic class QuickSort {\n";
        language = "java";
        this.editorTextField = new CodeJBHtmlPane(content, language);
        // 创建滚动条面板
        createJScrollPaneUI();
        this.add(jbScrollPane, BorderLayout.CENTER);
        // 渲染markdown内容
        if (StringUtils.isNotBlank(content)) {
            editorTextField.setLanguage(language);
            editorTextField.setText(content);
        }
        super.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                CodeContentCellHighlightComponent.this.revalidate();
                CodeContentCellHighlightComponent.this.repaint();
            }
        });
    }

private void createJScrollPaneUI() {
    jbScrollPane = new JBScrollPane(editorTextField);
    jbScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    jbScrollPane.setMinimumSize(new Dimension(500, 1000));
    jbScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jbScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
}

CodeJBHtmlPane

public class CodeJBHtmlPane extends JBHtmlPane implements RefreshColorComponent {
    private static final Logger log = LoggerFactory.getLogger(CodeTextPane.class);
    private static Code2HtmlConverter converter = new PrismCode2HtmlConverter();
    private static final String DEFAULT_LANGUAGE = "markdown";
    private String language;
    private String originText;
    private String template;

    public CodeJBHtmlPane(String content, String language) {
        super(JBHtmlPaneStyleConfiguration.builder().build(), JBHtmlPaneConfiguration.builder().build()); // 使用默认的配置
        // 初始化设置
        this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        this.originText = content;
        this.template = ThemeUtil.loadCodeGeneralTemplate();
        this.template = SchemaColorUtil.applySchemaColor(this.template);
        this.setEditable(false);
        this.setFocusable(true);
        this.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));

        if (StringUtils.isBlank(language)) {
            language = this.getDefaultLanguage();
        } else {
            language = language.trim();
        }

        this.language = language;
        this.setFont(FontUtil.loadFont("font/SourceHanSansCN-Regular.otf", Font.PLAIN, 14f));
        this.setText(content);
    }

    @Override
    public void scrollRectToVisible(Rectangle aRect) {
        // 此方法可留空,保持默认行为
    }

    private String buildText(String text, String language) {
        if (StringUtils.isBlank(this.template)) {
            return text;
        } else {
            language = language.toLowerCase(Locale.ROOT);
            text = StringUtils.stripEnd(text, "\n\t ");
            String html = converter.convert(text, language);
            if (html == null) {
                html = StringUtil.escapeXmlEntities(text);
            }

            EditorColorsScheme editorColorsScheme = EditorColorsManager.getInstance().getGlobalScheme();
            Font font = editorColorsScheme.getFont(EditorFontType.PLAIN);
            String fontFamily = font.getFamily();
            Color editorColor = UIUtil.getEditorPaneBackground();
            String editorBackgroundColor = String.format("#%02x%02x%02x", editorColor.getRed(), editorColor.getGreen(), editorColor.getBlue());
            return this.template.replace("{{fontFamily}}", fontFamily)
                    .replace("{{content}}", html)
                    .replace("{{language}}", language)
                    .replace("{{background}}", editorBackgroundColor);
        }
    }

    @Override
    public Color getSelectionColor() {
        return PluginColor.MARK_DOWN_CODE_SELECT_BG_COLOR;
    }

    @Override
    public void setText(String text) {
        this.originText = text;
        String htmlContent = this.buildText(text, this.language);

        try {
            super.setText(htmlContent);
        } catch (Exception e) {
            log.error("Error setting text in CodeTextPane", e);
        }
    }

    @Override
    public String getText() {
        return this.originText;
    }

    private String getDefaultLanguage() {
        return DEFAULT_LANGUAGE;
    }

    public void setLanguage(String language) {
        if (StringUtils.isNotBlank(language)) {
            this.language = language.trim();
        }
    }

    public void refreshColor(EditorColorsScheme scheme, Color themeColor) {
        this.template = ThemeUtil.loadCodeGeneralTemplate();
        this.template = SchemaColorUtil.applySchemaColor(this.template);
        this.setText(this.getText());
        this.invalidate();
        this.repaint();
    }
}

Converting code to HTML is correct because it can be rendered correctly when opened directly in a browser. Otherwise, IDEA will display normally between January 2023 and January 2024, but this issue will occur in February 2024

0

Post is closed for comments.