1 11 package org.eclipse.debug.internal.ui.views.console; 12 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.debug.internal.ui.DebugUIPlugin; 18 import org.eclipse.debug.ui.console.IConsoleLineTracker; 19 import org.eclipse.debug.ui.console.IConsoleLineTrackerExtension; 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IRegion; 23 import org.eclipse.jface.text.Region; 24 import org.eclipse.jface.util.IPropertyChangeListener; 25 import org.eclipse.jface.util.PropertyChangeEvent; 26 import org.eclipse.ui.console.IConsoleConstants; 27 import org.eclipse.ui.console.IPatternMatchListener; 28 import org.eclipse.ui.console.PatternMatchEvent; 29 import org.eclipse.ui.console.TextConsole; 30 31 35 public class ConsoleLineNotifier implements IPatternMatchListener, IPropertyChangeListener { 36 39 private List fListeners = new ArrayList (2); 40 41 44 private ProcessConsole fConsole = null; 45 46 49 public void connect(TextConsole console) { 50 if (console instanceof ProcessConsole) { 51 fConsole = (ProcessConsole)console; 52 53 IConsoleLineTracker[] lineTrackers = DebugUIPlugin.getDefault().getProcessConsoleManager().getLineTrackers(fConsole.getProcess()); 54 for (int i = 0; i < lineTrackers.length; i++) { 55 lineTrackers[i].init(fConsole); 56 addConsoleListener(lineTrackers[i]); 57 } 58 59 fConsole.addPropertyChangeListener(this); 60 } 61 } 62 63 66 public synchronized void disconnect() { 67 try { 68 IDocument document = fConsole.getDocument(); 69 if (document != null) { 70 int lastLine = document.getNumberOfLines() - 1; 71 if (document.getLineDelimiter(lastLine) == null) { 72 IRegion lineInformation = document.getLineInformation(lastLine); 73 lineAppended(lineInformation); 74 } 75 } 76 } catch (BadLocationException e) { 77 } 78 } 79 80 83 public synchronized void consoleClosed() { 84 int size = fListeners.size(); 85 for (int i = 0; i < size; i++) { 86 IConsoleLineTracker tracker = (IConsoleLineTracker) fListeners.get(i); 87 if (tracker instanceof IConsoleLineTrackerExtension) { 88 ((IConsoleLineTrackerExtension) tracker).consoleClosed(); 89 } 90 tracker.dispose(); 91 } 92 93 fConsole = null; 94 fListeners = null; 95 } 96 97 103 public void addConsoleListener(IConsoleLineTracker listener) { 104 if (!fListeners.contains(listener)) 105 fListeners.add(listener); 106 } 107 108 111 public void matchFound(PatternMatchEvent event) { 112 try { 113 IDocument document = fConsole.getDocument(); 114 int lineOfOffset = document.getLineOfOffset(event.getOffset()); 115 String delimiter = document.getLineDelimiter(lineOfOffset); 116 int strip = delimiter==null ? 0 : delimiter.length(); 117 Region region = new Region(event.getOffset(), event.getLength()-strip); 118 lineAppended(region); 119 } catch (BadLocationException e) {} 120 } 121 122 public void lineAppended(IRegion region) { 123 int size = fListeners.size(); 124 for (int i=0; i<size; i++) { 125 IConsoleLineTracker tracker = (IConsoleLineTracker) fListeners.get(i); 126 tracker.lineAppended(region); 127 } 128 } 129 130 133 public void propertyChange(PropertyChangeEvent event) { 134 if(event.getProperty().equals(IConsoleConstants.P_CONSOLE_OUTPUT_COMPLETE)) { 135 fConsole.removePropertyChangeListener(this); 136 consoleClosed(); 137 } 138 } 139 140 143 public String getPattern() { 144 return ".*\\r(\\n?)|.*\\n"; } 146 147 150 public int getCompilerFlags() { 151 return 0; 152 } 153 154 157 public String getLineQualifier() { 158 return "\\n|\\r"; } 160 161 } 162 | Popular Tags |