1 package eu.svjatoslav.instantlauncher;
3 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
6 import java.io.IOException;
7 import java.util.HashMap;
9 public class FileAssociationManager {
11 private static final String FILE_INDICATOR = "<FILE>";
13 private static final String[] TEXT_EDITOR_PATTERN = new String[]{"emc", FILE_INDICATOR};
15 private static final String DOCUMENT_EDITOR = "lowriter";
17 private static final String SPREADSHEET_EDITOR = "localc";
19 private static final String MUSIC_PLAYER = "clementine";
21 private static final HashMap<String, String[]> extensionToApplication = new HashMap<>();
23 public FileAssociationManager() {
25 extensionToApplication.put("txt", TEXT_EDITOR_PATTERN);
26 extensionToApplication.put("xml", TEXT_EDITOR_PATTERN);
27 extensionToApplication.put("org", TEXT_EDITOR_PATTERN);
28 extensionToApplication.put("properties", TEXT_EDITOR_PATTERN);
29 extensionToApplication.put("ini", TEXT_EDITOR_PATTERN);
30 extensionToApplication.put("", TEXT_EDITOR_PATTERN);
32 extensionToApplication.put("jpg", new String[]{"eog", FILE_INDICATOR});
33 extensionToApplication.put("jpeg", new String[]{"eog", FILE_INDICATOR});
34 extensionToApplication.put("png", new String[]{"eog", FILE_INDICATOR});
36 extensionToApplication.put("pdf", new String[]{"evince", FILE_INDICATOR});
38 extensionToApplication.put("mp3", new String[]{"banshee", FILE_INDICATOR});
39 extensionToApplication.put("ogg", new String[]{"banshee", FILE_INDICATOR});
41 extensionToApplication.put("avi", new String[]{"vlc", FILE_INDICATOR});
42 extensionToApplication.put("mkv", new String[]{"vlc", FILE_INDICATOR});
43 extensionToApplication.put("ogv", new String[]{"vlc", FILE_INDICATOR});
44 extensionToApplication.put("mp4", new String[]{"vlc", FILE_INDICATOR});
45 extensionToApplication.put("flv", new String[]{"vlc", FILE_INDICATOR});
46 extensionToApplication.put("wmv", new String[]{"vlc", FILE_INDICATOR});
48 extensionToApplication.put("flac", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
49 extensionToApplication.put("mp3", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
50 extensionToApplication.put("ogg", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
51 extensionToApplication.put("wav", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
53 extensionToApplication.put("ods", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
54 extensionToApplication.put("xls", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
55 extensionToApplication.put("xlsx", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
57 extensionToApplication.put("docx", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR});
58 extensionToApplication.put("doc", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR});
60 extensionToApplication.put("desktop", new String[]{"run-desktop", FILE_INDICATOR});
62 extensionToApplication.put("log", new String[]{"glogg", FILE_INDICATOR});
63 extensionToApplication.put("out", new String[]{"glogg", FILE_INDICATOR});
65 extensionToApplication.put("html", new String[]{"firefox", FILE_INDICATOR});
67 extensionToApplication.put("htm", new String[]{"firefox", FILE_INDICATOR});
71 private static boolean isLogFile(final String fileName) {
72 if (fileName.endsWith(".out"))
74 if (fileName.endsWith(".log"))
76 return fileName.contains(".log.");
81 * @return <code>true</code> if file was opened. <code>false</code> if
84 public static boolean openFile(final File file) {
86 if (isLogFile(file.getName())) {
87 runOpeningApplication(file, new String[]{"glogg", FILE_INDICATOR});
91 final String fileExtension = Utils.getFileExtension(file);
93 if (extensionToApplication.containsKey(fileExtension)) {
94 final String[] commands = extensionToApplication.get(fileExtension);
95 runOpeningApplication(file, commands);
102 private static void runOpeningApplication(final File file, final String[] commands) {
103 for (int i = 0; i < commands.length; i++)
104 if (commands[i].equals(FILE_INDICATOR))
105 commands[i] = file.getAbsolutePath();
108 Runtime.getRuntime().exec(commands);
109 InstantLauncher.exitProgram();
111 } catch (final IOException e) {
112 new ExceptionDialog(e);