Added String preferences
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MachineThread.java
1 package net.mograsim.plugin.launch;
2
3 import java.util.Arrays;
4
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.PlatformObject;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.debug.core.DebugEvent;
9 import org.eclipse.debug.core.DebugException;
10 import org.eclipse.debug.core.DebugPlugin;
11 import org.eclipse.debug.core.ILaunch;
12 import org.eclipse.debug.core.model.IBreakpoint;
13 import org.eclipse.debug.core.model.IDebugTarget;
14 import org.eclipse.debug.core.model.IStackFrame;
15 import org.eclipse.debug.core.model.IThread;
16
17 import net.mograsim.machine.Machine;
18 import net.mograsim.plugin.MograsimActivator;
19
20 public class MachineThread extends PlatformObject implements IThread
21 {
22         private final MachineDebugTarget debugTarget;
23         private final MachineStackFrame stackFrame;
24
25         public MachineThread(MachineDebugTarget debugTarget)
26         {
27                 this.debugTarget = debugTarget;
28                 this.stackFrame = new MachineStackFrame(this);
29
30                 DebugPlugin.getDefault().addDebugEventListener(es -> Arrays.stream(es).filter(e -> e.getSource() == debugTarget).forEach(e ->
31                 {
32                         switch (e.getKind())
33                         {
34                         case DebugEvent.RESUME:
35                                 fireResumeEvent(e.getDetail());
36                                 break;
37                         case DebugEvent.SUSPEND:
38                                 fireSuspendEvent(e.getDetail());
39                                 break;
40                         default:
41                         }
42                 }));
43
44                 fireCreationEvent();
45         }
46
47         @Override
48         public String getModelIdentifier()
49         {
50                 return MograsimActivator.PLUGIN_ID;
51         }
52
53         public Machine getMachine()
54         {
55                 return debugTarget.getMachine();
56         }
57
58         @Override
59         public IDebugTarget getDebugTarget()
60         {
61                 return debugTarget;
62         }
63
64         @Override
65         public ILaunch getLaunch()
66         {
67                 return debugTarget.getLaunch();
68         }
69
70         @Override
71         public boolean canResume()
72         {
73                 return debugTarget.canResume();
74         }
75
76         @Override
77         public boolean canSuspend()
78         {
79                 return debugTarget.canSuspend();
80         }
81
82         @Override
83         public boolean isSuspended()
84         {
85                 return debugTarget.isSuspended();
86         }
87
88         @Override
89         public void resume() throws DebugException
90         {
91                 debugTarget.resume();
92         }
93
94         @Override
95         public void suspend() throws DebugException
96         {
97                 debugTarget.suspend();
98         }
99
100         @Override
101         public boolean canStepInto()
102         {
103                 return false;
104         }
105
106         // TODO step over sounds like single-step execution...
107         @Override
108         public boolean canStepOver()
109         {
110                 return false;
111         }
112
113         @Override
114         public boolean canStepReturn()
115         {
116                 return false;
117         }
118
119         @Override
120         public boolean isStepping()
121         {
122                 return false;
123         }
124
125         @Override
126         public void stepInto() throws DebugException
127         {
128                 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Can't step into for a machine thread"));
129         }
130
131         @Override
132         public void stepOver() throws DebugException
133         {
134                 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Can't step over for a machine thread"));
135         }
136
137         @Override
138         public void stepReturn() throws DebugException
139         {
140                 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Can't step return for a machine thread"));
141         }
142
143         @Override
144         public boolean canTerminate()
145         {
146                 return debugTarget.canTerminate();
147         }
148
149         @Override
150         public boolean isTerminated()
151         {
152                 return debugTarget.isTerminated();
153         }
154
155         @Override
156         public void terminate() throws DebugException
157         {
158                 debugTarget.terminate();
159         }
160
161         @Override
162         public IStackFrame[] getStackFrames() throws DebugException
163         {
164                 return isSuspended() ? new IStackFrame[] { stackFrame } : new IStackFrame[0];
165         }
166
167         @Override
168         public boolean hasStackFrames() throws DebugException
169         {
170                 // required by Eclipse: see javadoc for org.eclipse.debug.core.DebugEvent
171                 return isSuspended();
172         }
173
174         @Override
175         public int getPriority() throws DebugException
176         {
177                 return 0;
178         }
179
180         @Override
181         public IStackFrame getTopStackFrame() throws DebugException
182         {
183                 return stackFrame;
184         }
185
186         @Override
187         public String getName() throws DebugException
188         {
189                 return "pseudo thread";
190         }
191
192         @Override
193         public IBreakpoint[] getBreakpoints()
194         {
195                 return new IBreakpoint[0];
196         }
197
198         /**
199          * Fires a creation event for this debug element.
200          */
201         private void fireCreationEvent()
202         {
203                 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
204         }
205
206         /**
207          * Fires a resume for this debug element with the specified detail code.
208          *
209          * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
210          */
211         private void fireResumeEvent(int detail)
212         {
213                 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
214         }
215
216         /**
217          * Fires a suspend event for this debug element with the specified detail code.
218          *
219          * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
220          */
221         private void fireSuspendEvent(int detail)
222         {
223                 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
224         }
225
226         /**
227          * Fires a debug event.
228          *
229          * @param event debug event to fire
230          */
231         private static void fireEvent(DebugEvent event)
232         {
233                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });
234         }
235 }