Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4cfeea7110dd89557c8995eba6450ab318efc296 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
 * Copyright (c) 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 ******************************************************************************/
/*
 * Created on Jun 12, 2003
 */
package org.eclipse.cdt.internal.ui.search;

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

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkingSet;

/**
 * @author aniefer
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CSearchScopeFactory {
	private static CSearchScopeFactory fgInstance;
	private static ICSearchScope EMPTY_SCOPE= SearchEngine.createCSearchScope(new ICElement[]{});
	/**
	 * 
	 */
	public CSearchScopeFactory() {
		super();
	}
	
	public static CSearchScopeFactory getInstance() {
		if (fgInstance == null)
			fgInstance = new CSearchScopeFactory();
		return fgInstance;
	}
	/**
	 * @param sets
	 * @return
	 */
	public ICSearchScope createCSearchScope(IWorkingSet[] sets) {
		if (sets == null || sets.length < 1)
		return EMPTY_SCOPE;

		Set cElements= new HashSet(sets.length * 10);
		for (int i= 0; i < sets.length; i++)
			addCElements(cElements, sets[i]);
		return createCSearchScope(cElements);
	}
	/**
	 * @param cElements
	 * @return
	 */
	private ICSearchScope createCSearchScope(Set cElements) {
		return SearchEngine.createCSearchScope((ICElement[])cElements.toArray(new ICElement[cElements.size()]));
	}
	/**
	 * @param cElements
	 * @param set
	 */
	private void addCElements(Set cElements, IWorkingSet set) {
		if (set == null)
			return;
				
		IAdaptable[] elements= set.getElements();
		for (int i= 0; i < elements.length; i++) {
			if (elements[i] instanceof ICElement)
				addCElements(cElements, (ICElement)elements[i]);
			else
				addCElements(cElements, elements[i]);
		}
	}
	/**
	 * @param cElements
	 * @param adaptable
	 */
	private void addCElements(Set cElements, IAdaptable resource) {
		ICElement cElement= (ICElement)resource.getAdapter(ICElement.class);
		if (cElement == null)
			// not an ICElement resource
			return;
				
		addCElements(cElements, cElement);
	}
	/**
	 * @param cElements
	 * @param element
	 */
	private void addCElements(Set cElements, ICElement element) {
				cElements.add(element);
	}

	/**
	 * @param fStructuredSelection
	 * @return
	 */
	public ICSearchScope createCSearchScope(IStructuredSelection fStructuredSelection) {
		Set cElements = new HashSet( fStructuredSelection.size() );
		
		Iterator iter = fStructuredSelection.iterator();
		while( iter.hasNext() ){
			addCElements( cElements, (IAdaptable)iter.next() );
		}
		
		return createCSearchScope( cElements );
	}
	
}

Back to the top