Implemeted basic settings functions.

This commit is contained in:
Servicepoint
2014-09-30 16:09:32 +02:00
parent 3965ee9392
commit 5b6506bae6
2 changed files with 76 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ import de.katho.kBorrow.gui.MainWindow;
public class Main {
public static void main(String[] args){
new Main();
Settings set = new Settings();
}
public Main(){

View File

@@ -1,51 +1,98 @@
package de.katho.kBorrow;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.io.OutputStream;
import java.util.Properties;
public class Settings {
private Properties properties;
private String filePath = System.getProperty("user.home")+"\\kBorrow\\Settings.cfg";
private File cfgFile;
private String filePath;
private String fileName;
public Settings() {
this.properties = new Properties();
this.cfgFile = new File(this.filePath);
this.filePath = System.getProperty("user.home")+"\\kBorrow";
this.fileName = "Settings.cfg";
if(!this.filePathHasValidConfig()){
this.createDefaultConfig();
}
}
private Properties readPropFile(String pFilePath){
Properties prop = new Properties();
File tFile = new File(pFilePath);
/**
*
* @return
*/
private boolean filePathHasValidConfig(){
try {
InputStream in = new FileInputStream(this.filePath+"\\"+this.fileName);
this.properties = new Properties();
InputStream is = Properties.class.getResourceAsStream(pFilePath);
this.properties.load(in);
// Check if the properties file holds certain keys and values.
if( (this.properties.containsKey("dBType") && this.properties.containsValue("sqlite") && this.properties.containsKey("sqlitePath")) ||
(this.properties.contains("dbType") && this.properties.containsValue("mysql") && this.properties.containsKey("mysqlDB") && this.properties.containsKey("mysqlHost") && this.properties.containsKey("mysqlUser") && this.properties.containsKey("mysqlPass"))) {
in.close();
return true;
}
else {
in.close();
return false;
}
} catch (FileNotFoundException e) {
return false;
} catch (IOException e){
e.printStackTrace();
return false;
}
}
/**
* Writes a default config to the config file.
*
* @param pFilePath
* @throws IOException
*/
private void createDefaultPropFile(String pFilePath) throws IOException {
Properties prop = new Properties();
File tFile = new File(pFilePath);
private void createDefaultConfig() {
try {
File dir = new File(this.filePath);
File file = new File(this.filePath+"\\"+this.fileName);
if(!dir.isDirectory()) dir.mkdir();
if(!file.isFile()) file.createNewFile();
else {
file.delete();
file.createNewFile();
}
if(!tFile.isFile() && !tFile.isDirectory()){
tFile.createNewFile();
OutputStream os = new FileOutputStream(this.filePath+"\\"+this.fileName);
this.properties.put("dBType", "sqlite");
this.properties.put("sqlitePath", System.getProperty("user.home")+"\\kBorrow\\kBorrow.db" );
this.properties.store(os, null);
os.close();
} catch (FileNotFoundException e) {
System.out.println("I couldn't find the specified properties file while trying to create a default config.");
e.printStackTrace();
} catch (IOException e) {
System.out.println(this.filePath);
System.out.println("I had problems, writing to the properties file while trying to create a default config.");
e.printStackTrace();
}
}
InputStream is = Properties.class.getResourceAsStream(pFilePath);
prop.load(is);
prop.clear();
prop.put("dBType", "sqlite");
prop.put("dBPath", System.getProperty("user.home")+"\\kBorrow.db" );
public String getProperty(String pKey){
return this.properties.getProperty(pKey);
}
public void setProperty(String pKey, String pValue){
this.properties.put(pKey, pValue);
}
}