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
9 IBM Corporation - Initial implementation
10 Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.debug.core.model.IDebugTarget;
22 import org.eclipse.debug.core.model.IStackFrame;
23 import org.eclipse.debug.core.model.IThread;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.model.IWorkbenchAdapter;
27 public class PHPThread implements IThread {
29 private PHPStackFrame[] frames;
31 private IDebugTarget target;
38 private boolean isSuspended = false;
40 private boolean isTerminated = false;
42 private boolean isStepping = false;
44 boolean isSuspended() {
48 boolean isTerminated() {
52 boolean isStepping() {
56 void setSuspended(boolean suspended) {
58 throw new IllegalStateException();
59 if (suspended && isStepping())
60 throw new IllegalStateException();
61 isSuspended = suspended;
64 void setStepping(boolean stepping) {
65 if (stepping && !isSuspended())
66 throw new IllegalStateException();
68 throw new IllegalStateException();
69 isStepping = stepping;
72 void setTerminated(boolean terminated) {
73 isTerminated = terminated;
77 private final State state = new State();
79 public PHPThread(IDebugTarget target, int id) {
84 public IStackFrame[] getStackFrames() throws DebugException {
88 public int getStackFramesSize() {
92 public boolean hasStackFrames() {
96 return frames.length > 0;
99 public int getPriority() throws DebugException {
103 public IStackFrame getTopStackFrame() throws DebugException {
104 if (frames == null || frames.length == 0) {
107 return (IStackFrame) frames[0];
110 public IBreakpoint[] getBreakpoints() {
114 public String getModelIdentifier() {
115 return this.getDebugTarget().getModelIdentifier();
118 public IDebugTarget getDebugTarget() {
122 public void setDebugTarget(IDebugTarget target) {
123 this.target = target;
126 public ILaunch getLaunch() {
127 return this.getDebugTarget().getLaunch();
130 public synchronized boolean canResume() {
131 return isSuspended();
134 public synchronized boolean canSuspend() {
135 return !isSuspended();
138 public synchronized boolean isSuspended() {
139 return state.isSuspended;
142 protected void prepareForResume() {
143 state.setSuspended(false);
145 DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME,
146 DebugEvent.CLIENT_REQUEST);
147 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
150 public synchronized void resume() throws DebugException {
153 this.prepareForResume();
154 ((PHPDebugTarget) this.getDebugTarget()).getPHPDBGProxy().resume();
158 * public void doSuspend(SuspensionPoint suspensionPoint) { //
159 * this.getPHPDebuggerProxy().readFrames(this);
160 * this.createName(suspensionPoint) ; this.suspend() ; }
163 public synchronized void suspend() throws DebugException {
166 state.setSuspended(true);
167 state.setStepping(false);
168 getDebugTarget().suspend();
169 DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND,
170 DebugEvent.BREAKPOINT);
171 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
174 public boolean canStepInto() {
175 return isSuspended() && isStepping() && this.hasStackFrames();
178 public boolean canStepOver() {
179 return isSuspended() && isStepping() && this.hasStackFrames();
182 public boolean canStepReturn() {
183 return isSuspended() && isStepping() && this.hasStackFrames();
186 public boolean isStepping() {
187 return state.isStepping();
190 public void stepInto() throws DebugException {
191 try { state.setStepping(true); }
192 catch (IllegalStateException x) {
193 throw new DebugException(PHPeclipsePlugin.error(x));
196 frames[0].stepInto();
199 public void stepOver() throws DebugException {
200 state.setStepping(true);
202 frames[0].stepOver();
205 public void stepReturn() throws DebugException {
208 public boolean canTerminate() {
209 return !isTerminated();
212 public boolean isTerminated() {
213 return state.isTerminated();
216 public synchronized void terminate() throws DebugException {
219 state.setTerminated(true);
221 getDebugTarget().terminate();
224 public Object getAdapter(Class arg0) {
225 if (IWorkbenchAdapter.class.equals(arg0)) {
226 return new IWorkbenchAdapter() {
227 public Object[] getChildren(Object o) {
228 Object[] children = null;
230 IStackFrame[] frames = getStackFrames();
231 if (null != frames) {
232 children = new Object[frames.length];
233 for (int i = 0; i < frames.length; ++i)
234 children[i] = frames[i];
236 } catch (DebugException x) {
237 PHPeclipsePlugin.log("Unable to get stack frames.", x);
242 public ImageDescriptor getImageDescriptor(Object object) {
246 public String getLabel(Object o) {
247 throw new UnsupportedOperationException();
250 public Object getParent(Object o) {
251 return getDebugTarget();
258 public void setStackFrames(PHPStackFrame[] frames) {
259 this.frames = frames;
262 public String getName() {
263 String name = this.name;
265 name = name + " (suspended)";
269 public void setName(String name) {
274 * protected void createName(SuspensionPoint suspensionPoint) { this.name =
275 * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
276 * suspensionPoint + ")" ; } }
283 public void setId(int id) {