From 6b79132d324a77ecced2f12a78d2c54a7852f3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20M=C3=A4kinen?= Date: Mon, 15 Aug 2016 15:09:28 +0300 Subject: [PATCH 1/4] Loading defaults from .properties file --- src/USBtinViewer.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/USBtinViewer.java b/src/USBtinViewer.java index 867f35b..2a20df1 100644 --- a/src/USBtinViewer.java +++ b/src/USBtinViewer.java @@ -40,6 +40,9 @@ import static java.awt.Toolkit.getDefaultToolkit; import static java.lang.System.getProperty; +import java.util.Properties; +import java.io.FileInputStream; + /** * Main window frame for USBtinViewer * @@ -47,6 +50,7 @@ */ public class USBtinViewer extends javax.swing.JFrame implements CANMessageListener { + public static final String USBTIN_PROPERTIES = "usbtin.properties"; /** Version string */ protected final String version = "1.3"; @@ -62,13 +66,23 @@ public class USBtinViewer extends javax.swing.JFrame implements CANMessageListen /** Start timestamp in system-milliseconds */ protected long baseTimestamp = 0; + private Properties props = null; + /** * Creates new form and initialize it */ public USBtinViewer() { - + // Load configs + props = new Properties(); + + try { + props.load(new FileInputStream(USBTIN_PROPERTIES)); + System.out.println("usbtin.properties loaded"); + } catch(Exception e) {} + // init view components initComponents(); + bitRate.setSelectedItem(props.getProperty("bitrate", "10000")); setTitle(getTitle() + " " + version); setIconImage(new ImageIcon(getClass().getResource("/res/icons/usbtinviewer.png")).getImage()); openmodeComboBox.setSelectedItem(USBtin.OpenMode.ACTIVE); From b8d707b0e32387e6bf6c3c6fdc64ce5572cccff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20M=C3=A4kinen?= Date: Fri, 19 Aug 2016 11:18:38 +0300 Subject: [PATCH 2/4] Saving configs to disk when closing the application --- src/USBtinViewer.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/USBtinViewer.java b/src/USBtinViewer.java index 2a20df1..eff2e9a 100644 --- a/src/USBtinViewer.java +++ b/src/USBtinViewer.java @@ -36,12 +36,17 @@ import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import static java.awt.Toolkit.getDefaultToolkit; import static java.lang.System.getProperty; import java.util.Properties; import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.File; /** * Main window frame for USBtinViewer @@ -86,6 +91,15 @@ public USBtinViewer() { setTitle(getTitle() + " " + version); setIconImage(new ImageIcon(getClass().getResource("/res/icons/usbtinviewer.png")).getImage()); openmodeComboBox.setSelectedItem(USBtin.OpenMode.ACTIVE); + + // Closing hooks to save config + // + this.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + SaveConfigsToDisk(); + System.exit(0); + } + }); // initialize message payload input fields and add listeners msgDataFields = new JTextField[]{msgData0, msgData1, msgData2, msgData3, msgData4, msgData5, msgData6, msgData7}; @@ -225,6 +239,18 @@ public void actionPerformed(ActionEvent e) { usbtin.addMessageListener(this); } + private void SaveConfigsToDisk() { + props.setProperty("bitrate",(String) bitRate.getSelectedItem()); + + try { + File f = new File("usbtin.properties"); + OutputStream out = new FileOutputStream(f); + props.store(out, "Automatically generated config file"); + } catch(Exception e) { + e.printStackTrace(); + } + } + /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always From 510818e4b870c452ddda4c3cb3bbe918d18017a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20M=C3=A4kinen?= Date: Fri, 19 Aug 2016 11:36:21 +0300 Subject: [PATCH 3/4] Changed tabs to spaces --- src/USBtinViewer.java | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/USBtinViewer.java b/src/USBtinViewer.java index eff2e9a..384a786 100644 --- a/src/USBtinViewer.java +++ b/src/USBtinViewer.java @@ -81,8 +81,8 @@ public USBtinViewer() { props = new Properties(); try { - props.load(new FileInputStream(USBTIN_PROPERTIES)); - System.out.println("usbtin.properties loaded"); + props.load(new FileInputStream(USBTIN_PROPERTIES)); + System.out.println("usbtin.properties loaded"); } catch(Exception e) {} // init view components @@ -92,14 +92,14 @@ public USBtinViewer() { setIconImage(new ImageIcon(getClass().getResource("/res/icons/usbtinviewer.png")).getImage()); openmodeComboBox.setSelectedItem(USBtin.OpenMode.ACTIVE); - // Closing hooks to save config - // - this.addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { - SaveConfigsToDisk(); - System.exit(0); - } - }); + // Closing hooks to save config + // + this.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + SaveConfigsToDisk(); + System.exit(0); + } + }); // initialize message payload input fields and add listeners msgDataFields = new JTextField[]{msgData0, msgData1, msgData2, msgData3, msgData4, msgData5, msgData6, msgData7}; @@ -239,17 +239,17 @@ public void actionPerformed(ActionEvent e) { usbtin.addMessageListener(this); } - private void SaveConfigsToDisk() { - props.setProperty("bitrate",(String) bitRate.getSelectedItem()); - - try { - File f = new File("usbtin.properties"); - OutputStream out = new FileOutputStream(f); - props.store(out, "Automatically generated config file"); - } catch(Exception e) { - e.printStackTrace(); - } - } + private void SaveConfigsToDisk() { + props.setProperty("bitrate",(String) bitRate.getSelectedItem()); + + try { + File f = new File("usbtin.properties"); + OutputStream out = new FileOutputStream(f); + props.store(out, "Automatically generated config file"); + } catch(Exception e) { + e.printStackTrace(); + } + } /** * This method is called from within the constructor to initialize the form. From d2448c44f80014a5b3d5ba8a99c3b3cfcc037dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20M=C3=A4kinen?= Date: Fri, 19 Aug 2016 11:53:46 +0300 Subject: [PATCH 4/4] Storing the .properties file next to the jar, not to working dir --- src/USBtinViewer.java | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/USBtinViewer.java b/src/USBtinViewer.java index 384a786..6a9268b 100644 --- a/src/USBtinViewer.java +++ b/src/USBtinViewer.java @@ -55,7 +55,7 @@ */ public class USBtinViewer extends javax.swing.JFrame implements CANMessageListener { - public static final String USBTIN_PROPERTIES = "usbtin.properties"; + public static final String USBTIN_PROPERTIES = "/usbtin.properties"; /** Version string */ protected final String version = "1.3"; @@ -73,17 +73,23 @@ public class USBtinViewer extends javax.swing.JFrame implements CANMessageListen private Properties props = null; + /** Config file location, same as where the .jar is **/ + private String configFilePath = null; + /** * Creates new form and initialize it */ public USBtinViewer() { - // Load configs - props = new Properties(); - try { - props.load(new FileInputStream(USBTIN_PROPERTIES)); + File jarFile = new File(USBtinViewer.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); + configFilePath = jarFile.getParentFile().getPath(); + + // Load configs + props = new Properties(); + + props.load(new FileInputStream(configFilePath + USBTIN_PROPERTIES)); System.out.println("usbtin.properties loaded"); - } catch(Exception e) {} + } catch(Exception e) { e.printStackTrace(); } // init view components initComponents(); @@ -243,7 +249,8 @@ private void SaveConfigsToDisk() { props.setProperty("bitrate",(String) bitRate.getSelectedItem()); try { - File f = new File("usbtin.properties"); + File f = new File(configFilePath + USBTIN_PROPERTIES); + System.out.println("Saving configs to: " + configFilePath + USBTIN_PROPERTIES); OutputStream out = new FileOutputStream(f); props.store(out, "Automatically generated config file"); } catch(Exception e) {