1

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?

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...

Edit 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)

NormanC
  • 41
  • 3
  • 1
    How about extending [WhitespaceCharacterPainter](https://git.eclipse.org/c/platform/eclipse.platform.text.git/tree/org.eclipse.jface.text/src/org/eclipse/jface/text/WhitespaceCharacterPainter.java) (and use `StyledText` widget via `TextViewer`)? – howlger Sep 11 '17 at 15:08
  • Nice idea! I'll give it a try... – NormanC Sep 12 '17 at 06:27

1 Answers1

0

For an E4 RCP application I have attached some code to start with.

Basically is uses the IEventBroker to add/remove the painter of the TextViewer by means of a button.

The StyledText can be obtained from

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";
}
J Robes
  • 467
  • 1
  • 6
  • 19
  • Thanks for the code snippets! Attaching a WhitespaceCharacterPainter is not really the point. it's how to draw non-printable chars that are normally just dropped in the view, like a HOP character (U+0081) - maybe via extending WhitespaceCharacterPainter in a way that has to be investigated... – NormanC Sep 12 '17 at 08:35