简体   繁体   English

如何在SWT文本小部件中显示不可打印的字符

[英]How to show non-printable characters in SWT a text widget

How is it possible to display non-printable characters in an SWT StyledText widget in a way it's done by Swing with an empty square? 如何以SWT StyledText窗口小部件中以不可显示的方式显示非可打印字符,方法是通过用一个空的正方形摆动来完成?

To replace all occurrences with a symbol via regex in the source string can't be the solution because copy & paste with the original codepoint won't be possible anymore. 在源字符串中通过正则表达式用符号替换所有出现的内容将不是解决方案,因为将无法再使用原始代码点进行复制和粘贴。

The best way might be to intercept the text rendering and replace them there, but that seems to open Pandora's box... 最好的方法可能是截取文本渲染并在那里替换它们,但这似乎打开了Pandora的盒子。

Edit 1: 编辑1:

The control characters, it's all about, are characters that are normally just skipped and not shown by the editor like HOP (U+0081) 控制字符就是所有这些字符,通常这些字符只是被跳过而没有像HOP(U + 0081)这样的编辑器显示

For an E4 RCP application I have attached some code to start with. 对于E4 RCP应用程序,我已经附加了一些代码。

Basically is uses the IEventBroker to add/remove the painter of the TextViewer by means of a button. 基本上是使用IEventBroker通过按钮来添加/删除TextViewer的绘画工具。

The StyledText can be obtained from 可以从以下位置获取StyledText

styledText = tv.getTextWidget();

as commented above 如上所述

import org.eclipse.jface.text.WhitespaceCharacterPainter;
public class TheEditor{
    @Inject MPart thePart;
    private WhitespaceCharacterPainter whitespaceCharacterPainter;

    @PostConstruct
    public void postConstruct(Composite parent) {
            TextViewer tv = new TextViewer(parent, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
            whitespaceCharacterPainter = new  WhitespaceCharacterPainter(tv);
            tv.addPainter(whitespaceCharacterPainter);
            whitespaceCharacterPainter.deactivate(true);
    }

    @Inject
    @Optional
    public void updatePartByWSButton(@UIEventTopic(EventConstants.WHITE_CHARACTERS_STATUS) boolean newButtonStatus) {
        final MElementContainer<MUIElement>container = thePart.getParent();
        if (thePart.equals((MPart)container.getSelectedElement())){
            wsToolBarButtonStatus = newButtonStatus;
            if(wsToolBarButtonStatus){
              this.whitespaceCharacterPainter.paint(IPainter.CONFIGURATION);
            }
            else{
                whitespaceCharacterPainter.deactivate(true);
                tv.removePainter(whitespaceCharacterPainter);
            }
        }
    }
}

Handler Class 处理程序类

public class WhitespaceCharactersHandler {
    boolean status;
    @Execute
    public void execute(final MToolItem item, IEventBroker broker) {
        if (item.isSelected()){
            status = true;
        }
        else{
            status = false;
        }
        broker.post(EventConstants.WHITE_CHARACTERS_STATUS, status);
    }
}

Contants interface: 接触界面:

public interface EventConstants {
    String WHITE_CHARACTERS_STATUS = "WHITE-CHARACTERS-STATUS";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM