1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.core.runtime.CoreException; 18 19 import org.eclipse.swt.custom.BusyIndicator; 20 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.ISelectionProvider; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 26 import org.eclipse.search.ui.ISearchResultViewEntry; 27 28 import org.eclipse.search.internal.ui.util.ExceptionHandler; 29 30 33 class RemoveResultAction extends Action { 34 35 private ISelectionProvider fSelectionProvider; 36 37 public RemoveResultAction(ISelectionProvider provider, boolean stringsDependOnMatchCount) { 38 fSelectionProvider= provider; 39 if (!stringsDependOnMatchCount || usePluralLabel()) { 40 setText(SearchMessages.SearchResultView_removeEntries_text); 41 setToolTipText(SearchMessages.SearchResultView_removeEntries_tooltip); 42 } 43 else { 44 setText(SearchMessages.SearchResultView_removeEntry_text); 45 setToolTipText(SearchMessages.SearchResultView_removeEntry_tooltip); 46 } 47 SearchPluginImages.setImageDescriptors(this, SearchPluginImages.T_LCL, SearchPluginImages.IMG_LCL_SEARCH_REM); 48 } 49 50 public void run() { 51 final IMarker[] markers= getMarkers(fSelectionProvider.getSelection()); 52 if (markers != null) { 53 BusyIndicator.showWhile(SearchPlugin.getActiveWorkbenchShell().getDisplay(), new Runnable () { 54 public void run() { 55 try { 56 SearchPlugin.getWorkspace().deleteMarkers(markers); 57 } catch (CoreException ex) { 58 ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message); 59 } 60 } 61 }); 62 } 63 } 64 65 private IMarker[] getMarkers(ISelection s) { 66 if (! (s instanceof IStructuredSelection) || s.isEmpty()) 67 return null; 68 69 IStructuredSelection selection= (IStructuredSelection)s; 70 int size= selection.size(); 71 if (size <= 0) 72 return null; 73 ArrayList markers= new ArrayList (size * 3); 74 int markerCount= 0; 75 Iterator iter= selection.iterator(); 76 for(int i= 0; iter.hasNext(); i++) { 77 SearchResultViewEntry entry= (SearchResultViewEntry)iter.next(); 78 markerCount += entry.getMatchCount(); 79 markers.addAll(entry.getMarkers()); 80 } 81 return (IMarker[])markers.toArray(new IMarker[markerCount]); 82 } 83 84 private boolean usePluralLabel() { 85 ISelection s= fSelectionProvider.getSelection(); 86 if (s == null || s.isEmpty() || !(s instanceof IStructuredSelection)) 87 return false; 88 IStructuredSelection selection= (IStructuredSelection)s; 89 90 if (selection.size() != 1) 91 return true; 92 93 Object firstElement= selection.getFirstElement(); 94 if (firstElement instanceof ISearchResultViewEntry) 95 return ((ISearchResultViewEntry)firstElement).getMatchCount() > 1; 96 return false; 97 } 98 } 99 | Popular Tags |