read file associations from configuration
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 20 Nov 2016 17:01:13 +0000 (19:01 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 20 Nov 2016 17:01:13 +0000 (19:01 +0200)
src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java [deleted file]
src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java
src/main/java/eu/svjatoslav/instantlauncher/Utils.java
src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java
src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java
src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java
src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java

diff --git a/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java b/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java
deleted file mode 100755 (executable)
index 5d4971e..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-package eu.svjatoslav.instantlauncher;
-
-import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
-
-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>";
-
-    private static final String[] TEXT_EDITOR_PATTERN = new String[]{"emc", FILE_INDICATOR};
-
-    private static final String DOCUMENT_EDITOR = "lowriter";
-
-    private static final String SPREADSHEET_EDITOR = "localc";
-
-    private static final String MUSIC_PLAYER = "clementine";
-
-    private static final HashMap<String, String[]> extensionToApplication = new HashMap<>();
-
-    public FileAssociationManager() {
-
-        extensionToApplication.put("txt", TEXT_EDITOR_PATTERN);
-        extensionToApplication.put("xml", TEXT_EDITOR_PATTERN);
-        extensionToApplication.put("org", TEXT_EDITOR_PATTERN);
-        extensionToApplication.put("properties", TEXT_EDITOR_PATTERN);
-        extensionToApplication.put("ini", TEXT_EDITOR_PATTERN);
-        extensionToApplication.put("", TEXT_EDITOR_PATTERN);
-
-        extensionToApplication.put("jpg", new String[]{"eog", FILE_INDICATOR});
-        extensionToApplication.put("jpeg", new String[]{"eog", FILE_INDICATOR});
-        extensionToApplication.put("png", new String[]{"eog", FILE_INDICATOR});
-
-        extensionToApplication.put("pdf", new String[]{"evince", FILE_INDICATOR});
-
-        extensionToApplication.put("mp3", new String[]{"banshee", FILE_INDICATOR});
-        extensionToApplication.put("ogg", new String[]{"banshee", FILE_INDICATOR});
-
-        extensionToApplication.put("avi", new String[]{"vlc", FILE_INDICATOR});
-        extensionToApplication.put("mkv", new String[]{"vlc", FILE_INDICATOR});
-        extensionToApplication.put("ogv", new String[]{"vlc", FILE_INDICATOR});
-        extensionToApplication.put("mp4", new String[]{"vlc", FILE_INDICATOR});
-        extensionToApplication.put("flv", new String[]{"vlc", FILE_INDICATOR});
-        extensionToApplication.put("wmv", new String[]{"vlc", FILE_INDICATOR});
-
-        extensionToApplication.put("flac", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
-        extensionToApplication.put("mp3", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
-        extensionToApplication.put("ogg", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
-        extensionToApplication.put("wav", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
-
-        extensionToApplication.put("ods", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
-        extensionToApplication.put("xls", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
-        extensionToApplication.put("xlsx", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
-
-        extensionToApplication.put("docx", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR});
-        extensionToApplication.put("doc", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR});
-
-        extensionToApplication.put("desktop", new String[]{"run-desktop", FILE_INDICATOR});
-
-        extensionToApplication.put("log", new String[]{"glogg", FILE_INDICATOR});
-        extensionToApplication.put("out", new String[]{"glogg", FILE_INDICATOR});
-
-        extensionToApplication.put("html", new String[]{"firefox", FILE_INDICATOR});
-
-        extensionToApplication.put("htm", new String[]{"firefox", FILE_INDICATOR});
-
-    }
-
-    private static boolean isLogFile(final String fileName) {
-        if (fileName.endsWith(".out"))
-            return true;
-        if (fileName.endsWith(".log"))
-            return true;
-        return fileName.contains(".log.");
-
-    }
-
-    /**
-     * @return <code>true</code> if file was opened. <code>false</code> if
-     * unknown file type.
-     */
-    public static boolean openFile(final File file) {
-
-        if (isLogFile(file.getName())) {
-            runOpeningApplication(file, new String[]{"glogg", FILE_INDICATOR});
-            return true;
-        }
-
-        final String fileExtension = getFileExtension(file);
-
-        if (extensionToApplication.containsKey(fileExtension)) {
-            final String[] commands = extensionToApplication.get(fileExtension);
-            runOpeningApplication(file, commands);
-            return true;
-        } else
-            return false;
-
-    }
-
-    private static void runOpeningApplication(final File file, final String[] commands) {
-        for (int i = 0; i < commands.length; i++)
-            if (commands[i].equals(FILE_INDICATOR))
-                commands[i] = file.getAbsolutePath();
-
-        try {
-            Runtime.getRuntime().exec(commands);
-            InstantLauncher.exitProgram();
-
-        } catch (final IOException e) {
-            new ExceptionDialog(e);
-        }
-    }
-
-}
index a0ff09e..b21b2b3 100755 (executable)
@@ -1,15 +1,38 @@
 package eu.svjatoslav.instantlauncher;
 
 import eu.svjatoslav.instantlauncher.configuration.ConfigurationManager;
+import eu.svjatoslav.instantlauncher.configuration.FileAssociation;
 import org.apache.log4j.Logger;
 
+import java.io.File;
 import java.io.IOException;
 
+import static eu.svjatoslav.instantlauncher.Utils.runOpeningApplication;
+
 public class InstantLauncher {
 
     private static final Logger LOGGER = Logger.getLogger(InstantLauncher.class);
-    public ConfigurationManager configuration;
-    FileAssociationManager associationManager = new FileAssociationManager();
+    public ConfigurationManager configurationManager;
+    final MainFrame mainFrame;
+
+    public InstantLauncher() throws IOException {
+        configurationManager = new ConfigurationManager();
+        mainFrame = new MainFrame(this);
+    }
+
+    /**
+     * @return <code>true</code> if file was opened. <code>false</code> if
+     * unknown file type.
+     */
+    public boolean openFile(final File file) {
+        FileAssociation fileAssociation = configurationManager.getConfiguration().findFileAssociation(file);
+        if (fileAssociation == null) return false;
+
+        final String[] commands = fileAssociation.getCommands();
+        runOpeningApplication(file, commands);
+
+        return false;
+    }
 
     public static void exitProgram() {
         LOGGER.info("Closing InstantLauncher");
@@ -17,9 +40,6 @@ public class InstantLauncher {
     }
 
     public void run() throws IOException {
-        configuration = new ConfigurationManager();
-
-        final MainFrame mainFrame = new MainFrame(this);
         mainFrame.show();
     }
 
index c5d7641..1884fec 100755 (executable)
@@ -1,8 +1,13 @@
 package eu.svjatoslav.instantlauncher;
 
+import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
+
 import javax.swing.*;
 import java.awt.*;
 import java.io.File;
+import java.io.IOException;
+
+import static eu.svjatoslav.commons.file.FilePathParser.getFileExtension;
 
 public class Utils {
 
@@ -11,7 +16,22 @@ public class Utils {
         component.setMaximumSize(size);
         component.setSize(size);
         component.setPreferredSize(size);
+    }
+
+    private static final String FILE_INDICATOR = "{file}";
+
+    public static void runOpeningApplication(final File file, final String[] commands) {
+        for (int i = 0; i < commands.length; i++)
+            if (commands[i].equals(FILE_INDICATOR))
+                commands[i] = file.getAbsolutePath();
+
+        try {
+            Runtime.getRuntime().exec(commands);
+            InstantLauncher.exitProgram();
 
+        } catch (final IOException e) {
+            new ExceptionDialog(e);
+        }
     }
 
 }
index 04713c5..63ef383 100644 (file)
@@ -1,8 +1,18 @@
 package eu.svjatoslav.instantlauncher.configuration;
 
+import java.io.File;
 import java.util.Set;
 
 public class Configuration {
     public Set<FileAssociation> fileAssociations;
     public String navigationRootPath;
+
+
+    public FileAssociation findFileAssociation(File file){
+        for (FileAssociation association : fileAssociations)
+            if (association.matchesFile(file)) return association;
+
+        return null;
+    }
+
 }
index 4bf00c9..0e72914 100755 (executable)
@@ -13,7 +13,11 @@ public class ConfigurationManager {
 
     private boolean propertiesChanged = false;
 
-    Configuration configuration;
+    public Configuration getConfiguration() {
+        return configuration;
+    }
+
+    private Configuration configuration;
 
     public ConfigurationManager() throws IOException {
         initialize();
@@ -73,6 +77,7 @@ public class ConfigurationManager {
         propertiesChanged = true;
     }
 
+
     private void registerDefaultAssociations(){
         registerFileAssociation(".jpeg$", "eog {file}");
     }
index dc3b5a7..e21a495 100644 (file)
@@ -1,5 +1,9 @@
 package eu.svjatoslav.instantlauncher.configuration;
 
+import java.io.File;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 public class FileAssociation implements Comparable<FileAssociation> {
     public String fileRegex;
     public String command;
@@ -16,4 +20,16 @@ public class FileAssociation implements Comparable<FileAssociation> {
     public int compareTo(FileAssociation o) {
         return fileRegex.compareTo(o.fileRegex);
     }
+
+    public boolean matchesFile(File file) {
+        String absolutePath = file.getAbsolutePath();
+
+        Pattern pattern = Pattern.compile(fileRegex);
+        Matcher matcher = pattern.matcher(absolutePath);
+        return matcher.matches();
+    }
+
+    public String[] getCommands() {
+        return command.split("\\s+");
+    }
 }
index 9178418..bc37046 100755 (executable)
@@ -1,7 +1,6 @@
 package eu.svjatoslav.instantlauncher.menu;
 
 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
-import eu.svjatoslav.instantlauncher.FileAssociationManager;
 import eu.svjatoslav.instantlauncher.InstantLauncher;
 import eu.svjatoslav.instantlauncher.Utils;
 
@@ -69,7 +68,7 @@ public class MultiLevelMenu implements MenuListener {
             contentPane.add(panel);
         }
 
-        final File directory = instantLauncher.configuration.getNavigationRootDirectory();
+        final File directory = instantLauncher.configurationManager.getNavigationRootDirectory();
         addMenu(directory);
 
         return contentPane;
@@ -111,7 +110,7 @@ public class MultiLevelMenu implements MenuListener {
             } else {
                 boolean fileOpened;
                 try {
-                    fileOpened = FileAssociationManager.openFile(chosenFile.getCanonicalFile());
+                    fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
                     if (fileOpened) {
                         InstantLauncher.exitProgram();
                     }
@@ -177,4 +176,5 @@ public class MultiLevelMenu implements MenuListener {
         }
     }
 
+
 }