Skip to main content
summaryrefslogtreecommitdiffstats
blob: 73323c0a7177cf7c842ff0b9b7a959cc2e1935d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
package org.eclipse.search.internal.core.text;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;

import org.eclipse.search.internal.core.SearchScope;
import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.internal.ui.util.StringMatcher;

/**
 * A special text search scope that take file extensions into account.
 */
public class TextSearchScope extends SearchScope {
	
	private static class WorkspaceScope extends TextSearchScope {
	
		private WorkspaceScope() {
			super(SearchMessages.getString("WorkspaceScope")); //$NON-NLS-1$
		}
		
		public void add(IResource element) {
			// do nothing
		}
		
		/**
		 * @see ISearchScope#encloses(Object)
		 */
		public boolean encloses(IResource element) {
			if (element instanceof IFile && skipFile((IFile)element))
				return false;
			return true;	
		}	
	}
	
	private Set fExtensions= new HashSet(3);

	/**
	 * Returns a new Workbench scope.
	 */
	public static TextSearchScope newWorkspaceScope() {
		return new WorkspaceScope();
	}

	public TextSearchScope(String description) {
		super(description);
	}

	public TextSearchScope(String description, IResource[] resources) {
		super(description, resources);
	}
	
	/**
	 * Adds an extension to the scope.
	 */
	public void addExtension(String extension) {
		fExtensions.add(new StringMatcher(extension, false, false));
	}
	/**
	 * Adds all string patterns contained in <code>extensions</code> to this
	 * scope. The allowed pattern characters are <code>*</code> for any character
	 * and <code>?</code> for one character.
	 */
	public void addExtensions(Set extensions) {
		if (extensions == null)
			return;
		Iterator iter= extensions.iterator();
		while (iter.hasNext()) {
			Object obj= iter.next();
			if (obj instanceof String)
				addExtension((String)obj);
		}
	}  

	/*
	 * Implements method from ISearchScope
	 */
	public boolean encloses(IResource element) {
		if (element instanceof IFile && skipFile((IFile)element))
			return false;
		return super.encloses(element);	
	}

	boolean skipFile(IFile file) {
		if (file != null) {
			Iterator iter= fExtensions.iterator();
			while (iter.hasNext()) {
				if (((StringMatcher)iter.next()).match(file.getName()))
					return false;
			}
		}
		return true;
	}
}

Back to the top