KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > BreakpointMarkerUpdater


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12
13 import org.eclipse.core.resources.IMarker;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.IBreakpointManager;
17 import org.eclipse.debug.core.model.IBreakpoint;
18 import org.eclipse.jdt.core.dom.AST;
19 import org.eclipse.jdt.core.dom.ASTParser;
20 import org.eclipse.jdt.core.dom.CompilationUnit;
21 import org.eclipse.jdt.debug.core.IJavaPatternBreakpoint;
22 import org.eclipse.jdt.debug.core.IJavaStratumLineBreakpoint;
23 import org.eclipse.jdt.internal.debug.ui.actions.ValidBreakpointLocationLocator;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.Position;
27 import org.eclipse.ui.texteditor.IMarkerUpdater;
28
29 /**
30  * This class provides a mechanism to correct the placement of a
31  * breakpoint marker when the related document is edited.
32  *
33  * This updater is used to cover the line number discrepency cases that <code>BasicMarkerUpdater</code> does not:
34  * <ul>
35  * <li>If you insert a blank line at the start of the line of code, the breakpoint
36  * is moved from the blank line to the next viable line down,
37  * following the same breakpoint placement rules as creating a breakpoint</li>
38  *
39  * <li>If you select the contents of an entire line and delete them
40  * (leaving the line blank), the breakpoint is moved to the next viable line down,
41  * following the same breakpoint placement rules as creating a breakpoint</li>
42  *
43  * <li>If the breakpoint is on the last viable line of a class file and the line is removed via either of
44  * the aforementioned deletion cases, the breakpoint is removed</li>
45  *
46  * <li>In the general deletion case if a valid breakpoint location can not be determined, it is removed</li>
47  * </ul>
48  *
49  * @since 3.3
50  */

51 public class BreakpointMarkerUpdater implements IMarkerUpdater {
52
53     public BreakpointMarkerUpdater() {}
54
55     /* (non-Javadoc)
56      * @see org.eclipse.ui.texteditor.IMarkerUpdater#getAttribute()
57      */

58     public String JavaDoc[] getAttribute() {
59         return new String JavaDoc[] {IMarker.LINE_NUMBER};
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.ui.texteditor.IMarkerUpdater#getMarkerType()
64      */

65     public String JavaDoc getMarkerType() {
66         return "org.eclipse.debug.core.breakpointMarker"; //$NON-NLS-1$
67
}
68
69     /* (non-Javadoc)
70      * @see org.eclipse.ui.texteditor.IMarkerUpdater#updateMarker(org.eclipse.core.resources.IMarker, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.Position)
71      */

72     public boolean updateMarker(IMarker marker, IDocument document, Position position) {
73         if(position.isDeleted()) {
74             return false;
75         }
76         IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager();
77         IBreakpoint breakpoint = manager.getBreakpoint(marker);
78         if (breakpoint instanceof IJavaStratumLineBreakpoint || breakpoint instanceof IJavaPatternBreakpoint) {
79             return true;
80         }
81         ASTParser parser = ASTParser.newParser(AST.JLS3);
82         parser.setSource(document.get().toCharArray());
83         CompilationUnit unit = (CompilationUnit) parser.createAST(null);
84         if(unit != null) {
85             try {
86                 ValidBreakpointLocationLocator loc = new ValidBreakpointLocationLocator(unit, document.getLineOfOffset(position.getOffset())+1, true, true);
87                 unit.accept(loc);
88                 if(loc.getLocationType() == ValidBreakpointLocationLocator.LOCATION_NOT_FOUND) {
89                     return false;
90                 }
91                 //if the line number is already good, perform no resource updating
92
if(marker.getAttribute(IMarker.LINE_NUMBER, -1) == loc.getLineLocation()) {
93                     return true;
94                 }
95                 marker.setAttribute(IMarker.LINE_NUMBER, loc.getLineLocation());
96                 return true;
97             }
98             catch (BadLocationException e) {JDIDebugUIPlugin.log(e);}
99             catch (CoreException e) {JDIDebugUIPlugin.log(e);}
100         }
101         return false;
102     }
103 }
104
Popular Tags