KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > console > ProcessConsole


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.console;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IStorage;
24 import org.eclipse.core.resources.IWorkspace;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.NullProgressMonitor;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.PlatformObject;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.core.runtime.jobs.Job;
35 import org.eclipse.core.variables.IStringVariableManager;
36 import org.eclipse.core.variables.VariablesPlugin;
37 import org.eclipse.debug.core.DebugEvent;
38 import org.eclipse.debug.core.DebugPlugin;
39 import org.eclipse.debug.core.IDebugEventSetListener;
40 import org.eclipse.debug.core.ILaunchConfiguration;
41 import org.eclipse.debug.core.ILaunchConfigurationType;
42 import org.eclipse.debug.core.IStreamListener;
43 import org.eclipse.debug.core.model.IFlushableStreamMonitor;
44 import org.eclipse.debug.core.model.IProcess;
45 import org.eclipse.debug.core.model.IStreamMonitor;
46 import org.eclipse.debug.core.model.IStreamsProxy;
47 import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
48 import org.eclipse.debug.internal.ui.DebugPluginImages;
49 import org.eclipse.debug.internal.ui.DebugUIPlugin;
50 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
51 import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants;
52 import org.eclipse.debug.ui.DebugUITools;
53 import org.eclipse.debug.ui.IDebugUIConstants;
54 import org.eclipse.debug.ui.console.IConsole;
55 import org.eclipse.debug.ui.console.IConsoleColorProvider;
56 import org.eclipse.debug.ui.console.IConsoleHyperlink;
57 import org.eclipse.debug.ui.console.IConsoleLineTracker;
58 import org.eclipse.jface.preference.IPreferenceStore;
59 import org.eclipse.jface.resource.ImageDescriptor;
60 import org.eclipse.jface.resource.JFaceResources;
61 import org.eclipse.jface.text.BadLocationException;
62 import org.eclipse.jface.text.IRegion;
63 import org.eclipse.jface.util.IPropertyChangeListener;
64 import org.eclipse.jface.util.PropertyChangeEvent;
65 import org.eclipse.swt.graphics.Color;
66 import org.eclipse.ui.IEditorInput;
67 import org.eclipse.ui.IPersistableElement;
68 import org.eclipse.ui.IStorageEditorInput;
69 import org.eclipse.ui.IWorkbenchPage;
70 import org.eclipse.ui.PartInitException;
71 import org.eclipse.ui.console.ConsolePlugin;
72 import org.eclipse.ui.console.IHyperlink;
73 import org.eclipse.ui.console.IOConsole;
74 import org.eclipse.ui.console.IOConsoleInputStream;
75 import org.eclipse.ui.console.IOConsoleOutputStream;
76 import org.eclipse.ui.console.IPatternMatchListener;
77 import org.eclipse.ui.console.PatternMatchEvent;
78 import org.eclipse.ui.console.TextConsole;
79 import org.eclipse.ui.editors.text.EditorsUI;
80 import org.eclipse.ui.part.FileEditorInput;
81
82 import com.ibm.icu.text.MessageFormat;
83
84 /**
85  * A console for a system process
86  * <p>
87  * Clients may instantiate this class. This class is not intended for
88  * sub-classing.
89  * </p>
90  *
91  * @since 3.0
92  */

