Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: b6c171f8db6795457443a156889c50824a52a00f (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.sse.ui.preferences;

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

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.wst.sse.ui.internal.contentassist.CompletionProposalCategory;

import com.ibm.icu.util.StringTokenizer;

/**
 * <p>Implements a completion proposal categories configuration
 * reader and writer using an {@link IPreferenceStore}</p>
 * 
 * <p>This class is meant to be subclasses by implementers of the
 * <code>org.eclipse.wst.sse.ui.completionProposalCategoriesConfiguration</code>
 * extension point as a convince rather then implementing
 * {@link ICompletionProposalCategoriesConfigurationReader} and
 * {@link ICompletionProposalCategoriesConfigurationWriter} from scratch.</p>
 */
public abstract class AbstractCompletionProposalCategoriesConfiguration
		implements ICompletionProposalCategoriesConfigurationReader,
		ICompletionProposalCategoriesConfigurationWriter {

	/** Separator used between preferences stored in the same key */
	private static final String PREFERENCE_CATEGORY_SEPERATOR = "\0"; //$NON-NLS-1$
	
	/**
	 * <p>{@link CompletionProposalCategory} IDs sorted by the order they should
	 * be listed on the default page if they are being displayed there</p>
	 * 
	 * <code>{@link List}<{@link String}></code>
	 * <ul><li><b>values:</b> {@link CompletionProposalCategory} IDs</li></ul>
	 */
	private List fDefaultPageSortOrder;
	
	/**
	 * <p>{@link CompletionProposalCategory} IDs sorted by the order they should
	 * be cycled through</p>
	 * 
	 * <code>{@link List}<{@link String}></code>
	 * <ul><li><b>values:</b> {@link CompletionProposalCategory} IDs</li></ul>
	 */
	private List fOwnPageSortOrder;
	
	/**
	 * <p>{@link CompletionProposalCategory} IDs that should not be displayed on
	 * their own page</p>
	 * 
	 * <code>{@link Set}<{@link String}></code>
	 * <ul><li><b>values:</b> {@link CompletionProposalCategory} IDs</li></ul>
	 */
	private Set fShouldNotDisplayOnOwnPage;
	
	/**
	 * <p>{@link CompletionProposalCategory} IDs that should not be displayed on
	 * on the default page</p>
	 * 
	 * <code>{@link Set}<{@link String}></code>
	 * <ul><li><b>values:</b> {@link CompletionProposalCategory} IDs</li></ul>
	 */
	private Set fShouldNotDisplayOnDefaultPage;
	
	/**
	 * <p>Create a new configuration by loading from the associated {@link IPreferenceStore}</p>
	 */
	public AbstractCompletionProposalCategoriesConfiguration() {
		this.fOwnPageSortOrder = new ArrayList();
		this.fDefaultPageSortOrder = new ArrayList();
		this.fShouldNotDisplayOnOwnPage = new HashSet();
		this.fShouldNotDisplayOnDefaultPage = new HashSet();
		
		this.loadUserConfiguration();
	}
	
	/**
	 * @return {@link IPreferenceStore} to read and write the configuration to and from
	 */
	protected abstract IPreferenceStore getPreferenceStore();
	
	/**
	 * @return Preference key to use in the {@link IPreferenceStore} for the category sort order
	 * when cycling through the pages
	 */
	protected abstract String getPageSortOrderPrefKey();
	
	/**
	 * @return Preference key to use in the {@link IPreferenceStore} for the category sort order
	 * on the default page
	 */
	protected abstract String getDefaultPageSortOrderPrefKey();
	
	/**
	 * @return Preference key to use in the {@link IPreferenceStore} for which
	 * categories not to display on their own page
	 */
	protected abstract String getShouldNotDisplayOnOwnPagePrefKey();
	
	/**
	 * @return Preference key to use in the {@link IPreferenceStore} for which
	 * categories not to display on the default page
	 */
	protected abstract String getShouldNotDisplayOnDefaultPagePrefKey();

	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationReader#getPageSortOrder(java.lang.String)
	 */
	public int getPageSortOrder(String categoryID) {
		int sortOrder = this.fOwnPageSortOrder.indexOf(categoryID);
		if(sortOrder == -1) {
			sortOrder = DEFAULT_SORT_ORDER;
		}
		
		return sortOrder;
	}
	
	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationReader#getDefaultPageSortOrder(java.lang.String)
	 */
	public int getDefaultPageSortOrder(String categoryID) {
		int sortOrder = this.fDefaultPageSortOrder.indexOf(categoryID);
		if(sortOrder == -1) {
			sortOrder = DEFAULT_SORT_ORDER;
		}
		
		return sortOrder;
	}

	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationReader#shouldDisplayOnDefaultPage(java.lang.String)
	 */
	public boolean shouldDisplayOnDefaultPage(String categoryID) {
		return !fShouldNotDisplayOnDefaultPage.contains(categoryID);
	}

	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationReader#shouldDisplayOnOwnPage(java.lang.String)
	 */
	public boolean shouldDisplayOnOwnPage(String categoryID) {
		return !this.fShouldNotDisplayOnOwnPage.contains(categoryID);
	}

	/**
	 * <p> Loads defaults from the associated {@link IPreferenceStore}</p>
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationWriter#loadDefaults()
	 */
	public void loadDefaults() {
		this.loadDefaultPagePreference(true);
		this.loadPageSortOrder(true);
		this.loadDefaultPageSortOrder(true);
		this.loadShouldNotDisplayOnOwnPage(true);
	}

	/**
	 * <p>Saves to the associated {@link IPreferenceStore}</p>
	 * 
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationWriter#saveConfiguration()
	 */
	public boolean saveConfiguration() {
		this.saveShouldDisplayOnDefaultPageConfiguration();
		this.saveShouldDisplayOnOwnPageConfiguration();
		this.saveDefaultPageSortOrderConfiguration();
		this.savePageSortOrderConfiguration();
		
		return true;
	}

	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationWriter#setShouldDisplayOnDefaultPage(java.lang.String, boolean)
	 */
	public void setShouldDisplayOnDefaultPage(String categoryID,
			boolean shouldDisplay) {
		
		if(shouldDisplay) {
			this.fShouldNotDisplayOnDefaultPage.remove(categoryID);
		} else {
			this.fShouldNotDisplayOnDefaultPage.add(categoryID);
		}
	}

	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationWriter#setShouldDisplayOnOwnPage(java.lang.String, boolean)
	 */
	public void setShouldDisplayOnOwnPage(String categoryID, boolean shouldDisplay) {
		
		if(shouldDisplay) {
			this.fShouldNotDisplayOnOwnPage.remove(categoryID);
		} else {
			this.fShouldNotDisplayOnOwnPage.add(categoryID);
		}
	}
	
	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationWriter#setPageOrder(java.util.List)
	 */
	public void setPageOrder(List order) {
		this.fOwnPageSortOrder = order;
	}
	
	/**
	 * @see org.eclipse.wst.sse.ui.preferences.ICompletionProposalCategoriesConfigurationWriter#setDefaultPageOrder(java.util.List)
	 */
	public void setDefaultPageOrder(List order) {
		this.fDefaultPageSortOrder = order;
	}

	/**
	 * <p>Loads the user configuration from the associated {@link IPreferenceStore}</p>
	 */
	private void loadUserConfiguration() {
		this.loadDefaultPagePreference(false);
		this.loadPageSortOrder(false);
		this.loadDefaultPageSortOrder(false);
		this.loadShouldNotDisplayOnOwnPage(false);
	}
	
	/**
	 * <p>Loads the user preferences for which categories to
	 * display on the default content assist page</p>
	 * 
	 * @param useDefaults if <code>true</code> then use the {@link IPreferenceStore} defaults,
	 * otherwise use the user specified preferences
	 */
	private void loadDefaultPagePreference(boolean useDefaults) {
		//clear the current display on default page configuration
		this.fShouldNotDisplayOnDefaultPage.clear();
		
		//parse either the default or user configuration preference
		String displayOnDefaultPage;
		if(useDefaults) {
			displayOnDefaultPage = getPreferenceStore().getDefaultString(getShouldNotDisplayOnDefaultPagePrefKey());
		} else {
			displayOnDefaultPage = getPreferenceStore().getString(getShouldNotDisplayOnDefaultPagePrefKey());
		}
		StringTokenizer defaultPageTokenizer = new StringTokenizer(displayOnDefaultPage, PREFERENCE_CATEGORY_SEPERATOR);
		while (defaultPageTokenizer.hasMoreTokens()) {
			fShouldNotDisplayOnDefaultPage.add(defaultPageTokenizer.nextToken());
		}
	}
	
	/**
	 * <p>Loads the user preferences for the sort order of the content assist pages</p>
	 * 
	 * @param useDefaults if <code>true</code> then use the {@link IPreferenceStore} defaults,
	 * otherwise use the user specified preferences
	 */
	private void loadPageSortOrder(boolean useDefaults){
		//clear the current sort order
		this.fOwnPageSortOrder.clear();
		
		//parse either the default or user configuration preference
		String sortOrder;
		if(useDefaults) {
			sortOrder = getPreferenceStore().getDefaultString(getPageSortOrderPrefKey());
		} else {
			sortOrder = getPreferenceStore().getString(getPageSortOrderPrefKey());
		}
		StringTokenizer tokenizer = new StringTokenizer(sortOrder, PREFERENCE_CATEGORY_SEPERATOR);
		while (tokenizer.hasMoreTokens()) {
			String categoryID = tokenizer.nextToken();
			this.fOwnPageSortOrder.add(categoryID);
		}
	}
	
	/**
	 * <p>Loads the user preferences for the sort order of the content assist pages</p>
	 * 
	 * @param useDefaults if <code>true</code> then use the {@link IPreferenceStore} defaults,
	 * otherwise use the user specified preferences
	 */
	private void loadDefaultPageSortOrder(boolean useDefaults){
		//clear the current sort order
		this.fDefaultPageSortOrder.clear();
		
		//parse either the default or user configuration preference
		String sortOrder;
		if(useDefaults) {
			sortOrder = getPreferenceStore().getDefaultString(getDefaultPageSortOrderPrefKey());
		} else {
			sortOrder = getPreferenceStore().getString(getDefaultPageSortOrderPrefKey());
		}
		StringTokenizer tokenizer = new StringTokenizer(sortOrder, PREFERENCE_CATEGORY_SEPERATOR);
		while (tokenizer.hasMoreTokens()) {
			String categoryID = tokenizer.nextToken();
			this.fDefaultPageSortOrder.add(categoryID);
		}
	}
	
	/**
	 * <p>Loads the user preferences for which categories should not be displayed on their own
	 * content assist page</p>
	 * 
	 * @param useDefaults if <code>true</code> then use the {@link IPreferenceStore} defaults,
	 * otherwise use the user specified preferences
	 */
	private void loadShouldNotDisplayOnOwnPage(boolean useDefaults){
		//clear the current sort order
		this.fShouldNotDisplayOnOwnPage.clear();
		
		//parse either the default or user configuration preference
		String preference;
		if(useDefaults) {
			preference = getPreferenceStore().getDefaultString(getShouldNotDisplayOnOwnPagePrefKey());
		} else {
			preference = getPreferenceStore().getString(getShouldNotDisplayOnOwnPagePrefKey());
		}
		StringTokenizer tokenizer = new StringTokenizer(preference, PREFERENCE_CATEGORY_SEPERATOR);
		while (tokenizer.hasMoreTokens()) {
			String categoryID = tokenizer.nextToken();
			this.fShouldNotDisplayOnOwnPage.add(categoryID);
		}
	}
	
	/**
	 * <p>Saves the user preferences for which categories to
	 * display on the default content assist page</p>
	 */
	private void saveShouldDisplayOnDefaultPageConfiguration() {
		//create the preference string
		StringBuffer defaultPageBuff = new StringBuffer();
		Iterator defaultPageIter = this.fShouldNotDisplayOnDefaultPage.iterator();
		while(defaultPageIter.hasNext()) {
			String categoryID = (String)defaultPageIter.next();
			defaultPageBuff.append(categoryID + PREFERENCE_CATEGORY_SEPERATOR);
		}
		
		//save the preference
		this.getPreferenceStore().setValue(this.getShouldNotDisplayOnDefaultPagePrefKey(), defaultPageBuff.toString());
	}
	
	/**
	 * <p>Saves the user preferences for the sort order of the content assist pages</p>
	 */
	private void savePageSortOrderConfiguration() {
		//create the preference string
		StringBuffer orderBuff = new StringBuffer();
		for(int i = 0; i < this.fOwnPageSortOrder.size(); ++i) {
			if(this.fOwnPageSortOrder.get(i) != null) {
				orderBuff.append(this.fOwnPageSortOrder.get(i) + PREFERENCE_CATEGORY_SEPERATOR);
			}
		}
		
		//save the preference
		this.getPreferenceStore().setValue(this.getPageSortOrderPrefKey(), orderBuff.toString());
	}
	
	/**
	 * <p>Saves the user preferences for the sort order of the categories on the default page</p>
	 */
	private void saveDefaultPageSortOrderConfiguration() {
		//create the preference string
		StringBuffer orderBuff = new StringBuffer();
		for(int i = 0; i < this.fDefaultPageSortOrder.size(); ++i) {
			if(this.fDefaultPageSortOrder.get(i) != null) {
				orderBuff.append(this.fDefaultPageSortOrder.get(i) + PREFERENCE_CATEGORY_SEPERATOR);
			}
		}
		
		//save the preference
		this.getPreferenceStore().setValue(this.getDefaultPageSortOrderPrefKey(), orderBuff.toString());
	}
	
	/**
	 * <p>Saves the user preferences for which categories should not be displayed on their own
	 * content assist page</p>
	 */
	private void saveShouldDisplayOnOwnPageConfiguration() {
		//create the preference string
		StringBuffer buff = new StringBuffer();
		Iterator iter = this.fShouldNotDisplayOnOwnPage.iterator();
		while(iter.hasNext()) {
			String categoryID = (String)iter.next();
			buff.append(categoryID + PREFERENCE_CATEGORY_SEPERATOR);
		}
		
		//save the preference
		this.getPreferenceStore().setValue(this.getShouldNotDisplayOnOwnPagePrefKey(), buff.toString());
	}
}

Back to the top