1 11 package org.eclipse.debug.internal.ui.views.console; 12 13 14 import com.ibm.icu.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.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 45 public class ProcessConsoleManager implements ILaunchListener { 46 47 50 private Map fColorProviders; 51 52 56 private IConsoleColorProvider fDefaultColorProvider; 57 58 61 private Map fLineTrackers; 62 63 66 private Map fProcesses; 67 68 69 70 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 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 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 126 public void launchAdded(ILaunch launch) { 127 launchChanged(launch); 128 } 129 130 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 IConsoleColorProvider colorProvider = getColorProvider(process.getAttribute(IProcess.ATTR_PROCESS_TYPE)); 145 String encoding = null; 146 try { 147 if (launchConfiguration != null) { 148 encoding = launchConfiguration.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, (String )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 ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{pc}); 157 } 158 } 159 List removed = getRemovedProcesses(launch); 160 if (removed != null) { 161 Iterator iterator = removed.iterator(); 162 while (iterator.hasNext()) { 163 IProcess p = (IProcess) iterator.next(); 164 removeProcess(p); 165 } 166 } 167 } 168 169 173 public IDocument getConsoleDocument(IProcess process) { 174 ProcessConsole console = (ProcessConsole) getConsole(process); 175 return (console != null ? console.getDocument() : null); 176 } 177 178 184 public void startup() { 185 ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager(); 186 launchManager.addLaunchListener(this); 187 188 ILaunch[] launches= launchManager.getLaunches(); 190 for (int i = 0; i < launches.length; i++) { 191 launchAdded(launches[i]); 192 } 193 } 194 195 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 220 public IConsoleColorProvider getColorProvider(String type) { 221 if (fColorProviders == null) { 222 fColorProviders = new HashMap (); 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); } 229 } 230 IConfigurationElement extension = (IConfigurationElement)fColorProviders.get(type); 231 if (extension != null) { 232 try { 233 Object colorProvider = extension.createExecutableExtension("class"); 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.", new String []{extension.getDeclaringExtension().getUniqueIdentifier()})); 240 } catch (CoreException e) { 241 DebugUIPlugin.log(e); 242 } 243 } 244 if (fDefaultColorProvider == null) { 246 fDefaultColorProvider = new ConsoleColorProvider(); 247 } 248 return fDefaultColorProvider; 249 } 250 251 256 public IConsoleLineTracker[] getLineTrackers(IProcess process) { 257 String type = process.getAttribute(IProcess.ATTR_PROCESS_TYPE); 258 259 if (fLineTrackers == null) { 260 fLineTrackers = new HashMap (); 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 processType = extension.getAttribute("processType"); List list = (List )fLineTrackers.get(processType); 267 if (list == null) { 268 list = new ArrayList (); 269 fLineTrackers.put(processType, list); 270 } 271 list.add(extension); 272 } 273 } 274 275 ArrayList trackers = new ArrayList (); 276 if (type != null) { 277 List lineTrackerExtensions = (List ) fLineTrackers.get(type); 278 if(lineTrackerExtensions != null) { 279 for(Iterator i = lineTrackerExtensions.iterator(); i.hasNext(); ) { 280 IConfigurationElement element = (IConfigurationElement) i.next(); 281 try { 282 trackers.add(element.createExecutableExtension("class")); } catch (CoreException e) { 284 DebugUIPlugin.log(e); 285 } 286 } 287 } 288 } 289 return (IConsoleLineTracker[]) trackers.toArray(new IConsoleLineTracker[0]); 290 } 291 292 299 private List getRemovedProcesses(ILaunch launch) { 300 List removed = null; 301 if (fProcesses == null) { 302 fProcesses = new HashMap (); 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 (); 312 } 313 removed.add(process); 314 } 315 } 316 } 317 fProcesses.put(launch, curr); 319 return removed; 320 } 321 322 329 private boolean contains(Object [] list, Object object) { 330 for (int i = 0; i < list.length; i++) { 331 Object object2 = list[i]; 332 if (object2.equals(object)) { 333 return true; 334 } 335 } 336 return false; 337 } 338 } 339 | Popular Tags |