1 11 package org.eclipse.ui.internal.console; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.ISafeRunnable; 19 import org.eclipse.core.runtime.ListenerList; 20 import org.eclipse.core.runtime.SafeRunner; 21 import org.eclipse.jface.action.IToolBarManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.util.IPropertyChangeListener; 24 import org.eclipse.jface.util.PropertyChangeEvent; 25 import org.eclipse.jface.viewers.IBasicPropertyConstants; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.ui.IPartListener2; 28 import org.eclipse.ui.IViewReference; 29 import org.eclipse.ui.IViewSite; 30 import org.eclipse.ui.IWorkbenchPart; 31 import org.eclipse.ui.IWorkbenchPartReference; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.console.AbstractConsole; 34 import org.eclipse.ui.console.ConsolePlugin; 35 import org.eclipse.ui.console.IConsole; 36 import org.eclipse.ui.console.IConsoleConstants; 37 import org.eclipse.ui.console.IConsoleListener; 38 import org.eclipse.ui.console.IConsoleManager; 39 import org.eclipse.ui.console.IConsolePageParticipant; 40 import org.eclipse.ui.console.IConsoleView; 41 import org.eclipse.ui.part.IPage; 42 import org.eclipse.ui.part.IPageBookViewPage; 43 import org.eclipse.ui.part.MessagePage; 44 import org.eclipse.ui.part.PageBook; 45 import org.eclipse.ui.part.PageBookView; 46 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 47 48 53 public class ConsoleView extends PageBookView implements IConsoleView, IConsoleListener, IPropertyChangeListener, IPartListener2 { 54 55 58 private boolean fPinned = false; 59 60 63 private List fStack = new ArrayList (); 64 65 68 private IConsole fActiveConsole = null; 69 70 73 private Map fConsoleToPart; 74 75 78 private Map fConsoleToPageParticipants; 79 80 83 private Map fPartToConsole; 84 85 88 private boolean fActive = false; 89 90 private PinConsoleAction fPinAction = null; 92 private ConsoleDropDownAction fDisplayConsoleAction = null; 93 94 private OpenConsoleAction fOpenConsoleAction = null; 95 96 private boolean fScrollLock; 97 98 private boolean isAvailable() { 99 return getPageBook() != null && !getPageBook().isDisposed(); 100 } 101 102 105 public void propertyChange(PropertyChangeEvent event) { 106 Object source = event.getSource(); 107 if (source instanceof IConsole && event.getProperty().equals(IBasicPropertyConstants.P_TEXT)) { 108 if (source.equals(getConsole())) { 109 updateTitle(); 110 } 111 } 112 113 } 114 115 118 public void partClosed(IWorkbenchPart part) { 119 super.partClosed(part); 120 fPinAction.update(); 121 } 122 123 126 public IConsole getConsole() { 127 return fActiveConsole; 128 } 129 130 133 protected void showPageRec(PageRec pageRec) { 134 if (fActiveConsole != null && pageRec.page != getDefaultPage() && fPinned && fConsoleToPart.size() > 1) { 137 IConsole console = (IConsole)fPartToConsole.get(pageRec.part); 138 if (!fStack.contains(console)) { 139 fStack.add(console); 140 } 141 return; 142 } 143 144 IConsole recConsole = (IConsole)fPartToConsole.get(pageRec.part); 145 if (recConsole!=null && recConsole.equals(fActiveConsole)) { 146 return; 147 } 148 149 super.showPageRec(pageRec); 150 fActiveConsole = recConsole; 151 IConsole tos = null; 152 if (!fStack.isEmpty()) { 153 tos = (IConsole) fStack.get(0); 154 } 155 if (tos != null && !tos.equals(fActiveConsole)) { 156 deactivateParticipants(tos); 157 } 158 if (fActiveConsole != null && !fActiveConsole.equals(tos)) { 159 fStack.remove(fActiveConsole); 160 fStack.add(0,fActiveConsole); 161 activateParticipants(fActiveConsole); 162 } 163 updateTitle(); 164 updateHelp(); 165 if (fPinAction != null) { 167 fPinAction.update(); 168 } 169 IPage page = getCurrentPage(); 170 if (page instanceof IOConsolePage) { 171 ((IOConsolePage)page).setAutoScroll(!fScrollLock); 172 } 173 } 174 175 180 private void activateParticipants(IConsole console) { 181 if (console != null && fActive) { 183 final ListenerList listeners = getParticipants(console); 184 if (listeners != null) { 185 Object [] participants = listeners.getListeners(); 186 for (int i = 0; i < participants.length; i++) { 187 final IConsolePageParticipant participant = (IConsolePageParticipant) participants[i]; 188 SafeRunner.run(new ISafeRunnable() { 189 public void run() throws Exception { 190 participant.activated(); 191 } 192 public void handleException(Throwable exception) { 193 ConsolePlugin.log(exception); 194 listeners.remove(participant); 195 } 196 }); 197 } 198 } 199 } 200 } 201 202 207 protected List getConsoleStack() { 208 return fStack; 209 } 210 211 214 protected void updateTitle() { 215 IConsole console = getConsole(); 216 if (console == null) { 217 setContentDescription(ConsoleMessages.ConsoleView_0); 218 } else { 219 String newName = console.getName(); 220 String oldName = getContentDescription(); 221 if (newName!=null && !(newName.equals(oldName))) { 222 setContentDescription(console.getName()); 223 } 224 } 225 } 226 227 protected void updateHelp() { 228 IConsole console = getConsole(); 229 String helpContextId = null; 230 if (console instanceof AbstractConsole) { 231 AbstractConsole abs = (AbstractConsole) console; 232 helpContextId = abs.getHelpContextId(); 233 } 234 if (helpContextId == null) { 235 helpContextId = IConsoleHelpContextIds.CONSOLE_VIEW; 236 } 237 PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook().getParent(), helpContextId); 238 } 239 240 243 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) { 244 IConsole console = (IConsole)fPartToConsole.get(part); 245 246 ListenerList listeners = (ListenerList) fConsoleToPageParticipants.remove(console); 248 if (listeners != null) { 249 Object [] participants = listeners.getListeners(); 250 for (int i = 0; i < participants.length; i++) { 251 final IConsolePageParticipant participant = (IConsolePageParticipant) participants[i]; 252 SafeRunner.run(new ISafeRunnable() { 253 public void run() throws Exception { 254 participant.dispose(); 255 } 256 public void handleException(Throwable exception) { 257 ConsolePlugin.log(exception); 258 } 259 }); 260 } 261 } 262 263 IPage page = pageRecord.page; 264 page.dispose(); 265 pageRecord.dispose(); 266 console.removePropertyChangeListener(this); 267 268 fPartToConsole.remove(part); 270 fConsoleToPart.remove(console); 271 if (fPartToConsole.isEmpty()) { 272 fActiveConsole = null; 273 } 274 275 fPinAction.update(); 277 } 278 279 285 private ListenerList getParticipants(IConsole console) { 286 return (ListenerList) fConsoleToPageParticipants.get(console); 287 } 288 289 292 protected PageRec doCreatePage(IWorkbenchPart dummyPart) { 293 ConsoleWorkbenchPart part = (ConsoleWorkbenchPart)dummyPart; 294 final IConsole console = part.getConsole(); 295 final IPageBookViewPage page = console.createPage(this); 296 initPage(page); 297 page.createControl(getPageBook()); 298 console.addPropertyChangeListener(this); 299 300 IConsolePageParticipant[] consoleParticipants = ((ConsoleManager)getConsoleManager()).getPageParticipants(console); 302 final ListenerList participants = new ListenerList(); 303 for (int i = 0; i < consoleParticipants.length; i++) { 304 participants.add(consoleParticipants[i]); 305 } 306 fConsoleToPageParticipants.put(console, participants); 307 Object [] listeners = participants.getListeners(); 308 for (int i = 0; i < listeners.length; i++) { 309 final IConsolePageParticipant participant = (IConsolePageParticipant) listeners[i]; 310 SafeRunner.run(new ISafeRunnable() { 311 public void run() throws Exception { 312 participant.init(page, console); 313 } 314 public void handleException(Throwable exception) { 315 ConsolePlugin.log(exception); 316 participants.remove(participant); 317 } 318 }); 319 } 320 321 PageRec rec = new PageRec(dummyPart, page); 322 return rec; 323 } 324 325 328 protected boolean isImportant(IWorkbenchPart part) { 329 return part instanceof ConsoleWorkbenchPart; 330 } 331 332 335 public void dispose() { 336 super.dispose(); 337 getViewSite().getPage().removePartListener((IPartListener2)this); 338 ConsoleManager consoleManager = (ConsoleManager) ConsolePlugin.getDefault().getConsoleManager(); 339 consoleManager.removeConsoleListener(this); 340 consoleManager.unregisterConsoleView(this); 341 } 342 343 348 private IConsoleManager getConsoleManager() { 349 return ConsolePlugin.getDefault().getConsoleManager(); 350 } 351 352 355 protected IPage createDefaultPage(PageBook book) { 356 MessagePage page = new MessagePage(); 357 page.createControl(getPageBook()); 358 initPage(page); 359 return page; 360 } 361 362 365 public void consolesAdded(final IConsole[] consoles) { 366 if (isAvailable()) { 367 Runnable r = new Runnable () { 368 public void run() { 369 for (int i = 0; i < consoles.length; i++) { 370 if (isAvailable()) { 371 IConsole console = consoles[i]; 372 IConsole[] allConsoles = getConsoleManager().getConsoles(); 374 for (int j = 0; j < allConsoles.length; j++) { 375 IConsole registered = allConsoles[j]; 376 if (registered.equals(console)) { 377 ConsoleWorkbenchPart part = new ConsoleWorkbenchPart(console, getSite()); 378 fConsoleToPart.put(console, part); 379 fPartToConsole.put(part, console); 380 partActivated(part); 381 break; 382 } 383 } 384 385 } 386 } 387 } 388 }; 389 asyncExec(r); 390 } 391 } 392 393 396 public void consolesRemoved(final IConsole[] consoles) { 397 if (isAvailable()) { 398 Runnable r = new Runnable () { 399 public void run() { 400 for (int i = 0; i < consoles.length; i++) { 401 if (isAvailable()) { 402 IConsole console = consoles[i]; 403 fStack.remove(console); 404 ConsoleWorkbenchPart part = (ConsoleWorkbenchPart)fConsoleToPart.get(console); 405 if (part != null) { 406 partClosed(part); 407 } 408 if (getConsole() == null) { 409 IConsole[] available = getConsoleManager().getConsoles(); 410 if (available.length > 0) { 411 display(available[available.length - 1]); 412 } 413 } 414 } 415 } 416 } 417 }; 418 asyncExec(r); 419 } 420 } 421 422 425 public ConsoleView() { 426 super(); 427 fConsoleToPart = new HashMap (); 428 fPartToConsole = new HashMap (); 429 fConsoleToPageParticipants = new HashMap (); 430 431 ConsoleManager consoleManager = (ConsoleManager) ConsolePlugin.getDefault().getConsoleManager(); 432 consoleManager.registerConsoleView(this); 433 } 434 435 protected void createActions() { 436 fPinAction = new PinConsoleAction(this); 437 fDisplayConsoleAction = new ConsoleDropDownAction(this); 438 ConsoleFactoryExtension[] extensions = ((ConsoleManager)ConsolePlugin.getDefault().getConsoleManager()).getConsoleFactoryExtensions(); 439 if (extensions.length > 0) { 440 fOpenConsoleAction = new OpenConsoleAction(); 441 } 442 } 443 444 protected void configureToolBar(IToolBarManager mgr) { 445 mgr.add(new Separator(IConsoleConstants.LAUNCH_GROUP)); 446 mgr.add(new Separator(IConsoleConstants.OUTPUT_GROUP)); 447 mgr.add(new Separator("fixedGroup")); mgr.add(fPinAction); 449 mgr.add(fDisplayConsoleAction); 450 if (fOpenConsoleAction != null) { 451 mgr.add(fOpenConsoleAction); 452 } 453 } 454 455 458 public void display(IConsole console) { 459 if (fPinned && fActiveConsole != null) { 460 return; 461 } 462 if (console.equals(fActiveConsole)) { 463 return; 464 } 465 ConsoleWorkbenchPart part = (ConsoleWorkbenchPart)fConsoleToPart.get(console); 466 if (part != null) { 467 partActivated(part); 468 } 469 } 470 471 474 public void setPinned(boolean pin) { 475 fPinned = pin; 476 if (fPinAction != null) { 477 fPinAction.update(); 478 } 479 } 480 481 484 public boolean isPinned() { 485 return fPinned; 486 } 487 488 491 protected IWorkbenchPart getBootstrapPart() { 492 return null; 493 } 494 495 501 public void asyncExec(Runnable r) { 502 if (isAvailable()) { 503 getPageBook().getDisplay().asyncExec(r); 504 } 505 } 506 507 523 public void createPartControl(Composite parent) { 524 super.createPartControl(parent); 525 createActions(); 526 IToolBarManager tbm= getViewSite().getActionBars().getToolBarManager(); 527 configureToolBar(tbm); 528 updateForExistingConsoles(); 529 getViewSite().getActionBars().updateActionBars(); 530 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IConsoleHelpContextIds.CONSOLE_VIEW); 531 getViewSite().getPage().addPartListener((IPartListener2)this); 532 } 533 534 537 private void updateForExistingConsoles() { 538 IConsoleManager manager = getConsoleManager(); 539 IConsole[] consoles = manager.getConsoles(); 541 consolesAdded(consoles); 542 manager.addConsoleListener(this); 544 } 545 546 549 public void warnOfContentChange(IConsole console) { 550 IWorkbenchPart part = (IWorkbenchPart)fConsoleToPart.get(console); 551 if (part != null) { 552 IWorkbenchSiteProgressService service = (IWorkbenchSiteProgressService) part.getSite().getAdapter(IWorkbenchSiteProgressService.class); 553 if (service != null) { 554 service.warnOfContentChange(); 555 } 556 } 557 } 558 559 public Object getAdapter(Class key) { 560 Object adpater = super.getAdapter(key); 561 if (adpater == null) { 562 IConsole console = getConsole(); 563 if (console != null) { 564 ListenerList listeners = getParticipants(console); 565 if (listeners != null) { 567 Object [] participants = listeners.getListeners(); 568 for (int i = 0; i < participants.length; i++) { 569 IConsolePageParticipant participant = (IConsolePageParticipant) participants[i]; 570 adpater = participant.getAdapter(key); 571 if (adpater != null) { 572 return adpater; 573 } 574 } 575 } 576 } 577 } 578 return adpater; 579 } 580 581 584 public void partActivated(IWorkbenchPartReference partRef) { 585 if (isThisPart(partRef)) { 586 fActive = true; 587 activateParticipants(fActiveConsole); 588 } 589 } 590 591 594 public void partBroughtToTop(IWorkbenchPartReference partRef) { 595 } 596 597 600 public void partClosed(IWorkbenchPartReference partRef) { 601 } 602 603 606 public void partDeactivated(IWorkbenchPartReference partRef) { 607 if (isThisPart(partRef)) { 608 fActive = false; 609 deactivateParticipants(fActiveConsole); 610 } 611 } 612 613 protected boolean isThisPart(IWorkbenchPartReference partRef) { 614 if (partRef instanceof IViewReference) { 615 IViewReference viewRef = (IViewReference) partRef; 616 if (viewRef.getId().equals(getViewSite().getId())) { 617 String secId = viewRef.getSecondaryId(); 618 String mySec = null; 619 if (getSite() instanceof IViewSite) { 620 mySec = ((IViewSite)getSite()).getSecondaryId(); 621 } 622 if (mySec == null) { 623 return secId == null; 624 } 625 return mySec.equals(secId); 626 } 627 } 628 return false; 629 } 630 631 636 private void deactivateParticipants(IConsole console) { 637 if (console != null) { 639 final ListenerList listeners = getParticipants(console); 640 if (listeners != null) { 641 Object [] participants = listeners.getListeners(); 642 for (int i = 0; i < participants.length; i++) { 643 final IConsolePageParticipant participant = (IConsolePageParticipant) participants[i]; 644 SafeRunner.run(new ISafeRunnable() { 645 public void run() throws Exception { 646 participant.deactivated(); 647 } 648 public void handleException(Throwable exception) { 649 ConsolePlugin.log(exception); 650 listeners.remove(participant); 651 } 652 }); 653 } 654 } 655 } 656 } 657 658 661 public void partOpened(IWorkbenchPartReference partRef) { 662 } 663 664 667 public void partHidden(IWorkbenchPartReference partRef) { 668 } 669 670 673 public void partVisible(IWorkbenchPartReference partRef) { 674 } 675 676 679 public void partInputChanged(IWorkbenchPartReference partRef) { 680 } 681 682 685 public void setScrollLock(boolean scrollLock) { 686 fScrollLock = scrollLock; 687 688 IPage page = getCurrentPage(); 689 if (page instanceof IOConsolePage) { 690 ((IOConsolePage)page).setAutoScroll(!scrollLock); 691 } 692 } 693 694 697 public boolean getScrollLock() { 698 return fScrollLock; 699 } 700 701 704 public void pin(IConsole console) { 705 if (console == null) { 706 setPinned(false); 707 } else { 708 if (isPinned()) { 709 setPinned(false); 710 } 711 display(console); 712 setPinned(true); 713 } 714 } 715 } 716 | Popular Tags |