1 19 20 package org.netbeans.modules.tasklist.suggestions; 21 22 import org.netbeans.apihole.tasklist.SPIHole; 23 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider; 24 import org.netbeans.modules.tasklist.providers.SuggestionContext; 25 import org.netbeans.modules.tasklist.providers.SuggestionProvider; 26 import org.netbeans.modules.tasklist.suggestions.settings.ManagerSettings; 27 import org.openide.ErrorManager; 28 import org.openide.loaders.DataObject; 29 import org.openide.windows.TopComponent; 30 31 import javax.swing.text.Document ; 32 import java.util.*; 33 34 43 final public class SuggestionManagerImpl extends DefaultSuggestionManager { 44 45 private final boolean stats = System.getProperty("netbeans.tasklist.stats") != null; 46 47 48 51 public SuggestionManagerImpl() { 52 } 53 54 77 public boolean isObserved(String id) { 78 TopComponent.Registry registry = TopComponent.getRegistry(); 79 Set opened = registry.getOpened(); 80 Iterator it = opened.iterator(); 81 while (it.hasNext()) { 82 TopComponent next = (TopComponent) it.next(); 83 if (next instanceof SuggestionView) { 84 SuggestionView view = (SuggestionView) next; 85 if (view.isObserved(id)) return true; 86 } 87 } 88 return false; 89 } 90 91 94 boolean running = false; 95 boolean prepared = false; 96 97 105 SuggestionProvider unfiltered = null; 106 107 108 111 112 public void dispatchPrepare() { 113 if (!prepared) { 114 List providers = getProviders(); 115 ListIterator it = providers.listIterator(); 116 while (it.hasNext()) { 117 SuggestionProvider provider = (SuggestionProvider) it.next(); 118 try { 119 provider.notifyPrepare(); 120 } catch (RuntimeException e) { 121 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 123 } catch (ThreadDeath e) { 124 throw e; 125 } catch (Error e) { 126 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 128 } 129 } 130 prepared = true; 131 } 132 } 133 134 139 public void dispatchRun() { 140 if (!running) { 141 if (!prepared) { 142 dispatchPrepare(); 143 } 144 List providers = getProviders(); 145 ListIterator it = providers.listIterator(); 146 while (it.hasNext()) { 147 SuggestionProvider provider = (SuggestionProvider) it.next(); 148 if ((unfiltered == null) || 149 (unfiltered == provider)) { 150 try { 151 provider.notifyRun(); 152 } catch (RuntimeException e) { 153 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 155 } catch (ThreadDeath e) { 156 throw e; 157 } catch (Error e) { 158 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 160 } 161 162 } 163 } 164 running = true; 165 } 166 } 167 168 170 public void dispatchStop() { 171 if (running) { 172 List providers = getProviders(); 173 ListIterator it = providers.listIterator(); 174 while (it.hasNext()) { 175 SuggestionProvider provider = (SuggestionProvider) it.next(); 176 if ((unfiltered == null) || 177 (unfiltered == provider)) { 178 try { 179 provider.notifyStop(); 180 } catch (RuntimeException e) { 181 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 183 } catch (ThreadDeath e) { 184 throw e; 185 } catch (Error e) { 186 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 188 } 189 190 } 191 } 192 running = false; 193 } 194 } 195 196 197 public void notifyViewClosed() { 198 if (prepared) { 199 if (running) { 200 dispatchStop(); 201 } 202 List providers = getProviders(); 203 ListIterator it = providers.listIterator(); 204 while (it.hasNext()) { 205 SuggestionProvider provider = (SuggestionProvider) it.next(); 206 provider.notifyFinish(); 207 } 208 prepared = false; 209 } 210 211 } 212 213 214 private SuggestionList list = null; 216 217 220 public final SuggestionList getList() { 221 if (list == null) { 222 list = new SuggestionList(); 223 } 224 return list; 225 } 226 227 228 241 public synchronized boolean isEnabled(String id) { 242 ManagerSettings settings = ManagerSettings.getDefault(); 243 return settings != null ? settings.isEnabled(id) : true; 244 } 245 246 259 public synchronized void setEnabled(String id, boolean enabled, 260 boolean dontSave) { 261 SuggestionType type = SuggestionTypes.getDefault().getType(id); 262 263 ManagerSettings.getDefault().setEnabled(id, enabled); 264 265 toggleProvider(type, enabled); 267 268 if (!dontSave) { 269 writeTypeRegistry(); 270 } 271 272 } 273 274 276 private void toggleProvider(SuggestionType type, boolean enable) { 277 SuggestionProvider provider = getProvider(type); 281 if (provider == null) { 282 return; 284 } 285 toggleProvider(provider, type, enable, false); 288 } 289 290 292 private void toggleProvider(SuggestionProvider provider, 293 SuggestionType type, boolean enable, 294 boolean allTypes) { 295 if (enable) { 296 provider.notifyPrepare(); 300 provider.notifyRun(); 301 } else { 302 if (!allTypes) { 303 String typeName = provider.getType(); 304 if (!typeName.equals(type.getName())) { 305 if (isEnabled(typeName)) { 306 getList().removeCategory(type); 308 return; 309 } 310 } 311 } 312 313 provider.notifyStop(); 318 provider.notifyFinish(); 319 320 String typeName = provider.getType(); 321 if (isEnabled(typeName)) { 322 getList().removeCategory(type); 324 return; 325 } 326 } 327 } 328 329 345 public synchronized boolean isConfirm(SuggestionType type) { 346 return ManagerSettings.getDefault().isConfirm(type); 347 } 348 349 350 362 synchronized void setConfirm(SuggestionType type, boolean confirm, boolean write) { 363 ManagerSettings.getDefault().setConfirm(type, confirm); 364 if (write) { 365 writeTypeRegistry(); 366 } 367 } 368 369 375 public void notifyFiltered(SuggestionList tasklist, SuggestionType type) { 376 SuggestionType prevFilterType = getUnfilteredType(); 377 setUnfilteredType(type); 378 379 if (type != null) { 380 List oldList = tasklist.getTasks(); 383 384 if (oldList != null) { 385 List allTasks = new ArrayList(oldList.size()); 386 allTasks.addAll(oldList); 387 tasklist.clear(); 388 Collection types = SuggestionTypes.getDefault().getAllTypes(); 389 Iterator it = types.iterator(); 390 while (it.hasNext()) { 391 SuggestionType t = (SuggestionType) it.next(); 392 ArrayList list = new ArrayList(100); 393 Iterator all = allTasks.iterator(); 394 SuggestionImpl category = 395 tasklist.getCategoryTask(t, false); 396 tasklist.removeCategory(category, true); 397 while (all.hasNext()) { 398 SuggestionImpl sg = (SuggestionImpl) all.next(); 399 if (sg.getSType() == t) { 400 if ((sg == category) && 401 sg.hasSubtasks()) { list.addAll(sg.getSubtasks()); 403 } else { 404 list.add(sg); 405 } 406 } 407 } 408 register(t.getName(), list, null, tasklist, true); 410 } 411 } 412 } else { 413 tasklist.clearCategoryTasks(); 414 List oldList = tasklist.getTasks(); 415 List suggestions = new ArrayList(); 416 if (oldList != null) 417 suggestions.addAll(oldList); 418 tasklist.clear(); 419 Iterator it = suggestions.iterator(); 420 List group = null; 421 SuggestionType prevType = null; 422 while (it.hasNext()) { 423 SuggestionImpl s = (SuggestionImpl) it.next(); 424 if (s.getSType() != prevType) { 425 if (group != null) { 426 register(prevType.getName(), group, null, 427 tasklist, true); 428 group.clear(); 429 } else { 430 group = new ArrayList(50); 431 } 432 prevType = s.getSType(); 433 } 434 if (group == null) { 435 group = new ArrayList(50); 436 } 437 group.add(s); 438 } 439 if ((group != null) && (group.size() > 0)) { 440 register(prevType.getName(), group, null, tasklist, true); 441 } 442 } 443 444 unfiltered = null; 445 446 451 List providers = getProviders(); 452 SuggestionTypes suggestionTypes = SuggestionTypes.getDefault(); 453 ListIterator it = providers.listIterator(); 454 while (it.hasNext()) { 455 SuggestionProvider provider = (SuggestionProvider) it.next(); 456 457 String typeName = provider.getType(); 459 if (type != null) { 460 boolean enabled = false; 463 SuggestionType tp = suggestionTypes.getType(typeName); 464 if (tp == type) { 465 enabled = true; 466 } 467 if (enabled) { 468 unfiltered = provider; 471 if (prevFilterType != null) { 472 SuggestionType sg = suggestionTypes.getType(typeName); 473 toggleProvider(provider, sg, true, true); 474 } } else { 480 SuggestionType sg = suggestionTypes.getType(typeName); 481 toggleProvider(provider, sg, false, true); 482 } 483 } else { 484 boolean isPrev = false; 489 SuggestionType tp = suggestionTypes.getType(typeName); 490 if (prevFilterType == tp) { 491 isPrev = true; 495 break; 496 } 497 if (isPrev) { 498 continue; 499 } 500 501 SuggestionType sg = suggestionTypes.getType(typeName); 502 toggleProvider(provider, sg, true, true); 503 } 504 505 } 506 } 507 508 509 510 public boolean isExpandedType(SuggestionType type) { 511 return ManagerSettings.getDefault().isExpandedType(type); 512 } 513 514 515 public void setExpandedType(SuggestionType type, boolean expanded) { 516 ManagerSettings.getDefault().setExpandedType(type, expanded); 517 } 518 519 520 public void register(String type, List add, List remove, Object request) { 522 SPIMonitor.log(" Response on " + request + " " + type + " add: " + ((add != null) ? ""+add.size() : "null") + " remove:" + ((remove != null) ? ""+remove.size() : "null")); 523 SuggestionList target = getList(); 526 if (target == null) { 527 return; 532 } 533 534 if ((type == null) && (add != null) && (remove != null)) { 535 register(type, null, remove, target, !switchingFiles); 539 register(type, add, null, target, !switchingFiles); 540 } else { 541 register(type, add, remove, target, !switchingFiles); 542 } 543 } 544 545 547 private boolean switchingFiles = false; 548 549 550 558 569 570 571 572 private boolean scanOnShow(DocumentSuggestionProvider provider) { 573 return ManagerSettings.getDefault().isScanOnShow(); 575 } 576 577 578 private boolean scanOnSave(DocumentSuggestionProvider provider) { 579 return ManagerSettings.getDefault().isScanOnSave(); 581 } 582 583 584 private boolean scanOnEdit(DocumentSuggestionProvider provider) { 585 return ManagerSettings.getDefault().isScanOnEdit(); 587 } 588 589 599 613 614 615 616 617 631 void DELETE_dispatchRescan(Document document, DataObject dataobject, final Object request, ProviderAcceptor acceptor) { 632 633 assert request != null : "Precondition for SuggestionsBroker.getCurrRequest()"; 635 if (dataobject.isValid() == false) return; 636 637 long start = 0, end = 0, total = 0; 638 List providers = getDocProviders(); 639 Iterator it = providers.iterator(); 640 641 SuggestionContext ctx = SPIHole.createSuggestionContext(dataobject); 642 while (it.hasNext()) { 643 if (SuggestionsBroker.getDefault().getCurrRequest() != request) return; 644 645 DocumentSuggestionProvider provider = (DocumentSuggestionProvider) it.next(); 646 if (acceptor.accept(provider) == false) continue; 647 if ((unfiltered == null) || (provider == unfiltered)) { if (stats) { 649 start = System.currentTimeMillis(); 650 } 651 try { 652 SPIMonitor.log("Enter rescan " + request + " " + provider.getClass()); 653 } catch (RuntimeException e) { 655 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 657 } catch (ThreadDeath e) { 658 throw e; 659 } catch (Error e) { 660 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 662 } finally { 663 SPIMonitor.log("Leave rescan " + request + " " + provider.getClass()); 664 } 665 if (stats) { 666 end = System.currentTimeMillis(); 667 System.out.println("Scan time for provider " + provider.getClass().getName() + " = " + (end - start) + " ms"); 668 total += (end - start); 669 } 670 } 671 } 672 if (stats) { 673 System.out.println("TOTAL SCAN TIME = " + total + "\n"); 674 } 675 } 676 677 678 List dispatchScan(DataObject dataobject, ProviderAcceptor acceptor) { 679 680 if (dataobject.isValid() == false) return Collections.EMPTY_LIST; 681 682 long start = 0, end = 0, total = 0; 683 List providers = getDocProviders(); 684 Iterator it = providers.iterator(); 685 686 final List result = new LinkedList(); 687 688 SuggestionContext ctx = SPIHole.createSuggestionContext(dataobject); 689 while (it.hasNext()) { 690 691 DocumentSuggestionProvider provider = (DocumentSuggestionProvider) it.next(); 692 if (acceptor.accept(provider) == false) continue; 693 if ((unfiltered == null) || (provider == unfiltered)) { if (stats) { 695 start = System.currentTimeMillis(); 696 } 697 try { 698 SPIMonitor.log("Enter scan " + provider.getClass()); 699 List found = provider.scan(ctx); 700 if (found != null) { 701 result.addAll(found); 702 } 703 } catch (RuntimeException e) { 704 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 706 } catch (ThreadDeath e) { 707 throw e; 708 } catch (Error e) { 709 ErrorManager.getDefault().annotate(e, "Skipping faulty provider (" + provider + ")."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 711 } finally { 712 SPIMonitor.log("Leave rescan " + provider.getClass()); 713 } 714 if (stats) { 715 end = System.currentTimeMillis(); 716 System.out.println("Scan time for provider " + provider.getClass().getName() + " = " + (end - start) + " ms"); 717 total += (end - start); 718 } 719 } 720 } 721 if (stats) { 722 System.out.println("TOTAL SCAN TIME = " + total + "\n"); 723 } 724 725 return result; 726 } 727 728 738 void setFixing(boolean wait) { 739 SuggestionsBroker.getDefault().setFixing(wait); 741 } 742 743 744 765 766 767 768 769 770 771 772 void editTypes(List enabled, List disabled, List confirmation) { 773 Iterator it = enabled.iterator(); 774 while (it.hasNext()) { 775 SuggestionType type = (SuggestionType) it.next(); 776 if (!isEnabled(type.getName())) { 777 setEnabled(type.getName(), true, true); 778 } 779 } 780 781 it = disabled.iterator(); 782 while (it.hasNext()) { 783 SuggestionType type = (SuggestionType) it.next(); 784 if (isEnabled(type.getName())) { 785 setEnabled(type.getName(), false, true); 786 } 787 } 788 789 Iterator allIt = SuggestionTypes.getDefault().getAllTypes().iterator(); 790 while (allIt.hasNext()) { 791 SuggestionType t = (SuggestionType) allIt.next(); 792 it = confirmation.iterator(); 793 boolean found = false; 794 while (it.hasNext()) { 795 SuggestionType type = (SuggestionType) it.next(); 796 if (type == t) { 797 found = true; 798 break; 799 } 800 } 801 setConfirm(t, !found, false); 802 } 803 804 writeTypeRegistry(); 806 } 807 808 private void writeTypeRegistry() { 809 ManagerSettings.getDefault().store(); 810 } 811 812 813 815 private List getProviders() { 816 return SuggestionProviders.getDefault().getProviders(); 817 } 818 819 private List getDocProviders() { 820 return SuggestionProviders.getDefault().getDocProviders(); 821 } 822 823 private SuggestionProvider getProvider(SuggestionType type) { 824 return SuggestionProviders.getDefault().getProvider(type); 825 } 826 827 } 828 | Popular Tags |