KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.MessageFormat;
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.ILaunchConfiguration;
28 import org.eclipse.debug.core.ILaunchListener;
29 import org.eclipse.debug.core.ILaunchManager;
30 import org.eclipse.debug.core.model.IProcess;
31 import org.eclipse.debug.internal.ui.DebugUIPlugin;
32 import org.eclipse.debug.ui.IDebugUIConstants;
33 import org.eclipse.debug.ui.console.ConsoleColorProvider;
34 import org.eclipse.debug.ui.console.IConsoleColorProvider;
35 import org.eclipse.debug.ui.console.IConsoleLineTracker;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.ui.console.ConsolePlugin;
38 import org.eclipse.ui.console.IConsole;
39 import org.eclipse.ui.console.IConsoleManager;
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 ProcessConsoleManager implements ILaunchListener {
46     
47     /**
48      * Console document content provider extensions, keyed by extension id
49      */

50     private Map JavaDoc fColorProviders;
51     
52     /**
53      * The default color provider. Used if no color provider is contributed
54      * for the given process type.
55      */

56     private IConsoleColorProvider fDefaultColorProvider;
57     
58     /**
59      * Console line trackers; keyed by process type to list of trackers (1:N)
60      */

61     private Map JavaDoc fLineTrackers;
62     
63     /**
64      * Map of processes for a launch to compute removed processes
65      */

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

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

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

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

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

133     public void launchChanged(final ILaunch launch) {
134         IProcess[] processes= launch.getProcesses();
135         for (int i= 0; i < processes.length; i++) {
136             if (getConsoleDocument(processes[i]) == null) {
137                 IProcess process = processes[i];
138                 if (process.getStreamsProxy() == null) {
139                     continue;
140                 }
141                 ILaunchConfiguration launchConfiguration = launch.getLaunchConfiguration();
142
143                 //create a new console.
144
IConsoleColorProvider colorProvider = getColorProvider(process.getAttribute(IProcess.ATTR_PROCESS_TYPE));
145                 String JavaDoc encoding = null;
146                 try {
147                     if (launchConfiguration != null) {
148                         encoding = launchConfiguration.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, (String JavaDoc)null);
149                     }
150                 } catch (CoreException e) {
151                 }
152                 ProcessConsole pc = new ProcessConsole(process, colorProvider, encoding);
153                 pc.setAttribute(IDebugUIConstants.ATTR_CONSOLE_PROCESS, process);
154
155                 //add new console to console manager.
156
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{pc});
157             }
158         }
159         List JavaDoc removed = getRemovedProcesses(launch);
160         if (removed != null) {
161             Iterator JavaDoc iterator = removed.iterator();
162             while (iterator.hasNext()) {
163                 IProcess p = (IProcess) iterator.next();
164                 removeProcess(p);
165             }
166         }
167     }
168     
169     /**
170      * Returns the document for the process, or <code>null</code>
171      * if none.
172      */

173     public IDocument getConsoleDocument(IProcess process) {
174         ProcessConsole console = (ProcessConsole) getConsole(process);
175         return (console != null ? console.getDocument() : null);
176     }
177     
178     /**
179      * Called by the debug ui plug-in on startup.
180      * The console document manager starts listening for
181      * launches to be registered and initializes if any launches
182      * already exist.
183      */

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

200     public void shutdown() {
201         ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager();
202         ILaunch[] launches = launchManager.getLaunches();
203         for (int i = 0; i < launches.length; i++) {
204             ILaunch launch = launches[i];
205             removeLaunch(launch);
206         }
207         launchManager.removeLaunchListener(this);
208         if (fProcesses != null) {
209             fProcesses.clear();
210         }
211     }
212           
213     /**
214      * Returns a new console document color provider extension for the given
215      * process type, or <code>null</code> if none.
216      *
217      * @param type corresponds to <code>IProcess.ATTR_PROCESS_TYPE</code>
218      * @return IConsoleColorProvider
219      */

