First submit for debug plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPDebugTarget.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.internal.debug.core.model;
13
14 import org.eclipse.core.resources.IMarkerDelta;
15 import org.eclipse.debug.core.DebugEvent;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.IBreakpointManager;
19 import org.eclipse.debug.core.IDebugEventSetListener;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchListener;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.core.model.IMemoryBlock;
25 import org.eclipse.debug.core.model.IProcess;
26 import org.eclipse.debug.core.model.IThread;
27
28 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
29 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
30
31 /**
32  * Debug target for PHP debug model.
33  */
34 public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugEventSetListener {
35                 
36         private IProcess process;
37         private boolean isTerminated;
38         private ILaunch launch;
39         private PHPThread[] threads;
40         private PHPDBGProxy phpDBGProxy;
41
42         public PHPDebugTarget(ILaunch launch, IProcess process) {
43                 this.launch = launch;
44                 this.process = process;
45                 this.threads = new PHPThread[0];
46                 IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
47                 manager.addBreakpointListener(this);
48                 DebugPlugin.getDefault().addDebugEventListener(this);
49                 initialize();
50         }
51
52         protected synchronized void initialize() {
53                 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
54                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });    
55         }
56
57         public void addThread(PHPThread phpThread) {
58                 int i;
59                 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
60                 
61                 for(i=0; i < threads.length; i++) {
62                         updatedThreads[i] = threads[i];
63                 }
64                 updatedThreads[i] = phpThread;
65                 threads = updatedThreads;
66
67                 fireChangeEvent();
68         }
69
70         private void fireChangeEvent() {
71                 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
72                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
73         }
74
75         protected PHPThread getThreadById(int id) {
76                 for (int i = 0; i < threads.length; i++) {
77                         if (threads[i].getId() == id) {
78                                 return threads[i];
79                         }
80                 }
81                 return null;
82         }
83
84         public IThread[] getThreads() {
85                 return threads;
86         }
87
88         public boolean hasThreads() throws DebugException {
89                 return threads.length > 0;
90         }
91
92         public String getName() throws DebugException {
93                 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
94         }
95
96         public boolean supportsBreakpoint(IBreakpoint arg0) {
97                 return false;
98         }
99
100         public String getModelIdentifier() {
101                 return PHPDebugCorePlugin.getUniqueIdentifier();
102         }
103
104         public IDebugTarget getDebugTarget() {
105                 return this;
106         }
107
108         public ILaunch getLaunch() {
109                 return launch;
110         }
111
112         public boolean canTerminate() {
113                 return !isTerminated;
114         }
115
116         public boolean isTerminated() {
117                 return isTerminated;
118         }
119
120         public void terminate() {
121                 phpDBGProxy.stop();
122                 this.threads = new PHPThread[0];
123                 isTerminated = true;
124                 fireChangeEvent();
125         }
126
127         public boolean canResume() {
128                 return false;
129         }
130
131         public boolean canSuspend() {
132                 return false;
133         }
134
135         public boolean isSuspended() {
136                 return false;
137         }
138
139         public void resume() throws DebugException {
140         }
141
142         public void suspend() throws DebugException {
143         }
144
145         public void breakpointAdded(IBreakpoint breakpoint) {
146                 this.getPHPDBGProxy().addBreakpoint(breakpoint) ;
147         }
148
149         public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
150                 this.getPHPDBGProxy().removeBreakpoint(breakpoint) ;            
151         }
152
153         public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta arg1) {
154                 // is called e.g. after a line has been inserted before a breakpoint
155                 // but then the debugger is out of sync with the file anyway, so debugging
156                 // should be stopped here.
157         }
158
159         public boolean canDisconnect() {
160                 return false;
161         }
162
163         public void disconnect() throws DebugException {
164         }
165
166         public boolean isDisconnected() {
167                 return false;
168         }
169
170         public boolean supportsStorageRetrieval() {
171                 return false;
172         }
173
174         public IMemoryBlock getMemoryBlock(long arg0, long arg1) throws DebugException {
175                 return null;
176         }
177
178         public Object getAdapter(Class arg0) {
179                 return null;
180         }
181
182         public IProcess getProcess() {
183                 return process;
184         }
185
186         public void setProcess(IProcess process) {
187                 this.process = process;
188         }
189
190         public PHPDBGProxy getPHPDBGProxy() {
191                 return phpDBGProxy;
192         }
193
194         public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
195                 this.phpDBGProxy = phpDBGProxy;
196         }
197         
198         /**
199          * @see ILaunchListener#launchRemoved(ILaunch)
200          */
201         public void launchRemoved(ILaunch launch) {
202                 if (!isTerminated()) {
203                         return;
204                 }
205                 if (launch.equals(getLaunch())) {
206                         // This target has been deregistered, but it hasn't successfully terminated.
207                         // Update internal state to reflect that it is disconnected
208                         terminate();
209                 }
210         }
211         
212         /**
213          * @see ILaunchListener#launchAdded(ILaunch)
214          */
215         public void launchAdded(ILaunch launch) {
216         }
217         
218         /**
219          * @see ILaunchListener#launchChanged(ILaunch)
220          */
221         public void launchChanged(ILaunch launch) {
222         }
223         
224         /**
225          * When a debug target or process terminates, terminate DBG Proxy.
226          * 
227          * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
228          */
229         public void handleDebugEvents(DebugEvent[] events) {
230                 for (int i = 0; i < events.length; i++) {
231                         DebugEvent event = events[i];
232                         if (event.getKind() == DebugEvent.TERMINATE) {
233                                 Object source = event.getSource();
234                                 if (source instanceof PHPDebugTarget || source instanceof IDebugTarget || source instanceof IProcess) {
235                                         getPHPDBGProxy().stop();
236                                 }
237                         }
238                 }
239         }
240 }
241