Tips on developing Eclipse plugins – II.

Here we go again :) In this article I try to shed some light on:

  • Dialogs
  • Compound document operations
  • Markers

Simple dialogs

– also see JavaDoc

Sometimes there is need to show simple messages to user. There are some nice functions in MessageDialog class. Example of an error message:
[java]
MessageDialog.openError(
shell /* e.g. editor.getSite().getShell()*/,
“Dialog title”,
“Error message”
);
[/java]

Example of confirmation dialog (with yes/no buttons):
[java]
if (MessageDialog.openConfirm(shell, title, question) == true) {
//user answered yes
} else {
//user answered no
}
[/java]

More complex dialogs

To create more complex dialogs simply extend the org.eclipse.jface.dialogs.Dialog class and:

  • override createDialogArea() and create dialog’s controls there
  • override configureShell() and set dialog’s title there
  • override createButtonsForButtonBar() and create dialog’s buttons there using createButton()
  • override buttonPressed() and setReturnCode() according to pressed button (also close() dialog if needed)
  • use setBlockOnOpen(true) in constructor for creating modal dialog

Compound document operations

To make Eclipse consider more operations as one compound operation (useful for more intuitive undo etc.) use the following code:

[java]
DocumentRewriteSession drws = null;
try {
if (document instanceof IDocumentExtension4) {
drws = ((IDocumentExtension4)document).startRewriteSession(
DocumentRewriteSessionType.UNRESTRICTED
);
}
//…modify document with multiple operations here…
finally {
if (document instanceof IDocumentExtension4 && drws != null) {
((IDocumentExtension4)document).stopRewriteSession(drws);
}
}
[/java]

Markers

– also see Tutorial, FAQ, FAQ2, FAQ3

Markers are means of highlighting parts of text in editors and can be also shown in Problems view. For creating custom markers define it in plugin.xml first:

[xml]



[/xml]

Next step is to create marker:

[java]
HashMap map = new HashMap();
MarkerUtilities.setLineNumber(map, lineNumber+1); //1-based line numbering
MarkerUtilities.setMessage(map, “This is some sample warning.”);
map.put(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
try {
MarkerUtilities.createMarker(f, map, “myproblem”);
} catch (CoreException e) {
//something went terribly wrong
}
[/java]

The above code creates marker for the whole line. To have more precious positioning add:
[java]
MarkerUtilities.setCharStart(map, offset);
MarkerUtilities.setCharEnd(map, offset + length);
[/java]

To get all markers of our newly specified type (and subtypes) for specified IResource (e.g. IFile) use:
[java]
myMarkers = file.findMarkers(
“myproblem”,
true,
IResource.DEPTH_INFINITE
);
[/java]

Finally, to delete all markers of specified type use:

[java]
try {
file.deleteMarkers(“myproblem”, true, IResource.DEPTH_INFINITE);
} catch (CoreException e) {
//…something is wrong
}
[/java]

If you create editor with custom SourceViewerConfiguration be sure to extend TextSourceViewerConfiguration to get marker tooltips support for free (otherwise your marker tooltips won’t be displayed without some additional coding).

Tags: , , , , ,

Comments are closed.