220     public IConsoleColorProvider getColorProvider(String JavaDoc type) {
221         if (fColorProviders == null) {
222             fColorProviders = new HashMap JavaDoc();
223             IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_CONSOLE_COLOR_PROVIDERS);
224             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
225             for (int i = 0; i < elements.length; i++) {
226                 IConfigurationElement extension = elements[i];
227                 fColorProviders.put(extension.getAttribute("processType"), extension); //$NON-NLS-1$
228
}
229         }
230         IConfigurationElement extension = (IConfigurationElement)fColorProviders.get(type);
231         if (extension != null) {
232             try {
233                 Object JavaDoc colorProvider = extension.createExecutableExtension("class"); //$NON-NLS-1$
234
if (colorProvider instanceof IConsoleColorProvider) {
235                     return (IConsoleColorProvider)colorProvider;
236                 }
237                 DebugUIPlugin.logErrorMessage(MessageFormat.format(
238                         "Extension {0} must specify an instanceof IConsoleColorProvider for class attribute.", //$NON-NLS-1$
239
new String JavaDoc[]{extension.getDeclaringExtension().getUniqueIdentifier()}));
240             } catch (CoreException e) {
241                 DebugUIPlugin.log(e);
242             }
243         }
244         //no color provider found of specified type, return default color provider.
245
if (fDefaultColorProvider == null) {
246             fDefaultColorProvider = new ConsoleColorProvider();
247         }
248         return fDefaultColorProvider;
249     }
250     
251     /**
252      * Returns the Line Trackers for a given process type.
253      * @param process The process for which line trackers are required.
254      * @return An array of line trackers which match the given process type.
255      */

256     public IConsoleLineTracker[] getLineTrackers(IProcess process) {
257         String JavaDoc type = process.getAttribute(IProcess.ATTR_PROCESS_TYPE);
258         
259         if (fLineTrackers == null) {
260             fLineTrackers = new HashMap JavaDoc();
261             IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_CONSOLE_LINE_TRACKERS);
262             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
263             for (int i = 0; i < elements.length; i++) {
264                 IConfigurationElement extension = elements[i];
265                 String JavaDoc processType = extension.getAttribute("processType"); //$NON-NLS-1$
266
List JavaDoc list = (List JavaDoc)fLineTrackers.get(processType);
267                 if (list == null) {
268                     list = new ArrayList JavaDoc();
269                     fLineTrackers.put(processType, list);
270                 }
271                 list.add(extension);
272             }
273         }
274         
275         ArrayList JavaDoc trackers = new ArrayList JavaDoc();
276         if (type != null) {
277             List JavaDoc lineTrackerExtensions = (List JavaDoc) fLineTrackers.get(type);
278             if(lineTrackerExtensions != null) {
279                 for(Iterator JavaDoc i = lineTrackerExtensions.iterator(); i.hasNext(); ) {
280                     IConfigurationElement element = (IConfigurationElement) i.next();
281                     try {
282                         trackers.add(element.createExecutableExtension("class")); //$NON-NLS-1$
283
} catch (CoreException e) {
284                         DebugUIPlugin.log(e);
285                     }
286                 }
287             }
288         }
289         return (IConsoleLineTracker[]) trackers.toArray(new IConsoleLineTracker[0]);
290     }
291     
292     /**
293      * Returns the processes that have been removed from the given
294      * launch, or <code>null</code> if none.
295      *
296      * @param launch launch that has changed
297      * @return removed processes or <code>null</code>
298      */

299     private List JavaDoc getRemovedProcesses(ILaunch launch) {
300         List JavaDoc removed = null;
301         if (fProcesses == null) {
302             fProcesses = new HashMap JavaDoc();
303         }
304         IProcess[] old = (IProcess[]) fProcesses.get(launch);
305         IProcess[] curr = launch.getProcesses();
306         if (old != null) {
307             for (int i = 0; i < old.length; i++) {
308                 IProcess process = old[i];
309                 if (!contains(curr, process)) {
310                     if (removed == null) {
311                         removed = new ArrayList JavaDoc();
312                     }
313                     removed.add(process);
314                 }
315             }
316         }
317         // update cache with current processes
318
fProcesses.put(launch, curr);
319         return removed;
320     }
321     
322     /**
323      * Returns whether the given object is contained in the list.
324      *
325      * @param list list to search
326      * @param object object to search for
327      * @return whether the given object is contained in the list
328      */

329     private boolean contains(Object JavaDoc[] list, Object JavaDoc object) {
330         for (int i = 0; i < list.length; i++) {
331             Object JavaDoc object2 = list[i];
332             if (object2.equals(object)) {
333                 return true;
334             }
335         }
336         return false;
337     }
338 }
339
Popular Tags