1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPThread.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 net.sourceforge.phpeclipse.PHPeclipsePlugin;
15
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;
26
27 public class PHPThread extends PHPDebugElement implements IThread {
28
29         private PHPStackFrame[] frames;         // The stackframes which belongs to this thread
30         private PHPDebugTarget  target;     // This is doppel moppel, fTarget exists in PHPDebugElement
31         private String                  name;       //
32         private int                     id;         // The port number through which we communicate to DBG
33         private boolean         fDebug      = true;
34
35         private class State {
36                 private boolean isSuspended  = false;
37                 private boolean isTerminated = false;
38                 private boolean isStepping       = false;
39
40                 boolean isSuspended () {
41                         return isSuspended;
42                 }
43
44                 boolean isTerminated () {
45                         return isTerminated;
46                 }
47
48                 boolean isStepping () {
49                         return isStepping;
50                 }
51
52                 void setSuspended (boolean suspended) {
53                         if (isTerminated ()) {
54                                 throw new IllegalStateException();
55                         }
56
57                         if (suspended && isStepping ()) {
58                                 throw new IllegalStateException ();
59                         }
60
61                         isSuspended = suspended;
62                 }
63
64                 void setStepping (boolean stepping) {
65                         if (stepping && !isSuspended ()) {
66                                 throw new IllegalStateException ();
67                         }
68
69                         if (isTerminated ()) {
70                                 throw new IllegalStateException ();
71                         }
72
73                         isStepping = stepping;
74                 }
75
76                 void setTerminated(boolean terminated) {
77                         isTerminated = terminated;
78                 }
79         }
80
81         private final State state = new State ();
82
83         /**
84          * @param target
85          * @param id            The port number through which we communicate to DBG
86          */
87         public PHPThread (PHPDebugTarget target, int id) {
88                 super (target);
89
90                 this.target = target;
91                 this.setId (id);
92         }
93
94         /**
95          *
96          */
97         public IStackFrame[] getStackFrames () throws DebugException {
98                 if (isSuspended()) {
99                 if (fDebug) {
100                     System.out.println ("PHPThread getStackFrames");
101                 }
102
103                         return ((PHPDebugTarget)getDebugTarget()).getStackFrames();
104                 } else {
105                         return new IStackFrame[0];
106                 }
107         }
108
109 //      public int getStackFramesSize () {
110 //              return frames.length;
111 //      }
112
113         public boolean hasStackFrames () {
114                 if (frames == null) {
115                         return false;
116                 }
117
118                 return frames.length > 0;
119         }
120
121         public int getPriority () throws DebugException {
122                 return 0;
123         }
124
125         public IStackFrame getTopStackFrame () throws DebugException {
126                 if (frames == null || frames.length == 0) {
127                         return null;
128                 }
129                 return (IStackFrame) frames[0];
130         }
131
132         public IBreakpoint[] getBreakpoints() {
133                 return new IBreakpoint[0];
134         }
135
136         public String getModelIdentifier() {
137                 return this.getDebugTarget().getModelIdentifier();
138         }
139
140         public IDebugTarget getDebugTarget() {
141                 return target;
142         }
143
144 //      public void setDebugTarget(PHPDebugTarget target) {
145 //              this.target = target;
146 //      }
147
148         public ILaunch getLaunch() {
149                 return this.getDebugTarget().getLaunch();
150         }
151
152         public synchronized boolean canResume() {
153                 return isSuspended();
154         }
155
156         public synchronized boolean canSuspend() {
157                 return !isSuspended();
158         }
159
160         public synchronized boolean isSuspended() {
161                 return state.isSuspended;
162         }
163
164         /**
165          *
166          * Is called from PHPstackframe whenever a stepInto, stepOver or stepReturn is
167          * to be performed
168          *
169          * @param de
170          */
171         protected void prepareForResume (int de) {
172                 DebugEvent ev;
173
174                 state.setSuspended (false);                                 // We will leave the suspended state
175                 frames      = null;                                         // Reset the stackframes
176                 ev          = new DebugEvent (this, DebugEvent.RESUME, de); // Create an event resume by stepping
177
178                 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });  // Fire the event
179         }
180
181         /**
182          *
183          */
184         public synchronized void resume () throws DebugException {
185                 if (!isSuspended ()) {                                                                          // Is the thread in suspended state?
186                         return;                                                                                                 // No, leave here
187                 }
188
189                 this.prepareForResume (DebugEvent.STEP_OVER);               // Use a STEP_OVER here because a 0 leads to a collapsing variable tree in UI
190
191                 ((PHPDebugTarget) this.getDebugTarget ()).getPHPDBGProxy ().resume ();
192         }
193
194         /*
195          * public void doSuspend(SuspensionPoint suspensionPoint) { //
196          * this.getPHPDebuggerProxy().readFrames(this);
197          * this.createName(suspensionPoint) ; this.suspend() ; }
198          */
199
200         public synchronized void suspend () throws DebugException {
201                 DebugEvent ev;
202
203                 if (isSuspended ()) {                                                                           // Is the thread in suspend state?
204                         return;                                                                                                 // Yes, leave here
205                 }
206
207                 state.setSuspended (true);                                  // Set thread to suspended state
208                 state.setStepping (false);                                  // Reset thread from stepping state
209
210                 getDebugTarget ().suspend ();                                                           //
211
212                 ev = new DebugEvent (this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
213
214                 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
215         }
216
217         /**
218          *
219          */
220         public boolean canStepInto () {
221                 return isSuspended () &&                                    // Is the thread in suspended mode (stopped)
222                            isStepping () &&                                     // and ???
223                            this.hasStackFrames ();                              // and does this thread have stack frames?
224         }
225
226         /**
227          *
228          */
229         public boolean canStepOver () {
230                 return isSuspended () &&                                    // Is the thread in suspended mode (stopped)
231                            isStepping () &&                                     // and ???
232                            this.hasStackFrames ();                              // and does this thread have stack frames?
233         }
234
235         /**
236          *
237          */
238         public boolean canStepReturn () {
239                 return isSuspended () &&                                    // Is the thread in suspended mode (stopped)
240                            isStepping () &&                                     // and ???
241                            this.hasStackFrames ();                              // and does this thread have stack frames?
242         }
243
244         /**
245          *
246          */
247         public boolean isStepping () {
248                 return state.isStepping ();
249         }
250
251         /**
252          *
253          */
254         public void stepInto () throws DebugException {
255                 try {
256                         state.setStepping (true);                               // Store the info about what we do
257                 }
258                 catch (IllegalStateException x) {
259                         throw new DebugException (PHPeclipsePlugin.error (x));
260                 }
261
262                 this.frames = null;
263
264                 frames[0].stepInto ();
265         }
266
267         /**
268          *
269          */
270         public void stepOver () throws DebugException {
271                 state.setStepping (true);
272
273                 this.frames = null;
274
275                 frames[0].stepOver ();
276         }
277
278         /**
279          *
280          */
281         public void stepReturn () throws DebugException {
282         }
283
284         /**
285          *
286          */
287         public boolean canTerminate () {
288                 return !isTerminated ();
289         }
290
291         /**
292          *
293          */
294         public boolean isTerminated () {
295                 return state.isTerminated ();
296         }
297
298         /**
299          *
300          */
301         public synchronized void terminate () throws DebugException {
302                 if (isTerminated ()) {
303                         return;
304                 }
305
306                 state.setTerminated (true);
307                 this.frames = null;
308                 getDebugTarget ().terminate ();
309                 fireTerminateEvent ();
310         }
311
312         /**
313          *
314          * @param arg0
315          * @return
316          */
317         public Object getAdapter (Class arg0) {
318                 if (IWorkbenchAdapter.class.equals (arg0)) {
319                         return new IWorkbenchAdapter() {
320                                 public Object[] getChildren(Object o) {
321                                         try {
322                                                 return getStackFrames ();
323                                         } catch (DebugException x) {
324                                                 PHPeclipsePlugin.log ("Unable to get stack frames.", x);
325                                         }
326
327                                         return new Object[0];
328                                 }
329
330                                 public ImageDescriptor getImageDescriptor(Object object) {
331                                         return null;
332                                 }
333
334                                 public String getLabel(Object o) {
335                                         throw new UnsupportedOperationException();
336                                 }
337
338                                 public Object getParent(Object o) {
339                                         return getDebugTarget();
340                                 }
341                         };
342                 }
343                 return super.getAdapter(arg0);
344         }
345
346         /**
347          *
348          */
349         public void setStackFrames(PHPStackFrame[] frames) {
350                 this.frames = frames;
351         }
352
353         /**
354          *
355          */
356         public String getName () {
357                 String name;
358
359                 name = this.name;
360
361                 if (isSuspended ()) {
362                         name = name + " (suspended)";
363                 }
364
365                 return name;
366         }
367
368         public void setName (String name) {
369                 this.name = name;
370         }
371
372         /*
373          * protected void createName(SuspensionPoint suspensionPoint) { this.name =
374          * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
375          * suspensionPoint + ")" ; } }
376          */
377
378         public int getId() {
379                 return id;
380         }
381
382         public void setId(int id) {
383                 this.id = id;
384         }
385
386 }