Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Eclipse Plug-in Development FAQ

Revision as of 16:13, 7 January 2008 by Remy.suen.gmail.com (Talk | contribs) (How do I change the editor that is being opened when a marker has been opened?: Introduce a question about responding to marker events.)

This page is a collection of FAQs that is intended to help a developer write Eclipse plug-ins.

This FAQ is intended to be complimentary to the Official Eclipse FAQ and RCP FAQ wiki pages. If you cannot find your question here, you should try checking those other two pages.

General Development

How do I find a class from Eclipse?

See here.

I see these $NON-NLS-1$ tags all over the place when I'm browsing Eclipse's source code? What do they mean?

They are meant to mark a string literal as not needing to be externalized (as in, translated / localized). You will often see something like...

if (string.equals("")) { //$NON-NLS-1$
    // do stuff
}

...this would be a scenario where a string wouldn't need to be localized because it is a string for the code to "manipulate", per se, over it being part of a message to the user at the UI level.

I need help debugging my plug-in...

Are you getting errors like "Unhandled loop event exception" messages in your console with nothing useful after it? Make sure you have -consoleLog as a Program Argument in your launch configuration. You may also want to take a look at these runtime tools.

I'm using third party jar files and my plug-in is not working...

Did you add those jars to your project's Java Build Path? Do not use that project property page when developing plug-ins. You have two options.

Option 1: turn the jars into plug-ins Use New > Project > Plug-in Development > Plug-in from existing JAR archive. That will turn one or more jar files into a single jar plug-in. For something like log4j you can then set up Buddy-Classloading, etc.

Prior to 3.2.1, you had to make modifications to the build.properties file. See bug 146042 (RCP export has problems with required plug-ins).

Option 2: include the jars in a plug-in

  1. Use Import>File System to import the jar files into your plug-in project, say in the <project>/lib directory.
  2. Use "Add..." to add the jars to the classpath section of the PDE Editor>Runtime tab.
  3. Use "New..." to add "." library back (with no quotes, of course). Some versions of eclipse automatically do this for you.
  4. Make sure your binary build exports the new jar files on the PDE Editor>Build tab.
  5. Save
  6. On the project, use the popup menu>PDE Tools>Update Classpath to correctly add the jars to the eclipse project classpath.
  7. Export any packages that you need to using the PDE Editor>Runtime tab
  8. Save

Check out bug 108781.

It talks about how adding a 3rd party jar removes the default "." classpath, and the need to add it back.

Also, Eclipse can handle jars within jars. It expands them into a temporary location during runtime.

User Interface

There's a view / editor that I want to model. How do I find out how it was designed?

Views and editors generally extend ViewPart and EditorPart respectively. Placing a breakpoint in its constructor when you show the view or editor will help you figure out what the class is composed of.

There's a preference / property page that I want to model. How do I find out how they designed it?

Put a breakpoint in the constructors of the PreferencePage / PropertyPage class. Open the preferences / properties dialog, and then select the page you are interested about. Now you can identify which class is constructing that page based on the stack trace.

There's a window / dialog / popup that I want to model. How do I find out how it was designed?

There are two usual suspects, an SWT Shell or a JFace Window. Generally, most developers subclass's JFace's Dialog class (which is a subclass of Window) for their dialog needs. So you should first try and put a breakpoint in Window's open() method and see if the window you're trying to model stops at that breakpoint when it has been opened (shown). If not, try Shell's open() or setVisible(boolean) methods.

There's a wizard page that I want to model. How do I find out how they designed it?

Wizard pages usually extend the WizardPage class. Putting a breakpoint in its constructor will help you identify the class that is creating that page.

How can I leverage the 'Outline' view?

In your IWorkbenchPart's implementation, you should override the getAdapter(Class) method and return your own implementation of IContentOutlinePage or you can choose to subclass ContentOutlinePage.

public Object getAdapter(Class adapter) {
    if (adapter.equals(IContentOutlinePage.class)) {
        return page;
    }
    // do NOT forget this line
    return super.getAdapter(adapter);
}

Editor

How do I add those rectangles in my source editor like what JDT does for parameter names during code completion?

To achieve this, you will need to use a LinkedModeModel and a LinkedModeUI. Open the 'Call Hierarchy' on either of the two constructors to find out how to use those two classes.

How do I implement a 'Quick Outline' for my editor?

JDT's implementing class is named 'org.eclipse.jdt.internal.ui.text.JavaOutlineInformationControl', you should take a look at that.

How do I get an editor's StyledText widget?

Since you cannot access the editor's ITextViewer, you will have to try using the code below.

StyledText text = (StyledText) editor.getAdapter(Control.class);

How can I get the IDocument from an editor?

Assuming your editor implements the ITextEditor interface, you can try the code below.

IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());

This code should work on most text editors.

How do I get an IFile given an IEditorPart?

The code below will demonstrate how to do this.

IFile file = (IFile) editorPart.getEditorInput().getAdapter(IFile.class);
if (file != null) {
    // do stuff
}

Note that IFiles are meant to represent files within the workspace and will not work if the file that has been opened is not contained within the workspace.

How do I hide the tabs of a MultiPageEditorPart if it only has one page?

Adding the code below into your subclass should do the trick.

protected void createPages() {
    super.createPages();
    if (getPageCount() == 1) {
        Composite container = getContainer();
        if (container instanceof CTabFolder) {
            ((CTabFolder) container).setTabHeight(0);
        }
    }
}

How do I change the editor that is being opened when a marker has been opened?

You can associate the string ID of your editor onto your marker with IMarker's setAttribute(String, Object) method by using the EDITOR_ID_ATTR string constant defined in the IDE class as the attribute name.

marker.setAttribute(IDE.EDITOR_ID_ATTR, "com.example.xyz.editorID");

How can I make my editor respond a user opening a marker?

When a marker has been opened, the Eclipse Platform tries to help the user out via the IGotoMarker interface. Your editor should either implement the interface or respond to this class by returning an implementation via the getAdapter(Class) method.

public Object getAdapter(Class adapter) {
    if (adapter.equals(IGotoMarker.class)) {
        return gotoMarker;
    }
    return super.getAdapter(adapter);
}

IGotoMarker's gotoMarker(IMarker) method will be called accordingly on the corresponding interface implementation and it is in that method implementation that you can react to a user opening a marker.

Debug

How do I invoke a process and have its output managed by the 'Console' view?

Use DebugPlugin's newProcess(ILaunch, Process, String) or newProcess(ILaunch, Process, String, Map) method. You will probably be calling this in your ILaunchConfigurationDelegate implementation.

How do I associate my executed process with its command line counterpart?

Your IProcess implementation must return a valid string that corresponds to the IProcess.ATTR_CMDLINE attribute. The sample code below will demonstrate how this is done with the DebugPlugin's newProcess(ILaunch, Process, String, Map) method.

String commandLine = "/usr/bin/make";
Map attributes = new HashMap();
attributes.put(IProcess.ATTR_CMDLINE, commandLine);
Process process = Runtime.getRuntime().exec(commandLine);
// this assumes that 'launch' is a non-null reference to an ILaunch implementation
DebugPlugin.newProcess(launch, process, "make", attributes);

Back to the top