thakshi
thakshi

Reputation: 21

plugin development in eclipse

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?

Upvotes: 2

Views: 490

Answers (2)

Andrew Eisenberg
Andrew Eisenberg

Reputation: 28757

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.

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80176

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

Upvotes: 0

Related Questions