blob: d6020180f02ce7d7498edebed219d86c59126f1f [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2016, 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.internal.r.core;
import java.util.ArrayList;
import java.util.List;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.statet.jcommons.collections.ImCollections;
import org.eclipse.statet.jcommons.collections.ImList;
import org.eclipse.statet.ltk.buildpaths.core.BuildpathElement;
import org.eclipse.statet.ltk.buildpaths.core.BuildpathElementType;
import org.eclipse.statet.ltk.buildpaths.core.BuildpathsUtils;
import org.eclipse.statet.ltk.buildpaths.core.IBuildpathAttribute;
import org.eclipse.statet.ltk.buildpaths.core.IBuildpathElement;
import org.eclipse.statet.r.core.RBuildpaths;
public class RBuildpathPrefs {
public static final String STAMP_KEY= "stamp"; //$NON-NLS-1$
private static final String VERSION_KEY= "version"; //$NON-NLS-1$
private static final String PATH_PREF_KEY= "path"; //$NON-NLS-1$
private static final ImList<IPath> DEFAULT_INCLUSION_PATTERNS= ImCollections.newList();
private static final ImList<IPath> DEFAULT_EXCLUSION_PATTERNS= ImCollections.<IPath>newList(
RBuildpaths.PKG_RCHECK_FOLDER_PATH.addTrailingSeparator() );
private final IEclipsePreferences rootNode;
private final IProject project;
public RBuildpathPrefs(final IScopeContext context, final String qualifier,
final IProject project) {
this.rootNode= context.getNode(qualifier);
this.project= project;
}
public ImList<IBuildpathElement> load() {
final List<IBuildpathElement> elements= new ArrayList<>();
{ final String childName= IBuildpathElement.SOURCE + ".0";
final Preferences entryNode= this.rootNode.node(childName);
final BuildpathElementType type= RBuildpaths.R_SOURCE_TYPE;
final IPath path= this.project.getFullPath();
ImList<IPath> inclusionPatterns= BuildpathsUtils.decodePatterns(
entryNode.get(IBuildpathAttribute.FILTER_INCLUSIONS, null) );
if (inclusionPatterns == null) {
inclusionPatterns= DEFAULT_INCLUSION_PATTERNS;
}
ImList<IPath> exclusionPatterns= BuildpathsUtils.decodePatterns(
entryNode.get(IBuildpathAttribute.FILTER_EXCLUSIONS, null) );
if (exclusionPatterns == null) {
exclusionPatterns= DEFAULT_EXCLUSION_PATTERNS;
}
elements.add(new BuildpathElement(type, path,
inclusionPatterns, exclusionPatterns,
null, null, null, null, true, null ));
}
return ImCollections.toList(elements);
}
public void save(final List<IBuildpathElement> elements, final boolean flush) {
try {
for (final IBuildpathElement element : elements) {
if (element.getType() == RBuildpaths.R_SOURCE_TYPE
&& element.getPath().equals(this.project.getFullPath()) ) {
final String childName= IBuildpathElement.SOURCE + ".0";
final Preferences entryNode= this.rootNode.node(childName);
IPath path= element.getPath();
if (path.isAbsolute() && this.project.getFullPath().isPrefixOf(path)) {
path= path.removeFirstSegments(1).makeRelative();
}
else {
continue;
}
entryNode.put(PATH_PREF_KEY, path.toPortableString());
entryNode.put(IBuildpathAttribute.FILTER_INCLUSIONS,
BuildpathsUtils.encodePatterns(element.getInclusionPatterns()) );
entryNode.put(IBuildpathAttribute.FILTER_EXCLUSIONS,
BuildpathsUtils.encodePatterns(element.getExclusionPatterns()) );
this.rootNode.putInt(VERSION_KEY, 1);
this.rootNode.putLong(STAMP_KEY, System.currentTimeMillis());
if (flush) {
this.rootNode.flush();
}
}
}
}
catch (final BackingStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}