jseashell
jseashell

Reputation: 755

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();
    }
}

Upvotes: 2

Views: 252

Answers (2)

Rajan
Rajan

Reputation: 73

You could have got contents of your editor input in init() method of your editor . And you could have manipulated the text of your editor input according to your decryption

Upvotes: 0

jseashell
jseashell

Reputation: 755

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;
    }
}

Upvotes: 2

Related Questions