1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IMarker; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IWorkspaceRoot; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.debug.core.DebugPlugin; 25 import org.eclipse.debug.core.IBreakpointManager; 26 import org.eclipse.debug.core.model.IBreakpoint; 27 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 28 import org.eclipse.jface.action.Action; 29 import org.eclipse.jface.text.BadLocationException; 30 import org.eclipse.jface.text.IDocument; 31 import org.eclipse.jface.text.IRegion; 32 import org.eclipse.jface.text.ITextSelection; 33 import org.eclipse.jface.text.Position; 34 import org.eclipse.jface.text.TextSelection; 35 import org.eclipse.jface.text.source.IAnnotationModel; 36 import org.eclipse.jface.text.source.IVerticalRulerInfo; 37 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel; 38 import org.eclipse.ui.texteditor.IDocumentProvider; 39 import org.eclipse.ui.texteditor.ITextEditor; 40 41 public class ManageBreakpointRulerAction extends Action { 42 43 private IVerticalRulerInfo fRuler; 44 private ITextEditor fTextEditor; 45 private ToggleBreakpointAdapter fBreakpointAdapter; 46 47 public ManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor) { 48 super(ActionMessages.getString("ManageBreakpointRulerAction.label")); fRuler= ruler; 50 fTextEditor= editor; 51 fBreakpointAdapter = new ToggleBreakpointAdapter(); 52 } 53 54 57 public void dispose() { 58 fTextEditor = null; 59 fRuler = null; 60 } 61 62 67 protected IVerticalRulerInfo getVerticalRulerInfo() { 68 return fRuler; 69 } 70 71 76 protected ITextEditor getTextEditor() { 77 return fTextEditor; 78 } 79 80 85 protected IDocument getDocument() { 86 IDocumentProvider provider= fTextEditor.getDocumentProvider(); 87 return provider.getDocument(fTextEditor.getEditorInput()); 88 } 89 90 93 public void run() { 94 try { 95 List list = getMarkers(); 96 if (list.isEmpty()) { 97 IDocument document= getDocument(); 99 int lineNumber= getVerticalRulerInfo().getLineOfLastMouseButtonActivity(); 100 if (lineNumber >= document.getNumberOfLines()) { 101 return; 102 } 103 try { 104 IRegion line= document.getLineInformation(lineNumber); 105 ITextSelection selection = new TextSelection(document, line.getOffset(), line.getLength()); 106 fBreakpointAdapter.toggleLineBreakpoints(fTextEditor, selection); 107 } catch (BadLocationException e) { 108 } 110 } else { 111 IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager(); 113 Iterator iterator = list.iterator(); 114 while (iterator.hasNext()) { 115 IMarker marker = (IMarker) iterator.next(); 116 IBreakpoint breakpoint = manager.getBreakpoint(marker); 117 if (breakpoint != null) { 118 breakpoint.delete(); 119 } 120 } 121 } 122 } catch (CoreException e) { 123 JDIDebugUIPlugin.errorDialog(ActionMessages.getString("ManageBreakpointRulerAction.error.adding.message1"), e); } 125 } 126 127 132 protected List getMarkers() { 133 134 List breakpoints= new ArrayList (); 135 136 IResource resource= ToggleBreakpointAdapter.getResource(fTextEditor); 137 IDocument document= getDocument(); 138 AbstractMarkerAnnotationModel model= getAnnotationModel(); 139 140 if (model != null) { 141 try { 142 143 IMarker[] markers= null; 144 if (resource instanceof IFile) 145 markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE); 146 else { 147 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 148 markers= root.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE); 149 } 150 151 if (markers != null) { 152 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager(); 153 for (int i= 0; i < markers.length; i++) { 154 IBreakpoint breakpoint= breakpointManager.getBreakpoint(markers[i]); 155 if (breakpoint != null && breakpointManager.isRegistered(breakpoint) && 156 includesRulerLine(model.getMarkerPosition(markers[i]), document)) 157 breakpoints.add(markers[i]); 158 } 159 } 160 } catch (CoreException x) { 161 JDIDebugUIPlugin.log(x.getStatus()); 162 } 163 } 164 return breakpoints; 165 } 166 167 172 protected AbstractMarkerAnnotationModel getAnnotationModel() { 173 IDocumentProvider provider= fTextEditor.getDocumentProvider(); 174 IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput()); 175 if (model instanceof AbstractMarkerAnnotationModel) { 176 return (AbstractMarkerAnnotationModel) model; 177 } 178 return null; 179 } 180 181 188 protected boolean includesRulerLine(Position position, IDocument document) { 189 190 if (position != null) { 191 try { 192 int markerLine= document.getLineOfOffset(position.getOffset()); 193 int line= fRuler.getLineOfLastMouseButtonActivity(); 194 if (line == markerLine) { 195 return true; 196 } 197 } catch (BadLocationException x) { 198 } 199 } 200 201 return false; 202 } 203 } 204 | Popular Tags |