1) Added attributes and methods for conditional breakpoints and skip/hit count.
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / breakpoints / PHPBreakpoint.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.breakpoints;
13
14 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.resources.IWorkspaceRunnable;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.DebugException;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.model.Breakpoint;
23 import org.eclipse.debug.core.model.IBreakpoint;
24
25 /**
26  * A breakpoint is capable of suspending the execution of a
27  * program at a specific location when a program is running
28  * in debug mode. Each breakpoint has an associated marker which
29  * stores and persists all attributes associated with a breakpoint.
30  * <p>
31  * A breakpoint is defined in two parts:
32  * <ol>
33  * <li>By an extension of kind <code>"org.eclipse.debug.core.breakpoints"</code></li>
34  * <li>By a marker definition that corresponds to the above breakpoint extension</li>
35  * </ol>
36  * <p>
37  * For example, following is a definition of corresponding breakpoint
38  * and breakpoint marker definitions. Note that the <code>markerType</code>
39  * attribute defined by the breakpoint extension corresponds to the 
40  * type of the marker definition.
41  * <pre>
42  * &lt;extension point="org.eclipse.debug.core.breakpoints"&gt;
43  *   &lt;breakpoint 
44  *      id="com.example.Breakpoint"
45  *      class="com.example.Breakpoint"
46  *      markerType="com.example.BreakpointMarker"&gt;
47  *   &lt;/breakpoint&gt;
48  * &lt;/extension&gt;
49  * &lt;extension point="org.eclipse.core.resources.markers"&gt;
50  *   &lt;marker 
51  *      id="com.example.BreakpointMarker"
52  *      super type="org.eclipse.debug.core.breakpointMarker"
53  *      attribute name ="exampleAttribute"&gt;
54  *   &lt;/marker&gt;
55  * &lt;/extension&gt;
56  * </pre>
57  * <p>
58  * The breakpoint manager instantiates persisted breakpoints by
59  * traversing all markers that are a subtype of
60  * <code>"org.eclipse.debug.core.breakpointMarker"</code>, and 
61  * instantiating the class defined by the <code>class</code> attribute
62  * on the associated breakpoint extension. The method <code>setMarker</code>
63  * is then called to associate a marker with the breakpoint.
64  * </p>
65  * <p>
66  * Breakpoints may or may not be registered with the breakpoint manager, and
67  * are persisted and restored as such. Since marker definitions only allow
68  * all or none of a specific marker type to be persisted, breakpoints define
69  * a <code>PERSISTED</code> attribute for selective persistence of breakpoints
70  * of the same type.
71  * </p>
72  * 
73  * @since 2.0
74  */
75
76 public abstract class PHPBreakpoint extends Breakpoint implements IBreakpoint {
77
78         /**
79          * Breakpoint attribute storing a breakpoint's hit count value
80          * (value <code>"net.sourceforge.phpeclipse.debug.hitCount"</code>). This attribute is stored as an
81          * <code>int</code>.
82          * 
83          * For DBG the hit count is really a skip count.
84          * Explanation: A hit count of e.g. 4 would break on the fourth occurence of the breakpoint.
85          * A skip count means skip the first four occurences of the breakpoint, and break on the fifth occurence.
86          */
87         protected static final String HIT_COUNT = "net.sourceforge.phpeclipse.debug.hitCount"; //$NON-NLS-1$
88
89         /**
90          * Breakpoint attribute storing a breakpoint's changeID.
91          * This is used for checking whether the breakpoint properties menu was finished with
92          * a OK-button. Which means a possible change of breakpoint condition or skip count.
93          * This is necessary because in method breakpointChanged in class PHPDebugTarget we need to know,
94          * whether the breakpoint has changed or not (breakpointChanged is called also when
95          * a PHP source file is modified and saved).
96          */
97         protected static final String CHANGE_ID = "net.sourceforge.phpeclipse.debug.changeID"; //$NON-NLS-1$
98
99         /**
100          * Breakpoint attribute storing a breakpoint's condition
101          * (value <code>"net.sourceforge.phpeclipse.debug.condition"</code>). This attribute is stored as an
102          * <code>string</code>.
103          */
104         protected static final String CONDITION = "net.sourceforge.phpeclipse.debug.condition"; //$NON-NLS-1$
105
106         /**
107          * Breakpoint attribute storing whether a breakpoint's condition is enabled or not
108          * (value <code>"net.sourceforge.phpeclipse.debug.conditionEnabled"</code>). This attribute is stored as an
109          * <code>boolean</code>.
110          */
111         protected static final String CONDITION_ENABLED = "net.sourceforge.phpeclipse.debug.conditionEnabled"; //$NON-NLS-1$
112
113         /**
114          * Breakpoint attribute storing the fully qualified name of the type
115          * this breakpoint is located in.
116          * (value <code>"net.sourceforge.phpeclipse.debug.typeName"</code>). This attribute is a <code>String</code>.
117          */
118         protected static final String TYPE_NAME = "net.sourceforge.phpeclipse.debug.typeName"; //$NON-NLS-1$            
119
120         /**
121          * Root breakpoint marker type  
122          * (value <code>"org.eclipse.debug.core.breakpoint"</code>).
123          */
124         public static final String BREAKPOINT_MARKER = DebugPlugin.getUniqueIdentifier() + ".breakpointMarker"; //$NON-NLS-1$
125         
126         /**
127          * Line breakpoint marker type
128          * (value <code>"org.eclipse.debug.core.lineBreakpoint"</code>).
129          */
130         public static final String LINE_BREAKPOINT_MARKER = DebugPlugin.getUniqueIdentifier() + ".lineBreakpointMarker"; //$NON-NLS-1$
131                         
132         /**
133          * Enabled breakpoint marker attribute (value <code>"org.eclipse.debug.core.enabled"</code>).
134          * The attribute is a <code>boolean</code> corresponding to the
135          * enabled state of a breakpoint.
136          *
137          * @see org.eclipse.core.resources.IMarker#getAttribute(String, boolean)
138          */
139         public static final String ENABLED= "org.eclipse.debug.core.enabled"; //$NON-NLS-1$
140         
141         /**
142          * Debug model identifier breakpoint marker attribute (value <code>"org.eclipse.debug.core.id"</code>).
143          * The attribute is a <code>String</code> corresponding to the
144          * identifier of the debug model a breakpoint is associated with.
145          */
146         public static final String ID= "org.eclipse.debug.core.id"; //$NON-NLS-1$
147         
148         /**
149          * Registered breakpoint marker attribute (value <code>"org.eclipse.debug.core.registered"</code>).
150          * The attribute is a <code>boolean</code> corresponding to
151          * whether a breakpoint has been registered with the breakpoint manager.
152          *
153          * @see org.eclipse.core.resources.IMarker#getAttribute(String, boolean)
154          */
155         public static final String REGISTERED= "org.eclipse.debug.core.registered"; //$NON-NLS-1$       
156         
157         /**
158          * Persisted breakpoint marker attribute (value <code>"org.eclipse.debug.core.persisted"</code>).
159          * The attribute is a <code>boolean</code> corresponding to
160          * whether a breakpoint is to be persisted accross workspace
161          * invocations.
162          *
163          * @see org.eclipse.core.resources.IMarker#getAttribute(String, boolean)
164          */
165         public static final String PERSISTED= "org.eclipse.debug.core.persisted"; //$NON-NLS-1$         
166
167         private int DBGBpNo=0;
168
169         public PHPBreakpoint() {
170         }       
171
172         /**
173          * @see IBreakpoint#setMarker(IMarker)
174          */
175         public void setMarker(IMarker marker) throws CoreException {
176                 super.setMarker(marker);
177         }
178         
179         /**
180          * Add this breakpoint to the breakpoint manager,
181          * or sets it as unregistered.
182          */
183         protected void register(boolean register) throws CoreException {
184                 if (register) {
185                         DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(this);
186                 } else {
187                         setRegistered(false);
188                 }
189         }
190         
191         /**
192          * Execute the given workspace runnable
193          */
194         protected void run(IWorkspaceRunnable wr) throws DebugException {
195                 try {
196                         ResourcesPlugin.getWorkspace().run(wr, null);
197                 } catch (CoreException e) {
198                         throw new DebugException(e.getStatus());
199                 }                       
200         }
201
202         /**
203          * @see IBreakpoint#getModelIdentifier()
204          */
205         public String getModelIdentifier() {
206                 return PHPDebugCorePlugin.getUniqueIdentifier();
207         }
208         
209         public void setDBGBpNo(int bpNo) {
210                 this.DBGBpNo = bpNo;
211         }
212         
213         public int getDBGBpNo() {
214                 return this.DBGBpNo;
215         }
216         
217         public int getHitCount () throws CoreException {
218                 return getMarker ().getAttribute (HIT_COUNT, -1);
219         }
220         
221         public void setHitCount (int hitCount) throws CoreException {
222                 if (hitCount > 0) {
223                         if (!isEnabled ()) {
224                                 getMarker ().setAttribute (ENABLED, true);
225                         }
226                 }
227                 
228                 getMarker ().setAttribute (HIT_COUNT, hitCount);
229         }
230 }