Switched to YAML config format.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 20 Nov 2016 16:33:31 +0000 (18:33 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 20 Nov 2016 16:33:31 +0000 (18:33 +0200)
instantlauncher.iml
pom.xml
src/main/java/eu/svjatoslav/instantlauncher/Configuration.java [deleted file]
src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java
src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java
src/main/java/eu/svjatoslav/instantlauncher/Main.java
src/main/java/eu/svjatoslav/instantlauncher/Utils.java
src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java [new file with mode: 0755]
src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java

index b48b302..77be61f 100644 (file)
@@ -12,5 +12,6 @@
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="library" name="Maven: eu.svjatoslav:svjatoslavcommons:1.5" level="project" />
     <orderEntry type="library" name="Maven: log4j:log4j:1.2.16" level="project" />
+    <orderEntry type="library" name="Maven: com.esotericsoftware.yamlbeans:yamlbeans:1.09" level="project" />
   </component>
 </module>
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 89cfb6a..8fa6dc6 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <artifactId>log4j</artifactId>
             <version>1.2.16</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.esotericsoftware.yamlbeans</groupId>
+            <artifactId>yamlbeans</artifactId>
+            <version>1.09</version>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java b/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java
deleted file mode 100755 (executable)
index 94f9de2..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-package eu.svjatoslav.instantlauncher;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.Properties;
-
-public class Configuration {
-
-    private static final String KEY_ROOT_DIRECTORY = "KEY_ROOT_DIRECTORY";
-    private static final String CONFIG_FILE_NAME = ".instantlauncher";
-    private final Properties properties = new Properties();
-
-    private boolean propertiesChanged = false;
-
-    public Configuration() throws IOException {
-        initialize();
-    }
-
-    private File getPropertiesFile() {
-        return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
-    }
-
-    public File getRootDirectory() {
-
-        if (properties.containsKey(KEY_ROOT_DIRECTORY)) {
-            return new File(properties.getProperty(KEY_ROOT_DIRECTORY));
-        } else {
-            properties.put(KEY_ROOT_DIRECTORY, System.getProperty("user.home") + "/");
-            propertiesChanged = true;
-            return getRootDirectory();
-        }
-
-    }
-
-    private void initialize() throws IOException {
-
-        loadIfFileExists();
-
-        validatePropertiesFile();
-
-        if (propertiesChanged) {
-            saveFile();
-        }
-    }
-
-    private void loadIfFileExists() throws IOException {
-        final File propertiesFile = getPropertiesFile();
-
-        if (propertiesFile.exists()) {
-            final FileInputStream inStream = new FileInputStream(propertiesFile);
-            properties.load(inStream);
-            inStream.close();
-        }
-    }
-
-    private void saveFile() throws IOException {
-        properties.store(new FileOutputStream(getPropertiesFile()), "Instantlauncher configuration file.");
-    }
-
-    private void validatePropertiesFile() {
-        getRootDirectory();
-    }
-
-}
index a4323ce..5d4971e 100755 (executable)
@@ -6,6 +6,8 @@ import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
 
+import static eu.svjatoslav.commons.file.FilePathParser.getFileExtension;
+
 public class FileAssociationManager {
 
     private static final String FILE_INDICATOR = "<FILE>";
@@ -88,7 +90,7 @@ public class FileAssociationManager {
             return true;
         }
 
-        final String fileExtension = Utils.getFileExtension(file);
+        final String fileExtension = getFileExtension(file);
 
         if (extensionToApplication.containsKey(fileExtension)) {
             final String[] commands = extensionToApplication.get(fileExtension);
index c68c70e..a0ff09e 100755 (executable)
@@ -1,5 +1,6 @@
 package eu.svjatoslav.instantlauncher;
 
+import eu.svjatoslav.instantlauncher.configuration.ConfigurationManager;
 import org.apache.log4j.Logger;
 
 import java.io.IOException;
@@ -7,7 +8,7 @@ import java.io.IOException;
 public class InstantLauncher {
 
     private static final Logger LOGGER = Logger.getLogger(InstantLauncher.class);
-    public Configuration configuration;
+    public ConfigurationManager configuration;
     FileAssociationManager associationManager = new FileAssociationManager();
 
     public static void exitProgram() {
@@ -16,7 +17,7 @@ public class InstantLauncher {
     }
 
     public void run() throws IOException {
-        configuration = new Configuration();
+        configuration = new ConfigurationManager();
 
         final MainFrame mainFrame = new MainFrame(this);
         mainFrame.show();
index e743390..08cc3a6 100755 (executable)
@@ -2,15 +2,12 @@ package eu.svjatoslav.instantlauncher;
 
 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
 
-class Main {
+import java.io.IOException;
 
-    public static void main(final String[] args) {
+class Main {
 
-        try {
-            new InstantLauncher().run();
-        } catch (final Exception e) {
-            new ExceptionDialog(e);
-        }
+    public static void main(final String[] args) throws IOException {
+       new InstantLauncher().run();
     }
 
 }
index f382e7c..c5d7641 100755 (executable)
@@ -6,22 +6,6 @@ import java.io.File;
 
 public class Utils {
 
-    public static String getFileExtension(File file) {
-        // extract file name without file name extension
-        String fullFileName = file.getName();
-
-        int dot = fullFileName.lastIndexOf('.');
-        String fileExtension;
-        if (dot == -1) {
-            fileExtension = "";
-        } else {
-            fileExtension = fullFileName.substring(dot + 1);
-            fileExtension = fileExtension.toLowerCase();
-        }
-
-        return fileExtension;
-    }
-
     public static void setComponentSize(JComponent component, Dimension size) {
         component.setMinimumSize(size);
         component.setMaximumSize(size);
diff --git a/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java b/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java
new file mode 100644 (file)
index 0000000..04713c5
--- /dev/null
@@ -0,0 +1,8 @@
+package eu.svjatoslav.instantlauncher.configuration;
+
+import java.util.Set;
+
+public class Configuration {
+    public Set<FileAssociation> fileAssociations;
+    public String navigationRootPath;
+}
diff --git a/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java b/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java
new file mode 100755 (executable)
index 0000000..4bf00c9
--- /dev/null
@@ -0,0 +1,79 @@
+package eu.svjatoslav.instantlauncher.configuration;
+
+import com.esotericsoftware.yamlbeans.YamlReader;
+import com.esotericsoftware.yamlbeans.YamlWriter;
+
+import java.io.*;
+import java.util.HashSet;
+import java.util.Map;
+
+public class ConfigurationManager {
+
+    private static final String CONFIG_FILE_NAME = ".instantlauncher";
+
+    private boolean propertiesChanged = false;
+
+    Configuration configuration;
+
+    public ConfigurationManager() throws IOException {
+        initialize();
+    }
+
+    private File getPropertiesFile() {
+        return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
+    }
+
+    public File getNavigationRootDirectory() {
+        if (configuration.navigationRootPath == null){
+            configuration.navigationRootPath = System.getProperty("user.home") + "/";
+            propertiesChanged = true;
+            registerDefaultAssociations();
+        }
+
+        return new File(configuration.navigationRootPath);
+    }
+
+    private void initialize() throws IOException {
+
+        loadIfFileExists();
+
+        validatePropertiesFile();
+
+        if (propertiesChanged) {
+            saveFile();
+        }
+    }
+
+    private void loadIfFileExists() throws IOException {
+        final File propertiesFile = getPropertiesFile();
+        if (!propertiesFile.exists())
+            return;
+
+        YamlReader reader = new YamlReader(new FileReader(propertiesFile));
+        configuration = reader.read(Configuration.class);
+        if (configuration == null) {
+            configuration = new Configuration();
+            configuration.fileAssociations = new HashSet<>();
+        };
+    }
+
+    private void saveFile() throws IOException {
+        YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
+        writer.write(configuration);
+        writer.close();
+    }
+
+    private void validatePropertiesFile() {
+        getNavigationRootDirectory();
+    }
+
+    private void registerFileAssociation(String fileRegex, String command){
+        FileAssociation association = new FileAssociation(fileRegex, command);
+        configuration.fileAssociations.add(association);
+        propertiesChanged = true;
+    }
+
+    private void registerDefaultAssociations(){
+        registerFileAssociation(".jpeg$", "eog {file}");
+    }
+}
diff --git a/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java b/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java
new file mode 100644 (file)
index 0000000..dc3b5a7
--- /dev/null
@@ -0,0 +1,19 @@
+package eu.svjatoslav.instantlauncher.configuration;
+
+public class FileAssociation implements Comparable<FileAssociation> {
+    public String fileRegex;
+    public String command;
+
+    public FileAssociation() {
+    }
+
+    public FileAssociation(String fileRegex, String command) {
+        this.fileRegex = fileRegex;
+        this.command = command;
+    }
+
+    @Override
+    public int compareTo(FileAssociation o) {
+        return fileRegex.compareTo(o.fileRegex);
+    }
+}
index 10d6f3f..9178418 100755 (executable)
@@ -69,7 +69,7 @@ public class MultiLevelMenu implements MenuListener {
             contentPane.add(panel);
         }
 
-        final File directory = instantLauncher.configuration.getRootDirectory();
+        final File directory = instantLauncher.configuration.getNavigationRootDirectory();
         addMenu(directory);
 
         return contentPane;