KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.text.MessageFormat JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionPoint;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.debug.core.DebugPlugin;
26 import org.eclipse.debug.core.ILaunch;
27 import org.eclipse.debug.core.ILaunchListener;
28 import org.eclipse.debug.core.ILaunchManager;
29 import org.eclipse.debug.core.model.IProcess;
30 import org.eclipse.debug.internal.ui.DebugUIPlugin;
31 import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants;
32 import org.eclipse.debug.ui.IDebugUIConstants;
33 import org.eclipse.debug.ui.console.IConsoleColorProvider;
34 import org.eclipse.debug.ui.console.IConsoleLineTracker;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.ui.console.ConsolePlugin;
37 import org.eclipse.ui.console.IConsole;
38 import org.eclipse.ui.console.IConsoleManager;
39 import org.eclipse.ui.texteditor.IDocumentProvider;
40
41 /**
42  * Creates documents for processes as they are registered with a launch.
43  * The singleton manager is accessible from the debug UI plugin.
44  */

45 public class ConsoleDocumentManager implements ILaunchListener {
46     
47     /**
48      * Console document content provider extensions, keyed by extension id
49      */

50     private Map JavaDoc fColorProviders;
51     
52     /**
53      * Console line trackers; keyed by process type to list of trackers (1:N)
54      */

55     private Map JavaDoc fLineTrackers;
56     
57     /**
58      * Default document provider.
59      */

60     protected IDocumentProvider fDefaultDocumentProvider = null;
61     
62     /**
63      * Map of processes for a launch to compute removed processes
64      */

65     private Map JavaDoc fProcesses;
66     
67     /**
68      * @see ILaunchListener#launchRemoved(ILaunch)
69      */

70     public void launchRemoved(ILaunch launch) {
71         removeLaunch(launch);
72     }
73     
74     protected void removeLaunch(ILaunch launch) {
75         IProcess[] processes= launch.getProcesses();
76         for (int i= 0; i < processes.length; i++) {
77             IProcess iProcess = processes[i];
78             removeProcess(iProcess);
79         }
80         if (fProcesses != null) {
81             fProcesses.remove(launch);
82         }
83     }
84     
85     /**
86      * Removes the console and document associated with the given process.
87      *
88      * @param iProcess process to clean up
89      */

90     private void removeProcess(IProcess iProcess) {
91         IConsole console = getConsole(iProcess);
92         if (console != null) {
93             IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
94             manager.removeConsoles(new IConsole[]{console});
95         }
96         IDocumentProvider provider = getDocumentProvider();
97         provider.disconnect(iProcess);
98     }
99
100     /**
101      * Returns the console for the given process, or <code>null</code> if none.
102      *
103      * @param process
104      * @return the console for the given process, or <code>null</code> if none
105      */

106     public IConsole getConsole(IProcess process) {
107         IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
108         IConsole[] consoles = manager.getConsoles();
109         for (int i = 0; i < consoles.length; i++) {
110             IConsole console = consoles[i];
111             if (console instanceof ProcessConsole) {
112                 ProcessConsole pc = (ProcessConsole)console;
113                 if (pc.getProcess().equals(process)) {
114                     return pc;
115                 }
116             }
117         }
118         return null;
119     }
120
121     /**
122      * @see ILaunchListener#launchAdded(ILaunch)
123      */

124     public void launchAdded(ILaunch launch) {
125         launchChanged(launch);
126     }
127
128     /**
129      * @see ILaunchListener#launchChanged(ILaunch)
130      */

131     public void launchChanged(final ILaunch launch) {
132         DebugUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc () {
133             public void run() {
134                 IProcess[] processes= launch.getProcesses();
135                 for (int i= 0; i < processes.length; i++) {
136                     if (getConsoleDocument(processes[i]) == null) {
137                         // create new document
138
IProcess process = processes[i];
139                         IDocumentProvider provider = getDocumentProvider();
140                         try {
141                             provider.connect(process);
142                         } catch (CoreException e) {
143                         }
144                         ProcessConsole pc = new ProcessConsole(process);
145                         ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{pc});
146                     }
147                 }
148                 List JavaDoc removed = getRemovedProcesses(launch);
149                 if (removed != null) {
150                     Iterator JavaDoc iterator = removed.iterator();
151                     while (iterator.hasNext()) {
152                         IProcess p = (IProcess) iterator.next();
153                         removeProcess(p);
154                     }
155                 }
156             }
157         });
158     }
159     
160     /**
161      * Returns the document for the process, or <code>null</code>
162      * if none.
163      */

