import java.lang.*;
import java.util.*;
import java.io.*;

class Configuration {

private static Configuration instance_ = null;
private static String FILE = "config.ini";

private Properties properties_;
public Properties properties() { return properties_; }
public void properties (Properties theProp) { properties_ = theProp; }

private Configuration() {

this.initialize();

}

public synchronized static Configuration instance() {

if (instance_ == null)

instance_ = new Configuration();

return instance_;

}

private void initialize() {

this.properties (new Properties());
this.load();
this.display();

}

public String valueOf(String aKey) {

String answer = this.properties().getProperty(aKey);
if (answer == null) {

System.out.println ("Config data " + aKey + " not found");

}
return answer;

}

public void load() {

try {

DataInputStream theStreamIn = new DataInputStream (
new FileInputStream (Configuration.FILE));
this.properties().load(theStreamIn);

}
catch (IOException e) {

System.out.println("INI FileIn Error, reloading defaults");
this.writeDefaults();

}

}

public void writeDefaults() {

Properties theSettings = this.properties();
theSettings.put("HEIGHT", "128");
theSettings.put("WIDTH", "128");
theSettings.put("CANVASHEIGHT", "600");
theSettings.put("CANVASWIDTH", "600");
theSettings.put("DEBUG", "on");

try {

PrintStream theStreamOut = new PrintStream (
new FileOutputStream (Configuration.FILE));
theSettings.save(theStreamOut, "GridTest Environment Settings");

}
catch (IOException e) { System.out.println("INI FileOut Error"); }

}

public void display() {

Enumeration theOC = this.properties().propertyNames();
while (theOC.hasMoreElements()) {

String key = (String) theOC.nextElement();
System.out.println (key + " value is " +
this.properties().getProperty(key));

}

}


}