Code refactoring and cleanup. Configurable directory opening command.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 20 Nov 2016 20:29:06 +0000 (22:29 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 20 Nov 2016 20:29:06 +0000 (22:29 +0200)
13 files changed:
instantlauncher.iml
pom.xml
src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java
src/main/java/eu/svjatoslav/instantlauncher/Main.java
src/main/java/eu/svjatoslav/instantlauncher/MainFrame.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/Menu.java
src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java
src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java
src/main/resources/log4j.xml [deleted file]

index 77be61f..e1061c8 100644 (file)
@@ -11,7 +11,6 @@
     <orderEntry type="inheritedJdk" />
     <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 8fa6dc6..048f50d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <version>1.5</version>
         </dependency>
 
-        <dependency>
-            <groupId>log4j</groupId>
-            <artifactId>log4j</artifactId>
-            <version>1.2.16</version>
-        </dependency>
-
         <dependency>
             <groupId>com.esotericsoftware.yamlbeans</groupId>
             <artifactId>yamlbeans</artifactId>
index b21b2b3..bb8681e 100755 (executable)
@@ -1,8 +1,8 @@
 package eu.svjatoslav.instantlauncher;
 
+import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
 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;
@@ -11,15 +11,18 @@ import static eu.svjatoslav.instantlauncher.Utils.runOpeningApplication;
 
 public class InstantLauncher {
 
-    private static final Logger LOGGER = Logger.getLogger(InstantLauncher.class);
+    private final MainFrame mainFrame;
     public ConfigurationManager configurationManager;
-    final MainFrame mainFrame;
 
     public InstantLauncher() throws IOException {
         configurationManager = new ConfigurationManager();
         mainFrame = new MainFrame(this);
     }
 
+    public static void exitProgram() {
+        System.exit(0);
+    }
+
     /**
      * @return <code>true</code> if file was opened. <code>false</code> if
      * unknown file type.
@@ -28,19 +31,25 @@ public class InstantLauncher {
         FileAssociation fileAssociation = configurationManager.getConfiguration().findFileAssociation(file);
         if (fileAssociation == null) return false;
 
-        final String[] commands = fileAssociation.getCommands();
-        runOpeningApplication(file, commands);
-
-        return false;
+        runOpeningApplication(fileAssociation.command, file);
+        return true;
     }
 
-    public static void exitProgram() {
-        LOGGER.info("Closing InstantLauncher");
-        System.exit(0);
+    public void run() {
+        mainFrame.show();
     }
 
-    public void run() throws IOException {
-        mainFrame.show();
+    public void openDirectory(File chosenFile) {
+        executeCommand(configurationManager.getConfiguration().directoryOpenCommand, chosenFile.getAbsolutePath());
     }
 
+    public void executeCommand(String... c) {
+        try {
+            Runtime.getRuntime().exec(c);
+            exitProgram();
+
+        } catch (final IOException e) {
+            new ExceptionDialog(e);
+        }
+    }
 }
index 08cc3a6..c5d320b 100755 (executable)
@@ -1,13 +1,11 @@
 package eu.svjatoslav.instantlauncher;
 
-import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
-
 import java.io.IOException;
 
 class Main {
 
     public static void main(final String[] args) throws IOException {
-       new InstantLauncher().run();
+        new InstantLauncher().run();
     }
 
 }
index 3ec2966..67c350a 100755 (executable)
@@ -9,7 +9,6 @@ import java.awt.event.WindowListener;
 
 class MainFrame {
 
-    public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1100, 850);
     private final InstantLauncher instantLauncher;
     private JFrame frame;
     private boolean isShowing;
index 1884fec..297f010 100755 (executable)
@@ -7,10 +7,10 @@ import java.awt.*;
 import java.io.File;
 import java.io.IOException;
 
-import static eu.svjatoslav.commons.file.FilePathParser.getFileExtension;
-
 public class Utils {
 
+    private static final String FILE_INDICATOR = "{file}";
+
     public static void setComponentSize(JComponent component, Dimension size) {
         component.setMinimumSize(size);
         component.setMaximumSize(size);
@@ -18,17 +18,17 @@ public class Utils {
         component.setPreferredSize(size);
     }
 
-    private static final String FILE_INDICATOR = "{file}";
+    public static void runOpeningApplication(String commands, final File file) {
+        runOpeningApplication(commands.split("\\s+"), file);
+    }
 
-    public static void runOpeningApplication(final File file, final String[] commands) {
+    private static void runOpeningApplication(final String[] commands, final File file) {
         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 63ef383..4a5b0dd 100644 (file)
@@ -6,13 +6,17 @@ import java.util.Set;
 public class Configuration {
     public Set<FileAssociation> fileAssociations;
     public String navigationRootPath;
+    public String directoryOpenCommand;
 
-
-    public FileAssociation findFileAssociation(File file){
+    public FileAssociation findFileAssociation(File file) {
         for (FileAssociation association : fileAssociations)
             if (association.matchesFile(file)) return association;
 
         return null;
     }
 
+    public File getNavigationRootDirectory() {
+        return new File(navigationRootPath);
+    }
+
 }
index 0e72914..4a06d06 100755 (executable)
@@ -5,80 +5,65 @@ 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 Configuration configuration;
 
-    private boolean propertiesChanged = false;
+    public ConfigurationManager() throws IOException {
+        initConfig();
+    }
 
     public Configuration getConfiguration() {
         return configuration;
     }
 
-    private 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 {
+    private void initConfig() throws IOException {
         final File propertiesFile = getPropertiesFile();
-        if (!propertiesFile.exists())
-            return;
+        if (!propertiesFile.exists()) {
+            initDefaultConfiguration();
+            saveConfigFile();
+        } else
+            loadConfigFile(propertiesFile);
+    }
 
+    private void loadConfigFile(File propertiesFile) throws FileNotFoundException, com.esotericsoftware.yamlbeans.YamlException {
         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 {
+    private void saveConfigFile() 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){
+    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}");
+    private void initDefaultConfiguration() {
+        configuration.navigationRootPath = System.getProperty("user.home") + "/";
+        configuration.directoryOpenCommand = "nautilus -w {file}";
+
+        registerFileAssociation(".*\\.jpeg$", "eog {file}");
+        registerFileAssociation(".*\\.jpg$", "eog {file}");
+        registerFileAssociation(".*\\.png$", "eog {file}");
+        registerFileAssociation(".*\\.txt$", "emacs {file}");
+        registerFileAssociation(".*\\.org$", "emacs {file}");
+        registerFileAssociation(".*\\.avi$", "vlc {file}");
+        registerFileAssociation(".*\\.mp4$", "vlc {file}");
+        registerFileAssociation(".*\\.mkv$", "vlc {file}");
     }
 }
index e21a495..4b65118 100644 (file)
@@ -5,8 +5,8 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class FileAssociation implements Comparable<FileAssociation> {
-    public String fileRegex;
     public String command;
+    private String fileRegex;
 
     public FileAssociation() {
     }
@@ -29,7 +29,4 @@ public class FileAssociation implements Comparable<FileAssociation> {
         return matcher.matches();
     }
 
-    public String[] getCommands() {
-        return command.split("\\s+");
-    }
 }
index 2201868..8456745 100755 (executable)
@@ -149,17 +149,17 @@ class Menu {
 
     private void menuItemSelected(final File chosenFile) {
         for (final MenuListener menuListener : menuListeners)
-            menuListener.menuItemSelected(this, chosenFile);
+            menuListener.menuItemSelected(chosenFile);
     }
 
     private void menuItemSelectedAlternative(final File chosenFile) {
         for (final MenuListener menuListener : menuListeners)
-            menuListener.menuItemSelectedAlternative(this, chosenFile);
+            menuListener.menuItemSelectedAlternative(chosenFile);
     }
 
     private void menuItemSelectedAlternative2(final File chosenFile) {
         for (final MenuListener menuListener : menuListeners)
-            menuListener.menuItemSelectedAlternative2(this, chosenFile);
+            menuListener.menuItemSelectedAlternative2(chosenFile);
     }
 
     private void setSelected(final JButton selectedButton, final File selectedFile) {
index 19f59d6..a9a6312 100755 (executable)
@@ -1,5 +1,7 @@
 package eu.svjatoslav.instantlauncher.menu;
 
+import java.io.File;
+
 interface MenuListener {
 
     /**
@@ -10,16 +12,16 @@ interface MenuListener {
     /**
      * left click
      */
-    void menuItemSelected(Menu menu, java.io.File chosenFile);
+    void menuItemSelected(File chosenFile);
 
     /**
      * right click
      */
-    void menuItemSelectedAlternative(Menu menu, java.io.File chosenFile);
+    void menuItemSelectedAlternative(File chosenFile);
 
     /**
      * middle click
      */
-    void menuItemSelectedAlternative2(Menu menu, java.io.File chosenFile);
+    void menuItemSelectedAlternative2(File chosenFile);
 
 }
index bc37046..3297259 100755 (executable)
@@ -1,6 +1,5 @@
 package eu.svjatoslav.instantlauncher.menu;
 
-import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
 import eu.svjatoslav.instantlauncher.InstantLauncher;
 import eu.svjatoslav.instantlauncher.Utils;
 
@@ -68,7 +67,7 @@ public class MultiLevelMenu implements MenuListener {
             contentPane.add(panel);
         }
 
-        final File directory = instantLauncher.configurationManager.getNavigationRootDirectory();
+        final File directory = instantLauncher.configurationManager.getConfiguration().getNavigationRootDirectory();
         addMenu(directory);
 
         return contentPane;
@@ -103,17 +102,17 @@ public class MultiLevelMenu implements MenuListener {
     }
 
     @Override
-    public void menuItemSelected(final Menu menu, final File chosenFile) {
+    public void menuItemSelected(final File chosenFile) {
         if (chosenFile.isFile()) {
             if (chosenFile.canExecute()) {
-                executeCommand(chosenFile.getAbsolutePath());
+                instantLauncher.executeCommand(chosenFile.getAbsolutePath());
             } else {
                 boolean fileOpened;
                 try {
                     fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
-                    if (fileOpened) {
+                    if (fileOpened)
                         InstantLauncher.exitProgram();
-                    }
+
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
@@ -122,30 +121,20 @@ public class MultiLevelMenu implements MenuListener {
 
         if (chosenFile.isDirectory()) {
             try {
-                executeCommand("nautilus", "-w", chosenFile.getCanonicalFile().getAbsolutePath());
+                instantLauncher.openDirectory(chosenFile.getCanonicalFile());
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 
-    private void executeCommand(String... c) {
-        try {
-            Runtime.getRuntime().exec(c);
-            InstantLauncher.exitProgram();
-
-        } catch (final IOException e) {
-            new ExceptionDialog(e);
-        }
-    }
-
     @Override
-    public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
-        if (chosenFile.isFile()) {
+    public void menuItemSelectedAlternative(final File chosenFile) {
+        if (chosenFile.isFile())
             chosenFile.setExecutable(!chosenFile.canExecute());
-        else {
+        else {
             try {
-                executeCommand("gnome-terminal", "--working-directory="
+                instantLauncher.executeCommand("gnome-terminal", "--working-directory="
                         + chosenFile.getCanonicalFile().getAbsolutePath());
             } catch (IOException e) {
                 e.printStackTrace();
@@ -166,10 +155,10 @@ public class MultiLevelMenu implements MenuListener {
     }
 
     @Override
-    public void menuItemSelectedAlternative2(Menu menu, File chosenFile) {
+    public void menuItemSelectedAlternative2(File chosenFile) {
         if (chosenFile.isFile()) {
             try {
-                executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
+                instantLauncher.executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
             } catch (IOException e) {
                 e.printStackTrace();
             }
diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml
deleted file mode 100644 (file)
index 8bb9ee1..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
-<log4j:configuration>
-    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
-        <layout class="org.apache.log4j.PatternLayout">
-            <param name="ConversionPattern" value="%d{ABSOLUTE}
-      %5p %c{1}:%L - %m%n"/>
-
-        </layout>
-    </appender>
-    <root>
-        <!-- possible values are: debug, info, error -->
-        <priority value="debug"></priority>
-        <appender-ref ref="stdout"/>
-    </root>
-</log4j:configuration>
\ No newline at end of file