From: Svjatoslav Agejenko Date: Sun, 20 Nov 2016 16:33:31 +0000 (+0200) Subject: Switched to YAML config format. X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=dcc5f6b34d80ffcebd604993305e9924bcdffbd3;p=instantlauncher.git Switched to YAML config format. --- diff --git a/instantlauncher.iml b/instantlauncher.iml index b48b302..77be61f 100644 --- a/instantlauncher.iml +++ b/instantlauncher.iml @@ -12,5 +12,6 @@ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 89cfb6a..8fa6dc6 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,13 @@ log4j 1.2.16 + + + com.esotericsoftware.yamlbeans + yamlbeans + 1.09 + + diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java b/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java deleted file mode 100755 index 94f9de2..0000000 --- a/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java +++ /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(); - } - -} diff --git a/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java b/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java index a4323ce..5d4971e 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java @@ -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 = ""; @@ -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); diff --git a/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java b/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java index c68c70e..a0ff09e 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java @@ -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(); diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Main.java b/src/main/java/eu/svjatoslav/instantlauncher/Main.java index e743390..08cc3a6 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Main.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Main.java @@ -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(); } } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Utils.java b/src/main/java/eu/svjatoslav/instantlauncher/Utils.java index f382e7c..c5d7641 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Utils.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Utils.java @@ -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 index 0000000..04713c5 --- /dev/null +++ b/src/main/java/eu/svjatoslav/instantlauncher/configuration/Configuration.java @@ -0,0 +1,8 @@ +package eu.svjatoslav.instantlauncher.configuration; + +import java.util.Set; + +public class Configuration { + public Set 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 index 0000000..4bf00c9 --- /dev/null +++ b/src/main/java/eu/svjatoslav/instantlauncher/configuration/ConfigurationManager.java @@ -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 index 0000000..dc3b5a7 --- /dev/null +++ b/src/main/java/eu/svjatoslav/instantlauncher/configuration/FileAssociation.java @@ -0,0 +1,19 @@ +package eu.svjatoslav.instantlauncher.configuration; + +public class FileAssociation implements Comparable { + 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); + } +} diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java index 10d6f3f..9178418 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java @@ -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;