1 14 package org.eclipse.search.internal.ui; 15 16 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.Assert; 22 23 import org.eclipse.core.resources.IMarker; 24 import org.eclipse.core.resources.IResource; 25 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Display; 28 29 import org.eclipse.jface.action.IAction; 30 import org.eclipse.jface.action.IToolBarManager; 31 import org.eclipse.jface.operation.IRunnableWithProgress; 32 import org.eclipse.jface.resource.ImageDescriptor; 33 import org.eclipse.jface.util.IPropertyChangeListener; 34 import org.eclipse.jface.util.PropertyChangeEvent; 35 import org.eclipse.jface.viewers.IBaseLabelProvider; 36 import org.eclipse.jface.viewers.ILabelProvider; 37 import org.eclipse.jface.viewers.ISelection; 38 39 import org.eclipse.ui.IActionBars; 40 import org.eclipse.ui.IMemento; 41 import org.eclipse.ui.IViewSite; 42 import org.eclipse.ui.PartInitException; 43 import org.eclipse.ui.actions.ActionFactory; 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.part.CellEditorActionHandler; 46 import org.eclipse.ui.part.ViewPart; 47 48 import org.eclipse.search.ui.IActionGroupFactory; 49 import org.eclipse.search.ui.IContextMenuContributor; 50 import org.eclipse.search.ui.IGroupByKeyComputer; 51 import org.eclipse.search.ui.ISearchResultView; 52 53 56 public class SearchResultView extends ViewPart implements ISearchResultView { 57 58 59 private static Map fgLabelProviders= new HashMap (5); 60 61 private SearchResultViewer fViewer; 62 private Map fResponse; 63 private IMemento fMemento; 64 private IPropertyChangeListener fPropertyChangeListener; 65 private CellEditorActionHandler fCellEditorActionHandler; 66 private SelectAllAction fSelectAllAction; 67 68 71 public void init(IViewSite site, IMemento memento) throws PartInitException { 72 super.init(site, memento); 73 fMemento= memento; 74 } 75 76 79 public void saveState(IMemento memento) { 80 if (fViewer == null) { 81 if (fMemento != null) memento.putMemento(fMemento); 84 return; 85 } 86 fViewer.saveState(memento); 87 } 88 89 92 public void createPartControl(Composite parent) { 93 Assert.isTrue(fViewer == null); 94 fViewer= new SearchResultViewer(this, parent); 95 if (fMemento != null) 96 fViewer.restoreState(fMemento); 97 fMemento= null; 98 SearchManager.getDefault().addSearchChangeListener(fViewer); 99 fViewer.init(); 100 101 fCellEditorActionHandler = new CellEditorActionHandler(getViewSite().getActionBars()); 103 fSelectAllAction= new SelectAllAction(); 104 fSelectAllAction.setViewer(fViewer); 105 fCellEditorActionHandler.setSelectAllAction(fSelectAllAction); 106 107 fillActionBars(getViewSite().getActionBars()); 108 109 fPropertyChangeListener= new IPropertyChangeListener() { 110 public void propertyChange(PropertyChangeEvent event) { 111 if (SearchPreferencePage.POTENTIAL_MATCH_FG_COLOR.equals(event.getProperty()) || SearchPreferencePage.EMPHASIZE_POTENTIAL_MATCHES.equals(event.getProperty())) 112 if (fViewer != null) 113 fViewer.updatedPotentialMatchFgColor(); 114 } 115 }; 116 117 SearchPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener); 118 119 PlatformUI.getWorkbench().getHelpSystem().setHelp(fViewer.getControl(), SearchPlugin.getDefault().getSearchViewHelpContextId()); 120 } 121 122 125 public SearchResultViewer getViewer() { 126 return fViewer; 127 } 128 129 131 132 public void setFocus() { 133 fViewer.getControl().setFocus(); 134 } 135 136 public void dispose() { 137 if (fViewer != null) { 138 SearchManager.getDefault().removeSearchChangeListener(fViewer); 139 fViewer= null; 140 } 141 if (fPropertyChangeListener != null) 142 SearchPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener); 143 if (fCellEditorActionHandler != null) { 144 fCellEditorActionHandler.dispose(); 145 fCellEditorActionHandler= null; 146 } 147 super.dispose(); 148 } 149 150 protected void setContentDescription(String title) { 151 super.setContentDescription(title); 152 } 153 154 protected void setTitleToolTip(String text) { 155 super.setTitleToolTip(text); 156 } 157 158 160 private void fillActionBars(IActionBars actionBars) { 161 IToolBarManager toolBar= actionBars.getToolBarManager(); 162 fillToolBar(toolBar); 163 actionBars.updateActionBars(); 164 165 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction); 167 } 168 169 private void fillToolBar(IToolBarManager tbm) { 170 fViewer.fillToolBar(tbm); 171 } 172 173 ILabelProvider getLabelProvider(String pageId) { 174 if (pageId != null) 175 return (ILabelProvider)fgLabelProviders.get(pageId); 176 return null; 177 } 178 179 public ILabelProvider getLabelProvider() { 180 if (fViewer == null) 181 return null; 182 IBaseLabelProvider labelProvider= fViewer.getLabelProvider(); 183 if (labelProvider == null) 184 return null; 185 186 return ((SearchResultLabelProvider)labelProvider).getLabelProvider(); 187 } 188 189 private void setGotoMarkerAction(final IAction gotoMarkerAction) { 190 getDisplay().syncExec(new Runnable () { 192 public void run() { 193 getViewer().setGotoMarkerAction(gotoMarkerAction); 194 } 195 }); 196 } 197 198 199 Display getDisplay() { 200 return fViewer.getControl().getDisplay(); 201 } 202 203 204 206 207 210 public ISelection getSelection() { 211 return fViewer.getSelection(); 212 } 213 214 217 public void searchStarted( 218 IActionGroupFactory groupFactory, 219 String singularLabel, 220 String pluralLabelPattern, 221 ImageDescriptor imageDescriptor, 222 String pageId, 223 ILabelProvider labelProvider, 224 IAction gotoAction, 225 IGroupByKeyComputer groupByKeyComputer, 226 IRunnableWithProgress operation) { 227 228 229 Assert.isNotNull(pageId); 230 Assert.isNotNull(pluralLabelPattern); 231 Assert.isNotNull(gotoAction); 232 233 fResponse= new HashMap (500); 234 setGotoMarkerAction(gotoAction); 235 236 ILabelProvider oldLabelProvider= (ILabelProvider)fgLabelProviders.get(pageId); 237 if (oldLabelProvider != null) 238 oldLabelProvider.dispose(); 239 fgLabelProviders.put(pageId, labelProvider); 240 241 SearchManager.getDefault().addNewSearch( 242 new Search( 243 pageId, 244 singularLabel, 245 pluralLabelPattern, 246 null, 247 imageDescriptor, 248 fViewer.getGotoMarkerAction(), 249 groupFactory, 250 groupByKeyComputer, 251 operation)); 252 } 253 254 258 public void searchStarted( 259 String pageId, 260 String label, 261 ImageDescriptor imageDescriptor, 262 IContextMenuContributor contributor, 263 ILabelProvider labelProvider, 264 IAction gotoAction, 265 IGroupByKeyComputer groupByKeyComputer, 266 IRunnableWithProgress operation) { 267 268 searchStarted(pageId, null, label, imageDescriptor, contributor, labelProvider, gotoAction, groupByKeyComputer, operation); 269 } 270 271 275 public void searchStarted( 276 String pageId, 277 String singularLabel, 278 String pluralLabelPattern, 279 ImageDescriptor imageDescriptor, 280 IContextMenuContributor contributor, 281 ILabelProvider labelProvider, 282 IAction gotoAction, 283 IGroupByKeyComputer groupByKeyComputer, 284 IRunnableWithProgress operation) { 285 286 287 Assert.isNotNull(pageId); 288 Assert.isNotNull(pluralLabelPattern); 289 Assert.isNotNull(gotoAction); 290 291 fResponse= new HashMap (500); 292 setGotoMarkerAction(gotoAction); 293 294 ILabelProvider oldLabelProvider= (ILabelProvider)fgLabelProviders.get(pageId); 295 if (oldLabelProvider != null) 296 oldLabelProvider.dispose(); 297 fgLabelProviders.put(pageId, labelProvider); 298 299 SearchManager.getDefault().addNewSearch( 300 new Search( 301 pageId, 302 singularLabel, 303 pluralLabelPattern, 304 null, 305 imageDescriptor, 306 fViewer.getGotoMarkerAction(), 307 contributor, 308 groupByKeyComputer, 309 operation)); 310 } 311 312 315 public void addMatch(String description, Object groupByKey, IResource resource, IMarker marker) { 316 SearchResultViewEntry entry= (SearchResultViewEntry)fResponse.get(groupByKey); 317 if (entry == null) { 318 entry= new SearchResultViewEntry(groupByKey, resource); 319 fResponse.put(groupByKey, entry); 320 } 321 entry.add(marker); 322 } 323 324 325 328 public void searchFinished() { 329 SearchManager.getDefault().searchFinished(new ArrayList (fResponse.values())); 330 fResponse= null; 331 } 332 } 333 | Popular Tags |