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