KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > ReplaceAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui.text;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
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 /* package */ class ReplaceAction extends Action {
55     
56     private IWorkbenchSite fSite;
57     private List JavaDoc fElements;
58     
59     public ReplaceAction(IWorkbenchSite site, List JavaDoc elements) {
60         Assert.isNotNull(site);
61         fSite= site;
62         if (elements != null)
63             fElements= elements;
64         else
65             fElements= new ArrayList JavaDoc(0);
66         setText(SearchMessages.getString("ReplaceAction.label_all")); //$NON-NLS-1$
67
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")); //$NON-NLS-1$
74
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")); //$NON-NLS-1$
88
}
89     }
90     
91     private boolean validateResources(final TextSearchOperation operation) {
92         final List JavaDoc outOfDateEntries= new ArrayList JavaDoc();
93         for (Iterator JavaDoc 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 JavaDoc outOfSyncEntries= new ArrayList JavaDoc();
101         for (Iterator JavaDoc 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 JavaDoc e) {
119                     ExceptionHandler.handle(e, fSite.getShell(), SearchMessages.getString("ReplaceAction.label"), SearchMessages.getString("ReplaceAction.research.error")); //$NON-NLS-1$ //$NON-NLS-2$
120
} catch (InterruptedException JavaDoc e) {
121                     // canceled
122
}
123             }
124             return false;
125         }
126         return true;
127     }
128
129     private void research(IProgressMonitor monitor, List JavaDoc outOfDateEntries, TextSearchOperation operation) throws CoreException {
130         IStatus status= null;
131         for (Iterator JavaDoc 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 JavaDoc outOfDateEntries, List JavaDoc 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 JavaDoc getDialogTitle() {
150         return SearchMessages.getString("ReplaceAction.dialog.title"); //$NON-NLS-1$
151
}
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 JavaDoc markers= new ArrayList JavaDoc();
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                 // nothing to do
178
}
179             
180             public void accept(IResourceProxy proxy, String JavaDoc 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 JavaDoc attributes= new HashMap JavaDoc(4);
186                 attributes.put(SearchUI.LINE, line);
187                 attributes.put(IMarker.CHAR_START, new Integer JavaDoc(start));
188                 attributes.put(IMarker.CHAR_END, new Integer JavaDoc(start + length));
189                 attributes.put(IMarker.LINE_NUMBER, new Integer JavaDoc(lineNumber));
190                 marker.setAttributes(attributes);
191             }
192             
193             public void done(){
194                 // nothing to do
195
}
196         });
197         IStatus status = operation.getStatus();
198         if (status == null || status.isOK()) {
199             for (Iterator JavaDoc 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