1 11 package org.eclipse.ui.internal.console; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Set ; 18 import java.util.regex.PatternSyntaxException ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtensionPoint; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.ISafeRunnable; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.ListenerList; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.SafeRunner; 29 import org.eclipse.core.runtime.Status; 30 import org.eclipse.core.runtime.jobs.Job; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.ui.IViewPart; 33 import org.eclipse.ui.IWorkbenchPage; 34 import org.eclipse.ui.IWorkbenchWindow; 35 import org.eclipse.ui.PartInitException; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.console.ConsolePlugin; 38 import org.eclipse.ui.console.IConsole; 39 import org.eclipse.ui.console.IConsoleConstants; 40 import org.eclipse.ui.console.IConsoleListener; 41 import org.eclipse.ui.console.IConsoleManager; 42 import org.eclipse.ui.console.IConsolePageParticipant; 43 import org.eclipse.ui.console.IConsoleView; 44 import org.eclipse.ui.console.IPatternMatchListener; 45 import org.eclipse.ui.console.TextConsole; 46 import org.eclipse.ui.progress.WorkbenchJob; 47 48 53 public class ConsoleManager implements IConsoleManager { 54 55 58 private ListenerList fListeners = null; 59 60 63 private List fConsoles = new ArrayList (10); 64 65 66 private final static int ADDED = 1; 68 private final static int REMOVED = 2; 69 70 private List fPatternMatchListeners; 71 72 private List fPageParticipants; 73 74 private List fConsoleFactoryExtensions; 75 76 private List fConsoleViews = new ArrayList (); 77 78 private boolean fWarnQueued = false; 79 80 private RepaintJob fRepaintJob = new RepaintJob(); 81 82 private class RepaintJob extends WorkbenchJob { 83 private Set list = new HashSet (); 84 85 public RepaintJob() { 86 super("schedule redraw() of viewers"); setSystem(true); 88 } 89 90 void addConsole(IConsole console) { 91 synchronized (list) { 92 list.add(console); 93 } 94 } 95 96 public IStatus runInUIThread(IProgressMonitor monitor) { 97 synchronized (list) { 98 if (list.isEmpty()) { 99 return Status.OK_STATUS; 100 } 101 102 IWorkbenchWindow[] workbenchWindows = PlatformUI.getWorkbench().getWorkbenchWindows(); 103 for (int i = 0; i < workbenchWindows.length; i++) { 104 IWorkbenchWindow window = workbenchWindows[i]; 105 if (window != null) { 106 IWorkbenchPage page = window.getActivePage(); 107 if (page != null) { 108 IViewPart part = page.findView(IConsoleConstants.ID_CONSOLE_VIEW); 109 if (part != null && part instanceof IConsoleView) { 110 ConsoleView view = (ConsoleView) part; 111 if (list.contains(view.getConsole())) { 112 Control control = view.getCurrentPage().getControl(); 113 if (!control.isDisposed()) { 114 control.redraw(); 115 } 116 } 117 } 118 119 } 120 } 121 } 122 list.clear(); 123 } 124 return Status.OK_STATUS; 125 } 126 } 127 128 131 class ConsoleNotifier implements ISafeRunnable { 132 133 private IConsoleListener fListener; 134 private int fType; 135 private IConsole[] fChanged; 136 137 140 public void handleException(Throwable exception) { 141 IStatus status = new Status(IStatus.ERROR, ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.INTERNAL_ERROR, ConsoleMessages.ConsoleManager_0, exception); 142 ConsolePlugin.log(status); 143 } 144 145 148 public void run() throws Exception { 149 switch (fType) { 150 case ADDED: 151 fListener.consolesAdded(fChanged); 152 break; 153 case REMOVED: 154 fListener.consolesRemoved(fChanged); 155 break; 156 } 157 } 158 159 165 public void notify(IConsole[] consoles, int update) { 166 if (fListeners == null) { 167 return; 168 } 169 fChanged = consoles; 170 fType = update; 171 Object [] copiedListeners= fListeners.getListeners(); 172 for (int i= 0; i < copiedListeners.length; i++) { 173 fListener = (IConsoleListener)copiedListeners[i]; 174 SafeRunner.run(this); 175 } 176 fChanged = null; 177 fListener = null; 178 } 179 } 180 181 public void registerConsoleView(ConsoleView view) { 182 synchronized (fConsoleViews) { 183 fConsoleViews.add(view); 184 } 185 } 186 public void unregisterConsoleView(ConsoleView view) { 187 synchronized (fConsoleViews) { 188 fConsoleViews.remove(view); 189 } 190 } 191 192 195 public void addConsoleListener(IConsoleListener listener) { 196 if (fListeners == null) { 197 fListeners = new ListenerList(); 198 } 199 fListeners.add(listener); 200 } 201 202 205 public void removeConsoleListener(IConsoleListener listener) { 206 if (fListeners != null) { 207 fListeners.remove(listener); 208 } 209 } 210 211 214 public synchronized void addConsoles(IConsole[] consoles) { 215 List added = new ArrayList (consoles.length); 216 for (int i = 0; i < consoles.length; i++) { 217 IConsole console = consoles[i]; 218 if(console instanceof TextConsole) { 219 TextConsole ioconsole = (TextConsole)console; 220 createPatternMatchListeners(ioconsole); 221 } 222 if (!fConsoles.contains(console)) { 223 fConsoles.add(console); 224 added.add(console); 225 } 226 } 227 if (!added.isEmpty()) { 228 fireUpdate((IConsole[])added.toArray(new IConsole[added.size()]), ADDED); 229 } 230 } 231 232 235 public synchronized void removeConsoles(IConsole[] consoles) { 236 List removed = new ArrayList (consoles.length); 237 for (int i = 0; i < consoles.length; i++) { 238 IConsole console = consoles[i]; 239 if (fConsoles.remove(console)) { 240 removed.add(console); 241 } 242 } 243 if (!removed.isEmpty()) { 244 fireUpdate((IConsole[])removed.toArray(new IConsole[removed.size()]), REMOVED); 245 } 246 } 247 248 251 public synchronized IConsole[] getConsoles() { 252 return (IConsole[])fConsoles.toArray(new IConsole[fConsoles.size()]); 253 } 254 255 261 private void fireUpdate(IConsole[] consoles, int type) { 262 new ConsoleNotifier().notify(consoles, type); 263 } 264 265 266 private class ShowConsoleViewJob extends WorkbenchJob { 267 private IConsole console; 268 269 ShowConsoleViewJob() { 270 super("Show Console View"); setSystem(true); 272 setPriority(Job.SHORT); 273 } 274 275 void setConsole(IConsole console) { 276 this.console = console; 277 } 278 279 public IStatus runInUIThread(IProgressMonitor monitor) { 280 boolean consoleFound = false; 281 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 282 if (window != null && console != null) { 283 IWorkbenchPage page= window.getActivePage(); 284 if (page != null) { 285 synchronized (fConsoleViews) { 286 for (Iterator iter = fConsoleViews.iterator(); iter.hasNext();) { 287 ConsoleView consoleView = (ConsoleView) iter.next(); 288 if (consoleView.getSite().getPage().equals(page)) { 289 boolean consoleVisible = page.isPartVisible(consoleView); 290 if (consoleVisible) { 291 consoleFound = true; 292 boolean bringToTop = shouldBringToTop(console, consoleView); 293 if (bringToTop) { 294 page.bringToTop(consoleView); 295 } 296 consoleView.display(console); 297 } 298 } 299 } 300 } 301 302 if (!consoleFound) { 303 try { 304 IConsoleView consoleView = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW, null, IWorkbenchPage.VIEW_CREATE); 305 boolean bringToTop = shouldBringToTop(console, consoleView); 306 if (bringToTop) { 307 page.bringToTop(consoleView); 308 } 309 consoleView.display(console); 310 } catch (PartInitException pie) { 311 ConsolePlugin.log(pie); 312 } 313 } 314 } 315 } 316 console = null; 317 return Status.OK_STATUS; 318 } 319 } 320 321 private ShowConsoleViewJob showJob = new ShowConsoleViewJob(); 322 325 public void showConsoleView(final IConsole console) { 326 showJob.setConsole(console); 327 showJob.schedule(100); 328 } 329 330 335 private boolean shouldBringToTop(IConsole console, IViewPart consoleView) { 336 boolean bringToTop= true; 337 if (consoleView instanceof IConsoleView) { 338 IConsoleView cView= (IConsoleView)consoleView; 339 if (cView.isPinned()) { 340 IConsole pinnedConsole= cView.getConsole(); 341 bringToTop = console.equals(pinnedConsole); 342 } 343 } 344 return bringToTop; 345 } 346 347 350 public void warnOfContentChange(final IConsole console) { 351 if (!fWarnQueued) { 352 fWarnQueued = true; 353 ConsolePlugin.getStandardDisplay().asyncExec(new Runnable (){ 354 public void run() { 355 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 356 if (window != null) { 357 IWorkbenchPage page= window.getActivePage(); 358 if (page != null) { 359 IConsoleView consoleView= (IConsoleView)page.findView(IConsoleConstants.ID_CONSOLE_VIEW); 360 if (consoleView != null) { 361 consoleView.warnOfContentChange(console); 362 } 363 } 364 } 365 fWarnQueued = false; 366 } 367 }); 368 } 369 } 370 371 374 public IPatternMatchListener[] createPatternMatchListeners(IConsole console) { 375 if (fPatternMatchListeners == null) { 376 fPatternMatchListeners = new ArrayList (); 377 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PATTERN_MATCH_LISTENERS); 378 IConfigurationElement[] elements = extensionPoint.getConfigurationElements(); 379 for (int i = 0; i < elements.length; i++) { 380 IConfigurationElement config = elements[i]; 381 PatternMatchListenerExtension extension = new PatternMatchListenerExtension(config); 382 fPatternMatchListeners.add(extension); 383 } 384 } 385 ArrayList list = new ArrayList (); 386 for(Iterator i = fPatternMatchListeners.iterator(); i.hasNext(); ) { 387 PatternMatchListenerExtension extension = (PatternMatchListenerExtension) i.next(); 388 try { 389 if (extension.getEnablementExpression() == null) { 390 i.remove(); 391 continue; 392 } 393 394 if (console instanceof TextConsole && extension.isEnabledFor(console)) { 395 TextConsole textConsole = (TextConsole) console; 396 PatternMatchListener patternMatchListener = new PatternMatchListener(extension); 397 try { 398 textConsole.addPatternMatchListener(patternMatchListener); 399 list.add(patternMatchListener); 400 } catch (PatternSyntaxException e) { 401 ConsolePlugin.log(e); 402 i.remove(); 403 } 404 } 405 } catch (CoreException e) { 406 ConsolePlugin.log(e); 407 } 408 } 409 return (PatternMatchListener[])list.toArray(new PatternMatchListener[0]); 410 } 411 412 415 public IConsolePageParticipant[] getPageParticipants(IConsole console) { 416 if(fPageParticipants == null) { 417 fPageParticipants = new ArrayList (); 418 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PAGE_PARTICIPANTS); 419 IConfigurationElement[] elements = extensionPoint.getConfigurationElements(); 420 for(int i = 0; i < elements.length; i++) { 421 IConfigurationElement config = elements[i]; 422 ConsolePageParticipantExtension extension = new ConsolePageParticipantExtension(config); 423 fPageParticipants.add(extension); 424 } 425 } 426 ArrayList list = new ArrayList (); 427 for(Iterator i = fPageParticipants.iterator(); i.hasNext(); ) { 428 ConsolePageParticipantExtension extension = (ConsolePageParticipantExtension) i.next(); 429 try { 430 if (extension.isEnabledFor(console)) { 431 list.add(extension.createDelegate()); 432 } 433 } catch (CoreException e) { 434 ConsolePlugin.log(e); 435 } 436 } 437 return (IConsolePageParticipant[]) list.toArray(new IConsolePageParticipant[0]); 438 } 439 440 443 public ConsoleFactoryExtension[] getConsoleFactoryExtensions() { 444 if (fConsoleFactoryExtensions == null) { 445 fConsoleFactoryExtensions = new ArrayList (); 446 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_FACTORIES); 447 IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements(); 448 for (int i = 0; i < configurationElements.length; i++) { 449 fConsoleFactoryExtensions.add(new ConsoleFactoryExtension(configurationElements[i])); 450 } 451 } 452 return (ConsoleFactoryExtension[]) fConsoleFactoryExtensions.toArray(new ConsoleFactoryExtension[0]); 453 } 454 455 456 public void refresh(final IConsole console) { 457 fRepaintJob.addConsole(console); 458 fRepaintJob.schedule(50); 459 } 460 461 } 462 | Popular Tags |