Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1fa50504efd85b69ee96beff04ef525d648fcacb (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package org.eclipse.cdt.internal.ui.preferences;

import java.util.ArrayList;

import org.eclipse.cdt.internal.ui.text.contentassist.ContentAssistPreference;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Combo;

/**
 * A class which encapsulates several utility functions
 * related to code completion preference settings.
 */
public class ProposalFilterPreferencesUtil {

	/**
	 * Private default constructor prevents instantiation
	 */
	private ProposalFilterPreferencesUtil() {
	}

	/**
	 * Get an array of proposal filter names
	 * (i.e. the human-readable text for display
	 * to fill into the Combo)
	 */
	public static String[] getProposalFilterNames() {
		ArrayList names = new ArrayList();
		try {
			IExtensionPoint point = Platform.getExtensionRegistry()
					.getExtensionPoint(CUIPlugin.PLUGIN_ID, "ProposalFilter"); //$NON-NLS-1$
			if (point != null) {
				IExtension[] extensions = point.getExtensions();
				for (int i = 0; i < extensions.length; i++) {
					IExtension extension = extensions[i];
					IConfigurationElement[] elements = extension
							.getConfigurationElements();
					for (int j = 0; j < elements.length; ++j) {
						IConfigurationElement element = elements[j];
						if ("ProposalFilter".equals(element.getName())) { //$NON-NLS-1$
							String filterName = element.getAttribute("name");
							if (null != filterName) {
								names.add(filterName);
							}
						}
					}
				}
			}
		} catch (InvalidRegistryObjectException e) {
			// No action required since we will at least be using the fail-safe default filter
			CUIPlugin.getDefault().log(e);
		}
		String[] filterNames = (String[]) names
				.toArray(new String[names.size()]);
		return filterNames;
	}

	/**
	 * Look up all contributed completion proposal filters 
	 * and return their names as a semicolon-separated list
	 * plus a leading entry for the selected index 0,
	 * plus a leading <default> entry. <br>
	 * A Combo may be initialized from this string.
	 * @return The list of filter names
	 */
	public static String getProposalFilternamesAsString() {
		StringBuffer filterNames = new StringBuffer("0;");
		filterNames.append("<Default Filter>"); // TODO: NP externalize this!
		String[] names = getProposalFilterNames();
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			filterNames.append(";");
			filterNames.append(name);
		}
		return filterNames.toString();
	}

	/**
	 * Return the configuration element which corresponds 
	 * to the human-readable filter name
	 * @param filterName The human-readable filter name
	 * @return The configuration element, or null if there is none
	 */
	public static IConfigurationElement getElementForName(String filterName) {
		IConfigurationElement element = null;
		IExtensionPoint point = Platform.getExtensionRegistry()
				.getExtensionPoint(CUIPlugin.PLUGIN_ID, "ProposalFilter"); //$NON-NLS-1$
		if (point != null) {
			try {
				IExtension[] extensions = point.getExtensions();
				if (extensions.length >= 1) {
					for (int i = 0; i < extensions.length; i++) {
						IExtension extension = extensions[i];

						IConfigurationElement[] elements = extension
								.getConfigurationElements();

						for (int j = 0; j < elements.length; ++j) {
							IConfigurationElement testElement = elements[j];
							if ("ProposalFilter".equals(testElement.getName())) { //$NON-NLS-1$
								String testName = testElement
										.getAttribute("name");
								if ((null != testName)
										&& (filterName.equals(testName))) {
									element = testElement;
									break;
								}
							}
						}
						// Did we find the corresponding element?
						if (null != element)
							break;
					}
				}
			} catch (InvalidRegistryObjectException e) {
				// In case of failure we'll just return null
			}
		}

		return element;
	}

	/**
	 * The state of a Combo consists of the list of entries
	 * and the index of the selected entry.
	 * This method converts the state of the given Combo 
	 * to a string representation for storage in a preference store. <br>
	 * The string contains a semicolon-separated list of entries.
	 * The first entry is the index of the selected entry.
	 * The following entries are the texts of the individual fields. <br>
	 * Since the semicolon is the separator, the entries cannot contain semicolons.
	 * This method will replace semicolons with commas if any are found.
	 * @param combo The Combo whose state shall be converted
	 * @return A string representation of the Combo state
	 */
	public static String comboStateAsString(Combo combo) {
		StringBuffer text = new StringBuffer();
		int selectionIndex = combo.getSelectionIndex();
		text.append(selectionIndex);
		String[] entries = combo.getItems();
		for (int i = 0; i < entries.length; i++) {
			text.append(";");
			String entry = entries[i].replaceAll(";", ",");
			text.append(entry);
		}
		return text.toString();
	}

	/**
	 * The state of a Combo consists of the list of entries
	 * and the index of the selected entry.
	 * This method takes a string representation of the state (e.g. from a preference store)
	 * and restores it into the Combo. <br>
	 * For a description of the text format see method comboStateAsString().
	 * @param combo The combo to be restored.
	 * @param text The text representation of the state.
	 */
	public static void restoreComboFromString(Combo combo, String text) {
		try {
			int endFirstEntry = text.indexOf(";");
			if (endFirstEntry > 0) { // First entry must contain at least one character
				String selectedString = text.substring(0, endFirstEntry);
				int selectedIndex = Integer.parseInt(selectedString);
				String[] entryList = text.substring(endFirstEntry + 1,
						text.length()).split(";");
				combo.setItems(entryList);
				combo.select(selectedIndex);
			}
		} catch (NumberFormatException e) {
			// If this fails we just return the unmodified Combo
		}
	}

	/**
	 * Convenience class wraps the data to initialize a Combo
	 */
	public static class ComboState {
		public int selectedIndex;

		public String[] items;
	}

	/** 
	 * Convenience method to extract the state of a Combo
	 * from the state string stored e.g. in a preference store
	 * @param comboPreference The state string
	 * @return A ComboState instance. 
	 */
	public static ComboState getComboState(String comboPreference) {
		ComboState state = new ComboState();
		try {
			int endFirstEntry = comboPreference.indexOf(";");
			if (endFirstEntry > 0) { // First entry must contain at least one character
				String selectedString = comboPreference.substring(0,
						endFirstEntry);
				state.selectedIndex = Integer.parseInt(selectedString);
				state.items = comboPreference.substring(endFirstEntry + 1,
						comboPreference.length()).split(";");
			}
		} catch (NumberFormatException e) {
			// If this fails we return an empty ComboState
			state.items = new String[0];
		}
		return state;
	}

	/**
	 * Look up the setting for the preferred proposal filter
	 * and return it's configuration element.
	 * @return The configuration element, or null if none is found.
	 */
	public static IConfigurationElement getPreferredFilterElement() {
		IConfigurationElement preferredElement = null;
		try {
			IPreferenceStore store = CUIPlugin.getDefault()
					.getPreferenceStore();
			String filterComboStateString = store
					.getString(ContentAssistPreference.PROPOSALS_FILTER);
			ComboState state = getComboState(filterComboStateString);
			preferredElement = getElementForName(state.items[state.selectedIndex]);
		} catch (Exception e) {
			// If anything goes wrong we'll just return null
		}
		return preferredElement;
	}

}

Back to the top