93 public class ProcessConsole extends IOConsole implements IConsole, IDebugEventSetListener, IPropertyChangeListener {
94     private IProcess fProcess = null;
95
96     private List JavaDoc fStreamListeners = new ArrayList JavaDoc();
97
98     private IConsoleColorProvider fColorProvider;
99
100     private IOConsoleInputStream fInput;
101
102     private FileOutputStream JavaDoc fFileOutputStream;
103
104     private boolean fAllocateConsole = true;
105
106     private boolean fStreamsClosed = false;
107     
108     /**
109      * Proxy to a console document
110      */

111     public ProcessConsole(IProcess process, IConsoleColorProvider colorProvider) {
112         this(process, colorProvider, null);
113     }
114
115     /**
116      * Constructor
117      * @param process the process to associate with this console
118      * @param colorProvider the colour provider for this console
119      * @param encoding the desired encoding for this console
120      */

121     public ProcessConsole(IProcess process, IConsoleColorProvider colorProvider, String JavaDoc encoding) {
122         super("", IDebugUIConstants.ID_PROCESS_CONSOLE_TYPE, null, encoding, true); //$NON-NLS-1$
123
fProcess = process;
124
125         ILaunchConfiguration configuration = process.getLaunch().getLaunchConfiguration();
126         String JavaDoc file = null;
127         boolean append = false;
128         if (configuration != null) {
129             try {
130                 file = configuration.getAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_FILE, (String JavaDoc) null);
131                 if (file != null) {
132                     IStringVariableManager stringVariableManager = VariablesPlugin.getDefault().getStringVariableManager();
133                     file = stringVariableManager.performStringSubstitution(file);
134                     append = configuration.getAttribute(IDebugUIConstants.ATTR_APPEND_TO_FILE, false);
135                 }
136             } catch (CoreException e) {
137             }
138         }
139
140         if (file != null) {
141             IWorkspace workspace = ResourcesPlugin.getWorkspace();
142             IWorkspaceRoot root = workspace.getRoot();
143             Path path = new Path(file);
144             IFile ifile = root.getFileForLocation(path);
145             String JavaDoc message = null;
146             
147             try {
148                 String JavaDoc fileLoc = null;
149                 if (ifile != null) {
150                     if (append && ifile.exists()) {
151                         ifile.appendContents(new ByteArrayInputStream JavaDoc(new byte[0]), true, true, new NullProgressMonitor());
152                     } else {
153                         if (ifile.exists()) {
154                             ifile.delete(true, new NullProgressMonitor());
155                         }
156                         ifile.create(new ByteArrayInputStream JavaDoc(new byte[0]), true, new NullProgressMonitor());
157                     }
158                 }
159                 
160                 File JavaDoc outputFile = new File JavaDoc(file);
161                 fFileOutputStream = new FileOutputStream JavaDoc(outputFile, append);
162                 fileLoc = outputFile.getAbsolutePath();
163                 
164                 message = MessageFormat.format(ConsoleMessages.ProcessConsole_1, new String JavaDoc[] {fileLoc});
165                 addPatternMatchListener(new ConsoleLogFilePatternMatcher(fileLoc));
166             } catch (FileNotFoundException JavaDoc e) {
167                 message = MessageFormat.format(ConsoleMessages.ProcessConsole_2, new String JavaDoc[] {file});
168             } catch (CoreException e) {
169                 DebugUIPlugin.log(e);
170             }
171             if (message != null) {
172                 try {
173                     IOConsoleOutputStream stream = newOutputStream();
174                     stream.write(message);
175                     stream.close();
176                 } catch (IOException JavaDoc e) {
177                     DebugUIPlugin.log(e);
178                 }
179             }
180             try {
181                 fAllocateConsole = configuration.getAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE, true);
182             } catch (CoreException e) {
183             }
184         }
185
186         fColorProvider = colorProvider;
187         fInput = getInputStream();
188         colorProvider.connect(fProcess, this);
189
190         setName(computeName());
191
192         Color color = fColorProvider.getColor(IDebugUIConstants.ID_STANDARD_INPUT_STREAM);
193         fInput.setColor(color);
194
195         IConsoleLineTracker[] lineTrackers = DebugUIPlugin.getDefault().getProcessConsoleManager().getLineTrackers(process);
196         if (lineTrackers.length > 0) {
197             addPatternMatchListener(new ConsoleLineNotifier());
198         }
199     }
200
201     /**
202      * Computes and returns the image descriptor for this console.
203      *
204      * @return an image descriptor for this console or <code>null</code>
205      */

206     protected ImageDescriptor computeImageDescriptor() {
207         ILaunchConfiguration configuration = getProcess().getLaunch().getLaunchConfiguration();
208         if (configuration != null) {
209             ILaunchConfigurationType type;
210             try {
211                 type = configuration.getType();
212                 return DebugPluginImages.getImageDescriptor(type.getIdentifier());
213             } catch (CoreException e) {
214                 DebugUIPlugin.log(e);
215             }
216         }
217         return null;
218     }
219
220     /**
221      * Computes and returns the current name of this console.
222      *
223      * @return a name for this console
224      */

