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.ant.internal.ui.editor.actions;12 13 import org.eclipse.ant.internal.ui.AntUIPlugin;14 import org.eclipse.ant.internal.ui.debug.IAntDebugConstants;15 import org.eclipse.ant.internal.ui.debug.model.AntLineBreakpoint;16 import org.eclipse.core.resources.IFile;17 import org.eclipse.core.resources.IResource;18 import org.eclipse.core.runtime.CoreException;19 import org.eclipse.debug.core.DebugPlugin;20 import org.eclipse.debug.core.model.IBreakpoint;21 import org.eclipse.jface.action.Action;22 import org.eclipse.jface.text.BadLocationException;23 import org.eclipse.jface.text.IDocument;24 import org.eclipse.jface.text.Position;25 import org.eclipse.jface.text.source.IAnnotationModel;26 import org.eclipse.jface.text.source.IVerticalRulerInfo;27 import org.eclipse.ui.IEditorInput;28 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;29 import org.eclipse.ui.texteditor.IDocumentProvider;30 import org.eclipse.ui.texteditor.ITextEditor;31 import org.eclipse.ui.texteditor.IUpdate;32 33 public abstract class AbstractBreakpointRulerAction extends Action implements IUpdate {34 35 private IVerticalRulerInfo fInfo;36 37 private ITextEditor fTextEditor;38 39 private IBreakpoint fBreakpoint;40 41 protected IBreakpoint determineBreakpoint() {42 IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL);43 for (int i= 0; i < breakpoints.length; i++) {44 IBreakpoint breakpoint= breakpoints[i];45 if (breakpoint instanceof AntLineBreakpoint) {46 AntLineBreakpoint antBreakpoint= (AntLineBreakpoint)breakpoint;47 try {48 if (breakpointAtRulerLine(antBreakpoint)) {49 return breakpoint;50 }51 } catch (CoreException ce) {52 AntUIPlugin.log(ce);53 continue;54 }55 }56 }57 return null;58 }59 60 protected IVerticalRulerInfo getInfo() {61 return fInfo;62 }63 64 protected void setInfo(IVerticalRulerInfo info) {65 fInfo = info;66 }67 68 protected ITextEditor getTextEditor() {69 return fTextEditor;70 }71 72 protected void setTextEditor(ITextEditor textEditor) {73 fTextEditor = textEditor;74 }75 76 /** 77 * Returns the resource for which to create the marker, 78 * or <code>null</code> if there is no applicable resource.79 *80 * @return the resource for which to create the marker or <code>null</code>81 */82 protected IResource getResource() {83 IEditorInput input= fTextEditor.getEditorInput();84 IResource resource= (IResource) input.getAdapter(IFile.class);85 if (resource == null) {86 resource= (IResource) input.getAdapter(IResource.class);87 } 88 return resource;89 }90 91 protected boolean breakpointAtRulerLine(AntLineBreakpoint antBreakpoint) throws CoreException {92 AbstractMarkerAnnotationModel model = getAnnotationModel();93 if (model != null) {94 Position position= model.getMarkerPosition(antBreakpoint.getMarker());95 if (position != null) {96 IDocumentProvider provider= getTextEditor().getDocumentProvider();97 IDocument doc= provider.getDocument(getTextEditor().getEditorInput());98 try {99 int markerLineNumber= doc.getLineOfOffset(position.getOffset());100 int rulerLine= getInfo().getLineOfLastMouseButtonActivity();101 if (rulerLine == markerLineNumber) {102 if (getTextEditor().isDirty()) {103 return antBreakpoint.getLineNumber() == markerLineNumber + 1;104 }105 return true;106 }107 } catch (BadLocationException x) {108 }109 }110 }111 112 return false;113 }114 115 protected IBreakpoint getBreakpoint() {116 return fBreakpoint;117 }118 119 protected void setBreakpoint(IBreakpoint breakpoint) {120 fBreakpoint = breakpoint;121 }122 123 /**124 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.125 *126 * @return the marker annotation model127 */128 protected AbstractMarkerAnnotationModel getAnnotationModel() {129 IDocumentProvider provider= fTextEditor.getDocumentProvider();130 IAnnotationModel model= provider.getAnnotationModel(getTextEditor().getEditorInput());131 if (model instanceof AbstractMarkerAnnotationModel) {132 return (AbstractMarkerAnnotationModel) model;133 }134 return null;135 }136 }137