Eclipse Editor: Partially editable/read-only editor Part 2

Decorating read-only section by line background

The second part of this series I am going to explain changing line background color for particular section of editor, in this case read-only code section. We had reached till restricting editing in read-only code section in previous post.

After previous post changes, user won’t able to edit in read-only code section, but user should get some kind of feedback about not able to edit the particular section of file. It would be very informative to color the background with grey color so that user understand that greyed out section is read-only.

This can be achieved by providing Line background color listen over source viewer.

LineBackgroundListener lineBackgroundListener = new LineBackgroundListener(){
    @Override
    public void lineGetBackground(LineBackgroundEvent event) {
        int offset = event.lineOffset;
        if(editorSupport.isInsideReadOnlyBlock(offset, 0)){
            event.lineBackground = colorManager.getColor(new RGB(245,245,245));
        }
    }
};
//Install line background color listener on source viewer 
getSourceViewer().getTextWidget().addLineBackgroundListener(lineBackgroundListener);


Result of installing above listener


Untitled


You can see grey background for the read-only code section ( highlighted with red rectangle in above image).


Complete XMLEditor implementation



package sample.xml.editor.editors;
 
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IEventConsumer;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
 
public class XMLEditor extends TextEditor {
 
    private ColorManager colorManager;
    public CustomEditorSupport editorSupport = null;
 
    public XMLEditor() {
        super();
        editorSupport = new CustomEditorSupport(this);
        colorManager = new ColorManager();
        setSourceViewerConfiguration(new XMLConfiguration(colorManager));
        setDocumentProvider(new XMLDocumentProvider());
    }
    public void dispose() {
        colorManager.dispose();
        super.dispose();
    }
    
    public IDocument getDocument() {
        IDocumentProvider documentProvider = getDocumentProvider();
        if (documentProvider != null) {
            return documentProvider.getDocument(getEditorInput());
        }
        return null;
    }
    
    @Override
    public void createPartControl(Composite parent) {
        super.createPartControl(parent);
        /* Install Keyboard event consumer to disable any editing  read only
         * code section */
        getSourceViewer().setEventConsumer(new IEventConsumer() {
            @Override
            public void processEvent(VerifyEvent event) {
                int offset = event.start;
                if(editorSupport.isInsideReadOnlyBlock(offset, 0)){
                    // swallow the event.
                    event.doit = false;
                    return;
                }
            }
        });
        
        LineBackgroundListener lineBackgroundListener = new LineBackgroundListener(){
            @Override
            public void lineGetBackground(LineBackgroundEvent event) {
                int offset = event.lineOffset;
                if(editorSupport.isInsideReadOnlyBlock(offset, 0)){
                    event.lineBackground = colorManager.getColor(new RGB(245,245,245));
                }
            }
        };
        //Install line background color listener on source viewer 
        getSourceViewer().getTextWidget().addLineBackgroundListener(lineBackgroundListener);
    }
 
}

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester