1 11 package org.eclipse.debug.internal.ui.views.console; 12 13 14 import java.text.MessageFormat ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 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 45 public class ConsoleDocumentManager implements ILaunchListener { 46 47 50 private Map fColorProviders; 51 52 55 private Map fLineTrackers; 56 57 60 protected IDocumentProvider fDefaultDocumentProvider = null; 61 62 65 private Map fProcesses; 66 67 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 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 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 124 public void launchAdded(ILaunch launch) { 125 launchChanged(launch); 126 } 127 128 131 public void launchChanged(final ILaunch launch) { 132 DebugUIPlugin.getStandardDisplay().syncExec(new Runnable () { 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 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 removed = getRemovedProcesses(launch); 149 if (removed != null) { 150 Iterator iterator = removed.iterator(); 151 while (iterator.hasNext()) { 152 IProcess p = (IProcess) iterator.next(); 153 removeProcess(p); 154 } 155 } 156 } 157 }); 158 } 159 160 164 public IDocument getConsoleDocument(IProcess process) { 165 IDocumentProvider provider = getDocumentProvider(); 166 return provider.getDocument(process); 167 } 168 169 174 private IDocumentProvider getDocumentProvider() { 175 if (fDefaultDocumentProvider == null) { 176 fDefaultDocumentProvider = new ConsoleDocumentProvider(); 177 } 178 return fDefaultDocumentProvider; 179 } 180 181 187 public void startup() { 188 ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager(); 189 launchManager.addLaunchListener(this); 190 191 ILaunch[] launches= launchManager.getLaunches(); 193 for (int i = 0; i < launches.length; i++) { 194 launchAdded(launches[i]); 195 } 196 } 197 198 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 221 protected void aboutToWriteSystemErr(IProcess process) { 222 if (DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR)) { 223 showConsole(process); 224 } 225 } 226 227 233 protected void aboutToWriteSystemOut(IProcess process) { 234 if (DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT)) { 235 showConsole(process); 236 } 237 } 238 239 242 protected void showConsole(final IProcess process) { 243 ConsolePlugin.getDefault().getConsoleManager().showConsoleView(getConsole(process)); 244 } 245 246 253 public IConsoleColorProvider getColorProvider(String type) { 254 if (fColorProviders == null) { 255 fColorProviders = new HashMap (); 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); } 262 } 263 IConfigurationElement extension = (IConfigurationElement)fColorProviders.get(type); 264 if (extension != null) { 265 try { 266 Object colorProvider = extension.createExecutableExtension("class"); if (colorProvider instanceof IConsoleColorProvider) { 268 return (IConsoleColorProvider)colorProvider; 269 } 270 DebugUIPlugin.logErrorMessage(MessageFormat.format(ConsoleMessages.getString("ConsoleDocumentManager.1"),new String []{extension.getDeclaringExtension().getUniqueIdentifier()} )); } catch (CoreException e) { 272 DebugUIPlugin.log(e); 273 } 274 } 275 return null; 276 } 277 278 286 public ConsoleLineNotifier newLineNotifier(String type) { 287 if (fLineTrackers == null) { 288 fLineTrackers = new HashMap (); 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 processType = extension.getAttributeAsIs("processType"); List list = (List )fLineTrackers.get(processType); 295 if (list == null) { 296 list = new ArrayList (); 297 fLineTrackers.put(processType, list); 298 } 299 list.add(extension); 300 } 301 } 302 List extensions = (List )fLineTrackers.get(type); 303 ConsoleLineNotifier lineNotifier = null; 304 if (extensions != null) { 305 lineNotifier = new ConsoleLineNotifier(); 306 Iterator iter = extensions.iterator(); 307 while (iter.hasNext()) { 308 IConfigurationElement extension = (IConfigurationElement)iter.next(); 309 try { 310 Object tracker = extension.createExecutableExtension("class"); if (tracker instanceof IConsoleLineTracker) { 312 lineNotifier.addConsoleListener((IConsoleLineTracker)tracker); 313 } else { 314 DebugUIPlugin.logErrorMessage(MessageFormat.format(ConsoleMessages.getString("ConsoleDocumentManager.2"),new String []{extension.getDeclaringExtension().getUniqueIdentifier()})); } 316 } catch (CoreException e) { 317 DebugUIPlugin.log(e); 318 } 319 } 320 } 321 return lineNotifier; 322 } 323 324 331 private List getRemovedProcesses(ILaunch launch) { 332 List removed = null; 333 if (fProcesses == null) { 334 fProcesses = new HashMap (); 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 (); 344 } 345 removed.add(process); 346 } 347 } 348 } 349 fProcesses.put(launch, curr); 351 return removed; 352 } 353 354 361 private boolean contains(Object [] list, Object object) { 362 for (int i = 0; i < list.length; i++) { 363 Object object2 = list[i]; 364 if (object2.equals(object)) { 365 return true; 366 } 367 } 368 return false; 369 } 370 } 371 | Popular Tags |