Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3470f85deacc6c57d634000ebcf053bf27fd769 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.eclipse.cdt.internal.ui.cview;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
 
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;

import org.eclipse.cdt.internal.ui.util.StringMatcher;
import org.eclipse.cdt.ui.CUIPlugin;

/**
 * The FiltersContent provides the elements for use by the list dialog
 * for selecting the patterns to apply.
 */ 
public class CElementFilters {
	static List definedFilters = null;
	static List defaultFilters = null;
	static StringMatcher [] matchers = null;
	static String FILTERS_TAG= "CElementFilters"; //$NON-NLS-1$
	static String COMMA_SEPARATOR = ","; //$NON-NLS-1$

	/**
	 * Returns the filters which are enabled by default.
	 *
	 * @return a list of strings
	 */
	public static List getDefaultFilters() {
		if (defaultFilters == null) {
			readFilters();
		}
		return defaultFilters;
	}

	/**
	 * Returns the filters currently defined for the workbench. 
	 */
	public static List getDefinedFilters() {
		if (definedFilters == null) {
			// Overide the default by the user preference
			CUIPlugin plugin = CUIPlugin.getDefault();
			String storedPatterns= plugin.getPluginPreferences().getString(FILTERS_TAG);

			if (storedPatterns.length() > 0) {
				StringTokenizer entries = new StringTokenizer(storedPatterns, COMMA_SEPARATOR);
				definedFilters = new ArrayList();

				while (entries.hasMoreElements()) {
					String nextToken = entries.nextToken();
					definedFilters.add(nextToken);
				}
			} else {
				readFilters();
			}
		}
		return definedFilters;
	}

	public static StringMatcher [] getMatchers() {
		if (matchers == null) {
			List list = getDefinedFilters();
			matchers = new StringMatcher[list.size()];
			for (int i = 0; i < matchers.length; i++) {
				matchers[i] = new StringMatcher((String)(list.get(i)), true, false);
			}
		}
		return matchers;
	}

	/**
     * Define new Patterns for the Duration of the session.
     */
    public static void setPatterns(String[] newPatterns) {
		//System.out.println ("SetPatterns call");
        matchers = new StringMatcher[newPatterns.length];
        for (int i = 0; i < newPatterns.length; i++) {
			//System.out.println ("Patterns " + newPatterns[i]);
            matchers[i] = new StringMatcher(newPatterns[i], true, false);
        }
		//CElementFactory.getDefault().refreshDeadBranchParents();
    }


	public static boolean match(String name) {
		StringMatcher [] m = getMatchers();
		if (m == null)
			return false;
		//System.out.println ("Pattern " + name);
		for (int i = 0; i < m.length; i++) {
			if (m[i].match(name)) {
				//System.out.println ("Match " + name);
				return true;
			}
		}
		return false;
	}

	private CElementFilters() {
	}
	/**
 	 * Reads the filters currently defined for the workbench. 
 	 */
	static void readFilters() {
		definedFilters = new ArrayList();
		defaultFilters = new ArrayList();
		CUIPlugin plugin = CUIPlugin.getDefault();
		if (plugin != null) {
			IExtensionPoint extension = plugin.getDescriptor().getExtensionPoint(FILTERS_TAG);
			if (extension != null) {
				IExtension[] extensions =  extension.getExtensions();
				for(int i = 0; i < extensions.length; i++){
					IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
					for(int j = 0; j < configElements.length; j++){
						String pattern = configElements[j].getAttribute("pattern"); //$NON-NLS-1$
						if (pattern != null)
							definedFilters.add(pattern);
						String selected = configElements[j].getAttribute("selected"); //$NON-NLS-1$
						if (selected != null && selected.equalsIgnoreCase("true")) //$NON-NLS-1$
							defaultFilters.add(pattern);
					}
				}
			}		
		}
	}
}

Back to the top