j.seashell j.seashell - 3 months ago 7
Java Question

Process a file before opening in custom Eclipse text editor plugin

I'm creating a plugin to edit encrypted contents of a

run.properties
file. I've got the editor window able to open properties files, but I'm unsure how to implement the code to process (read decrypt) the selected file. I need to get my selected file, use my decryption code to get it to readable plain-text, then open the file in the editor window

Currently, I have my class
TextEditor
extending
AbstractTextEditor
. My
ISelection
object is null in the following code snippet

public class TextEditor extends AbstractTextEditor
{
public TextEditor()
{
super();

setSourceViewerConfiguration(new TextSourceViewerConfiguration());
setDocumentProvider(new TextFileDocumentProvider());

ISelection selection = doGetSelection();
}

@Override
public void dispose()
{
super.dispose();
}
}

Answer

I found the answer to my question. TextFileDocumentProdiver extends FileDocumentProvider which has a method named createDocument().

I created MyDocumentProvider to extend FileDocumentProvider and override the createDocument() method. Here is the code

/**
 * Class to set up editor
 */
public class MyEditor extends TextEditor
{
    public MyEditor()
    {
        super();
        setSourceViewerConfiguration(new TextSourceViewerConfiguration());
        setDocumentProvider(new MyDocumentProvider());
    }

    @Override
    public void dispose()
    {
        super.dispose();
    }
}

/**
 * Class for document provider
 */
public class MyDocumentProvider extends FileDocumentProvider
{
    @Override
    protected IDocument createDocument(Object selectedFile) throws CoreException
    {
        IDocument doc = super.createDocument(selectedFile);
        if(doc != null)
        {
            // Manipulate document with my convenience method
            String manipulatedText = manipulate(doc);

            // Set the text of the displayed document
            doc.set(manipulatedText);
        }

        return doc;
    }
}
Comments