diff options
author | Jonathan Bauer | 2014-09-11 18:52:27 +0200 |
---|---|---|
committer | Jonathan Bauer | 2014-09-11 18:52:27 +0200 |
commit | f3d2dc54c2ec1ffc6420172995360f6f493bcfa1 (patch) | |
tree | 7b90b84f2e7f4fae99826f78dd8ac17bc494a432 | |
parent | master pom file for both subprojects (diff) | |
download | tutor-module-f3d2dc54c2ec1ffc6420172995360f6f493bcfa1.tar.gz tutor-module-f3d2dc54c2ec1ffc6420172995360f6f493bcfa1.tar.xz tutor-module-f3d2dc54c2ec1ffc6420172995360f6f493bcfa1.zip |
reworked config file class, now a static class.
see code for an example...
4 files changed, 143 insertions, 214 deletions
diff --git a/dozentenmodul/src/main/java/config/config_file.java b/dozentenmodul/src/main/java/config/config_file.java index 754e67a9..3d562b35 100644 --- a/dozentenmodul/src/main/java/config/config_file.java +++ b/dozentenmodul/src/main/java/config/config_file.java @@ -6,69 +6,65 @@ import java.io.IOException; import org.ini4j.Wini; public class config_file { - - private String configPath = null; - public boolean createConfig() throws IOException{ + // Nur die Wini als member + private static Wini ini = null; + + // Konstruktor ermittelt der Pfad zur Konfigurationsdatei. + // Unterscheidet dabei Windows/Linux + public static void init() throws IOException { + // Hauptvariablen nur lokal notwendig + String configPath = null; + File configFile = null; // Unterscheide zwischen Windows/Unix String OSName = System.getProperty("os.name").toLowerCase(); System.out.println("Machine's OS: " + OSName); - if (OSName.contains("windows")) - { + if (OSName.contains("windows")) { // Windows machine. Use the environment variable 'APPDATA' which // should point to a path similar to: // C:\Users\<user>\AppData\Roaming String appDataPath = System.getenv("APPDATA"); - if (!appDataPath.isEmpty()) - { - System.out.println("APPDATA: " + appDataPath); + if (!appDataPath.isEmpty()) { configPath = appDataPath + "\\bwSuite\\config.ini"; - } - else - { + } else { // APPDATA was empty, let's build it ourselves... System.out.println("APPDATA ist leer."); - configPath = System.getProperty("user.home") + "\\AppData\\Roaming\\bwSuite\\config.ini" - + configPath = System.getProperty("user.home") + "\\AppData\\Roaming\\bwSuite\\config.ini"; } - } - else if (OSName.contains("linux")) - { + } else if (OSName.contains("linux")) { configPath=System.getProperty("user.home") + "/.config/bwSuite/config.ini"; - + } else { + // Nicht Windows oder Linux, TODO MacOS Support? + configPath = null; } - if (configPath == null) - { - System.out.println("Config file path could not be determined."); - } - //config_file="C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Roaming\\bwLehrpoolSuite"; //File fuer den Ordner erzeugen - File configFile=new File(configPath); - Wini ini; + if (!(configPath.isEmpty()||configPath == null)) { + configFile = new File(configPath); + } else { + throw new IOException("Konnte kein Pfad für die Konfigurationsdatei ermitteln."); + } - //Pruefen ob Ordner schon existiert - if(!configFile.exists()) - { - System.out.println("Ordner \"bwLehrpoolSuite\" exisitiert nicht - lege ihn jetzt an."); - //Wenn nicht erzeuge Ordner - if (!configFile.getParentFile().mkdirs()) - { - System.out.print("Konnte Verzeichnisstruktur nicht erstellen: " + configFile.getParentFile()); - System.out.println(" - keine weitere Aktion"); - // jetzt nichts mehr + // Checke, ob der Ordner existiert. + if (!configFile.getParentFile().exists()) { + System.out.println("Ordner " + configFile.getParentFile() + " exisitiert nicht - lege ihn jetzt an."); + // Wenn nicht, erzeuge Ordner + if (!configFile.getParentFile().mkdirs()) { + throw new IOException("Konnte '" + configFile.getParentFile() + "' nicht erstellen."); } - - //Erzeuge eine neue Datei + } + + // Pruefen, ob Datei schon existiert + if (!configFile.exists()) { + // Erzeuge eine neue Datei configFile.createNewFile(); - ini=new Wini(configFile); - - //Wenn in die Datei geschrieben werden kann - if(configFile.canWrite()==true) - { - System.out.println("Erzeuge \"config.ini.\""); + + // Wenn in die Datei geschrieben werden kann + if(configFile.canWrite()) { + ini = new Wini(configFile); + System.out.println("Erzeuge '" + configFile + "'..."); //Schreibe Config ini.put("main", "BillOfRights", false); ini.put("main", "vmware", false); @@ -78,44 +74,56 @@ public class config_file { ini.put("main", "Letzter Uploadpfad", ""); ini.store(); - return true; } else { - System.out.println("Konnte \"config.ini\" nicht anlegen - keine weitere Aktion."); + throw new IOException("Konnte nicht in '" + configFile + "' schreiben. Haben Sie Rechte dazu?"); } - + } else { + System.out.println("'" + configFile + "' existiert bereits - keine weitere Aktion."); + ini = new Wini(configFile); } - else - { - System.out.println("Ordner \"bwLehrpoolSuite\" existiert bereits - keine weitere Aktion."); - //Wenn Ordner schon existiert, pruefe ob Datei existiert - if(!configFile.exists()) - { - System.out.println("\"config.ini\" existiert nicht - lege jetzt an."); - //Wenn nicht, erzeuge diese - configFile.createNewFile(); - ini=new Wini(configFile); - - if(configFile.canWrite()==true) - { - System.out.println("Schreibe jetzt \"config.ini\"."); - //Schreibe Konfig in File - ini.put("main", "BillOfRights",false); - ini.put("main", "vmware", false); - ini.put("main", "Benutzername speichern", false); - ini.put("main", "Benutzername", ""); - ini.put("main", "Letzter Downloadpfad", ""); - ini.put("main", "Letzter Uploadpfad", ""); - ini.store(); - - return true; - } else { - System.out.println("Kann \"config.ini\" nicht schreiben - keine weitere Aktion."); - } - } else { - System.out.println("\"config.ini\" existiert bereits - keine weitere Aktion."); - } + } + // Ende Konstruktor. + + // Public funktion zur Abfrage der Boolean-Werte + // Gibt Wert von Attribut 'key' in Sektion 'section', falls es existiert. + // Sonst gibt es 'defaultValue' zurück. + // + // Usage: getBoolean(<section>, <key>, <defaultValue>) + // Ex.: getBoolean("main", "Benutzername", "NichtGesetzt"); + public static boolean getBoolean(String section, String key, Boolean defaultValue) { + if (ini.containsKey(section) && ini.get(section).containsKey(key)) { + return ini.get(section, key, Boolean.class); + } else { + return defaultValue; + } + } + + // Public funktion zur Abfrage der String-Werte + // Usage: getBoolean(<key>) + public static String getString(String section, String key, String defaultValue) { + if (ini.containsKey(section) && ini.get(section).containsKey(key)) { + return ini.get(section, key); + } else { + return defaultValue; + } + } + + public static boolean setBoolean(String section, String key, Boolean value) { + return ini.put(section, key, value) != null; + } + + public static boolean setString(String section, String key, String value) { + return ini.put(section, key, value) != null; + } + + public static boolean store() { + try { + ini.store(); + return true; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; } - return true; - } } diff --git a/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java b/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java index b11dc746..b66e0a73 100644 --- a/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java +++ b/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java @@ -40,6 +40,7 @@ import models.Links; import org.ini4j.InvalidFileFormatException; import org.ini4j.Wini; +import config.config_file; import util.GuiOrganizer; import util.OpenLinks; @@ -128,23 +129,8 @@ public class BillOfRights_GUI extends JFrame { fwdButton = new JButton("Weiter"); fwdButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - try { - Wini ini=new Wini(new File("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Roaming\\bwLehrpoolSuite\\config.ini")); - ini.put("main", "BillOfRights",true); - ini.store(); - } catch (InvalidFileFormatException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - e1.getCause()+"\n"+e1.getStackTrace(), - "Debug-Message", JOptionPane.ERROR_MESSAGE); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - e1.getCause()+"\n"+e1.getStackTrace(), - "Debug-Message", JOptionPane.ERROR_MESSAGE); - } + config_file.setBoolean("main", "BillOfRights", true); + config_file.store(); VmWareLink_GUI ac=new VmWareLink_GUI(); ac.setVisible(true); dispose(); diff --git a/dozentenmodul/src/main/java/gui/intro/Login_GUI.java b/dozentenmodul/src/main/java/gui/intro/Login_GUI.java index b27c7179..6e990f04 100644 --- a/dozentenmodul/src/main/java/gui/intro/Login_GUI.java +++ b/dozentenmodul/src/main/java/gui/intro/Login_GUI.java @@ -30,13 +30,16 @@ import models.person; import org.apache.thrift.TException; import org.ini4j.InvalidFileFormatException; import org.ini4j.Wini; - import org.openslx.imagemaster.thrift.iface.ImageServer.Client; + import thrift.MasterThriftConnection; import thrift.ThriftConnection; + import org.openslx.imagemaster.thrift.iface.UserInfo; + import util.GuiOrganizer; import config.config_file; + import javax.swing.JCheckBox; @SuppressWarnings("serial") @@ -62,17 +65,18 @@ public class Login_GUI extends JFrame { public void run() { try { // Pruefe und Erzeuge gegebenfalls Config - config_file cf = new config_file(); - boolean conf = cf.createConfig(); - if (conf == true) { - // Aufruf und Anzeige des Login Fensters - Login_GUI frame = new Login_GUI(); - frame.setVisible(true); - } else { - JOptionPane.showMessageDialog(null, - "Fehler beim erzeugen der Konfigurationsfile", - "Message", JOptionPane.ERROR_MESSAGE); + try { + config_file.init(); + } catch (IOException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, e.getMessage(), + "Fehler", JOptionPane.ERROR_MESSAGE); + return; } + + // Aufruf und Anzeige des Login Fensters + Login_GUI frame = new Login_GUI(); + frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); @@ -163,30 +167,11 @@ public class Login_GUI extends JFrame { lblusername = new JTextField(); lblusername .setToolTipText("Bitte geben Sie Ihren bwIDM-Benutzernamen ein."); - try { - Wini ini = new Wini( - new File( - "C:\\Users\\" - + System.getProperty("user.name") - + "\\AppData\\Roaming\\bwLehrpoolSuite\\config.ini")); - if(ini.get("main", "Benutzername").isEmpty()){ - - }else{ - lblusername.setText(ini.get("main", "Benutzername").toString()); - - } - - if(ini.get("main", "Benutzername speichern", Boolean.class)==true){ - chckbxBenutzernameSpeichern.setSelected(true); - - } - } catch (InvalidFileFormatException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } + + // Lese Information aus der Konfigurationsdatei + lblusername.setText(config_file.getString("main", "Benutzername", "")); + chckbxBenutzernameSpeichern.setSelected(config_file.getBoolean("main", "Benutzername speichern", false)); + lblusername.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { performLogin(); @@ -305,79 +290,44 @@ public class Login_GUI extends JFrame { person.verantwortlicher.setHochschule(hochschule); - //Sp�ter �ber result.getRole zum Beispiel die Rolle holen + //Spaeter ueber result.getRole zum Beispiel die Rolle holen person.verantwortlicher.setRole("Dozent"); //person.verantwortlicher.setRole("Admin"); //person.verantwortlicher.setRole("Student"); //person.verantwortlicher.setRole("GetToTheChopper!"); - - - try { - // Lege config File an und entscheide welches Fenster - // als naechstes geoeffnet wird - Wini ini = new Wini( - new File( - "C:\\Users\\" - + System.getProperty("user.name") - + "\\AppData\\Roaming\\bwLehrpoolSuite\\config.ini")); - if(lblusername.getText().equals(ini.get("main","Benutzername",String.class))==true){ - if(chckbxBenutzernameSpeichern.isSelected()){ - ini.put("main", "Benutzername", lblusername.getText()); - ini.put("main", "Benutzername speichern", true); - ini.store(); - } - else{ - ini.put("main", "Benutzername", ""); - ini.put("main", "Benutzername speichern", false); - ini.store(); - } - }else{ - - if(chckbxBenutzernameSpeichern.isSelected()){ - ini.put("main", "Benutzername", lblusername.getText()); - ini.put("main", "Benutzername speichern", true); - ini.put("main", "BillOfRights",false); - ini.put("main", "vmware",false); - ini.store(); - } - else{ - ini.put("main", "Benutzername", ""); - ini.put("main", "Benutzername speichern", false); - ini.put("main", "BillOfRights",false); - ini.put("main", "vmware",false); - ini.store(); - } - } - - // Pruefe ob Bills Of Rights schon akzeptiert wurden, - // wenn - // nicht zeige diese an - if (ini.get("main", "BillOfRights", boolean.class) == false) { - // Erstellen einer Instanz der Aktionsauswahl - BillOfRights_GUI re = new BillOfRights_GUI(); - re.setVisible(true); - // Schliessen des Fensters nach erfolgreichen Login + + if (chckbxBenutzernameSpeichern.isSelected()){ + config_file.setString("main", "Benutzername", lblusername.getText()); + config_file.setBoolean("main", "Benutzername speichern", true); + } else { + config_file.setString("main", "Benutzername", ""); + config_file.setBoolean("main", "Benutzername speichern", false); + } + // speichern + config_file.store(); + + // Pruefe ob Bills Of Rights schon akzeptiert wurden, + // wenn nicht zeige diese an + if (!config_file.getBoolean("main", "BillOfRights", false)) { + // Erstellen einer Instanz der Aktionsauswahl + BillOfRights_GUI re = new BillOfRights_GUI(); + re.setVisible(true); + // Schliessen des Fensters nach erfolgreichen Login + setVisible(false); + } else { + // Pruefe ob die Links zu vmware angezeigt werden + // sollen + if (!config_file.getBoolean("main", "vmware", false)) { + VmWareLink_GUI vm = new VmWareLink_GUI(); + vm.setVisible(true); setVisible(false); } else { - // Pruefe ob die Links zu vmware angezeigt werden - // sollen - if (ini.get("main", "vmware", boolean.class) == false) { - VmWareLink_GUI vm = new VmWareLink_GUI(); - vm.setVisible(true); - setVisible(false); - } else { - - // oeffne das Hauptmenue - MainMenue_GUI main = new MainMenue_GUI(); - main.setVisible(true); - setVisible(false); - } + + // oeffne das Hauptmenue + MainMenue_GUI main = new MainMenue_GUI(); + main.setVisible(true); + setVisible(false); } - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } } else { diff --git a/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java b/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java index d5350fbd..a9c00acd 100644 --- a/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java +++ b/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java @@ -38,6 +38,7 @@ import models.Links; import org.ini4j.InvalidFileFormatException; import org.ini4j.Wini; +import config.config_file; import util.GuiOrganizer; import util.OpenLinks; @@ -111,25 +112,9 @@ public class VmWareLink_GUI extends JFrame { chckbxNewCheckBox = new JCheckBox("Diese Benachrichtigung nicht mehr anzeigen."); chckbxNewCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent arg0) { - if(chckbxNewCheckBox.isSelected()==true) - { - try { - Wini ini=new Wini(new File("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Roaming\\bwLehrpoolSuite\\config.ini")); - ini.put("main", "vmware",true); - ini.store(); - } catch (InvalidFileFormatException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - e1.getCause()+"\n"+e1.getStackTrace(), - "Debug-Message", JOptionPane.ERROR_MESSAGE); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - e1.getCause()+"\n"+e1.getStackTrace(), - "Debug-Message", JOptionPane.ERROR_MESSAGE); - } + if(chckbxNewCheckBox.isSelected()) { + config_file.setBoolean("main", "vmware", true); + config_file.store(); } } }); |