225     protected String JavaDoc computeName() {
226         String JavaDoc label = null;
227         IProcess process = getProcess();
228         ILaunchConfiguration config = process.getLaunch().getLaunchConfiguration();
229
230         label = process.getAttribute(IProcess.ATTR_PROCESS_LABEL);
231         if (label == null) {
232             if (config == null) {
233                 label = process.getLabel();
234             } else {
235                 // check if PRIVATE config
236
if (DebugUITools.isPrivate(config)) {
237                     label = process.getLabel();
238                 } else {
239                     String JavaDoc type = null;
240                     try {
241                         type = config.getType().getName();
242                     } catch (CoreException e) {
243                     }
244                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
245                     buffer.append(config.getName());
246                     if (type != null) {
247                         buffer.append(" ["); //$NON-NLS-1$
248
buffer.append(type);
249                         buffer.append("] "); //$NON-NLS-1$
250
}
251                     buffer.append(process.getLabel());
252                     label = buffer.toString();
253                 }
254             }
255         }
256
257         if (process.isTerminated()) {
258             return MessageFormat.format(ConsoleMessages.ProcessConsole_0, new String JavaDoc[] { label });
259         }
260         return label;
261     }
262
263     /**
264      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
265      */

266     public void propertyChange(PropertyChangeEvent evt) {
267         String JavaDoc property = evt.getProperty();
268         IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore();
269         if (property.equals(IDebugPreferenceConstants.CONSOLE_WRAP) || property.equals(IDebugPreferenceConstants.CONSOLE_WIDTH)) {
270             boolean fixedWidth = store.getBoolean(IDebugPreferenceConstants.CONSOLE_WRAP);
271             if (fixedWidth) {
272                 int width = store.getInt(IDebugPreferenceConstants.CONSOLE_WIDTH);
273                 setConsoleWidth(width);
274             } else {
275                 setConsoleWidth(-1);
276             }
277         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_LIMIT_CONSOLE_OUTPUT) || property.equals(IDebugPreferenceConstants.CONSOLE_HIGH_WATER_MARK) || property.equals(IDebugPreferenceConstants.CONSOLE_LOW_WATER_MARK)) {
278             boolean limitBufferSize = store.getBoolean(IDebugPreferenceConstants.CONSOLE_LIMIT_CONSOLE_OUTPUT);
279             if (limitBufferSize) {
280                 int highWater = store.getInt(IDebugPreferenceConstants.CONSOLE_HIGH_WATER_MARK);
281                 int lowWater = store.getInt(IDebugPreferenceConstants.CONSOLE_LOW_WATER_MARK);
282                 if (highWater > lowWater) {
283                     setWaterMarks(lowWater, highWater);
284                 }
285             } else {
286                 setWaterMarks(-1, -1);
287             }
288         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_TAB_WIDTH)) {
289             int tabWidth = store.getInt(IDebugPreferenceConstants.CONSOLE_TAB_WIDTH);
290             setTabWidth(tabWidth);
291         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT)) {
292             boolean activateOnOut = store.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT);
293             IOConsoleOutputStream stream = getStream(IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM);
294             if (stream != null) {
295                 stream.setActivateOnWrite(activateOnOut);
296             }
297         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR)) {
298             boolean activateOnErr = store.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR);
299             IOConsoleOutputStream stream = getStream(IDebugUIConstants.ID_STANDARD_ERROR_STREAM);
300             if (stream != null) {
301                 stream.setActivateOnWrite(activateOnErr);
302             }
303         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_SYS_OUT_COLOR)) {
304             IOConsoleOutputStream stream = getStream(IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM);
305             if (stream != null) {
306                 stream.setColor(fColorProvider.getColor(IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM));
307             }
308         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_SYS_ERR_COLOR)) {
309             IOConsoleOutputStream stream = getStream(IDebugUIConstants.ID_STANDARD_ERROR_STREAM);
310             if (stream != null) {
311                 stream.setColor(fColorProvider.getColor(IDebugUIConstants.ID_STANDARD_ERROR_STREAM));
312             }
313         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_SYS_IN_COLOR)) {
314             if (fInput != null) {
315                 fInput.setColor(fColorProvider.getColor(IDebugUIConstants.ID_STANDARD_INPUT_STREAM));
316             }
317         } else if (property.equals(IDebugUIConstants.PREF_CONSOLE_FONT)) {
318             setFont(JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT));
319         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR)) {
320             setBackground(DebugUIPlugin.getPreferenceColor(IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR));
321         }
322     }
323
324     /**
325      * @see org.eclipse.debug.ui.console.IConsole#getStream(java.lang.String)
326      */

