Skip to content

Some Eclipse Tips & Tricks

Ori Roth edited this page Apr 2, 2017 · 3 revisions

Eclipse Helpful and life saving Tips & Tricks:

appendix - http://gnuarmeclipse.github.io/developer/eclipse/tips-tricks/

Display a message box from a non-UI thread:

class ErrorMessageDialog implements Runnable {
    public int retCode = 0;
    @Override
    public void run() {
        String[] buttons = new String[] { "Retry", "Ignore", "Abort" };
        MessageDialog dialog = new MessageDialog(shell,
                "Read error", null, sourceUrl.toString() + "\n"
                        + e.getMessage(), MessageDialog.ERROR,
                buttons, 0);
        retCode = dialog.open();
    }
}
ErrorMessageDialog messageDialog = new ErrorMessageDialog();
Display.getDefault().syncExec(messageDialog);
if (messageDialog.retCode == 2) {
    throw e; // Abort
} else if (messageDialog.retCode == 1) {
    return false; // Ignore
}

Run code in UI thread:

Display.getDefault().asyncExec(new Runnable() {
    public void run() {
        // Do something
    }
});

Get the path of folder where eclipse is installed:

URL url = Platform.getInstallLocation().getURL(); 
IPath path = new Path(url.getPath());

Get the path of a resource in another plug-in:

Bundle bundle = Platform.getBundle(bundleId);
URL url = FileLocator.find(bundle, new Path(relativePath), null);
String location;
try {
    location = FileLocator.resolve(url).getPath();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Global markers - using the workspace root as the resource:

public static void reportInfo(String message) {
try {
  IMarker marker = ResourcesPlugin.getWorkspace().getRoot()
            .createMarker(IMarker.PROBLEM);
  marker.setAttribute(IMarker.MESSAGE, message);
  marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
  marker.setAttribute(IMarker.LOCATION, "-");
  } catch (CoreException e) {
    System.out.println(message);
  }
}
Clone this wiki locally