package de.kwirz.yapne.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;



public class Settings {

	private static final String FILE_NAME = System.getProperty("user.home") + "/.yapne.conf";
	private Properties properties = new Properties();
	
	static { // erstellt Einstellungsdatei falls die noch nicht exisitiert
		final File file = new File(FILE_NAME);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				System.err.println("couldn't create file: " + FILE_NAME);
				e.printStackTrace();
			}
		}
	}
	
	public Settings() {
		try {
			properties.load(new FileInputStream(FILE_NAME));
		} catch (IOException e) {
			System.err.println("couldn't read settings from: " + FILE_NAME);
			e.printStackTrace();
		}
	}
	
	public void save() throws IOException {
		try {
			properties.store(new FileOutputStream(FILE_NAME), "YAPNE Settings");
		} catch (IOException e) {
			System.err.println("couldn't write settings file");
			throw e;
		}
	}
	
	public Set<String> getAllKeys() {
		return properties.stringPropertyNames();
	}
	
	public boolean hasKey(String key) {
		return getAllKeys().contains(key);
	}
	
	public void setValue(String key, String value) throws Exception {
		properties.setProperty(key, value);
		save();
	}
	
	public String getValue(String key) throws IllegalArgumentException {
		String value = properties.getProperty(key);
		if (value == null)
			throw new IllegalArgumentException("no such key '" + key + "'");
		
		return value; 
	}
	
	public String getValue(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}
		
	public String toString() {
		return String.format("Settings { file: '%s', keys: %d }", FILE_NAME, getAllKeys().size());
	}
	
	public static void main(String[] args) throws Exception {
		Settings settings = new Settings();
		
		for (String key : settings.getAllKeys()) {
			System.out.println(key + " = " + settings.getValue(key));
		}
	}
}

@@ -193,34 +193,225 @@
 et;%0A
-%0A%0A%0Apublic class Settings %7B
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A%0A/**%0A * Persistente Einstellungen.%0A *%0A */%0Apublic class Settings %7B%0A%09%0A%09private static final Logger logger = Logger.getLogger(Settings.class.getName());%0A%09
 %0A%0A%09p
@@ -757,35 +757,41 @@
 ) %7B%0A%09%09%09%09
-System.err.println(
+logger.log(Level.SEVERE, 
 %22couldn'
@@ -822,33 +822,11 @@
 NAME
-);%0A%09%09%09%09e.printStackTrace(
+, e
 );%0A%09
@@ -950,35 +950,41 @@
 e) %7B%0A%09%09%09
-System.err.println(
+logger.log(Level.SEVERE, 
 %22couldn'
@@ -1022,32 +1022,11 @@
 NAME
-);%0A%09%09%09e.printStackTrace(
+, e
 );%0A%09
@@ -1154,16 +1154,72 @@
 ings%22);%0A
+%09%09%09logger.info(%22save settings to '%22 + FILE_NAME + %22'%22);%0A
 %09%09%7D catc
@@ -1245,27 +1245,33 @@
 %0A%09%09%09
-System.err.println(
+logger.log(Level.SEVERE, 
 %22cou
@@ -1295,17 +1295,34 @@
 ngs file
-%22
+: %22 + FILE_NAME, e
 );%0A%09%09%09th
@@ -1502,24 +1502,28 @@
 %09%7D%0A%09%0A%09public
+ %3CT%3E
  void setVal
@@ -1537,22 +1537,17 @@
 ng key, 
-String
+T
  value) 
@@ -1565,16 +1565,163 @@
 ption %7B%0A
+%09%09String valueAsString = %22%22;%0A%09%09if (value instanceof Object)%0A%09%09%09valueAsString = value.toString();%0A%09%09else%0A%09%09%09valueAsString = String.valueOf(value);%0A%0A
 %09%09proper
@@ -1743,24 +1743,32 @@
 y(key, value
+AsString
 );%0A%09%09save();
@@ -1764,24 +1764,94 @@
 ;%0A%09%09save();%0A
+%09%09logger.info(String.format(%22set '%25s' to '%25s'%22, key, valueAsString));%0A
 %09%7D%0A%09%0A%09public
@@ -2320,219 +2320,6 @@
 %7D%0A%09%0A
-%09public static void main(String%5B%5D args) throws Exception %7B%0A%09%09Settings settings = new Settings();%0A%09%09%0A%09%09for (String key : settings.getAllKeys()) %7B%0A%09%09%09System.out.println(key + %22 = %22 + settings.getValue(key));%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

make setValue generic