KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
32  * Tracks text appended to the console and notifies listeners in terms of whole
33  * lines.
34  */

35 public class ConsoleLineNotifier implements IPatternMatchListener, IPropertyChangeListener {
36     /**
37      * Console listeners
38      */

39     private List JavaDoc fListeners = new ArrayList JavaDoc(2);
40
41     /**
42      * The console this notifier is tracking
43      */

44     private ProcessConsole fConsole = null;
45     
46     /* (non-Javadoc)
47      * @see org.eclipse.ui.console.IPatternMatchListenerDelegate#connect(org.eclipse.ui.console.TextConsole)
48      */

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     /* (non-Javadoc)
64      * @see org.eclipse.ui.console.IPatternMatchListener#disconnect()
65      */

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     /**
81      * Notification the console's streams have been closed
82      */

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     /**
98      * Adds the given listener to the list of listeners notified when a line of
99      * text is appended to the console.
100      *
101      * @param listener
102      */

103     public void addConsoleListener(IConsoleLineTracker listener) {
104         if (!fListeners.contains(listener))
105             fListeners.add(listener);
106     }
107     
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.console.IPatternMatchListener#matchFound(org.eclipse.ui.console.PatternMatchEvent)
110      */

111     public void matchFound(PatternMatchEvent event) {
112         try {
113             IDocument document = fConsole.getDocument();
114             int lineOfOffset = document.getLineOfOffset(event.getOffset());
115             String JavaDoc 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     /* (non-Javadoc)
131      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
132      */

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     /* (non-Javadoc)
141      * @see org.eclipse.ui.console.IPatternMatchListener#getPattern()
142      */

143     public String JavaDoc getPattern() {
144         return ".*\\r(\\n?)|.*\\n"; //$NON-NLS-1$
145
}
146
147     /* (non-Javadoc)
148      * @see org.eclipse.ui.console.IPatternMatchListener#getCompilerFlags()
149      */

150     public int getCompilerFlags() {
151         return 0;
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.ui.console.IPatternMatchListener#getLineQualifier()
156      */

157     public String JavaDoc getLineQualifier() {
158         return "\\n|\\r"; //$NON-NLS-1$
159
}
160
161 }
162
Popular Tags