Implemeted basic settings functions.
This commit is contained in:
@@ -7,6 +7,7 @@ import de.katho.kBorrow.gui.MainWindow;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
new Main();
|
new Main();
|
||||||
|
Settings set = new Settings();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Main(){
|
public Main(){
|
||||||
|
|||||||
@@ -1,51 +1,98 @@
|
|||||||
package de.katho.kBorrow;
|
package de.katho.kBorrow;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.*;
|
import java.io.OutputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
|
|
||||||
private Properties properties;
|
private Properties properties;
|
||||||
private String filePath = System.getProperty("user.home")+"\\kBorrow\\Settings.cfg";
|
private String filePath;
|
||||||
private File cfgFile;
|
private String fileName;
|
||||||
|
|
||||||
public Settings() {
|
public Settings() {
|
||||||
this.properties = new Properties();
|
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.
|
* Writes a default config to the config file.
|
||||||
*
|
*
|
||||||
* @param pFilePath
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
private void createDefaultPropFile(String pFilePath) throws IOException {
|
private void createDefaultConfig() {
|
||||||
Properties prop = new Properties();
|
try {
|
||||||
File tFile = new File(pFilePath);
|
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()){
|
OutputStream os = new FileOutputStream(this.filePath+"\\"+this.fileName);
|
||||||
tFile.createNewFile();
|
|
||||||
|
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);
|
public String getProperty(String pKey){
|
||||||
prop.load(is);
|
return this.properties.getProperty(pKey);
|
||||||
prop.clear();
|
}
|
||||||
|
|
||||||
prop.put("dBType", "sqlite");
|
|
||||||
prop.put("dBPath", System.getProperty("user.home")+"\\kBorrow.db" );
|
|
||||||
|
|
||||||
|
public void setProperty(String pKey, String pValue){
|
||||||
|
this.properties.put(pKey, pValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user