blob: 0f57e31d4af752ff61828bb9b3f9057592a3a081 [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2007 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 implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.common.utils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.preference.IPreferenceStore;
import com.ibm.icu.util.StringTokenizer;
/**
* Utility class for accessing preferences.
*
* @author Kelvin Low
* @since 1.1
*/
public class PreferenceUtil {
// The multi-string preference delimiter.
private static final String PREFERENCE_DELIMITER = ";"; //$NON-NLS-1$
private static final Object[] EMPTY_LIST = new Object[0];
/**
* Gets the string values associated with a named preference.
*
* @param prefStore
* a preference store
* @param name
* the preference name
* @return a list of string preference values
*/
public static List getList(IPreferenceStore prefStore, String name) {
List values = new ArrayList();
if (prefStore != null && name != null) {
String value = prefStore.getString(name);
StringTokenizer tokenizer = new StringTokenizer(value,
PREFERENCE_DELIMITER);
int tokenCount = tokenizer.countTokens();
for (int i = 0; i < tokenCount; i++) {
values.add(tokenizer.nextToken());
}
}
return values;
}
/**
* Saves a list of string values associated with a named preference.
*
* @param prefStore
* a preference store
* @param name
* the preference name
* @return a list of string preference values
*/
public static void setList(IPreferenceStore prefStore, String name,
List values) {
if (prefStore != null && name != null && values != null) {
StringBuffer buffer = new StringBuffer();
for (Iterator it = values.iterator(); it.hasNext();) {
String value = (String) it.next();
buffer.append(value);
buffer.append(PREFERENCE_DELIMITER);
}
prefStore.setValue(name, buffer.toString());
}
}
}