Drawing boxes in StyledText that do not move when scrolling into the eclipse plugin

I am trying to color the boxes against the background of my text editor in the Eclipse plug-in that I am developing. I succeed, but the problem is that when I scroll through the code in a text editor, the boxes also move in the direction I scroll. Does anyone know how to make boxes that keep their starting position there? My code for drawing:

<!-- language: lang-java -->

    public class BoxHighLighting {

    protected StyledText boxText;
    private Color bc;
    int startingChar;
    int endingChar; 


    public BoxHighLighting(StyledText boxText, int startingChar, int endingChar, Color color){
        this.boxText = boxText;
        this.startingChar = startingChar;
        this.endingChar = endingChar;
        bc = color;
        setStyledText();
    }

    public void setStyledText() {

        //boxText.getOffsetAtLine(3);

        if (boxText == null)
            return;

        Rectangle r0 = boxText.getClientArea();

        if ((r0.width < 1) || (r0.height < 1)) {
            return;
        }
        int xOffset = this.boxText.getHorizontalPixel();
        int yOffset = this.boxText.getTopPixel();

        Image newImage = new Image(null, r0.width, r0.height);
        GC gc = new GC(newImage);
        Rectangle rec = boxText.getTextBounds(startingChar, endingChar);
        Rectangle editorWin = newImage.getBounds();
        fillRectangle(bc, gc, rec.x, rec.y, editorWin.width, rec.height);
        gc.drawRectangle(rec.x, rec.y, editorWin.width, rec.height);
        Image oldImage = this.boxText.getBackgroundImage();
        this.boxText.setBackgroundImage(newImage);
        if (oldImage != null)
            oldImage.dispose();
        gc.dispose();
    }

    void fillRectangle(Color c, GC gc, int x, int y, int width, int height) {
        if (c == null) {
            return;
        }
         gc.setBackground(c);
                 gc.fillRoundRectangle(x, y, width, height, 5, 5);

    }
}
+3
source share
1 answer

You can put the paint listener in the ITextEditor StyledText property and determine when the x, y scroll position has changed, and then redraw your fields when the person scrolls

styledText.addPaintListener(new PaintListener() {
    private int oldXOffset = 0;
    private int oldYOffset = 0;

    private boolean offsetMoved(int xOffset, int yOffset) {
       if ((xOffset != oldXOffset) || (yOffset != oldYOffset)) {
         oldXOffset = xOffset;
         oldYOffset = yOffset;
         return true;
       }
       return false;
    }

    public void paintControl(PaintEvent event) {
        if(event.getSource() instanceof StyledText)  {
            StyledText st = (StyledText) event.getSource();
            if(offsetMoved(st.getHorizontalPixel(), st.getTopPixel())) {
                    // do a redraw of the rectangles here!!!!
            }
         }
    }

});

+4
source

All Articles