From 6d5083906b87a8f4c9c3f00b69405eb5037f5722 Mon Sep 17 00:00:00 2001 From: Claes Rosell Date: Mon, 23 Oct 2023 09:54:01 +0200 Subject: [PATCH] null guard for AnnotationModel in EditorsUtil --- .../tools/core/ui/utils/EditorUtils.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/org.eclipse.babel.tapiji.tools.core.ui/src/org/eclipse/babel/tapiji/tools/core/ui/utils/EditorUtils.java b/org.eclipse.babel.tapiji.tools.core.ui/src/org/eclipse/babel/tapiji/tools/core/ui/utils/EditorUtils.java index bc4343b..b89ba60 100644 --- a/org.eclipse.babel.tapiji.tools.core.ui/src/org/eclipse/babel/tapiji/tools/core/ui/utils/EditorUtils.java +++ b/org.eclipse.babel.tapiji.tools.core.ui/src/org/eclipse/babel/tapiji/tools/core/ui/utils/EditorUtils.java @@ -14,6 +14,7 @@ import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.source.Annotation; import org.eclipse.jface.text.source.IAnnotationModel; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchPage; @@ -49,8 +50,7 @@ public static IEditorPart openEditor(IWorkbenchPage page, IFile file, String editor, String key) { // open the rb-editor for this file type and selects given msg key IEditorPart part = openEditor(page, file, editor); - if (part instanceof IMessagesEditor) { - IMessagesEditor msgEditor = (IMessagesEditor) part; + if (part instanceof IMessagesEditor msgEditor) { msgEditor.setSelectedKey(key); } return part; @@ -60,13 +60,14 @@ public static void updateMarker(IMarker marker) { FileEditorInput input = new FileEditorInput( (IFile) marker.getResource()); - AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel) getAnnotationModel(marker); IDocument doc = JavaUI.getDocumentProvider().getDocument(input); - - try { - model.updateMarker(doc, marker, getCurPosition(marker, model)); - } catch (CoreException e) { - Logger.logError(e); + AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel) getAnnotationModel(marker); + if ( model != null ) { + try { + model.updateMarker(doc, marker, getCurPosition(marker, model)); + } catch (CoreException e) { + Logger.logError(e); + } } } @@ -79,14 +80,14 @@ public static IAnnotationModel getAnnotationModel(IMarker marker) { private static Position getCurPosition(IMarker marker, IAnnotationModel model) { - Iterator iter = model.getAnnotationIterator(); + Iterator iter = model.getAnnotationIterator(); + Logger.logInfo("Updates Position!"); while (iter.hasNext()) { - Object curr = iter.next(); - if (curr instanceof SimpleMarkerAnnotation) { - SimpleMarkerAnnotation annot = (SimpleMarkerAnnotation) curr; - if (marker.equals(annot.getMarker())) { - return model.getPosition(annot); + Annotation annotation = iter.next(); + if (annotation instanceof SimpleMarkerAnnotation simpleMarkerAnnotation ) { + if (marker.equals(simpleMarkerAnnotation.getMarker())) { + return model.getPosition(simpleMarkerAnnotation); } } }