Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aff2ec9230ac09543fb5506ca3dbe27e395f3bf5 (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
/*******************************************************************************
 * Copyright (c) 2013 Pivotal Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Pivotal Software, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.text.quicksearch.internal.core.preferences;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.text.quicksearch.internal.core.priority.DefaultPriorityFunction;
import org.eclipse.text.quicksearch.internal.ui.QuickSearchActivator;
import org.eclipse.text.quicksearch.internal.util.LineReader;

/**
 * Helper class to access the QuickSearch Preferences.
 *
 * @author Kris De Volder
 */
public class QuickSearchPreferences {

	//Keys used to fetch 'raw' preferences values from the preferences store.
	public static final String IGNORED_EXTENSIONS = "ignored.extensions";
	public static final String IGNORED_NAMES = "ignored.names";
	public static final String IGNORED_PREFIXES = "ignored.prefixes";
	public static final String MAX_LINE_LEN = "LineReader.MAX_LINE_LEN";
	private static boolean initializedDefaults;

	private IPreferenceStore store;

	public QuickSearchPreferences(IPreferenceStore preferenceStore) {
		this.store = preferenceStore;
		initializeDefaults();
	}

	public String[] getIgnoredExtensions() {
		return getAndParseStringList(IGNORED_EXTENSIONS);
	}

	public String[] getIgnoredPrefixes() {
		return getAndParseStringList(IGNORED_PREFIXES);
	}

	public String[] getIgnoredNames() {
		return getAndParseStringList(IGNORED_NAMES);
	}

	public int getMaxLineLen() {
		return store.getInt(MAX_LINE_LEN);
	}

	private String[] getAndParseStringList(String key) {
		String raw = store.getString(key);
		if (raw!=null) {
			return parseStringList(raw);
		}
		return null;
	}

	/**
	 * Takes a raw string list as entered in the prefs page input field and parses it.
	 * <p>
	 * Commas and newline are treated as 'separators' between elements. Further, any trailing
	 * and leading whitespace is stripped from individual elements and empty strings are silently
	 * dropped.
	 */
	private String[] parseStringList(String raw) {
		String[] elements = raw.split("[,\n]");
		List<String> list = new ArrayList<String>(elements.length);
		for (String e : elements) {
			e = e.trim();
			if (!"".equals(e)) {
				list.add(e);
			}
		}
		return list.toArray(new String[list.size()]);
	}

	public static void initializeDefaults() {
		if (!initializedDefaults) {
			initializedDefaults = true;
			IPreferenceStore store = QuickSearchActivator.getDefault().getPreferenceStore();
			store.setDefault(QuickSearchPreferences.MAX_LINE_LEN, LineReader.DEFAULT_MAX_LINE_LENGTH);

			DefaultPriorityFunction dpf =  new DefaultPriorityFunction();
			store.setDefault(QuickSearchPreferences.IGNORED_EXTENSIONS, encode(dpf.ignoredExtensions));
			store.setDefault(QuickSearchPreferences.IGNORED_NAMES, encode(dpf.ignoredNames));
			store.setDefault(QuickSearchPreferences.IGNORED_PREFIXES, encode(dpf.ignoredPrefixes));
		}
	}

	private static String encode(String[] strings) {
		StringBuilder encoded = new StringBuilder();
		for (int i = 0; i < strings.length; i++) {
			if (i>0) {
				encoded.append(", ");
			}
			encoded.append(strings[i]);
		}
		return encoded.toString();
	}


}

Back to the top