Patches from Robert Kraske (robekras):
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPThread.java
index efc55ea..c6508e8 100644 (file)
@@ -26,46 +26,49 @@ import org.eclipse.ui.model.IWorkbenchAdapter;
 
 public class PHPThread extends PHPDebugElement implements IThread {
 
-       private PHPStackFrame[] frames;
-
-       private PHPDebugTarget target;
-
-       private String name;
-
-       private int id;
+       private PHPStackFrame[] frames;         // The stackframes which belongs to this thread
+       private PHPDebugTarget  target;     //
+       private String                  name;       //
+       private int                     id;         // The port number through which we communicate to DBG
 
        private class State {
-               private boolean isSuspended = false;
-
+               private boolean isSuspended  = false;
                private boolean isTerminated = false;
+               private boolean isStepping       = false;
 
-               private boolean isStepping = false;
-
-               boolean isSuspended() {
+               boolean isSuspended () {
                        return isSuspended;
                }
 
-               boolean isTerminated() {
+               boolean isTerminated () {
                        return isTerminated;
                }
 
-               boolean isStepping() {
+               boolean isStepping () {
                        return isStepping;
                }
 
-               void setSuspended(boolean suspended) {
-                       if (isTerminated())
-                               throw new IllegalStateException();
-                       if (suspended && isStepping())
+               void setSuspended (boolean suspended) {
+                       if (isTerminated ()) {
                                throw new IllegalStateException();
+                       }
+
+                       if (suspended && isStepping ()) {
+                               throw new IllegalStateException ();
+                       }
+
                        isSuspended = suspended;
                }
 
-               void setStepping(boolean stepping) {
-                       if (stepping && !isSuspended())
-                               throw new IllegalStateException();
-                       if (isTerminated())
-                               throw new IllegalStateException();
+               void setStepping (boolean stepping) {
+                       if (stepping && !isSuspended ()) {
+                               throw new IllegalStateException ();
+                       }
+
+                       if (isTerminated ()) {
+                               throw new IllegalStateException ();
+                       }
+
                        isStepping = stepping;
                }
 
@@ -74,34 +77,43 @@ public class PHPThread extends PHPDebugElement implements IThread {
                }
        }
 
-       private final State state = new State();
+       private final State state = new State ();
 
-  public PHPThread (PHPDebugTarget target, int id) {
+       /**
+        * @param target
+        * @param id            The port number through which we communicate to DBG
+        */
+       public PHPThread (PHPDebugTarget target, int id) {
                super (target);
+
                this.target = target;
-               this.setId(id);
+               this.setId (id);
        }
 
-       public IStackFrame[] getStackFrames() throws DebugException {
+       /**
+        *
+        */
+       public IStackFrame[] getStackFrames () throws DebugException {
                return frames;
        }
 
-       public int getStackFramesSize() {
+       public int getStackFramesSize () {
                return frames.length;
        }
 
-       public boolean hasStackFrames() {
+       public boolean hasStackFrames () {
                if (frames == null) {
                        return false;
                }
+
                return frames.length > 0;
        }
 
-       public int getPriority() throws DebugException {
+       public int getPriority () throws DebugException {
                return 0;
        }
 
-       public IStackFrame getTopStackFrame() throws DebugException {
+       public IStackFrame getTopStackFrame () throws DebugException {
                if (frames == null || frames.length == 0) {
                        return null;
                }
@@ -140,19 +152,34 @@ public class PHPThread extends PHPDebugElement implements IThread {
                return state.isSuspended;
        }
 
-       protected void prepareForResume() {
-               state.setSuspended(false);
-               this.frames = null;
-               DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME,
-                               DebugEvent.CLIENT_REQUEST);
-               DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
+       /**
+        *
+        * Is called from PHPstackframe whenever a stepInto, stepOver or stepReturn is
+        * to be performed
+        *
+        * @param de
+        */
+       protected void prepareForResume (int de) {
+               DebugEvent ev;
+
+               state.setSuspended (false);                                 // We will leave the suspended state
+               this.frames = null;                                         // Reset the stackframes
+        ev          = new DebugEvent (this, DebugEvent.RESUME, de); // Create an event resume by stepping
+
+               DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });  // Fire the event
        }
 
-       public synchronized void resume() throws DebugException {
-               if (!isSuspended())
-                       return;
-               this.prepareForResume();
-               ((PHPDebugTarget) this.getDebugTarget()).getPHPDBGProxy().resume();
+       /**
+        *
+        */
+       public synchronized void resume () throws DebugException {
+               if (!isSuspended ()) {                                                                          // Is the thread in suspended state?
+                       return;                                                                                                 // No, leave here
+               }
+
+               this.prepareForResume (DebugEvent.STEP_OVER);               // Use a STEP_OVER here because a 0 leads to a collapsing variable tree in UI
+
+               ((PHPDebugTarget) this.getDebugTarget ()).getPHPDBGProxy ().resume ();
        }
 
        /*
@@ -161,82 +188,141 @@ public class PHPThread extends PHPDebugElement implements IThread {
         * this.createName(suspensionPoint) ; this.suspend() ; }
         */
 
-       public synchronized void suspend() throws DebugException {
-               if (isSuspended())
-                       return;
-               state.setSuspended(true);
-               state.setStepping(false);
-               getDebugTarget().suspend();
-               DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND,
-                               DebugEvent.BREAKPOINT);
-               DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
+       public synchronized void suspend () throws DebugException {
+               DebugEvent ev;
+
+               if (isSuspended ()) {                                                                           // Is the thread in suspend state?
+                       return;                                                                                                 // Yes, leave here
+               }
+
+               state.setSuspended (true);                                  // Set thread to suspended state
+               state.setStepping (false);                                  // Reset thread from stepping state
+
+               getDebugTarget ().suspend ();                                                           //
+
+               ev = new DebugEvent (this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
+
+               DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
        }
 
-       public boolean canStepInto() {
-               return isSuspended() && isStepping() && this.hasStackFrames();
+       /**
+        *
+        */
+       public boolean canStepInto () {
+               return isSuspended () &&                                    // Is the thread in suspended mode (stopped)
+                      isStepping () &&                                     // and ???
+                          this.hasStackFrames ();                              // and does this thread have stack frames?
        }
 
-       public boolean canStepOver() {
-               return isSuspended() && isStepping() && this.hasStackFrames();
+       /**
+        *
+        */
+       public boolean canStepOver () {
+               return isSuspended () &&                                    // Is the thread in suspended mode (stopped)
+                      isStepping () &&                                     // and ???
+                          this.hasStackFrames ();                              // and does this thread have stack frames?
        }
 
-       public boolean canStepReturn() {
-               return isSuspended() && isStepping() && this.hasStackFrames();
+       /**
+        *
+        */
+       public boolean canStepReturn () {
+               return isSuspended () &&                                    // Is the thread in suspended mode (stopped)
+                      isStepping () &&                                     // and ???
+                          this.hasStackFrames ();                              // and does this thread have stack frames?
        }
 
-       public boolean isStepping() {
-               return state.isStepping();
+       /**
+        *
+        */
+       public boolean isStepping () {
+               return state.isStepping ();
        }
 
-       public void stepInto() throws DebugException {
-               try { state.setStepping(true); }
+       /**
+        *
+        */
+       public void stepInto () throws DebugException {
+               try {
+                       state.setStepping (true);                               // Store the info about what we do
+               }
                catch (IllegalStateException x) {
-                       throw new DebugException(PHPeclipsePlugin.error(x));
+                       throw new DebugException (PHPeclipsePlugin.error (x));
                }
+
                this.frames = null;
-               frames[0].stepInto();
+
+               frames[0].stepInto ();
        }
 
-       public void stepOver() throws DebugException {
-               state.setStepping(true);
+       /**
+        *
+        */
+       public void stepOver () throws DebugException {
+               state.setStepping (true);
+
                this.frames = null;
-               frames[0].stepOver();
+
+               frames[0].stepOver ();
        }
 
-       public void stepReturn() throws DebugException {
+       /**
+        *
+        */
+       public void stepReturn () throws DebugException {
        }
 
-       public boolean canTerminate() {
-               return !isTerminated();
+       /**
+        *
+        */
+       public boolean canTerminate () {
+               return !isTerminated ();
        }
 
-       public boolean isTerminated() {
-               return state.isTerminated();
+       /**
+        *
+        */
+       public boolean isTerminated () {
+               return state.isTerminated ();
        }
 
-       public synchronized void terminate() throws DebugException {
-               if (isTerminated())
+       /**
+        *
+        */
+       public synchronized void terminate () throws DebugException {
+               if (isTerminated ()) {
                        return;
-               state.setTerminated(true);
+               }
+
+               state.setTerminated (true);
                this.frames = null;
-               getDebugTarget().terminate();
+               getDebugTarget ().terminate ();
        }
 
-       public Object getAdapter(Class arg0) {
-               if (IWorkbenchAdapter.class.equals(arg0)) {
+       /**
+        *
+        * @param arg0
+        * @return
+        */
+       public Object getAdapter (Class arg0) {
+               if (IWorkbenchAdapter.class.equals (arg0)) {
                        return new IWorkbenchAdapter() {
                                public Object[] getChildren(Object o) {
                                        Object[] children = null;
+
                                        try {
                                                IStackFrame[] frames = getStackFrames();
+
                                                if (null != frames) {
                                                        children = new Object[frames.length];
-                                                       for (int i = 0; i < frames.length; ++i)
+                                                       for (int i = 0; i < frames.length; ++i) {
                                                                children[i] = frames[i];
+                                                       }
                                                }
                                        } catch (DebugException x) {
-                                               PHPeclipsePlugin.log("Unable to get stack frames.", x);
-                                       }
+                                               PHPeclipsePlugin.log ("Unable to get stack frames.", x);
+                                       }
+
                                        return children;
                                }
 
@@ -253,21 +339,33 @@ public class PHPThread extends PHPDebugElement implements IThread {
                                }
                        };
                }
+
                return null;
        }
 
+       /**
+        *
+        */
        public void setStackFrames(PHPStackFrame[] frames) {
                this.frames = frames;
        }
 
-       public String getName() {
-               String name = this.name;
-               if (isSuspended())
+       /**
+        *
+        */
+       public String getName () {
+               String name;
+
+               name = this.name;
+
+               if (isSuspended ()) {
                        name = name + " (suspended)";
+               }
+
                return name;
        }
 
-       public void setName(String name) {
+       public void setName (String name) {
                this.name = name;
        }