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