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