1 11 package org.eclipse.debug.ui.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IAdapterManager; 15 import org.eclipse.core.runtime.Platform; 16 import org.eclipse.debug.internal.ui.DebugUIPlugin; 17 import org.eclipse.debug.internal.ui.actions.ActionMessages; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.IRegion; 22 import org.eclipse.jface.text.ITextSelection; 23 import org.eclipse.jface.text.TextSelection; 24 import org.eclipse.jface.text.source.IVerticalRulerInfo; 25 import org.eclipse.ui.IWorkbenchPart; 26 import org.eclipse.ui.texteditor.IDocumentProvider; 27 import org.eclipse.ui.texteditor.ITextEditor; 28 import org.eclipse.ui.texteditor.IUpdate; 29 30 41 public class ToggleBreakpointAction extends Action implements IUpdate { 42 43 private IWorkbenchPart fPart; 44 private IDocument fDocument; 45 private IVerticalRulerInfo fRulerInfo; 46 47 58 public ToggleBreakpointAction(IWorkbenchPart part, IDocument document, IVerticalRulerInfo rulerInfo) { 59 super(ActionMessages.ToggleBreakpointAction_0); 60 fPart = part; 61 fDocument = document; 62 fRulerInfo = rulerInfo; 63 } 64 68 public void run() { 69 IDocument document= getDocument(); 70 if (document == null) { 71 return; 72 } 73 IToggleBreakpointsTarget adapter = (IToggleBreakpointsTarget) fPart.getAdapter(IToggleBreakpointsTarget.class); 74 if (adapter == null) { 75 IAdapterManager manager = Platform.getAdapterManager(); 77 if (manager.hasAdapter(fPart, IToggleBreakpointsTarget.class.getName())) { 78 adapter = (IToggleBreakpointsTarget) manager.loadAdapter(fPart, IToggleBreakpointsTarget.class.getName()); 79 } 80 } 81 if (adapter == null) { 82 return; 83 } 84 int line = fRulerInfo.getLineOfLastMouseButtonActivity(); 85 86 if (line == -1) 88 return; 89 90 94 if (line >= document.getNumberOfLines()) 95 return; 96 97 try { 98 IRegion region = document.getLineInformation(line); 99 ITextSelection selection = new TextSelection(document, region.getOffset(), 0); 100 if (adapter instanceof IToggleBreakpointsTargetExtension) { 101 IToggleBreakpointsTargetExtension extension = (IToggleBreakpointsTargetExtension) adapter; 102 if (extension.canToggleBreakpoints(fPart, selection)) { 103 extension.toggleBreakpoints(fPart, selection); 104 return; 105 } 106 } 107 if (adapter.canToggleLineBreakpoints(fPart, selection)) { 108 adapter.toggleLineBreakpoints(fPart, selection); 109 } else if (adapter.canToggleWatchpoints(fPart, selection)) { 110 adapter.toggleWatchpoints(fPart, selection); 111 } else if (adapter.canToggleMethodBreakpoints(fPart, selection)) { 112 adapter.toggleMethodBreakpoints(fPart, selection); 113 } 114 } catch (BadLocationException e) { 115 reportException(e); 116 } catch (CoreException e) { 117 reportException(e); 118 } 119 } 120 121 126 private void reportException(Exception e) { 127 DebugUIPlugin.errorDialog(fPart.getSite().getShell(), ActionMessages.ToggleBreakpointAction_1, ActionMessages.ToggleBreakpointAction_2, e); } 129 130 134 public void dispose() { 135 fDocument = null; 136 fPart = null; 137 fRulerInfo = null; 138 } 139 140 145 private IDocument getDocument() { 146 if (fDocument != null) 147 return fDocument; 148 149 if (fPart instanceof ITextEditor) { 150 ITextEditor editor= (ITextEditor)fPart; 151 IDocumentProvider provider = editor.getDocumentProvider(); 152 if (provider != null) 153 return provider.getDocument(editor.getEditorInput()); 154 } 155 156 IDocument doc = (IDocument) fPart.getAdapter(IDocument.class); 157 if (doc != null) { 158 return doc; 159 } 160 161 return null; 162 } 163 164 167 public void update() { 168 IDocument document= getDocument(); 169 if (document != null) { 170 IToggleBreakpointsTarget adapter = (IToggleBreakpointsTarget) fPart.getAdapter(IToggleBreakpointsTarget.class); 171 if (adapter == null) { 172 IAdapterManager manager = Platform.getAdapterManager(); 174 if (manager.hasAdapter(fPart, IToggleBreakpointsTarget.class.getName())) { 175 adapter = (IToggleBreakpointsTarget) manager.loadAdapter(fPart, IToggleBreakpointsTarget.class.getName()); 176 } 177 } 178 if (adapter != null) { 179 int line = fRulerInfo.getLineOfLastMouseButtonActivity(); 180 184 if (line > -1 & line < document.getNumberOfLines()) { 185 try { 186 IRegion region = document.getLineInformation(line); 187 ITextSelection selection = new TextSelection(document, region.getOffset(), 0); 188 if (adapter instanceof IToggleBreakpointsTargetExtension) { 189 IToggleBreakpointsTargetExtension extension = (IToggleBreakpointsTargetExtension) adapter; 190 if (extension.canToggleBreakpoints(fPart, selection)) { 191 setEnabled(true); 192 return; 193 } 194 } 195 if (adapter.canToggleLineBreakpoints(fPart, selection) | 196 adapter.canToggleWatchpoints(fPart, selection) | 197 adapter.canToggleMethodBreakpoints(fPart, selection)) { 198 setEnabled(true); 199 return; 200 } 201 } catch (BadLocationException e) { 202 reportException(e); 203 } 204 } 205 } 206 } 207 setEnabled(false); 208 } 209 210 } 211 | Popular Tags |