Commit f2d2971d authored by Daniel Dreibrodt's avatar Daniel Dreibrodt

Configuration system added

parent 4d7f1d00
/*****************************************************************************
* Config.java
*****************************************************************************
* Copyright (C) 2007 Daniel Dreibrodt
*
* VLC Skin Editor
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package vlcskineditor;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* Class to manage the configuration
* @author Daniel Dreibrodt
*/
public class Config {
private static Hashtable<String, String> strings = new Hashtable<String, String>();
private static File configFile = new File("VLCSkinEditor.cfg");
static {
strings.put("autoupdate", "true");
strings.put("language", "English");
}
public static void loadConfig() {
try {
FileReader fr = new FileReader(configFile);
BufferedReader br = new BufferedReader(fr);
String line = "";
while( (line = br.readLine()) != null) {
if(!line.startsWith("#")) {
String[] fields = line.split("\\|");
if(fields.length==2) {
strings.put(fields[0], fields[1].replaceAll("\\\\n", "\n"));
}
}
}
br.close();
fr.close();
} catch(FileNotFoundException ex) {
System.out.println("Configuration does not yet exist. Creating it...");
saveConfig();
} catch(IOException ex) {
ex.printStackTrace();
}
}
public static String getValue(String key) {
String s = strings.get(key);
return (s!=null)?s:key;
}
public static void setValue(String key, String value) {
strings.put(key, value);
}
public static void saveConfig() {
try {
FileWriter fw = new FileWriter(configFile);
BufferedWriter bw = new BufferedWriter(fw);
Enumeration<String> keys = strings.keys();
while(keys.hasMoreElements()) {
String key = keys.nextElement();
String value = strings.get(key);
bw.write(key+"|"+value);
bw.newLine();
}
bw.close();
fw.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
......@@ -51,6 +51,8 @@ public class Language {
}
}
br.close();
fr.close();
} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex) {
......
......@@ -123,8 +123,8 @@ public class Main extends javax.swing.JFrame implements ActionListener, TreeSele
*/
public Main(String[] args) {
//TODO: Load configuration
Language.loadLanguage(new File("lang/English.txt"));
Config.loadConfig();
Language.loadLanguage(new File("lang"+File.separator+Config.getValue("language")+".txt"));
setTitle("VLC Skin Editor "+VERSION);
......@@ -950,29 +950,7 @@ public class Main extends javax.swing.JFrame implements ActionListener, TreeSele
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Quit">
else if(e.getSource().equals(m_file_quit)) {
if(!saved) {
Object[] options= { "Yes", "No", "Don't quit" };
int n = JOptionPane.showOptionDialog(
this,
"Would you like to save your skin before exiting?",
"Your changes were not saved",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]
);
if(n==0) {
s.save();
System.exit(0);
}
else if(n==1) {
System.exit(0);
}
}
else {
System.exit(0);
}
exit();
}
// </editor-fold>
else if(e.getSource().equals(m_edit_theme)) s.showThemeOptions();
......@@ -1528,32 +1506,7 @@ public class Main extends javax.swing.JFrame implements ActionListener, TreeSele
}
}
public void windowClosing(WindowEvent e) {
if(!saved) {
Object[] options= { Language.getString("CHOICE_YES"), Language.getString("CHOICE_NO"), Language.getString("CHOICE_CANCEL") };
int n = JOptionPane.showOptionDialog(
this,
Language.getString("CONFIRM_EXIT_MSG"),
Language.getString("CONFIRM_EXIT_TITLE"),
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]
);
if(n==0) {
s.save();
System.exit(0);
}
else if(n==1) {
System.exit(0);
}
else {
return;
}
}
else {
System.exit(0);
}
exit();
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {
......@@ -1771,6 +1724,38 @@ public class Main extends javax.swing.JFrame implements ActionListener, TreeSele
ex.printStackTrace();
}
}
private void exit() {
if(!saved) {
Object[] options= { Language.getString("CHOICE_YES"), Language.getString("CHOICE_NO"), Language.getString("CHOICE_CANCEL") };
int n = JOptionPane.showOptionDialog(
this,
Language.getString("CONFIRM_EXIT_MSG"),
Language.getString("CONFIRM_EXIT_TITLE"),
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]
);
if(n==0) {
s.save();
Config.saveConfig();
System.exit(0);
}
else if(n==1) {
Config.saveConfig();
System.exit(0);
}
else {
return;
}
}
else {
Config.saveConfig();
System.exit(0);
}
}
/**
* Creates a new instance of Main and thus launches the editor
* @param args the command line arguments
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment