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