First submit for debug plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / debug / core / PHPDebugModel.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.debug.core;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
18 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.Preferences;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.IBreakpointManager;
24 import org.eclipse.debug.core.model.IBreakpoint;
25
26 /**
27  * Provides utility methods for creating debug targets and breakpoints specific
28  * to the PHP debug model.
29  * <p>
30  * Clients are not intended to instantiate or subclass this class;
31  * this class provides static utility methods only.
32  * </p>
33  */
34 public class PHPDebugModel {
35         
36         /**
37          * Not to be instantiated.
38          */
39         private PHPDebugModel() {
40                 super();
41         }
42         
43         /**
44          * Returns the identifier for the PHP debug model plug-in
45          *
46          * @return plugin identifier
47          */
48         public static String getPluginIdentifier() {
49                 return PHPDebugCorePlugin.getUniqueIdentifier();
50         }
51         
52         /**
53          * Creates and returns a line breakpoint in the type with
54          * at the given line number. The marker associated with the
55          * breakpoint will be created on the specified resource. If a character
56          * range within the line is known, it may be specified by charStart/charEnd.
57          * If hitCount is > 0, the breakpoint will suspend execution when it is
58          * "hit" the specified number of times.
59          * 
60          * @param resource the resource on which to create the associated breakpoint
61          *  marker
62          * @param lineNumber the lineNumber on which the breakpoint is set - line
63          *   numbers are 1 based, associated with the source file in which
64          *   the breakpoint is set
65          * @param charStart the first character index associated with the breakpoint,
66          *   or -1 if unspecified, in the source file in which the breakpoint is set
67          * @param charEnd the last character index associated with the breakpoint,
68          *   or -1 if unspecified, in the source file in which the breakpoint is set
69          * @param hitCount the number of times the breakpoint will be hit before
70          *   suspending execution - 0 if it should always suspend
71          * @param register whether to add this breakpoint to the breakpoint manager
72          * @param attributes a map of client defined attributes that should be assigned
73          *  to the underlying breakpoint marker on creation, or <code>null</code> if none.
74          * @return a line breakpoint
75          * @exception CoreException If this method fails. Reasons include:<ul> 
76          *<li>Failure creating underlying marker.  The exception's status contains
77          * the underlying exception responsible for the failure.</li></ul>
78          * @since 2.0
79          */
80         public static void createLineBreakpoint(IResource resource, int lineNumber, int charStart, int charEnd, int hitCount, boolean register, Map attributes) throws CoreException {
81                 if (attributes == null) {
82                         attributes = new HashMap(10);
83                 }
84                 new PHPLineBreakpoint(resource, lineNumber, charStart, charEnd, hitCount, true, attributes);
85         }
86
87         /**
88          * Returns true if line breakpoint is already registered with the breakpoint
89          * manager for the given line number.
90          * 
91          * @param typeName fully qualified type name
92          * @param lineNumber line number
93          * @return true if line breakpoint is already registered with the breakpoint
94          *  manager for the given line number or <code>false</code>
95          * if no such breakpoint is registered
96          * @exception CoreException If this method fails.
97          */
98         public static boolean lineBreakpointExists(int lineNumber) throws CoreException {
99                 String modelId= getPluginIdentifier();
100                 String markerType= PHPLineBreakpoint.getMarkerType();
101                 IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
102                 IBreakpoint[] breakpoints= manager.getBreakpoints(modelId);
103                 for (int i = 0; i < breakpoints.length; i++) {
104                         if (!(breakpoints[i] instanceof PHPLineBreakpoint)) {
105                                 continue;
106                         }
107                         PHPLineBreakpoint breakpoint = (PHPLineBreakpoint) breakpoints[i];
108                         if (breakpoint.getMarker().getType().equals(markerType)) {
109                                         if (breakpoint.getLineNumber() == lineNumber) {
110                                                 return true;
111                                         }
112                         }
113                 }
114                 return false;
115         }       
116
117         /**
118          * Returns the preference store for this plug-in or <code>null</code>
119          * if the store is not available.
120          * 
121          * @return the preference store for this plug-in
122          */
123         public static Preferences getPreferences() {
124                 PHPDebugCorePlugin deflt= PHPDebugCorePlugin.getDefault();
125                 if (deflt != null) {
126                         return deflt.getPluginPreferences();
127                 }
128                 return null;
129         }
130
131         /**
132          * Saves the preference store for this plug-in.
133          * 
134          * @return the preference store for this plug-in
135          */
136         public static void savePreferences() {
137                 PHPDebugCorePlugin.getDefault().savePluginPreferences();
138         }
139         
140         /**
141          * Creates and returns a debug target for the given VM, with
142          * the specified name, and associates the debug target with the
143          * given process for console I/O. The allow terminate flag specifies whether
144          * the debug target will support termination (<code>ITerminate</code>).
145          * The allow disconnect flag specifies whether the debug target will
146          * support disconnection (<code>IDisconnect</code>). The resume
147          * flag specifies if the target VM should be resumed on startup (has
148          * no effect if the VM was already running when the connection to the
149          * VM was esatbished). Launching the actual VM is a client responsibility.
150          * The debug target is added to the given launch.
151          *
152          * @param launch the launch the new debug target will be contained in
153          * @param vm the VM to create a debug target for
154          * @param name the name to associate with the VM, which will be 
155          *   returned from <code>IDebugTarget.getName</code>. If <code>null</code>
156          *   the name will be retrieved from the underlying VM.
157          * @param process the process to associate with the debug target,
158          *   which will be returned from <code>IDebugTarget.getProcess</code>
159          * @param allowTerminate whether the target will support termianation
160          * @param allowDisconnect whether the target will support disconnection
161          * @param resume whether the target is to be resumed on startup. Has
162          *   no effect if the target was already running when the connection
163          *   to the VM was established.
164          * @return a debug target
165          * @see org.eclipse.debug.core.model.ITerminate
166          * @see org.eclipse.debug.core.model.IDisconnect
167          * @since 2.0
168          */
169         /*
170         public static IDebugTarget newDebugTarget(final ILaunch launch, final String name, final IProcess process) {
171                 final IDebugTarget[] target = new IDebugTarget[1];
172                 IWorkspaceRunnable r = new IWorkspaceRunnable() {
173                         public void run(IProgressMonitor m) {
174                                 target[0]= new PHPDebugTarget(launch, process);
175                         }
176                 };
177                 try {
178                         ResourcesPlugin.getWorkspace().run(r, null);
179                 } catch (CoreException e) {
180                         //PHPDebugPlugin.log(e);
181                 }
182                 return target[0];
183         }
184         */
185 }