327     public IOConsoleOutputStream getStream(String JavaDoc streamIdentifier) {
328         for (Iterator JavaDoc i = fStreamListeners.iterator(); i.hasNext();) {
329             StreamListener listener = (StreamListener) i.next();
330             if (listener.fStreamId.equals(streamIdentifier)) {
331                 return listener.fStream;
332             }
333         }
334         return null;
335     }
336
337     /**
338      * @see org.eclipse.debug.ui.console.IConsole#getProcess()
339      */

340     public IProcess getProcess() {
341         return fProcess;
342     }
343
344     /**
345      * @see org.eclipse.ui.console.IOConsole#dispose()
346      */

347     protected void dispose() {
348         super.dispose();
349         fColorProvider.disconnect();
350         closeStreams();
351         disposeStreams();
352         DebugPlugin.getDefault().removeDebugEventListener(this);
353         DebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
354         JFaceResources.getFontRegistry().removeListener(this);
355     }
356
357     /**
358      * cleanup method to clsoe all of the open stream to this console
359      */

360     private synchronized void closeStreams() {
361         if (fStreamsClosed) {
362             return;
363         }
364         for (Iterator JavaDoc i = fStreamListeners.iterator(); i.hasNext();) {
365             StreamListener listener = (StreamListener) i.next();
366             listener.closeStream();
367         }
368         if (fFileOutputStream != null) {
369             synchronized (fFileOutputStream) {
370                 try {
371                     fFileOutputStream.flush();
372                     fFileOutputStream.close();
373                 } catch (IOException JavaDoc e) {
374                 }
375             }
376         }
377         try {
378             fInput.close();
379         } catch (IOException JavaDoc e) {
380         }
381         fStreamsClosed = true;
382     }
383
384     /**
385      * disposes ofthe listeners for each of the stream associated with this console
386      */

387     private synchronized void disposeStreams() {
388         for (Iterator JavaDoc i = fStreamListeners.iterator(); i.hasNext();) {
389             StreamListener listener = (StreamListener) i.next();
390             listener.dispose();
391         }
392         fFileOutputStream = null;
393         fInput = null;
394     }
395
396     /**
397      * @see org.eclipse.ui.console.AbstractConsole#init()
398      */

399     protected void init() {
400         super.init();
401         if (fProcess.isTerminated()) {
402             closeStreams();
403             resetName();
404         } else {
405             DebugPlugin.getDefault().addDebugEventListener(this);
406         }
407         IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore();
408         store.addPropertyChangeListener(this);
409         JFaceResources.getFontRegistry().addListener(this);
410         if (store.getBoolean(IDebugPreferenceConstants.CONSOLE_WRAP)) {
411             setConsoleWidth(store.getInt(IDebugPreferenceConstants.CONSOLE_WIDTH));
412         }
413         setTabWidth(store.getInt(IDebugPreferenceConstants.CONSOLE_TAB_WIDTH));
414
415         if (store.getBoolean(IDebugPreferenceConstants.CONSOLE_LIMIT_CONSOLE_OUTPUT)) {
416             int highWater = store.getInt(IDebugPreferenceConstants.CONSOLE_HIGH_WATER_MARK);
417             int lowWater = store.getInt(IDebugPreferenceConstants.CONSOLE_LOW_WATER_MARK);
418             setWaterMarks(lowWater, highWater);
419         }
420
421         DebugUIPlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
422             public void run() {
423                 setFont(JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT));
424                 setBackground(DebugUIPlugin.getPreferenceColor(IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR));
425             }
426         });
427     }
428
429     /**
430      * Notify listeners when name changes.
431      *
432      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
433      */

