blob: 94c3715134d522bb6e08056763b5ec0280f15339 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 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.docmlet.wikitext.core;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import org.eclipse.statet.ecommons.preferences.AbstractPreferencesModelObject;
import org.eclipse.statet.ecommons.preferences.core.Preference;
import org.eclipse.statet.ecommons.preferences.core.Preference.BooleanPref;
import org.eclipse.statet.ecommons.preferences.core.Preference.EnumPref;
import org.eclipse.statet.ecommons.preferences.core.Preference.IntPref;
import org.eclipse.statet.ecommons.preferences.core.PreferenceAccess;
import org.eclipse.statet.ecommons.text.IIndentSettings;
/**
* Settings for style of R code.
*/
public class WikitextCodeStyleSettings extends AbstractPreferencesModelObject
implements IIndentSettings {
public static final String QUALIFIER= WikitextCore.BUNDLE_ID + "/codestyle/Wikitext"; //$NON-NLS-1$
public static final String INDENT_GROUP_ID= "Wikitext/Wikitext.codestyle/indent"; //$NON-NLS-1$
public static final String[] ALL_GROUP_IDS= new String[] { INDENT_GROUP_ID };
public static final IntPref TAB_SIZE_PREF= new IntPref(
QUALIFIER, "Tab.size"); //$NON-NLS-1$
public static final EnumPref<IndentationType> INDENT_DEFAULT_TYPE_PREF= new EnumPref<>(
QUALIFIER, "Indent.type", IndentationType.class, //$NON-NLS-1$
IndentationType.TAB );
public static final IntPref INDENT_SPACES_COUNT_PREF= new IntPref(
QUALIFIER, "Indent.Level.spaces_count"); //$NON-NLS-1$
public static final BooleanPref REPLACE_CONVERSATIVE_PREF= new BooleanPref(
QUALIFIER, "Indent.ReplaceConservativ.enabled"); //$NON-NLS-1$
public static final BooleanPref REPLACE_TABS_WITH_SPACES_PREF= new BooleanPref(
QUALIFIER, "Indent.ReplaceOtherTabs.enabled"); //$NON-NLS-1$
public static final Preference<Integer> WRAP_LINE_WIDTH_PREF= new IntPref(
QUALIFIER, "Wrap.LineWidth.max"); //$NON-NLS-1$
private int tabSize;
private IndentationType indentDefaultType;
private int indentSpacesCount;
private boolean replaceOtherTabsWithSpaces;
private boolean replaceConservative;
private int lineWidth;
/**
* Creates an instance with default settings.
*/
public WikitextCodeStyleSettings(final int mode) {
if (mode >= 1) {
installLock();
}
loadDefaults();
resetDirty();
}
@Override
public String[] getNodeQualifiers() {
return new String[] { QUALIFIER };
}
@Override
public void loadDefaults() {
setTabSize(4);
setIndentDefaultType(IndentationType.SPACES);
setIndentSpacesCount(4);
setReplaceConservative(false);
setReplaceOtherTabsWithSpaces(false);
setLineWidth(80);
}
@Override
public void load(final PreferenceAccess prefs) {
setTabSize(prefs.getPreferenceValue(TAB_SIZE_PREF));
setIndentDefaultType(prefs.getPreferenceValue(INDENT_DEFAULT_TYPE_PREF));
setIndentSpacesCount(prefs.getPreferenceValue(INDENT_SPACES_COUNT_PREF));
setReplaceConservative(prefs.getPreferenceValue(REPLACE_CONVERSATIVE_PREF));
setReplaceOtherTabsWithSpaces(prefs.getPreferenceValue(REPLACE_TABS_WITH_SPACES_PREF));
setLineWidth(prefs.getPreferenceValue(WRAP_LINE_WIDTH_PREF));
}
public void load(final WikitextCodeStyleSettings source) {
final Lock writeLock= getWriteLock();
final Lock sourceLock= source.getReadLock();
try {
sourceLock.lock();
writeLock.lock();
setTabSize(source.tabSize);
setIndentDefaultType(source.indentDefaultType);
setIndentSpacesCount(source.indentSpacesCount);
setReplaceConservative(source.replaceConservative);
setReplaceOtherTabsWithSpaces(source.replaceOtherTabsWithSpaces);
setLineWidth(source.lineWidth);
}
finally {
sourceLock.unlock();
writeLock.unlock();
}
}
@Override
public Map<Preference<?>, Object> deliverToPreferencesMap(final Map<Preference<?>, Object> map) {
map.put(TAB_SIZE_PREF, getTabSize());
map.put(INDENT_DEFAULT_TYPE_PREF, getIndentDefaultType());
map.put(INDENT_SPACES_COUNT_PREF, getIndentSpacesCount());
map.put(REPLACE_CONVERSATIVE_PREF, getReplaceConservative());
map.put(REPLACE_TABS_WITH_SPACES_PREF, getReplaceOtherTabsWithSpaces());
map.put(WRAP_LINE_WIDTH_PREF, getLineWidth());
return map;
}
/*[ Properties ]===============================================================*/
public void setTabSize(final int size) {
final int oldValue= this.tabSize;
this.tabSize= size;
firePropertyChange(TAB_SIZE_PROP, oldValue, size);
}
@Override
public int getTabSize() {
return this.tabSize;
}
public void setIndentDefaultType(final IndentationType type) {
final IndentationType oldValue= this.indentDefaultType;
this.indentDefaultType= type;
firePropertyChange(INDENT_DEFAULT_TYPE_PROP, oldValue, type);
}
@Override
public IndentationType getIndentDefaultType() {
return this.indentDefaultType;
}
public void setIndentSpacesCount(final int count) {
final int oldValue= this.indentSpacesCount;
this.indentSpacesCount= count;
firePropertyChange(INDENT_SPACES_COUNT_PROP, oldValue, count);
}
@Override
public int getIndentSpacesCount() {
return this.indentSpacesCount;
}
public void setReplaceConservative(final boolean enable) {
final boolean oldValue= this.replaceConservative;
this.replaceConservative= enable;
firePropertyChange(REPLACE_CONSERVATIVE_PROP, oldValue, enable);
}
@Override
public boolean getReplaceConservative() {
return this.replaceConservative;
}
public void setReplaceOtherTabsWithSpaces(final boolean enable) {
final boolean oldValue= this.replaceOtherTabsWithSpaces;
this.replaceOtherTabsWithSpaces= enable;
firePropertyChange(REPLACE_TABS_WITH_SPACES_PROP, oldValue, getReplaceOtherTabsWithSpaces());
}
@Override
public boolean getReplaceOtherTabsWithSpaces() {
return this.replaceOtherTabsWithSpaces;
}
public void setLineWidth(final int max) {
final int oldValue= this.lineWidth;
this.lineWidth= max;
firePropertyChange(WRAP_LINE_WIDTH_PROP, oldValue, max);
}
@Override
public int getLineWidth() {
return this.lineWidth;
}
}