KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > RemovePotentialMatchesAction


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

11 package org.eclipse.search.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.CoreException;
18
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23
24 import org.eclipse.ui.IWorkbenchSite;
25
26 import org.eclipse.search.ui.SearchUI;
27
28 import org.eclipse.search.internal.ui.util.ExceptionHandler;
29
30 /**
31  * @deprecated old search
32  */

33 class RemovePotentialMatchesAction extends Action {
34
35     private IWorkbenchSite fSite;
36
37     public RemovePotentialMatchesAction(IWorkbenchSite site) {
38         fSite= site;
39
40         if (usePluralLabel()) {
41             setText(SearchMessages.RemovePotentialMatchesAction_removePotentialMatches_text);
42             setToolTipText(SearchMessages.RemovePotentialMatchesAction_removePotentialMatches_tooltip);
43         }
44         else {
45             setText(SearchMessages.RemovePotentialMatchesAction_removePotentialMatch_text);
46             setToolTipText(SearchMessages.RemovePotentialMatchesAction_removePotentialMatch_tooltip);
47         }
48     }
49     
50     public void run() {
51         IMarker[] markers= getMarkers();
52         if (markers != null)
53             try {
54                 SearchPlugin.getWorkspace().deleteMarkers(markers);
55             } catch (CoreException ex) {
56                 ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message);
57             }
58         else {
59             String JavaDoc title= SearchMessages.RemovePotentialMatchesAction_dialog_title;
60             String JavaDoc message= SearchMessages.RemovePotentialMatchesAction_dialog_message;
61             MessageDialog.openInformation(fSite.getShell(), title, message);
62         }
63
64         // action only makes sense once
65
setEnabled(false);
66     }
67     
68     private IMarker[] getMarkers() {
69
70         ISelection s= fSite.getSelectionProvider().getSelection();
71         if (! (s instanceof IStructuredSelection))
72             return null;
73         IStructuredSelection selection= (IStructuredSelection)s;
74
75         int size= selection.size();
76         if (size <= 0)
77             return null;
78
79         ArrayList JavaDoc markers= new ArrayList JavaDoc(size * 3);
80         Iterator JavaDoc iter= selection.iterator();
81         for(int i= 0; iter.hasNext(); i++) {
82             SearchResultViewEntry entry= (SearchResultViewEntry)iter.next();
83             Iterator JavaDoc entryIter= entry.getMarkers().iterator();
84             while (entryIter.hasNext()) {
85                 IMarker marker= (IMarker)entryIter.next();
86                 if (marker.getAttribute(SearchUI.POTENTIAL_MATCH, false))
87                     markers.add(marker);
88             }
89         }
90         return (IMarker[])markers.toArray(new IMarker[markers.size()]);
91     }
92
93     private boolean usePluralLabel() {
94         ISelection s= fSite.getSelectionProvider().getSelection();
95
96         if (! (s instanceof IStructuredSelection) || s.isEmpty())
97             return false;
98     
99         IStructuredSelection selection= (IStructuredSelection)s;
100         int size= selection.size();
101         if (size <= 0)
102             return false;
103
104         int markerCount= 0;
105         Iterator JavaDoc iter= selection.iterator();
106         for(int i= 0; iter.hasNext(); i++) {
107             SearchResultViewEntry entry= (SearchResultViewEntry)iter.next();
108             Iterator JavaDoc entryIter= entry.getMarkers().iterator();
109             while (entryIter.hasNext()) {
110                 IMarker marker= (IMarker)entryIter.next();
111                 if (marker.getAttribute(SearchUI.POTENTIAL_MATCH, false)) {
112                     markerCount++;
113                 }
114                 if (markerCount > 1)
115                     return true;
116             }
117         }
118         return false;
119     }
120 }
121
Popular Tags