164     public IDocument getConsoleDocument(IProcess process) {
165         IDocumentProvider provider = getDocumentProvider();
166         return provider.getDocument(process);
167     }
168     
169     /**
170      * Returns the document provider.
171      *
172      * @return document provider
173      */

174     private IDocumentProvider getDocumentProvider() {
175         if (fDefaultDocumentProvider == null) {
176             fDefaultDocumentProvider = new ConsoleDocumentProvider();
177         }
178         return fDefaultDocumentProvider;
179     }
180         
181     /**
182      * Called by the debug ui plug-in on startup.
183      * The console document manager starts listening for
184      * launches to be registered and initializes if any launches
185      * already exist.
186      */

187     public void startup() {
188         ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager();
189         launchManager.addLaunchListener(this);
190
191         //set up the docs for launches already registered
192
ILaunch[] launches= launchManager.getLaunches();
193         for (int i = 0; i < launches.length; i++) {
194             launchAdded(launches[i]);
195         }
196     }
197     
198     /**
199      * Called by the debug ui plug-in on shutdown.
200      * The console document manager de-registers as a
201      * launch listener and kills all existing console documents.
202      */

203     public void shutdown() {
204         ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager();
205         ILaunch[] launches = launchManager.getLaunches();
206         for (int i = 0; i < launches.length; i++) {
207             ILaunch launch = launches[i];
208             removeLaunch(launch);
209         }
210         launchManager.removeLaunchListener(this);
211         if (fProcesses != null) {
212             fProcesses.clear();
213         }
214     }
215     
216     /**
217      * Notifies the console document manager that system err is about to be written
218      * to the console. The manager will open the console if the preference is
219      * set to show the console on system err.
220      */

221     protected void aboutToWriteSystemErr(IProcess process) {
222         if (DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR)) {
223             showConsole(process);
224         }
225     }
226     
227     /**
228      * Notifies the console document manager that system out is about to be written
229      * to the console. The manager will open the console if the preference is
230      * set to show the console on system out and the console document being written
231      * is associated with the current process.
232      */

233     protected void aboutToWriteSystemOut(IProcess process) {
234         if (DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT)) {
235             showConsole(process);
236         }
237     }
238     
239     /**
240      * Opens the console view. If the view is already open, it is brought to the front.
241      */

242     protected void showConsole(final IProcess process) {
243         ConsolePlugin.getDefault().getConsoleManager().showConsoleView(getConsole(process));
244     }
245     
246     /**
247      * Returns a new console document color provider extension for the given
248      * process type, or <code>null</code> if none.
249      *
250      * @param type corresponds to <code>IProcess.ATTR_PROCESS_TYPE</code>
251      * @return IConsoleColorProvider
252      */

253     public IConsoleColorProvider getColorProvider(String JavaDoc type) {
254         if (fColorProviders == null) {
255             fColorProviders = new HashMap JavaDoc();
256             IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_CONSOLE_COLOR_PROVIDERS);
257             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
258             for (int i = 0; i < elements.length; i++) {
259                 IConfigurationElement extension = elements[i];
260                 fColorProviders.put(extension.getAttributeAsIs("processType"), extension); //$NON-NLS-1$
261
}
262         }
263         IConfigurationElement extension = (IConfigurationElement)fColorProviders.get(type);
264         if (extension != null) {
265             try {
266                 Object JavaDoc colorProvider = extension.createExecutableExtension("class"); //$NON-NLS-1$
267
if (colorProvider instanceof IConsoleColorProvider) {
268                     return (IConsoleColorProvider)colorProvider;
269                 }
270                 DebugUIPlugin.logErrorMessage(MessageFormat.format(ConsoleMessages.getString("ConsoleDocumentManager.1"),new String JavaDoc[]{extension.getDeclaringExtension().getUniqueIdentifier()} )); //$NON-NLS-1$
271
} catch (CoreException e) {
272                 DebugUIPlugin.log(e);
273             }
274         }
275         return null;
276     }
277     
278     /**
279      * Creates and retuns a new line notifier for the given type of process, or
280      * <code>null</code> if none. The notifier will be seeded with new console
281      * line listeners registered for the given process type.
282      *
283      * @param type process type
284      * @return line notifier or <code>null</code>
285      */