434     public void handleDebugEvents(DebugEvent[] events) {
435         for (int i = 0; i < events.length; i++) {
436             DebugEvent event = events[i];
437             if (event.getSource().equals(getProcess())) {
438
439                 if (event.getKind() == DebugEvent.TERMINATE) {
440                     closeStreams();
441                     DebugPlugin.getDefault().removeDebugEventListener(this);
442                 }
443
444                 resetName();
445             }
446         }
447     }
448
449     /**
450      * resets the name of this console to the original computed name
451      */

452     private void resetName() {
453         final String JavaDoc newName = computeName();
454         String JavaDoc name = getName();
455         if (!name.equals(newName)) {
456             Runnable JavaDoc r = new Runnable JavaDoc() {
457                 public void run() {
458                     setName(newName);
459                     warnOfContentChange();
460                 }
461             };
462             DebugUIPlugin.getStandardDisplay().asyncExec(r);
463         }
464     }
465
466     /**
467      * send notification of a change of content in this console
468      */

469     private void warnOfContentChange() {
470         ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(DebugUITools.getConsole(fProcess));
471     }
472
473     /**
474      * @see org.eclipse.debug.ui.console.IConsole#connect(org.eclipse.debug.core.model.IStreamsProxy)
475      */

476     public void connect(IStreamsProxy streamsProxy) {
477         IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore();
478         IStreamMonitor streamMonitor = streamsProxy.getErrorStreamMonitor();
479         if (streamMonitor != null) {
480             connect(streamMonitor, IDebugUIConstants.ID_STANDARD_ERROR_STREAM,
481                     store.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR));
482         }
483         streamMonitor = streamsProxy.getOutputStreamMonitor();
484         if (streamMonitor != null) {
485             connect(streamMonitor, IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM,
486                     store.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT));
487         }
488         InputReadJob readJob = new InputReadJob(streamsProxy);
489         readJob.setSystem(true);
490         readJob.schedule();
491     }
492
493     /**
494      * @see org.eclipse.debug.ui.console.IConsole#connect(org.eclipse.debug.core.model.IStreamMonitor, java.lang.String)
495      */

496     public void connect(IStreamMonitor streamMonitor, String JavaDoc streamIdentifier) {
497         connect(streamMonitor, streamIdentifier, false);
498     }
499     
500     /**
501      * Connects the given stream monitor to a new output stream with the given identifier.
502      *
503      * @param streamMonitor stream monitor
504      * @param streamIdentifier stream identifier
505      * @param activateOnWrite whether the stream should displayed when written to
506      */

507     private void connect(IStreamMonitor streamMonitor, String JavaDoc streamIdentifier, boolean activateOnWrite) {
508         IOConsoleOutputStream stream = null;
509         if (fAllocateConsole) {
510             stream = newOutputStream();
511             Color color = fColorProvider.getColor(streamIdentifier);
512             stream.setColor(color);
513             stream.setActivateOnWrite(activateOnWrite);
514         }
515         synchronized (streamMonitor) {
516             StreamListener listener = new StreamListener(streamIdentifier, streamMonitor, stream);
517             fStreamListeners.add(listener);
518         }
519     }
520
521     /**
522      * @see org.eclipse.debug.ui.console.IConsole#addLink(org.eclipse.debug.ui.console.IConsoleHyperlink, int, int)
523      */

524     public void addLink(IConsoleHyperlink link, int offset, int length) {
525         try {
526             addHyperlink(link, offset, length);
527         } catch (BadLocationException e) {
528             DebugUIPlugin.log(e);
529         }
530     }
531
532     /**
533      * @see org.eclipse.debug.ui.console.IConsole#addLink(org.eclipse.ui.console.IHyperlink, int, int)
534      */

535     public void addLink(IHyperlink link, int offset, int length) {
536         try {
537             addHyperlink(link, offset, length);
538         } catch (BadLocationException e) {
539             DebugUIPlugin.log(e);
540         }
541     }
542
543     /**
544      * @see org.eclipse.debug.ui.console.IConsole#getRegion(org.eclipse.debug.ui.console.IConsoleHyperlink)
545      */

546     public IRegion getRegion(IConsoleHyperlink link) {
547         return super.getRegion(link);
548     }
549
550     /**
551      * This class listens to a specified IO stream
552      */

