1 11 package org.eclipse.search.internal.ui.text; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IResourceProxy; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 27 import org.eclipse.core.filebuffers.FileBuffers; 28 import org.eclipse.core.filebuffers.ITextFileBuffer; 29 import org.eclipse.core.filebuffers.ITextFileBufferManager; 30 31 import org.eclipse.jface.action.Action; 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.dialogs.MessageDialog; 34 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 35 import org.eclipse.jface.operation.IRunnableWithProgress; 36 import org.eclipse.jface.util.Assert; 37 import org.eclipse.jface.viewers.ILabelProvider; 38 import org.eclipse.jface.viewers.IStructuredSelection; 39 40 import org.eclipse.ui.IWorkbenchSite; 41 import org.eclipse.ui.actions.WorkspaceModifyOperation; 42 43 import org.eclipse.search.ui.SearchUI; 44 45 import org.eclipse.search.internal.core.text.ITextSearchResultCollector; 46 import org.eclipse.search.internal.ui.Search; 47 import org.eclipse.search.internal.ui.SearchManager; 48 import org.eclipse.search.internal.ui.SearchMessages; 49 import org.eclipse.search.internal.ui.SearchPlugin; 50 import org.eclipse.search.internal.ui.SearchResultView; 51 import org.eclipse.search.internal.ui.SearchResultViewEntry; 52 import org.eclipse.search.internal.ui.util.ExceptionHandler; 53 54 class ReplaceAction extends Action { 55 56 private IWorkbenchSite fSite; 57 private List fElements; 58 59 public ReplaceAction(IWorkbenchSite site, List elements) { 60 Assert.isNotNull(site); 61 fSite= site; 62 if (elements != null) 63 fElements= elements; 64 else 65 fElements= new ArrayList (0); 66 setText(SearchMessages.getString("ReplaceAction.label_all")); setEnabled(!fElements.isEmpty()); 68 } 69 70 public ReplaceAction(IWorkbenchSite site, IStructuredSelection selection) { 71 Assert.isNotNull(site); 72 fSite= site; 73 setText(SearchMessages.getString("ReplaceAction.label_selected")); fElements= selection.toList(); 75 setEnabled(!fElements.isEmpty()); 76 } 77 78 public void run() { 79 Search search= SearchManager.getDefault().getCurrentSearch(); 80 IRunnableWithProgress operation= search.getOperation(); 81 if (operation instanceof TextSearchOperation) { 82 if (validateResources((TextSearchOperation) operation)) { 83 ReplaceDialog dialog= new ReplaceDialog(fSite.getShell(), fElements, (TextSearchOperation)operation); 84 dialog.open(); 85 } 86 } else { 87 MessageDialog.openError(fSite.getShell(), getDialogTitle(), SearchMessages.getString("ReplaceAction.error.only_on_text_search")); } 89 } 90 91 private boolean validateResources(final TextSearchOperation operation) { 92 final List outOfDateEntries= new ArrayList (); 93 for (Iterator elements = fElements.iterator(); elements.hasNext();) { 94 SearchResultViewEntry entry = (SearchResultViewEntry) elements.next(); 95 if (isOutOfDate(entry)) { 96 outOfDateEntries.add(entry); 97 } 98 } 99 100 final List outOfSyncEntries= new ArrayList (); 101 for (Iterator elements = fElements.iterator(); elements.hasNext();) { 102 SearchResultViewEntry entry = (SearchResultViewEntry) elements.next(); 103 if (isOutOfSync(entry)) { 104 outOfSyncEntries.add(entry); 105 } 106 } 107 108 if (outOfDateEntries.size() > 0 || outOfSyncEntries.size() > 0) { 109 if (askForResearch(outOfDateEntries, outOfSyncEntries)) { 110 ProgressMonitorDialog pmd= new ProgressMonitorDialog(fSite.getShell()); 111 try { 112 pmd.run(true, true, new WorkspaceModifyOperation(null) { 113 protected void execute(IProgressMonitor monitor) throws CoreException { 114 research(monitor, outOfDateEntries, operation); 115 } 116 }); 117 return true; 118 } catch (InvocationTargetException e) { 119 ExceptionHandler.handle(e, fSite.getShell(), SearchMessages.getString("ReplaceAction.label"), SearchMessages.getString("ReplaceAction.research.error")); } catch (InterruptedException e) { 121 } 123 } 124 return false; 125 } 126 return true; 127 } 128 129 private void research(IProgressMonitor monitor, List outOfDateEntries, TextSearchOperation operation) throws CoreException { 130 IStatus status= null; 131 for (Iterator elements = outOfDateEntries.iterator(); elements.hasNext();) { 132 SearchResultViewEntry entry = (SearchResultViewEntry) elements.next(); 133 status = research(operation, monitor, entry); 134 if (status != null && !status.isOK()) { 135 throw new CoreException(status); 136 } 137 } 138 } 139 140 private boolean askForResearch(List outOfDateEntries, List outOfSyncEntries) { 141 SearchResultView view= (SearchResultView) SearchPlugin.getSearchResultView(); 142 ILabelProvider labelProvider= null; 143 if (view != null) 144 labelProvider= view.getLabelProvider(); 145 SearchAgainConfirmationDialog dialog= new SearchAgainConfirmationDialog(fSite.getShell(), labelProvider, outOfSyncEntries, outOfDateEntries); 146 return dialog.open() == IDialogConstants.OK_ID; 147 } 148 149 private String getDialogTitle() { 150 return SearchMessages.getString("ReplaceAction.dialog.title"); } 152 153 private boolean isOutOfDate(SearchResultViewEntry entry) { 154 IResource resource= entry.getResource(); 155 if (entry.getModificationStamp() != resource.getModificationStamp()) 156 return true; 157 ITextFileBufferManager bm= FileBuffers.getTextFileBufferManager(); 158 ITextFileBuffer fb= bm.getTextFileBuffer(resource.getFullPath()); 159 if (fb != null && fb.isDirty()) 160 return true; 161 return false; 162 } 163 164 private boolean isOutOfSync(SearchResultViewEntry entry) { 165 return !entry.getResource().isSynchronized(IResource.DEPTH_ZERO); 166 } 167 168 private IStatus research(TextSearchOperation operation, final IProgressMonitor monitor, SearchResultViewEntry entry) throws CoreException { 169 List markers= new ArrayList (); 170 markers.addAll(entry.getMarkers()); 171 operation.searchInFile((IFile) entry.getResource(), new ITextSearchResultCollector() { 172 public IProgressMonitor getProgressMonitor() { 173 return monitor; 174 } 175 176 public void aboutToStart() { 177 } 179 180 public void accept(IResourceProxy proxy, String line, int start, int length, int lineNumber) throws CoreException { 181 IFile file= (IFile)proxy.requestResource(); 182 if (start < 0 || length < 1) 183 return; 184 IMarker marker= file.createMarker(SearchUI.SEARCH_MARKER); 185 HashMap attributes= new HashMap (4); 186 attributes.put(SearchUI.LINE, line); 187 attributes.put(IMarker.CHAR_START, new Integer (start)); 188 attributes.put(IMarker.CHAR_END, new Integer (start + length)); 189 attributes.put(IMarker.LINE_NUMBER, new Integer (lineNumber)); 190 marker.setAttributes(attributes); 191 } 192 193 public void done(){ 194 } 196 }); 197 IStatus status = operation.getStatus(); 198 if (status == null || status.isOK()) { 199 for (Iterator markerIter = markers.iterator(); markerIter.hasNext();) { 200 IMarker marker = (IMarker) markerIter.next(); 201 marker.delete(); 202 } 203 } 204 return status; 205 } 206 207 } 208 | Popular Tags |