From: Svjatoslav Agejenko Date: Sun, 20 Nov 2016 20:29:06 +0000 (+0200) Subject: Code refactoring and cleanup. Configurable directory opening command. X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=6f3b6383805ab24b835adaa7845e9306880ff7a3;p=instantlauncher.git Code refactoring and cleanup. Configurable directory opening command. --- diff --git a/instantlauncher.iml b/instantlauncher.iml index 77be61f..e1061c8 100644 --- a/instantlauncher.iml +++ b/instantlauncher.iml @@ -11,7 +11,6 @@ - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8fa6dc6..048f50d 100644 --- a/pom.xml +++ b/pom.xml @@ -17,12 +17,6 @@ 1.5 - - log4j - log4j - 1.2.16 - - com.esotericsoftware.yamlbeans yamlbeans diff --git a/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java b/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java index b21b2b3..bb8681e 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java @@ -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 true if file was opened. false 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); + } + } } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Main.java b/src/main/java/eu/svjatoslav/instantlauncher/Main.java index 08cc3a6..c5d320b 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Main.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Main.java @@ -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(); } } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java b/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java index 3ec2966..67c350a 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java @@ -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; diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Utils.java b/src/main/java/eu/svjatoslav/instantlauncher/Utils.java index 1884fec..297f010 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Utils.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Utils.java @@ -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); } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java b/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java index 63ef383..4a5b0dd 100644 --- a/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java @@ -6,13 +6,17 @@ import java.util.Set; public class Configuration { public Set 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); + } + } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java b/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java index 0e72914..4a06d06 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java @@ -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}"); } } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java b/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java index e21a495..4b65118 100644 --- a/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java @@ -5,8 +5,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class FileAssociation implements Comparable { - public String fileRegex; public String command; + private String fileRegex; public FileAssociation() { } @@ -29,7 +29,4 @@ public class FileAssociation implements Comparable { return matcher.matches(); } - public String[] getCommands() { - return command.split("\\s+"); - } } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java index 2201868..8456745 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java @@ -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) { diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java index 19f59d6..a9a6312 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java @@ -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); } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java index bc37046..3297259 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java @@ -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 index 8bb9ee1..0000000 --- a/src/main/resources/log4j.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file