553     private class StreamListener implements IStreamListener {
554
555         private IOConsoleOutputStream fStream;
556
557         private IStreamMonitor fStreamMonitor;
558
559         private String JavaDoc fStreamId;
560
561         private boolean fFlushed = false;
562
563         private boolean fListenerRemoved = false;
564
565         public StreamListener(String JavaDoc streamIdentifier, IStreamMonitor monitor, IOConsoleOutputStream stream) {
566             this.fStreamId = streamIdentifier;
567             this.fStreamMonitor = monitor;
568             this.fStream = stream;
569             fStreamMonitor.addListener(this);
570             //fix to bug 121454. Ensure that output to fast processes is processed.
571
streamAppended(null, monitor);
572         }
573
574         /*
575          * (non-Javadoc)
576          *
577          * @see org.eclipse.debug.core.IStreamListener#streamAppended(java.lang.String,
578          * org.eclipse.debug.core.model.IStreamMonitor)
579          */

580         public void streamAppended(String JavaDoc text, IStreamMonitor monitor) {
581             String JavaDoc encoding = getEncoding();
582             if (fFlushed) {
583                 try {
584                     if (fStream != null) {
585                         if (encoding == null)
586                             fStream.write(text);
587                         else
588                             fStream.write(text.getBytes(encoding));
589                     }
590                     if (fFileOutputStream != null) {
591                         synchronized (fFileOutputStream) {
592                             if (encoding == null)
593                                 fFileOutputStream.write(text.getBytes());
594                             else
595                                 fFileOutputStream.write(text.getBytes(encoding));
596                         }
597                     }
598                 } catch (IOException JavaDoc e) {
599                     DebugUIPlugin.log(e);
600                 }
601             } else {
602                 String JavaDoc contents = null;
603                 synchronized (fStreamMonitor) {
604                     fFlushed = true;
605                     contents = fStreamMonitor.getContents();
606                     if (fStreamMonitor instanceof IFlushableStreamMonitor) {
607                         IFlushableStreamMonitor m = (IFlushableStreamMonitor) fStreamMonitor;
608                         m.flushContents();
609                         m.setBuffered(false);
610                     }
611                 }
612                 try {
613                     if (contents != null && contents.length() > 0) {
614                         if (fStream != null) {
615                             fStream.write(contents);
616                         }
617                         if (fFileOutputStream != null) {
618                             synchronized (fFileOutputStream) {
619                                 fFileOutputStream.write(contents.getBytes());
620                             }
621                         }
622                     }
623                 } catch (IOException JavaDoc e) {
624                     DebugUIPlugin.log(e);
625                 }
626             }
627         }
628
629         public IStreamMonitor getStreamMonitor() {
630             return fStreamMonitor;
631         }
632
633         public void closeStream() {
634             if (fStreamMonitor == null) {
635                 return;
636             }
637             synchronized (fStreamMonitor) {
638                 fStreamMonitor.removeListener(this);
639                 if (!fFlushed) {
640                     String JavaDoc contents = fStreamMonitor.getContents();
641                     streamAppended(contents, fStreamMonitor);
642                 }
643                 fListenerRemoved = true;
644                 try {
645                     if (fStream != null) {
646                         fStream.close();
647                     }
648                 } catch (IOException JavaDoc e) {
649                 }
650             }
651         }
652
653         public void dispose() {
654             if (!fListenerRemoved) {
655                 closeStream();
656             }
657             fStream = null;
658             fStreamMonitor = null;
659             fStreamId = null;
660         }
661     }
662
663     private class InputReadJob extends Job {
664
665         private IStreamsProxy streamsProxy;
666
667         InputReadJob(IStreamsProxy streamsProxy) {
668             super("Process Console Input Job"); //$NON-NLS-1$
669
this.streamsProxy = streamsProxy;
670         }
671
672         /*
673          * (non-Javadoc)
674          *
675          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
676          */

677         protected IStatus run(IProgressMonitor monitor) {
678             try {
679                 byte[] b = new byte[1024];
680                 int read = 0;
681                 while (fInput != null && read >= 0) {
682                     read = fInput.read(b);
683                     if (read > 0) {
684                         String JavaDoc s = new String JavaDoc(b, 0, read);
685                         streamsProxy.write(s);
686                     }
687                 }
688             } catch (IOException JavaDoc e) {
689                 DebugUIPlugin.log(e);
690             }
691             return Status.OK_STATUS;
692         }
693     }
694
695     /*
696      * (non-Javadoc)
697      *
698      * @see org.eclipse.ui.console.IConsole#getImageDescriptor()
699      */

