Package: VdmDebugConsole$StreamListener

VdmDebugConsole$StreamListener

nameinstructionbranchcomplexitylinemethod
VdmDebugConsole.StreamListener(VdmDebugConsole, IStreamMonitor, IOConsoleOutputStream)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
closeStream()
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
streamAppended(String, IStreamMonitor)
M: 75 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui;
23:
24: import java.io.IOException;
25: import java.util.ArrayList;
26: import java.util.HashSet;
27: import java.util.List;
28: import java.util.Set;
29:
30: import org.eclipse.core.runtime.IProgressMonitor;
31: import org.eclipse.core.runtime.IStatus;
32: import org.eclipse.core.runtime.Status;
33: import org.eclipse.debug.core.DebugEvent;
34: import org.eclipse.debug.core.DebugPlugin;
35: import org.eclipse.debug.core.IDebugEventSetListener;
36: import org.eclipse.debug.core.ILaunch;
37: import org.eclipse.debug.core.IStreamListener;
38: import org.eclipse.debug.core.model.IFlushableStreamMonitor;
39: import org.eclipse.debug.core.model.IProcess;
40: import org.eclipse.debug.core.model.IStreamMonitor;
41: import org.eclipse.debug.core.model.IStreamsProxy;
42: import org.eclipse.debug.ui.IDebugUIConstants;
43: import org.eclipse.debug.ui.console.IConsoleColorProvider;
44: import org.eclipse.jface.resource.ImageDescriptor;
45: import org.eclipse.ui.console.ConsolePlugin;
46: import org.eclipse.ui.console.IConsole;
47: import org.eclipse.ui.console.IOConsole;
48: import org.eclipse.ui.console.IOConsoleInputStream;
49: import org.eclipse.ui.console.IOConsoleOutputStream;
50: import org.eclipse.ui.progress.UIJob;
51: import org.overture.ide.debug.core.VdmDebugPlugin;
52:
53: public class VdmDebugConsole extends IOConsole implements
54:                 IDebugEventSetListener
55: {
56:
57:         /**
58:          * @since 2.0
59:          */
60:         public static final String TYPE = VdmDebugPlugin.PLUGIN_ID
61:                         + ".ScriptDebugConsoleType"; //$NON-NLS-1$
62:
63:         private final ILaunch launch;
64:         private final IConsoleColorProvider fColorProvider;
65:
66:         public ILaunch getLaunch()
67:         {
68:                 return launch;
69:         }
70:
71:         /**
72:          * @since 2.0
73:          */
74:         public IProcess getProcess()
75:         {
76:                 final IProcess[] processes = launch.getProcesses();
77:                 if (processes.length != 0)
78:                 {
79:                         return processes[0];
80:                 } else
81:                 {
82:                         return null;
83:                 }
84:         }
85:
86:         /**
87:          * @since 2.0
88:          */
89:         public VdmDebugConsole(ILaunch launch, String name,
90:                         ImageDescriptor imageDescriptor, String encoding,
91:                         IConsoleColorProvider colorProvider)
92:         {
93:                 super(name, TYPE, imageDescriptor, encoding, true);
94:                 this.launch = launch;
95:                 this.fColorProvider = colorProvider;
96:                 // this.addPatternMatchListener(new ScriptDebugConsoleTraceTracker());
97:         }
98:
99:         @Override
100:         public void matcherFinished()
101:         {
102:                 super.matcherFinished();
103:         }
104:
105:         @Override
106:         public void partitionerFinished()
107:         {
108:                 super.partitionerFinished();
109:         }
110:
111:         /*
112:          * Increase visibility
113:          */
114:         @Override
115:         protected void setName(String name)
116:         {
117:                 super.setName(name);
118:         }
119:
120:         @Override
121:         protected void dispose()
122:         {
123:                 closeStreams();
124:                 disposeStreams();
125:                 super.dispose();
126:         }
127:
128:         private Set<IProcess> connectedProcesses;
129:
130:         /**
131:          * @param process
132:          * @since 2.0
133:          */
134:         public synchronized void connect(IProcess process)
135:         {
136:                 if (connectedProcesses == null)
137:                 {
138:                         connectedProcesses = new HashSet<IProcess>();
139:                 }
140:                 if (connectedProcesses.add(process))
141:                 {
142:                         final IStreamsProxy proxy = process.getStreamsProxy();
143:                         if (proxy == null)
144:                         {
145:                                 return;
146:                         }
147:                         connect(proxy);
148:                 }
149:         }
150:
151:         /**
152:          * @since 2.0
153:          */
154:         public void connect(final IStreamsProxy proxy)
155:         {
156:                 IStreamMonitor streamMonitor = proxy.getErrorStreamMonitor();
157:                 if (streamMonitor != null)
158:                 {
159:                         connect(streamMonitor, IDebugUIConstants.ID_STANDARD_ERROR_STREAM);
160:                 }
161:                 streamMonitor = proxy.getOutputStreamMonitor();
162:                 if (streamMonitor != null)
163:                 {
164:                         connect(streamMonitor, IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM);
165:                 }
166:
167:                 IOConsoleInputStream input = getInputStream();
168:                 if (input != null)
169:                 {
170:                         getInputStream().setColor(fColorProvider.getColor(IDebugUIConstants.ID_STANDARD_INPUT_STREAM));
171:                 }
172:
173:         }
174:
175:         private List<StreamListener> fStreamListeners = new ArrayList<StreamListener>();
176:
177:         /**
178:          * @param streamMonitor
179:          * @param idStandardErrorStream
180:          */
181:         private void connect(IStreamMonitor streamMonitor, String streamIdentifier)
182:         {
183:                 synchronized (streamMonitor)
184:                 {
185:                         IOConsoleOutputStream stream = newOutputStream();
186:                         stream.setActivateOnWrite(true);
187:                         stream.setColor(fColorProvider.getColor(streamIdentifier));
188:                         StreamListener listener = new StreamListener(streamMonitor, stream);
189:                         fStreamListeners.add(listener);
190:                 }
191:         }
192:
193:         /**
194:          * cleanup method to close all of the open stream to this console
195:          */
196:         private synchronized void closeStreams()
197:         {
198:                 for (StreamListener listener : fStreamListeners)
199:                 {
200:                         listener.closeStream();
201:                 }
202:         }
203:
204:         /**
205:          * disposes of the listeners for each of the stream associated with this console
206:          */
207:         private synchronized void disposeStreams()
208:         {
209:                 for (StreamListener listener : fStreamListeners)
210:                 {
211:                         listener.dispose();
212:                 }
213:         }
214:
215:         /**
216:          * This class listens to a specified IO stream
217:          */
218:         private class StreamListener implements IStreamListener
219:         {
220:                 private IOConsoleOutputStream fStream;
221:                 private IStreamMonitor fStreamMonitor;
222:                 private boolean fFlushed = false;
223:                 private boolean fListenerRemoved = false;
224:
225:                 public StreamListener(IStreamMonitor monitor,
226:                                 IOConsoleOutputStream stream)
227:                 {
228:                         this.fStream = stream;
229:                         this.fStreamMonitor = monitor;
230:                         fStreamMonitor.addListener(this);
231:                         // fix to bug 121454. Ensure that output to fast processes is
232:                         // processed.
233:                         streamAppended(null, monitor);
234:                 }
235:
236:                 public void streamAppended(String text, IStreamMonitor monitor)
237:                 {
238:                         String encoding = getEncoding();
239:•                        if (fFlushed)
240:                         {
241:                                 try
242:                                 {
243:•                                        if (fStream != null)
244:                                         {
245:•                                                if (encoding == null)
246:                                                 {
247:                                                         fStream.write(text);
248:                                                 } else
249:                                                 {
250:                                                         fStream.write(text.getBytes(encoding));
251:                                                 }
252:                                         }
253:                                 } catch (IOException e)
254:                                 {
255:                                         VdmDebugPlugin.log(e);
256:                                 }
257:                         } else
258:                         {
259:                                 String contents = null;
260:                                 synchronized (fStreamMonitor)
261:                                 {
262:                                         fFlushed = true;
263:                                         contents = fStreamMonitor.getContents();
264:•                                        if (fStreamMonitor instanceof IFlushableStreamMonitor)
265:                                         {
266:                                                 IFlushableStreamMonitor m = (IFlushableStreamMonitor) fStreamMonitor;
267:                                                 m.flushContents();
268:                                                 m.setBuffered(false);
269:                                         }
270:                                 }
271:                                 try
272:                                 {
273:•                                        if (contents != null && contents.length() > 0)
274:                                         {
275:•                                                if (fStream != null)
276:                                                 {
277:                                                         fStream.write(contents);
278:                                                 }
279:                                         }
280:                                 } catch (IOException e)
281:                                 {
282:                                         VdmDebugPlugin.log(e);
283:                                 }
284:                         }
285:                 }
286:
287:                 public void closeStream()
288:                 {
289:•                        if (fStreamMonitor == null)
290:                         {
291:                                 return;
292:                         }
293:                         synchronized (fStreamMonitor)
294:                         {
295:                                 fStreamMonitor.removeListener(this);
296:•                                if (!fFlushed)
297:                                 {
298:                                         String contents = fStreamMonitor.getContents();
299:                                         streamAppended(contents, fStreamMonitor);
300:                                 }
301:                                 fListenerRemoved = true;
302:                         }
303:                 }
304:
305:                 public void dispose()
306:                 {
307:•                        if (!fListenerRemoved)
308:                         {
309:                                 closeStream();
310:                         }
311:                         fStreamMonitor = null;
312:                         fStream = null;
313:                 }
314:         }
315:
316:         /**
317:          * Notify listeners when name changes.
318:          *
319:          * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
320:          */
321:         public void handleDebugEvents(DebugEvent[] events)
322:         {
323:                 for (int i = 0; i < events.length; i++)
324:                 {
325:                         DebugEvent event = events[i];
326:                         if (event.getSource().equals(getProcess()))
327:                         {
328:
329:                                 if (event.getKind() == DebugEvent.TERMINATE)
330:                                 {
331:                                         closeStreams();
332:                                         DebugPlugin.getDefault().removeDebugEventListener(this);
333:                                 }
334:
335:                                 resetName();
336:                         }
337:                 }
338:         }
339:
340:         /**
341:          * resets the name of this console to the original computed name
342:          */
343:         private void resetName()
344:         {
345:
346:                 UIJob job = new UIJob("Activating Console") { //$NON-NLS-1$
347:                         public IStatus runInUIThread(IProgressMonitor monitor)
348:                         {
349:
350:                                 warnOfContentChange();
351:                                 return Status.OK_STATUS;
352:                         }
353:                 };
354:                 job.setSystem(true);
355:                 job.schedule();
356:
357:         }
358:
359:         /**
360:          * send notification of a change of content in this console
361:          */
362:         private void warnOfContentChange()
363:         {
364:                 IConsole[] consoles = ConsolePlugin.getDefault().getConsoleManager().getConsoles();
365:                 for (IConsole iConsole : consoles)
366:                 {
367:                         if (iConsole instanceof VdmDebugConsole)
368:                         {
369:                                 VdmDebugConsole vdmC = (VdmDebugConsole) iConsole;
370:
371:                                 if(vdmC != null && vdmC.getProcess() == getProcess())
372:                                 {
373:                                         vdmC.activate();
374:                                 }
375:                         }
376:                 }
377:
378:                 // if (warn != null) {
379:                 // ConsolePlugin.getDefault().getConsoleManager()
380:                 // .warnOfContentChange(warn);
381:                 // }
382:         }
383:
384: }