Skip to content

Commit

Permalink
small code cleanup/simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Jun 22, 2015
1 parent c22b927 commit abb4b4a
Showing 1 changed file with 70 additions and 97 deletions.
Expand Up @@ -45,26 +45,26 @@ public class Configuration {


private static final String CONFIG_FILE = ".languagetool.cfg"; private static final String CONFIG_FILE = ".languagetool.cfg";


private static final String DISABLED_RULES_CONFIG_KEY = "disabledRules"; private static final String DISABLED_RULES_KEY = "disabledRules";
private static final String ENABLED_RULES_CONFIG_KEY = "enabledRules"; private static final String ENABLED_RULES_KEY = "enabledRules";
private static final String DISABLED_CATEGORIES_CONFIG_KEY = "disabledCategories"; private static final String DISABLED_CATEGORIES_KEY = "disabledCategories";
private static final String LANGUAGE_CONFIG_KEY = "language"; private static final String LANGUAGE_KEY = "language";
private static final String MOTHER_TONGUE_CONFIG_KEY = "motherTongue"; private static final String MOTHER_TONGUE_KEY = "motherTongue";
private static final String NGRAM_DIR_CONFIG_KEY = "ngramDir"; private static final String NGRAM_DIR_KEY = "ngramDir";
private static final String AUTO_DETECT_CONFIG_KEY = "autoDetect"; private static final String AUTO_DETECT_KEY = "autoDetect";
private static final String SERVER_RUN_CONFIG_KEY = "serverMode"; private static final String SERVER_RUN_KEY = "serverMode";
private static final String SERVER_PORT_CONFIG_KEY = "serverPort"; private static final String SERVER_PORT_KEY = "serverPort";
private static final String USE_GUI_CONFIG_KEY = "useGUIConfig"; private static final String USE_GUI_KEY = "useGUIConfig";
private static final String FONT_NAME_CONFIG_KEY = "font.name"; private static final String FONT_NAME_KEY = "font.name";
private static final String FONT_STYLE_CONFIG_KEY = "font.style"; private static final String FONT_STYLE_KEY = "font.style";
private static final String FONT_SIZE_CONFIG_KEY = "font.size"; private static final String FONT_SIZE_KEY = "font.size";
private static final String LF_NAME_CONFIG_KEY = "lookAndFeelName"; private static final String LF_NAME_KEY = "lookAndFeelName";
private static final String ERROR_COLORS_CONFIG_KEY = "errorColors"; private static final String ERROR_COLORS_KEY = "errorColors";


private static final String DELIMITER = ","; private static final String DELIMITER = ",";
private static final String EXTERNAL_RULE_DIRECTORY = "extRulesDirectory"; private static final String EXTERNAL_RULE_DIRECTORY = "extRulesDirectory";


private final Map<String, String> configForOtherLangs = new HashMap<>(); private final Map<String, String> configForOtherLanguages = new HashMap<>();
private final Map<ITSIssueType, Color> errorColors = new HashMap<>(); private final Map<ITSIssueType, Color> errorColors = new HashMap<>();


private File configFile; private File configFile;
Expand All @@ -78,8 +78,8 @@ public class Configuration {
private boolean autoDetect; private boolean autoDetect;
private boolean guiConfig; private boolean guiConfig;
private String fontName; private String fontName;
private int fontStyle; private int fontStyle = FONT_STYLE_INVALID;
private int fontSize; private int fontSize = FONT_SIZE_INVALID;
private int serverPort = DEFAULT_SERVER_PORT; private int serverPort = DEFAULT_SERVER_PORT;
private String externalRuleDirectory; private String externalRuleDirectory;
private String lookAndFeelName; private String lookAndFeelName;
Expand All @@ -92,24 +92,21 @@ public class Configuration {
public Configuration(final Language lang) throws IOException { public Configuration(final Language lang) throws IOException {
this(new File(System.getProperty("user.home")), CONFIG_FILE, lang); this(new File(System.getProperty("user.home")), CONFIG_FILE, lang);
} }


public Configuration(final File baseDir, final Language lang) throws IOException {
this(baseDir, CONFIG_FILE, lang);
}

public Configuration(final File baseDir, final String filename, final Language lang) public Configuration(final File baseDir, final String filename, final Language lang)
throws IOException { throws IOException {
this();
if (!baseDir.isDirectory()) { if (!baseDir.isDirectory()) {
throw new IllegalArgumentException("Not a directory: " + baseDir); throw new IllegalArgumentException("Not a directory: " + baseDir);
} }
configFile = new File(baseDir, filename); configFile = new File(baseDir, filename);
loadConfiguration(lang); loadConfiguration(lang);
} }


public Configuration(final File baseDir, final Language lang) throws IOException {
this(baseDir, CONFIG_FILE, lang);
}

Configuration() { Configuration() {
fontStyle = FONT_STYLE_INVALID;
fontSize = FONT_SIZE_INVALID;
} }


/** /**
Expand Down Expand Up @@ -152,9 +149,9 @@ void restoreState(Configuration configuration) {
this.enabledRuleIds.addAll(configuration.enabledRuleIds); this.enabledRuleIds.addAll(configuration.enabledRuleIds);
this.disabledCategoryNames.clear(); this.disabledCategoryNames.clear();
this.disabledCategoryNames.addAll(configuration.disabledCategoryNames); this.disabledCategoryNames.addAll(configuration.disabledCategoryNames);
this.configForOtherLangs.clear(); this.configForOtherLanguages.clear();
for (String key : configuration.configForOtherLangs.keySet()) { for (String key : configuration.configForOtherLanguages.keySet()) {
this.configForOtherLangs.put(key, configuration.configForOtherLangs.get(key)); this.configForOtherLanguages.put(key, configuration.configForOtherLanguages.get(key));
} }
} }


Expand Down Expand Up @@ -239,103 +236,79 @@ public void setExternalRuleDirectory(final String path) {
} }


/** /**
*
* Returns the name of the GUI's editing textarea font. * Returns the name of the GUI's editing textarea font.
*
* @return the name of the font. * @return the name of the font.
* @since 2.6 * @since 2.6
* * @see Font#getFamily()
* @see java.awt.Font#getFamily()
*/ */
public String getFontName() { public String getFontName() {
return fontName; return fontName;
} }


/** /**
*
* Sets the name of the GUI's editing textarea font. * Sets the name of the GUI's editing textarea font.
*
* @param fontName the name of the font. * @param fontName the name of the font.
* @since 2.6 * @since 2.6
* * @see Font#getFamily()
* @see java.awt.Font#getFamily()
*/ */
public void setFontName(String fontName) { public void setFontName(String fontName) {
this.fontName = fontName; this.fontName = fontName;
} }


/** /**
*
* Returns the style of the GUI's editing textarea font. * Returns the style of the GUI's editing textarea font.
*
* @return the style of the font. * @return the style of the font.
* @since 2.6 * @since 2.6
* * @see Font#getStyle()
* @see java.awt.Font#getStyle()
*/ */
public int getFontStyle() { public int getFontStyle() {
return fontStyle; return fontStyle;
} }


/** /**
*
* Sets the style of the GUI's editing textarea font. * Sets the style of the GUI's editing textarea font.
*
* @param fontStyle the style of the font. * @param fontStyle the style of the font.
* @since 2.6 * @since 2.6
* * @see Font#getStyle()
* @see java.awt.Font#getStyle()
*/ */
public void setFontStyle(int fontStyle) { public void setFontStyle(int fontStyle) {
this.fontStyle = fontStyle; this.fontStyle = fontStyle;
} }


/** /**
*
* Returns the size of the GUI's editing textarea font. * Returns the size of the GUI's editing textarea font.
*
* @return the size of the font. * @return the size of the font.
* @since 2.6 * @since 2.6
* * @see Font#getSize()
* @see java.awt.Font#getSize()
*/ */
public int getFontSize() { public int getFontSize() {
return fontSize; return fontSize;
} }


/** /**
*
* Sets the size of the GUI's editing textarea font. * Sets the size of the GUI's editing textarea font.
*
* @param fontSize the size of the font. * @param fontSize the size of the font.
* @since 2.6 * @since 2.6
* * @see Font#getSize()
* @see java.awt.Font#getSize()
*/ */
public void setFontSize(int fontSize) { public void setFontSize(int fontSize) {
this.fontSize = fontSize; this.fontSize = fontSize;
} }


/** /**
*
* Returns the name of the GUI's LaF. * Returns the name of the GUI's LaF.
*
* @return the name of the LaF. * @return the name of the LaF.
* @since 2.6 * @since 2.6
*
* @see javax.swing.UIManager.LookAndFeelInfo#getName() * @see javax.swing.UIManager.LookAndFeelInfo#getName()
*/ */
public String getLookAndFeelName() { public String getLookAndFeelName() {
return this.lookAndFeelName; return this.lookAndFeelName;
} }


/** /**
*
* Sets the name of the GUI's LaF. * Sets the name of the GUI's LaF.
*
* @param lookAndFeelName the name of the LaF. * @param lookAndFeelName the name of the LaF.
* @since 2.6 @see * @since 2.6 @see
*
* @see javax.swing.UIManager.LookAndFeelInfo#getName() * @see javax.swing.UIManager.LookAndFeelInfo#getName()
*/ */
public void setLookAndFeelName(String lookAndFeelName) { public void setLookAndFeelName(String lookAndFeelName) {
Expand Down Expand Up @@ -375,45 +348,45 @@ private void loadConfiguration(final Language lang) throws IOException {
final Properties props = new Properties(); final Properties props = new Properties();
props.load(fis); props.load(fis);


disabledRuleIds.addAll(getListFromProperties(props, DISABLED_RULES_CONFIG_KEY + qualifier)); disabledRuleIds.addAll(getListFromProperties(props, DISABLED_RULES_KEY + qualifier));
enabledRuleIds.addAll(getListFromProperties(props, ENABLED_RULES_CONFIG_KEY + qualifier)); enabledRuleIds.addAll(getListFromProperties(props, ENABLED_RULES_KEY + qualifier));
disabledCategoryNames.addAll(getListFromProperties(props, DISABLED_CATEGORIES_CONFIG_KEY + qualifier)); disabledCategoryNames.addAll(getListFromProperties(props, DISABLED_CATEGORIES_KEY + qualifier));


final String languageStr = (String) props.get(LANGUAGE_CONFIG_KEY); final String languageStr = (String) props.get(LANGUAGE_KEY);
if (languageStr != null) { if (languageStr != null) {
language = Languages.getLanguageForShortName(languageStr); language = Languages.getLanguageForShortName(languageStr);
} }
final String motherTongueStr = (String) props.get(MOTHER_TONGUE_CONFIG_KEY); final String motherTongueStr = (String) props.get(MOTHER_TONGUE_KEY);
if (motherTongueStr != null && !motherTongueStr.equals("xx")) { if (motherTongueStr != null && !motherTongueStr.equals("xx")) {
motherTongue = Languages.getLanguageForShortName(motherTongueStr); motherTongue = Languages.getLanguageForShortName(motherTongueStr);
} }
final String ngramDir = (String) props.get(NGRAM_DIR_CONFIG_KEY); final String ngramDir = (String) props.get(NGRAM_DIR_KEY);
if (ngramDir != null) { if (ngramDir != null) {
ngramDirectory = new File(ngramDir); ngramDirectory = new File(ngramDir);
} }


autoDetect = "true".equals(props.get(AUTO_DETECT_CONFIG_KEY)); autoDetect = "true".equals(props.get(AUTO_DETECT_KEY));
guiConfig = "true".equals(props.get(USE_GUI_CONFIG_KEY)); guiConfig = "true".equals(props.get(USE_GUI_KEY));
runServer = "true".equals(props.get(SERVER_RUN_CONFIG_KEY)); runServer = "true".equals(props.get(SERVER_RUN_KEY));


fontName = (String) props.get(FONT_NAME_CONFIG_KEY); fontName = (String) props.get(FONT_NAME_KEY);
if (props.get(FONT_STYLE_CONFIG_KEY) != null) { if (props.get(FONT_STYLE_KEY) != null) {
try { try {
fontStyle = Integer.parseInt((String) props.get(FONT_STYLE_CONFIG_KEY)); fontStyle = Integer.parseInt((String) props.get(FONT_STYLE_KEY));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Ignore // Ignore
} }
} }
if (props.get(FONT_SIZE_CONFIG_KEY) != null) { if (props.get(FONT_SIZE_KEY) != null) {
try { try {
fontSize = Integer.parseInt((String) props.get(FONT_SIZE_CONFIG_KEY)); fontSize = Integer.parseInt((String) props.get(FONT_SIZE_KEY));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Ignore // Ignore
} }
} }
lookAndFeelName = (String) props.get(LF_NAME_CONFIG_KEY); lookAndFeelName = (String) props.get(LF_NAME_KEY);


final String serverPortString = (String) props.get(SERVER_PORT_CONFIG_KEY); final String serverPortString = (String) props.get(SERVER_PORT_KEY);
if (serverPortString != null) { if (serverPortString != null) {
serverPort = Integer.parseInt(serverPortString); serverPort = Integer.parseInt(serverPortString);
} }
Expand All @@ -422,7 +395,7 @@ private void loadConfiguration(final Language lang) throws IOException {
externalRuleDirectory = extRules; externalRuleDirectory = extRules;
} }


String colorsString = (String) props.get(ERROR_COLORS_CONFIG_KEY); String colorsString = (String) props.get(ERROR_COLORS_KEY);
parseErrorColors(colorsString); parseErrorColors(colorsString);


//store config for other languages //store config for other languages
Expand Down Expand Up @@ -460,16 +433,16 @@ private void loadConfigForOtherLanguages(final Language lang, final Properties p
for (Language otherLang : Languages.get()) { for (Language otherLang : Languages.get()) {
if (!otherLang.equals(lang)) { if (!otherLang.equals(lang)) {
final String languageSuffix = "." + otherLang.getShortNameWithCountryAndVariant(); final String languageSuffix = "." + otherLang.getShortNameWithCountryAndVariant();
storeConfigKeyFromProp(prop, DISABLED_RULES_CONFIG_KEY + languageSuffix); storeConfigKeyFromProp(prop, DISABLED_RULES_KEY + languageSuffix);
storeConfigKeyFromProp(prop, ENABLED_RULES_CONFIG_KEY + languageSuffix); storeConfigKeyFromProp(prop, ENABLED_RULES_KEY + languageSuffix);
storeConfigKeyFromProp(prop, DISABLED_CATEGORIES_CONFIG_KEY + languageSuffix); storeConfigKeyFromProp(prop, DISABLED_CATEGORIES_KEY + languageSuffix);
} }
} }
} }


private void storeConfigKeyFromProp(final Properties prop, final String key) { private void storeConfigKeyFromProp(final Properties prop, final String key) {
if (prop.containsKey(key)) { if (prop.containsKey(key)) {
configForOtherLangs.put(key, prop.getProperty(key)); configForOtherLanguages.put(key, prop.getProperty(key));
} }
} }


Expand All @@ -487,33 +460,33 @@ public void saveConfiguration(final Language lang) throws IOException {
final Properties props = new Properties(); final Properties props = new Properties();
final String qualifier = getQualifier(lang); final String qualifier = getQualifier(lang);


addListToProperties(props, DISABLED_RULES_CONFIG_KEY + qualifier, disabledRuleIds); addListToProperties(props, DISABLED_RULES_KEY + qualifier, disabledRuleIds);
addListToProperties(props, ENABLED_RULES_CONFIG_KEY + qualifier, enabledRuleIds); addListToProperties(props, ENABLED_RULES_KEY + qualifier, enabledRuleIds);
addListToProperties(props, DISABLED_CATEGORIES_CONFIG_KEY + qualifier, disabledCategoryNames); addListToProperties(props, DISABLED_CATEGORIES_KEY + qualifier, disabledCategoryNames);
if (language != null && !language.isExternal()) { // external languages won't be known at startup, so don't save them if (language != null && !language.isExternal()) { // external languages won't be known at startup, so don't save them
props.setProperty(LANGUAGE_CONFIG_KEY, language.getShortNameWithCountryAndVariant()); props.setProperty(LANGUAGE_KEY, language.getShortNameWithCountryAndVariant());
} }
if (motherTongue != null) { if (motherTongue != null) {
props.setProperty(MOTHER_TONGUE_CONFIG_KEY, motherTongue.getShortName()); props.setProperty(MOTHER_TONGUE_KEY, motherTongue.getShortName());
} }
if (ngramDirectory != null) { if (ngramDirectory != null) {
props.setProperty(NGRAM_DIR_CONFIG_KEY, ngramDirectory.getAbsolutePath()); props.setProperty(NGRAM_DIR_KEY, ngramDirectory.getAbsolutePath());
} }
props.setProperty(AUTO_DETECT_CONFIG_KEY, Boolean.toString(autoDetect)); props.setProperty(AUTO_DETECT_KEY, Boolean.toString(autoDetect));
props.setProperty(USE_GUI_CONFIG_KEY, Boolean.toString(guiConfig)); props.setProperty(USE_GUI_KEY, Boolean.toString(guiConfig));
props.setProperty(SERVER_RUN_CONFIG_KEY, Boolean.toString(runServer)); props.setProperty(SERVER_RUN_KEY, Boolean.toString(runServer));
props.setProperty(SERVER_PORT_CONFIG_KEY, Integer.toString(serverPort)); props.setProperty(SERVER_PORT_KEY, Integer.toString(serverPort));
if (fontName != null) { if (fontName != null) {
props.setProperty(FONT_NAME_CONFIG_KEY, fontName); props.setProperty(FONT_NAME_KEY, fontName);
} }
if (fontStyle != FONT_STYLE_INVALID) { if (fontStyle != FONT_STYLE_INVALID) {
props.setProperty(FONT_STYLE_CONFIG_KEY, Integer.toString(fontStyle)); props.setProperty(FONT_STYLE_KEY, Integer.toString(fontStyle));
} }
if (fontSize != FONT_SIZE_INVALID) { if (fontSize != FONT_SIZE_INVALID) {
props.setProperty(FONT_SIZE_CONFIG_KEY, Integer.toString(fontSize)); props.setProperty(FONT_SIZE_KEY, Integer.toString(fontSize));
} }
if (this.lookAndFeelName != null) { if (this.lookAndFeelName != null) {
props.setProperty(LF_NAME_CONFIG_KEY, lookAndFeelName); props.setProperty(LF_NAME_KEY, lookAndFeelName);
} }
if (externalRuleDirectory != null) { if (externalRuleDirectory != null) {
props.setProperty(EXTERNAL_RULE_DIRECTORY, externalRuleDirectory); props.setProperty(EXTERNAL_RULE_DIRECTORY, externalRuleDirectory);
Expand All @@ -524,10 +497,10 @@ public void saveConfiguration(final Language lang) throws IOException {
rgb = rgb.substring(2, rgb.length()); rgb = rgb.substring(2, rgb.length());
sb.append(entry.getKey()).append(":").append("#").append(rgb).append(", "); sb.append(entry.getKey()).append(":").append("#").append(rgb).append(", ");
} }
props.setProperty(ERROR_COLORS_CONFIG_KEY, sb.toString()); props.setProperty(ERROR_COLORS_KEY, sb.toString());


for (final String key : configForOtherLangs.keySet()) { for (final String key : configForOtherLanguages.keySet()) {
props.setProperty(key, configForOtherLangs.get(key)); props.setProperty(key, configForOtherLanguages.get(key));
} }


try (FileOutputStream fos = new FileOutputStream(configFile)) { try (FileOutputStream fos = new FileOutputStream(configFile)) {
Expand Down

0 comments on commit abb4b4a

Please sign in to comment.