286     public ConsoleLineNotifier newLineNotifier(String JavaDoc type) {
287         if (fLineTrackers == null) {
288             fLineTrackers = new HashMap JavaDoc();
289             IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_CONSOLE_LINE_TRACKERS);
290             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
291             for (int i = 0; i < elements.length; i++) {
292                 IConfigurationElement extension = elements[i];
293                 String JavaDoc processType = extension.getAttributeAsIs("processType"); //$NON-NLS-1$
294
List JavaDoc list = (List JavaDoc)fLineTrackers.get(processType);
295                 if (list == null) {
296                     list = new ArrayList JavaDoc();
297                     fLineTrackers.put(processType, list);
298                 }
299                 list.add(extension);
300             }
301         }
302         List JavaDoc extensions = (List JavaDoc)fLineTrackers.get(type);
303         ConsoleLineNotifier lineNotifier = null;
304         if (extensions != null) {
305             lineNotifier = new ConsoleLineNotifier();
306             Iterator JavaDoc iter = extensions.iterator();
307             while (iter.hasNext()) {
308                 IConfigurationElement extension = (IConfigurationElement)iter.next();
309                 try {
310                     Object JavaDoc tracker = extension.createExecutableExtension("class"); //$NON-NLS-1$
311
if (tracker instanceof IConsoleLineTracker) {
312                         lineNotifier.addConsoleListener((IConsoleLineTracker)tracker);
313                     } else {
314                         DebugUIPlugin.logErrorMessage(MessageFormat.format(ConsoleMessages.getString("ConsoleDocumentManager.2"),new String JavaDoc[]{extension.getDeclaringExtension().getUniqueIdentifier()})); //$NON-NLS-1$
315
}
316                 } catch (CoreException e) {
317                     DebugUIPlugin.log(e);
318                 }
319             }
320         }
321         return lineNotifier;
322     }
323     
324     /**
325      * Returns the processes that have been removed from the given
326      * launch, or <code>null</code> if none.
327      *
328      * @param launch launch that has changed
329      * @return removed processes or <code>null</code>
330      */

331     private List JavaDoc getRemovedProcesses(ILaunch launch) {
332         List JavaDoc removed = null;
333         if (fProcesses == null) {
334             fProcesses = new HashMap JavaDoc();
335         }
336         IProcess[] old = (IProcess[]) fProcesses.get(launch);
337         IProcess[] curr = launch.getProcesses();
338         if (old != null) {
339             for (int i = 0; i < old.length; i++) {
340                 IProcess process = old[i];
341                 if (!contains(curr, process)) {
342                     if (removed == null) {
343                         removed = new ArrayList JavaDoc();
344                     }
345                     removed.add(process);
346                 }
347             }
348         }
349         // update cache with current processes
350
fProcesses.put(launch, curr);
351         return removed;
352     }
353     
354     /**
355      * Returns whether the given object is contained in the list.
356      *
357      * @param list list to search
358      * @param object object to search for
359      * @return whether the given object is contained in the list
360      */

361     private boolean contains(Object JavaDoc[] list, Object JavaDoc object) {
362         for (int i = 0; i < list.length; i++) {
363             Object JavaDoc object2 = list[i];
364             if (object2.equals(object)) {
365                 return true;
366             }
367         }
368         return false;
369     }
370 }
371
Popular Tags