blob: e70d372c2ff7a1f3b08ef89e19f0f4de2316473c [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2006, 2019 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.base.core.preferences;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.statet.jcommons.collections.ImCollections;
import org.eclipse.statet.jcommons.collections.ImList;
import org.eclipse.statet.ecommons.preferences.core.Preference;
import org.eclipse.statet.ecommons.preferences.core.Preference.EnumListPref;
import org.eclipse.statet.ecommons.preferences.core.Preference.StringArrayPref;
import org.eclipse.statet.ecommons.preferences.core.PreferenceAccess;
import org.eclipse.statet.ltk.issues.core.ITaskTag;
import org.eclipse.statet.ltk.issues.core.TaskPriority;
import org.eclipse.statet.ltk.issues.core.impl.TaskTag;
public class TaskTagsPreferences {
public static final String GROUP_ID= "statet.task_tags"; //$NON-NLS-1$
private static final String KEY_TAGS= "TaskTags.keyword"; //$NON-NLS-1$
private static final String KEY_PRIORITIES= "TaskTags.priority"; //$NON-NLS-1$
public static final StringArrayPref PREF_TAGS= new StringArrayPref(
StatetCorePreferenceNodes.CAT_MANAGMENT_QUALIFIER, KEY_TAGS);
public static final EnumListPref<TaskPriority> PREF_PRIORITIES= new EnumListPref<>(
StatetCorePreferenceNodes.CAT_MANAGMENT_QUALIFIER, KEY_PRIORITIES, TaskPriority.class);
public static String[] loadTagsOnly(final PreferenceAccess prefs) {
return prefs.getPreferenceValue(PREF_TAGS);
}
private final ImList<ITaskTag> taskTags;
/**
* Creates preferences with the specified values.
*
* @param taskTags
*/
public TaskTagsPreferences(final Collection<? extends ITaskTag> taskTags) {
this.taskTags= ImCollections.toList(taskTags);
}
/**
* Creates preferences with default values.
*/
public TaskTagsPreferences() {
this(ImCollections.newList(
new TaskTag("TODO", TaskPriority.NORMAL), //$NON-NLS-1$
new TaskTag("FIXME", TaskPriority.NORMAL) )); //$NON-NLS-1$
}
/**
* Creates preferences with values loaded from store.
*
* @param prefs
*/
public TaskTagsPreferences(final PreferenceAccess prefs) {
final String[] keywords= prefs.getPreferenceValue(PREF_TAGS);
final List<TaskPriority> priorities= prefs.getPreferenceValue(PREF_PRIORITIES);
if (keywords.length == priorities.size()) {
final ITaskTag[] array= new ITaskTag[keywords.length];
for (int i= 0; i < array.length; i++) {
array[i]= new TaskTag(keywords[i], priorities.get(i));
}
this.taskTags= ImCollections.newList(array);
}
else {
this.taskTags= ImCollections.emptyList();
}
}
public String[] getTags() {
final String[] array= new String[this.taskTags.size()];
for (int i= 0; i < array.length; i++) {
array[i]= this.taskTags.get(i).getKeyword();
}
return array;
}
public TaskPriority[] getPriorities() {
final TaskPriority[] array= new TaskPriority[this.taskTags.size()];
for (int i= 0; i < array.length; i++) {
array[i]= this.taskTags.get(i).getPriority();
}
return array;
}
public List<ITaskTag> getTaskTags() {
return this.taskTags;
}
/**
* Allows to save the preferences.
*
* <p>Note: Intended to usage in preference/property page only.</p>
*/
public Map<Preference<?>, Object> addPreferencesToMap(final Map<Preference<?>, Object> map) {
map.put(PREF_TAGS, getTags());
map.put(PREF_PRIORITIES, ImCollections.newList(getPriorities()));
return map;
}
/**
* Allows to save the preferences.
*
* <p>Note: Intended to usage in preference/property page only.</p>
*/
public Map<Preference<?>, Object> getPreferencesMap() {
return addPreferencesToMap(new HashMap<Preference<?>, Object>(2));
}
}