bdfe73d1465889f747644728e8e98a325923755e
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugLineBreakpoint.java
1 /*
2  * Created on 25.11.2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sourceforge.phpeclipse.xdebug.php.model;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
13
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspaceRunnable;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.debug.core.model.LineBreakpoint;
22
23 /**
24  * @author Axel
25  * 
26  * TODO To change the template for this generated type comment go to Window -
27  * Preferences - Java - Code Style - Code Templates
28  */
29 public class XDebugLineBreakpoint extends LineBreakpoint {
30
31         /**
32          * Default constructor is required for the breakpoint manager to re-create
33          * persisted breakpoints. After instantiating a breakpoint, the
34          * <code>setMarker(...)</code> method is called to restore this
35          * breakpoint's attributes.
36          */
37         private static final String XDEBUG_LINE_BREAKPOINT = "net.sourceforge.phpeclipse.xdebug.core.XDebugLineBreakpoint"; //$NON-NLS-1$
38
39         /**
40          * Breakpoint attribute storing the fully qualified name of the type this
41          * breakpoint is located in. (value
42          * <code>"net.sourceforge.phpeclipse.debug.typeName"</code>). This
43          * attribute is a <code>String</code>.
44          */
45         protected static final String TYPE_NAME = "net.sourceforge.phpeclipse.debug.typeName"; //$NON-NLS-1$            
46
47         // public PHPLineBreakpoint() {
48         // }
49         //
50         // public PHPLineBreakpoint(IResource resource, int lineNumber, int
51         // charStart, int charEnd, int hitCount, boolean add, Map attributes) throws
52         // DebugException {
53         // this(resource, lineNumber, charStart, charEnd, hitCount, add, attributes,
54         // PHP_LINE_BREAKPOINT);
55         // }
56         //      
57         // public PHPLineBreakpoint(IResource resource, int lineNumber, int
58         // hitCount, boolean add, Map attributes) throws DebugException {
59         // this(resource, lineNumber, -1, -1, hitCount, add, attributes,
60         // PHP_LINE_BREAKPOINT);
61         // }
62
63         public static final String BREAKPOINT_ID = "XDebugLineBreakpointID";
64
65         public XDebugLineBreakpoint() {
66         }
67
68         /**
69          * Constructs a line breakpoint on the given resource at the given line
70          * number. The line number is 1-based (i.e. the first line of a file is line
71          * number 1).
72          * 
73          * @param resource
74          *            file on which to set the breakpoint
75          * @param lineNumber
76          *            1-based line number of the breakpoint
77          * @throws CoreException
78          *             if unable to create the breakpoint
79          */
80         public XDebugLineBreakpoint(final IResource resource, final int lineNumber)
81                         throws CoreException {
82                 // IMarker marker =
83                 // resource.createMarker("net.sourceforge.phpeclipse.xdebug.core.XDebugLineBreakpoint");
84                 // setMarker(marker);
85                 // setEnabled(true);
86                 // XDebugCorePlugin.log(IStatus.INFO,"Markertype: "+ marker.getType());
87                 // ensureMarker().setAttribute(IMarker.LINE_NUMBER, lineNumber);
88                 // ensureMarker().setAttribute(IBreakpoint.ID,
89                 // IXDebugConstants.ID_PHP_DEBUG_MODEL);
90                 // ensureMarker().setAttribute(BREAKPOINT_ID,-1);
91                 IWorkspaceRunnable wr = new IWorkspaceRunnable() {
92                         public void run(IProgressMonitor monitor) throws CoreException {
93
94                                 // create the marker
95                                 setMarker(resource.createMarker(XDEBUG_LINE_BREAKPOINT));
96
97                                 // add attributes
98                                 Map attributes = new HashMap(10);
99                                 addLineBreakpointAttributes(attributes, getModelIdentifier(),
100                                                 true, lineNumber, -1, -1);
101
102                                 // set attributes
103                                 ensureMarker().setAttributes(attributes);
104
105                                 // add to breakpoint manager if requested
106                                 register(true);
107                         }
108                 };
109                 run(getMarkerRule(resource), wr);
110
111         }
112
113         protected void register(boolean register) throws CoreException {
114                 if (register) {
115                         DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(this);
116                 } else {
117                         setRegistered(false);
118                 }
119         }
120
121         public void addLineBreakpointAttributes(Map attributes,
122                         String modelIdentifier, boolean enabled, int lineNumber,
123                         int charStart, int charEnd) {
124                 attributes.put(IBreakpoint.ID, modelIdentifier);
125                 attributes.put(IBreakpoint.ENABLED, new Boolean(enabled));
126                 attributes.put(IMarker.LINE_NUMBER, new Integer(lineNumber));
127                 if (charStart != -1) {
128                         attributes.put(IMarker.CHAR_START, new Integer(charStart));
129                         attributes.put(IMarker.CHAR_END, new Integer(charEnd));
130                 }
131                 attributes.put(TYPE_NAME, "typeName");
132                 attributes.put(BREAKPOINT_ID, new Integer(-1));
133         }
134
135         /*
136          * (non-Javadoc)
137          * 
138          * @see org.eclipse.debug.core.model.IBreakpoint#getModelIdentifier()
139          */
140         public String getModelIdentifier() {
141                 return IXDebugConstants.ID_PHP_DEBUG_MODEL;
142         }
143
144         public void setID(int id) throws CoreException {
145                 ensureMarker().setAttribute(BREAKPOINT_ID, id);
146         }
147
148         public int getID() throws CoreException {
149                 return ensureMarker().getAttribute(BREAKPOINT_ID, -1);
150         }
151 }