2

i have this problem with my project these days. i'm developing a plugin in eclipse, i need to write a text on the active window(coding area) when i click a button.

i use the following code in my button.java class

public class Button implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
/**
 * The constructor.
 */
public Button() {
}

/**
 * The action has been activated. The argument of the
 * method represents the 'real' action sitting
 * in the workbench UI.
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
    MessageDialog.openInformation(
        window.getShell(),
        "Button",
        "Code of the Button goes here");
}

how can i do it inside the run method? here I'm displaying a message, instead of showing a message i want to display some text in the text editor pane. please help me to achieve this.

if you guys can please give me some links to understand about eclipse plug-in developments? any blog posts that are easy to understand will be much better?

2 Answers 2

1

You should do something like this. It is completely untested and you will need to add lots of null checks and try-catch blocks, but the code below gets the currently active editor and replaces the current selection with whatever is passed in as an argument:

void method (String text) {
    IEditorPart part = Workbench.getInstance().getWorkbenchWindows()[0].getActivePage().getActiveEditor();
    IEditorInput editorInput = part.getEditorInput();
    if (part instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) part;
        IDocument doc = textEditor.getDocumentProvider().getDocument(editorInput);
        ITextSelection sel = textEditor.getSelectionProvider().getSelection();
        doc.replace(sel.getOffset(), sel.getLength(), text);
    }
}

It is messy and complicated, but that's the Eclipse framework for you.

This might be a good place for you to look at Eclipse plugin development: http://www.ibm.com/developerworks/views/opensource/libraryview.jsp?search_by=Create+commercial-quality+eclipse+ide

Developer Works in general has a lot of good content on Eclipse, so if this series is not exactly what you need, you can explore Developer Works for other things.

Sign up to request clarification or add additional context in comments.

1 Comment

I tested above code and it is exactly what I want. Thanks Andrew.
0

I'd recommend this one. It is a very good introductory tutorial

1 Comment

thank you , i have already gone through this one... but thanx for sharing it... what i really need is to achieve the task I've mentioned above... ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.