package net.sf.cannagrower;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import javax.swing.UIManager.LookAndFeelInfo;
import net.sf.cannagrower.i18n.Messages;
public class CannaGrowerProperties extends Properties {
private static final long serialVersionUID = 1L;
private static final String propertyShowDemo="demo"; // Store demo 'show at startup' flag (0/1)
private static final String propertyShowSettings="settings"; // Store settings 'show at startup' flag (0/1)
private static final String propertyLocale="locale"; // Store language name ('fr','en',...)
private static final String propertyLaf="laf"; // Store laf class name
private static CannaGrowerProperties properties=new CannaGrowerProperties();
private File file;
public static CannaGrowerProperties getProperties(){return properties;}
public synchronized boolean getShowDemo(){
String buffer;
boolean show=true;
load();
buffer=getProperty(propertyShowDemo,"1");
if(buffer.equals("0")){show=false;}
return show;
}
public synchronized void setShowDemo(boolean show){
String buffer="0";
if(show)buffer="1";
setProperty(propertyShowDemo,buffer);
}
public synchronized boolean getShowSettings(){
String buffer;
boolean show=true;
load();
buffer=getProperty(propertyShowSettings,"1");
if(buffer.equals("0")){show=false;}
return show;
}
public synchronized void setShowSettings(boolean show){
String buffer="0";
if(show)buffer="1";
setProperty(propertyShowSettings,buffer);
}
public synchronized LookAndFeelInfo getLaf(){
String buffer;
LookAndFeelInfo lafInfo=null;
load();
buffer=getProperty(propertyLaf,"");
if(!buffer.equals("")){
try{
lafInfo=new LookAndFeelInfo(Class.forName(buffer).getSimpleName(),buffer);
}catch(Exception e){
lafInfo=null;
}
}
return lafInfo;
}
public synchronized void setLaf(LookAndFeelInfo lafInfo){
setProperty(propertyLaf,lafInfo.getClassName());
}
public synchronized Locale getLocale(){
String buffer;
Locale locale=null;
load();
buffer=getProperty(propertyLocale,"");
if(!buffer.equals("")){
locale=new Locale(buffer);
}
return locale;
}
public synchronized void setLocale(Locale locale){
setProperty(propertyLocale,locale.getLanguage());
}
public Object setProperty(String key,String value){
load();
Object result=super.setProperty(key,value);
save();
return result;
}
private CannaGrowerProperties(){
String folder=CannaGrower.getCannaGrowerFolder().getPath();
if(!folder.endsWith(System.getProperty("file.separator"))){folder+=System.getProperty("file.separator");}
file=new File(folder+CannaGrower.class.getSimpleName()+".properties");
try{
if(!file.exists()){file.createNewFile();}
}catch(IOException e){
Messages.showException(e);
}
}
private void load(){
if(file==null || !file.exists()){return;}
try{
FileInputStream inStream=new FileInputStream(file);
this.load(inStream);
}catch(IOException e){
Messages.showException(e);
}
}
private void save(){
if(file==null){return;}
try{
FileOutputStream outStream=new FileOutputStream(file);
this.store(outStream, CannaGrower.class.getSimpleName());
}catch(IOException e){
Messages.showException(e);
}
}
}