1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.ResourceBundle ; 18 19 import org.osgi.framework.Bundle; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IMarker; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.ILog; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.Status; 29 30 import org.eclipse.swt.widgets.Shell; 31 32 import org.eclipse.jface.text.BadLocationException; 33 import org.eclipse.jface.text.IDocument; 34 import org.eclipse.jface.text.Position; 35 import org.eclipse.jface.text.source.Annotation; 36 import org.eclipse.jface.text.source.IAnnotationAccess; 37 import org.eclipse.jface.text.source.IAnnotationAccessExtension; 38 import org.eclipse.jface.text.source.IAnnotationModel; 39 import org.eclipse.jface.text.source.IVerticalRuler; 40 import org.eclipse.jface.text.source.IVerticalRulerInfo; 41 42 import org.eclipse.jface.dialogs.ErrorDialog; 43 import org.eclipse.ui.IEditorInput; 44 import org.eclipse.ui.IWorkbenchPage; 45 import org.eclipse.ui.PlatformUI; 46 import org.eclipse.ui.ide.IGotoMarker; 47 import org.eclipse.ui.views.markers.MarkerViewUtil; 48 49 50 58 public class SelectMarkerRulerAction extends ResourceAction implements IUpdate { 59 60 61 private IVerticalRulerInfo fRuler; 62 63 private ITextEditor fTextEditor; 64 65 private ResourceBundle fBundle; 66 67 private String fPrefix; 68 69 81 public SelectMarkerRulerAction(ResourceBundle bundle, String prefix, ITextEditor editor, IVerticalRulerInfo ruler) { 82 super(bundle, prefix); 83 fRuler= ruler; 84 fTextEditor= editor; 85 86 fBundle= bundle; 87 fPrefix= prefix; 88 } 89 90 100 public SelectMarkerRulerAction(ResourceBundle bundle, String prefix, IVerticalRuler ruler, ITextEditor editor) { 101 this(bundle, prefix, editor, ruler); 102 } 103 104 107 public void update() { 108 setEnabled(hasMarkers()); 109 } 110 111 114 public void run() { 115 116 IMarker marker= chooseMarker(getMarkers()); 117 if (marker == null) 118 return; 119 120 IWorkbenchPage page= fTextEditor.getSite().getPage(); 121 MarkerViewUtil.showMarker(page, marker, false); 122 123 gotoMarker(marker); 124 } 125 126 private void gotoMarker(IMarker marker) { 127 128 IGotoMarker gotoMarkerAdapter= (IGotoMarker)fTextEditor.getAdapter(IGotoMarker.class); 130 if (gotoMarkerAdapter != null) { 131 gotoMarkerAdapter.gotoMarker(marker); 132 return; 133 } 134 135 int start= MarkerUtilities.getCharStart(marker); 136 int end= MarkerUtilities.getCharEnd(marker); 137 138 boolean selectLine= start < 0 || end < 0; 139 140 IDocumentProvider documentProvider= fTextEditor.getDocumentProvider(); 141 IEditorInput editorInput= fTextEditor.getEditorInput(); 142 143 IAnnotationModel model= documentProvider.getAnnotationModel(editorInput); 145 if (model instanceof AbstractMarkerAnnotationModel) { 146 147 AbstractMarkerAnnotationModel markerModel= (AbstractMarkerAnnotationModel) model; 148 Position pos= markerModel.getMarkerPosition(marker); 149 if (pos != null && !pos.isDeleted()) { 150 start= pos.getOffset(); 152 end= pos.getOffset() + pos.getLength(); 153 } 154 155 if (pos != null && pos.isDeleted()) { 156 return; 158 } 159 } 160 161 IDocument document= documentProvider.getDocument(editorInput); 162 163 if (selectLine) { 164 int line; 165 try { 166 if (start >= 0) 167 line= document.getLineOfOffset(start); 168 else { 169 line= MarkerUtilities.getLineNumber(marker); 170 -- line; 172 } 173 end= start + document.getLineLength(line) - 1; 174 } catch (BadLocationException e) { 175 return; 176 } 177 } 178 179 int length= document.getLength(); 180 if (end - 1 < length && start < length) 181 fTextEditor.selectAndReveal(start, end - start); 182 } 183 184 185 192 protected IMarker chooseMarker(List markers) { 193 194 AbstractMarkerAnnotationModel model= getAnnotationModel(); 195 IAnnotationAccessExtension access= getAnnotationAccessExtension(); 196 197 IMarker marker= null; 198 int maxLayer= 0; 199 200 Iterator iter= markers.iterator(); 201 while (iter.hasNext()) { 202 IMarker m= (IMarker) iter.next(); 203 Annotation a= model.getMarkerAnnotation(m); 204 if (a != null) { 205 if (access == null) { 206 marker= m; 207 break; 208 } 209 int l= access.getLayer(a); 210 if (l == maxLayer) { 211 if (marker == null) 212 marker= m; 213 } else if (l > maxLayer) { 214 maxLayer= l; 215 marker= m; 216 } 217 } 218 } 219 220 return marker; 221 } 222 223 230 protected IAnnotationAccessExtension getAnnotationAccessExtension() { 231 Object adapter= fTextEditor.getAdapter(IAnnotationAccess.class); 232 if (adapter instanceof IAnnotationAccessExtension) 233 return (IAnnotationAccessExtension)adapter; 234 235 return null; 236 } 237 238 244 protected IResource getResource() { 245 IEditorInput input= fTextEditor.getEditorInput(); 246 247 IResource resource= (IResource) input.getAdapter(IFile.class); 248 249 if (resource == null) 250 resource= (IResource) input.getAdapter(IResource.class); 251 252 return resource; 253 } 254 255 260 protected AbstractMarkerAnnotationModel getAnnotationModel() { 261 IDocumentProvider provider= fTextEditor.getDocumentProvider(); 262 IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput()); 263 if (model instanceof AbstractMarkerAnnotationModel) 264 return (AbstractMarkerAnnotationModel) model; 265 return null; 266 } 267 268 273 protected IDocument getDocument() { 274 IDocumentProvider provider= fTextEditor.getDocumentProvider(); 275 return provider.getDocument(fTextEditor.getEditorInput()); 276 } 277 278 285 protected boolean includesRulerLine(Position position, IDocument document) { 286 287 if (position != null) { 288 try { 289 int markerLine= document.getLineOfOffset(position.getOffset()); 290 int line= fRuler.getLineOfLastMouseButtonActivity(); 291 if (line == markerLine) 292 return true; 293 } catch (BadLocationException x) { 296 } 297 } 298 299 return false; 300 } 301 302 311 private boolean includesLine(Position position, IDocument document, int line) { 312 313 if (position != null) { 314 try { 315 int markerLine= document.getLineOfOffset(position.getOffset()); 316 if (line == markerLine) 317 return true; 318 } catch (BadLocationException x) { 321 } 322 } 323 324 return false; 325 } 326 327 334 protected void handleCoreException(CoreException exception, String message) { 335 Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID); 336 ILog log= Platform.getLog(bundle); 337 338 if (message != null) 339 log.log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, message, exception)); 340 else 341 log.log(exception.getStatus()); 342 343 344 Shell shell= fTextEditor.getSite().getShell(); 345 String title= getString(fBundle, fPrefix + "error.dialog.title", fPrefix + "error.dialog.title"); String msg= getString(fBundle, fPrefix + "error.dialog.message", fPrefix + "error.dialog.message"); 348 ErrorDialog.openError(shell, title, msg, exception.getStatus()); 349 } 350 351 357 protected List getMarkers() { 358 final IResource resource= getResource(); 359 if (resource == null || !resource.exists()) 360 return Collections.EMPTY_LIST; 361 362 final IDocument document= getDocument(); 363 if (document == null) 364 return Collections.EMPTY_LIST; 365 366 final AbstractMarkerAnnotationModel model= getAnnotationModel(); 367 if (model == null) 368 return Collections.EMPTY_LIST; 369 370 final IMarker[] allMarkers; 371 try { 372 allMarkers= resource.findMarkers(null, true, IResource.DEPTH_ZERO); 373 } catch (CoreException x) { 374 handleCoreException(x, TextEditorMessages.SelectMarkerRulerAction_getMarker); 375 return Collections.EMPTY_LIST; 376 } 377 378 if (allMarkers.length == 0) 379 return Collections.EMPTY_LIST; 380 381 final int activeLine= fRuler.getLineOfLastMouseButtonActivity(); 382 List markers= null; 383 for (Iterator it= model.getAnnotationIterator(); it.hasNext();) { 384 Annotation annotation= (Annotation) it.next(); 385 if (annotation instanceof MarkerAnnotation) { 386 Position position= model.getPosition(annotation); 387 if (includesLine(position, document, activeLine)) { 388 if (markers == null) 389 markers= new ArrayList (10); 390 391 markers.add(((MarkerAnnotation) annotation).getMarker()); 392 } 393 } 394 } 395 396 if (markers == null) 397 return Collections.EMPTY_LIST; 398 399 return Collections.unmodifiableList(markers); 400 } 401 402 409 protected boolean hasMarkers() { 410 final IResource resource= getResource(); 411 if (resource == null || !resource.exists()) 412 return false; 413 414 final IDocument document= getDocument(); 415 if (document == null) 416 return false; 417 418 final AbstractMarkerAnnotationModel model= getAnnotationModel(); 419 if (model == null) 420 return false; 421 422 final IMarker[] allMarkers; 423 try { 424 allMarkers= resource.findMarkers(null, true, IResource.DEPTH_ZERO); 425 } catch (CoreException x) { 426 handleCoreException(x, TextEditorMessages.SelectMarkerRulerAction_getMarker); 427 return false; 428 } 429 430 if (allMarkers.length == 0) 431 return false; 432 433 final int activeLine= fRuler.getLineOfLastMouseButtonActivity(); 434 for (Iterator it= model.getAnnotationIterator(); it.hasNext();) { 435 Annotation annotation= (Annotation) it.next(); 436 if (annotation instanceof MarkerAnnotation) { 437 Position position= model.getPosition(annotation); 438 if (includesLine(position, document, activeLine)) { 439 return true; 440 } 441 } 442 } 443 444 return false; 445 } 446 } 447 | Popular Tags |