1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.util.launcher;
21
22 import java.io.File;
23 import java.net.URL;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28
29 import org.xnap.XNap;
30 import org.xnap.util.Preferences;
31 import org.xnap.util.QuotedStringTokenizer;
32
33 /***
34 * Manager class providing static functions which handle the OS dependent
35 * opening of files with the correct program.
36 */
37 public class LauncherManager
38 {
39
40
41
42 private static Preferences prefs = Preferences.getInstance();
43
44 private static LauncherManager singleton;
45
46 private Hashtable launcherByKey = new Hashtable();
47
48
49
50 private LauncherManager()
51 {
52 add(new WindowsLauncher());
53 add(new MacOSXLauncher());
54 add(new KDELauncher());
55 add(new DefaultLauncher("other", XNap.tr("Other"), ""));
56 }
57
58
59
60 public static LauncherManager getInstance()
61 {
62 if (singleton == null) {
63 synchronized (LauncherManager.class) {
64 if (singleton == null) {
65 singleton = new LauncherManager();
66 }
67 }
68 }
69
70 return singleton;
71 }
72
73 public static boolean isEnabled()
74 {
75 Launcher launcher = getInstance().get(prefs.getFileLauncherType());
76 return (launcher != null) ? launcher.isEnabled() : false;
77 }
78
79 public static void open(File file) throws IOException
80 {
81 Launcher launcher = getInstance().get(prefs.getFileLauncherType());
82 if (launcher == null) {
83 throw new IOException(XNap.tr("No launcher selected."));
84 }
85 launcher.open(file);
86 }
87
88 public static void open(URL url) throws IOException
89 {
90 Launcher launcher = getInstance().get(prefs.getFileLauncherType());
91 if (launcher == null) {
92 throw new IOException(XNap.tr("No launcher selected."));
93 }
94 launcher.open(url);
95 }
96
97 public void add(Launcher launcher)
98 {
99 launcherByKey.put(launcher.getKey(), launcher);
100 }
101
102 public Launcher get(String type)
103 {
104 return (Launcher)launcherByKey.get(type);
105 }
106
107 public Iterator iterator()
108 {
109 return launcherByKey.values().iterator();
110 }
111
112 public void remove(Launcher launcher)
113 {
114 launcherByKey.remove(launcher.getKey());
115 }
116
117 /***
118 * Executes <code>args</code> using the system runtime.
119 * Writes the output to console if "captureLauncherOutput"
120 * preference is true.
121 */
122 public static Process exec(String[] args) throws IOException
123 {
124 Process opener = Runtime.getRuntime().exec(args);
125 if (Preferences.getInstance().getCaptureLauncherOutput()) {
126
127 }
128 return opener;
129 }
130
131 /***
132 * Executes command. Ever occurence of "{}" is replaced by
133 * <code>files</code>. If <code>command</code> does not contain
134 * "{}", <code>files</code> are appended.
135 *
136 * @param command tokenized by {@link QuotedStringTokenizer}
137 * @param files if null, no additional arguments are appended
138 */
139 public static Process exec(String command, File[] files)
140 throws IOException
141 {
142 boolean inserted = false;
143 QuotedStringTokenizer t = new QuotedStringTokenizer(command);
144 ArrayList args = new ArrayList(t.countTokens() + files.length);
145 while (t.hasMoreTokens()) {
146 String s = t.nextToken();
147 if (s.equals("{}")) {
148 insertFiles(args, files);
149 inserted = true;
150 }
151 else {
152 args.add(s);
153 }
154 }
155
156 if (!inserted) {
157 insertFiles(args, files);
158 }
159
160 return exec((String[])args.toArray(new String[0]));
161 }
162
163 private static void insertFiles(ArrayList args, File[] files)
164 {
165 if (files != null) {
166 for (int i = 0; i < files.length; i++) {
167 if (files[i] != null || files[i].length() != 0) {
168 args.add(files[i].getAbsolutePath());
169 }
170 }
171 }
172 }
173
174 }
175
176