1 /*******************************************************************************2 * Copyright (c) 2000, 2005 IBM Corporation and others.3 * All rights reserved. This program and the accompanying materials4 * are made available under the terms of the Eclipse Public License v1.05 * which accompanies this distribution, and is available at6 * http://www.eclipse.org/legal/epl-v10.html7 * 8 * Contributors:9 * IBM Corporation - initial API and implementation10 *******************************************************************************/11 package org.eclipse.jdt.internal.debug.ui.actions;12 13 14 import org.eclipse.core.resources.IFile;15 import org.eclipse.core.resources.IResource;16 import org.eclipse.core.runtime.CoreException;17 import org.eclipse.debug.core.DebugPlugin;18 import org.eclipse.debug.core.model.IBreakpoint;19 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;20 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;21 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;22 import org.eclipse.jface.action.Action;23 import org.eclipse.jface.text.BadLocationException;24 import org.eclipse.jface.text.IDocument;25 import org.eclipse.jface.text.Position;26 import org.eclipse.jface.text.source.IAnnotationModel;27 import org.eclipse.jface.text.source.IVerticalRulerInfo;28 import org.eclipse.ui.IEditorInput;29 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;30 import org.eclipse.ui.texteditor.IDocumentProvider;31 import org.eclipse.ui.texteditor.ITextEditor;32 import org.eclipse.ui.texteditor.IUpdate;33 34 public abstract class AbstractBreakpointRulerAction extends Action implements IUpdate {35 36 private IVerticalRulerInfo fInfo;37 38 private ITextEditor fTextEditor;39 40 private IBreakpoint fBreakpoint;41 42 protected IBreakpoint determineBreakpoint() {43 IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(JDIDebugPlugin.getUniqueIdentifier());44 for (int i= 0; i < breakpoints.length; i++) {45 IBreakpoint breakpoint= breakpoints[i];46 if (breakpoint instanceof IJavaLineBreakpoint) {47 IJavaLineBreakpoint jBreakpoint= (IJavaLineBreakpoint)breakpoint;48 try {49 if (breakpointAtRulerLine(jBreakpoint)) {50 return jBreakpoint;51 }52 } catch (CoreException ce) {53 JDIDebugUIPlugin.log(ce);54 continue;55 }56 }57 }58 return null;59 }60 61 protected IVerticalRulerInfo getInfo() {62 return fInfo;63 }64 65 protected void setInfo(IVerticalRulerInfo info) {66 fInfo = info;67 }68 69 protected ITextEditor getTextEditor() {70 return fTextEditor;71 }72 73 protected void setTextEditor(ITextEditor textEditor) {74 fTextEditor = textEditor;75 }76 77 /** 78 * Returns the resource for which to create the marker, 79 * or <code>null</code> if there is no applicable resource.80 *81 * @return the resource for which to create the marker or <code>null</code>82 */83 protected IResource getResource() {84 IEditorInput input= fTextEditor.getEditorInput();85 IResource resource= (IResource) input.getAdapter(IFile.class);86 if (resource == null) {87 resource= (IResource) input.getAdapter(IResource.class);88 } 89 return resource;90 }91 92 protected boolean breakpointAtRulerLine(IJavaLineBreakpoint jBreakpoint) throws CoreException {93 AbstractMarkerAnnotationModel model = getAnnotationModel();94 if (model != null) {95 Position position= model.getMarkerPosition(jBreakpoint.getMarker());96 if (position != null) {97 IDocumentProvider provider= getTextEditor().getDocumentProvider();98 IDocument doc= provider.getDocument(getTextEditor().getEditorInput());99 try {100 int markerLineNumber= doc.getLineOfOffset(position.getOffset());101 int rulerLine= getInfo().getLineOfLastMouseButtonActivity();102 if (rulerLine == markerLineNumber) {103 if (getTextEditor().isDirty()) {104 return jBreakpoint.getLineNumber() == markerLineNumber + 1;105 }106 return true;107 }108 } catch (BadLocationException x) {109 }110 }111 }112 113 return false;114 }115 116 protected IBreakpoint getBreakpoint() {117 return fBreakpoint;118 }119 120 protected void setBreakpoint(IBreakpoint breakpoint) {121 fBreakpoint = breakpoint;122 }123 124 /**125 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.126 *127 * @return the marker annotation model128 */129 protected AbstractMarkerAnnotationModel getAnnotationModel() {130 IDocumentProvider provider= fTextEditor.getDocumentProvider();131 IAnnotationModel model= provider.getAnnotationModel(getTextEditor().getEditorInput());132 if (model instanceof AbstractMarkerAnnotationModel) {133 return (AbstractMarkerAnnotationModel) model;134 }135 return null;136 }137 }138