+++ /dev/null
-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);
- }
- }
-
-}
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");
}
public void run() throws IOException {
- configuration = new ConfigurationManager();
-
- final MainFrame mainFrame = new MainFrame(this);
mainFrame.show();
}