KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
31  * @deprecated old search
32  */

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 JavaDoc() {
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 JavaDoc markers= new ArrayList JavaDoc(size * 3);
74         int markerCount= 0;
75         Iterator JavaDoc 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 JavaDoc firstElement= selection.getFirstElement();
94         if (firstElement instanceof ISearchResultViewEntry)
95             return ((ISearchResultViewEntry)firstElement).getMatchCount() > 1;
96         return false;
97     }
98 }
99
Popular Tags