1 11 package org.eclipse.search2.internal.ui.text; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.ui.IEditorPart; 19 import org.eclipse.ui.IEditorReference; 20 import org.eclipse.ui.IPartListener2; 21 import org.eclipse.ui.IWorkbenchPage; 22 import org.eclipse.ui.IWorkbenchPartReference; 23 import org.eclipse.ui.IWorkbenchWindow; 24 25 import org.eclipse.search.ui.text.AbstractTextSearchResult; 26 27 public class WindowAnnotationManager { 28 private IWorkbenchWindow fWindow; 29 private Map fAnnotationManagers; 30 private IPartListener2 fPartListener; 31 private ArrayList fSearchResults; 32 33 public WindowAnnotationManager(IWorkbenchWindow window) { 34 fWindow = window; 35 fAnnotationManagers = new HashMap (); 36 37 fSearchResults= new ArrayList (); 38 39 initEditors(); 40 fPartListener= new IPartListener2() { 41 public void partActivated(IWorkbenchPartReference partRef) { 42 startHighlighting(getEditor(partRef)); 43 } 44 45 public void partBroughtToTop(IWorkbenchPartReference partRef) { 46 startHighlighting(getEditor(partRef)); 47 } 48 49 public void partClosed(IWorkbenchPartReference partRef) { 50 stopHighlighting(getEditor(partRef)); 51 } 52 53 public void partDeactivated(IWorkbenchPartReference partRef) { 54 } 55 56 public void partOpened(IWorkbenchPartReference partRef) { 57 } 58 59 public void partHidden(IWorkbenchPartReference partRef) { 60 stopHighlighting(getEditor(partRef)); 61 } 62 63 public void partVisible(IWorkbenchPartReference partRef) { 64 startHighlighting(getEditor(partRef)); 65 } 66 67 public void partInputChanged(IWorkbenchPartReference partRef) { 68 updateHighlighting(getEditor(partRef)); 69 } 70 }; 71 fWindow.getPartService().addPartListener(fPartListener); 72 73 } 74 75 private void startHighlighting(IEditorPart editor) { 76 if (editor == null) 77 return; 78 EditorAnnotationManager mgr= (EditorAnnotationManager) fAnnotationManagers.get(editor); 79 if (mgr == null) { 80 mgr= new EditorAnnotationManager(editor); 81 fAnnotationManagers.put(editor, mgr); 82 mgr.setSearchResults(fSearchResults); 83 } 84 } 85 86 private void updateHighlighting(IEditorPart editor) { 87 if (editor == null) 88 return; 89 EditorAnnotationManager mgr= (EditorAnnotationManager) fAnnotationManagers.get(editor); 90 if (mgr != null) { 91 mgr.doEditorInputChanged(); 92 } 93 } 94 95 96 private void initEditors() { 97 IWorkbenchPage[] pages= fWindow.getPages(); 98 for (int i = 0; i < pages.length; i++) { 99 IEditorReference[] editors= pages[i].getEditorReferences(); 100 for (int j = 0; j < editors.length; j++) { 101 IEditorPart editor= editors[j].getEditor(false); 102 if (editor != null && pages[i].isPartVisible(editor)) { 103 startHighlighting(editor); 104 } 105 } 106 } 107 } 108 109 private void stopHighlighting(IEditorPart editor) { 110 if (editor == null) 111 return; 112 EditorAnnotationManager mgr= (EditorAnnotationManager) fAnnotationManagers.remove(editor); 113 if (mgr != null) 114 mgr.dispose(); 115 } 116 117 private IEditorPart getEditor(IWorkbenchPartReference partRef) { 118 if (partRef instanceof IEditorReference) { 119 return ((IEditorReference)partRef).getEditor(false); 120 } 121 return null; 122 } 123 124 void dispose() { 125 fWindow.getPartService().removePartListener(fPartListener); 126 for (Iterator mgrs = fAnnotationManagers.values().iterator(); mgrs.hasNext();) { 127 EditorAnnotationManager mgr = (EditorAnnotationManager) mgrs.next(); 128 mgr.dispose(); 129 } 130 fAnnotationManagers= null; 131 } 132 133 void addSearchResult(AbstractTextSearchResult result) { 134 boolean alreadyShown= fSearchResults.contains(result); 135 fSearchResults.add(result); 136 if (!alreadyShown) { 137 for (Iterator mgrs = fAnnotationManagers.values().iterator(); mgrs.hasNext();) { 138 EditorAnnotationManager mgr = (EditorAnnotationManager) mgrs.next(); 139 mgr.addSearchResult(result); 140 } 141 } 142 } 143 144 void removeSearchResult(AbstractTextSearchResult result) { 145 fSearchResults.remove(result); 146 boolean stillShown= fSearchResults.contains(result); 147 if (!stillShown) { 148 for (Iterator mgrs = fAnnotationManagers.values().iterator(); mgrs.hasNext();) { 149 EditorAnnotationManager mgr = (EditorAnnotationManager) mgrs.next(); 150 mgr.removeSearchResult(result); 151 } 152 } 153 } 154 155 } 156 | Popular Tags |