700     public ImageDescriptor getImageDescriptor() {
701         if (super.getImageDescriptor() == null) {
702             setImageDescriptor(computeImageDescriptor());
703         }
704         return super.getImageDescriptor();
705     }
706
707     private class ConsoleLogFilePatternMatcher implements IPatternMatchListener {
708         String JavaDoc fFilePath;
709
710         public ConsoleLogFilePatternMatcher(String JavaDoc filePath) {
711             fFilePath = escape(filePath);
712         }
713         
714         private String JavaDoc escape(String JavaDoc path) {
715             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(path);
716             int index = buffer.indexOf("\\"); //$NON-NLS-1$
717
while (index >= 0) {
718                 buffer.insert(index, '\\');
719                 index = buffer.indexOf("\\", index+2); //$NON-NLS-1$
720
}
721             return buffer.toString();
722         }
723         
724         public String JavaDoc getPattern() {
725             return fFilePath;
726         }
727
728         public void matchFound(PatternMatchEvent event) {
729             try {
730                 addHyperlink(new ConsoleLogFileHyperlink(fFilePath), event.getOffset(), event.getLength());
731                 removePatternMatchListener(this);
732             } catch (BadLocationException e) {
733             }
734         }
735
736         public int getCompilerFlags() {
737             return 0;
738         }
739
740         public String JavaDoc getLineQualifier() {
741             return null;
742         }
743
744         public void connect(TextConsole console) {
745         }
746
747         public void disconnect() {
748         }
749     }
750
751     private class ConsoleLogFileHyperlink implements IHyperlink {
752         String JavaDoc fFilePath;
753         ConsoleLogFileHyperlink(String JavaDoc filePath) {
754             fFilePath = filePath;
755         }
756         
757         public void linkActivated() {
758             IEditorInput input;
759             Path path = new Path(fFilePath);
760             IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
761             IFile ifile = root.getFileForLocation(path);
762             if (ifile == null) { // The file is not in the workspace
763
File JavaDoc file = new File JavaDoc(fFilePath);
764                 LocalFileStorage lfs = new LocalFileStorage(file);
765                 input = new StorageEditorInput(lfs, file);
766
767             } else {
768                 input = new FileEditorInput(ifile);
769             }
770             
771             IWorkbenchPage activePage = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage();
772             try {
773                 activePage.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID, true);
774             } catch (PartInitException e) {
775             }
776         }
777         public void linkEntered() {
778         }
779         public void linkExited() {
780         }
781     }
782     
783     class StorageEditorInput extends PlatformObject implements IStorageEditorInput {
784         private File JavaDoc fFile;
785         private IStorage fStorage;
786         
787         public StorageEditorInput(IStorage storage, File JavaDoc file) {
788             fStorage = storage;
789             fFile = file;
790         }
791         
792         public IStorage getStorage() {
793             return fStorage;
794         }
795
796         public ImageDescriptor getImageDescriptor() {
797             return null;
798         }
799
800         public String JavaDoc getName() {
801             return getStorage().getName();
802         }
803
804         public IPersistableElement getPersistable() {
805             return null;
806         }
807
808         public String JavaDoc getToolTipText() {
809             return getStorage().getFullPath().toOSString();
810         }
811         
812         public boolean equals(Object JavaDoc object) {
813             return object instanceof StorageEditorInput &&
814              getStorage().equals(((StorageEditorInput)object).getStorage());
815         }
816         
817         public int hashCode() {
818             return getStorage().hashCode();
819         }
820
821         public boolean exists() {
822             return fFile.exists();
823         }
824     }
825
826     /* (non-Javadoc)
827      * @see org.eclipse.ui.console.AbstractConsole#getHelpContextId()
828      */

829     public String JavaDoc getHelpContextId() {
830         return IDebugHelpContextIds.PROCESS_CONSOLE;
831     }
832 }
833
Popular Tags