1 package eu.svjatoslav.instantlauncher;
4 import java.io.IOException;
5 import java.util.HashMap;
7 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
9 public class FileAssociationManager {
11 private static boolean isLogFile(final String fileName) {
12 if (fileName.endsWith(".out"))
14 if (fileName.endsWith(".log"))
16 if (fileName.contains(".log."))
23 * @return <code>true</code> if file was opened. <code>false</code> if
26 public static boolean openFile(final File file) {
28 if (isLogFile(file.getName())) {
29 runOpeningApplication(file,
30 new String[] { "glogg", FILE_INDICATOR });
34 final String fileExtension = Utils.getFileExtension(file);
36 if (extensionToApplication.containsKey(fileExtension)) {
37 final String[] commands = extensionToApplication.get(fileExtension);
38 runOpeningApplication(file, commands);
45 private static void runOpeningApplication(final File file,
46 final String[] commands) {
47 for (int i = 0; i < commands.length; i++)
48 if (commands[i].equals(FILE_INDICATOR))
49 commands[i] = file.getAbsolutePath();
52 Runtime.getRuntime().exec(commands);
53 InstantLauncher.exitProgram();
55 } catch (final IOException e) {
56 new ExceptionDialog(e);
60 private static final String FILE_INDICATOR = "<FILE>";
62 private static final String[] TEXT_EDITOR_PATTERN = new String[] {
63 "emacsclient", "-a", "\"\"", "-c", FILE_INDICATOR };
65 private static final String DOCUMENT_EDITOR = "lowriter";
67 private static final String SPREADSHEET_EDITOR = "localc";
69 private static final String MUSIC_PLAYER = "clementine";
71 public static HashMap<String, String[]> extensionToApplication = new HashMap<String, String[]>();
73 public FileAssociationManager() {
75 extensionToApplication.put("txt", TEXT_EDITOR_PATTERN);
76 extensionToApplication.put("xml", TEXT_EDITOR_PATTERN);
77 extensionToApplication.put("org", TEXT_EDITOR_PATTERN);
78 extensionToApplication.put("properties", TEXT_EDITOR_PATTERN);
79 extensionToApplication.put("ini", TEXT_EDITOR_PATTERN);
80 extensionToApplication.put("", TEXT_EDITOR_PATTERN);
82 extensionToApplication.put("jpg",
83 new String[] { "eog", FILE_INDICATOR });
84 extensionToApplication.put("jpeg",
85 new String[] { "eog", FILE_INDICATOR });
86 extensionToApplication.put("png",
87 new String[] { "eog", FILE_INDICATOR });
89 extensionToApplication.put("pdf", new String[] { "evince",
92 extensionToApplication.put("mp3", new String[] { "banshee",
94 extensionToApplication.put("ogg", new String[] { "banshee",
97 extensionToApplication.put("avi",
98 new String[] { "vlc", FILE_INDICATOR });
99 extensionToApplication.put("mkv",
100 new String[] { "vlc", FILE_INDICATOR });
101 extensionToApplication.put("ogv",
102 new String[] { "vlc", FILE_INDICATOR });
103 extensionToApplication.put("mp4",
104 new String[] { "vlc", FILE_INDICATOR });
105 extensionToApplication.put("flv",
106 new String[] { "vlc", FILE_INDICATOR });
107 extensionToApplication.put("wmv",
108 new String[] { "vlc", FILE_INDICATOR });
110 extensionToApplication.put("flac", new String[] { MUSIC_PLAYER,
112 extensionToApplication.put("mp3", new String[] { MUSIC_PLAYER,
114 extensionToApplication.put("ogg", new String[] { MUSIC_PLAYER,
116 extensionToApplication.put("wav", new String[] { MUSIC_PLAYER,
119 extensionToApplication.put("ods", new String[] { SPREADSHEET_EDITOR,
121 extensionToApplication.put("xls", new String[] { SPREADSHEET_EDITOR,
123 extensionToApplication.put("xlsx", new String[] { SPREADSHEET_EDITOR,
126 extensionToApplication.put("docx", new String[] { DOCUMENT_EDITOR,
128 extensionToApplication.put("doc", new String[] { DOCUMENT_EDITOR,
131 extensionToApplication.put("desktop", new String[] { "run-desktop",
134 extensionToApplication.put("log", new String[] { "glogg",
136 extensionToApplication.put("out", new String[] { "glogg",
139 extensionToApplication.put("html", new String[] { "firefox",
142 extensionToApplication.put("htm", new String[] { "firefox",