Commit | Line | Data |
---|---|---|
0b0b2b0f NR |
1 | package be.nikiroo.jvcard.tui; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
d66e24cc NR |
5 | import java.lang.reflect.Field; |
6 | import java.nio.charset.Charset; | |
0b0b2b0f NR |
7 | import java.util.LinkedList; |
8 | import java.util.List; | |
9 | ||
668268fc NR |
10 | import be.nikiroo.jvcard.i18n.Trans; |
11 | import be.nikiroo.jvcard.i18n.Trans.StringId; | |
7f82bf68 | 12 | import be.nikiroo.jvcard.resources.Bundles; |
0b0b2b0f NR |
13 | import be.nikiroo.jvcard.tui.panes.FileList; |
14 | ||
0b0b2b0f | 15 | import com.googlecode.lanterna.gui2.Window; |
668268fc | 16 | import com.googlecode.lanterna.input.KeyStroke; |
0b0b2b0f | 17 | |
bcb54330 NR |
18 | /** |
19 | * This class contains the runnable Main method. It will parse the user supplied | |
20 | * parameters and take action based upon those. Most of the time, it will start | |
21 | * a MainWindow. | |
22 | * | |
23 | * @author niki | |
24 | * | |
25 | */ | |
0b0b2b0f NR |
26 | public class Main { |
27 | public static final String APPLICATION_TITLE = "jVcard"; | |
26d2bd05 | 28 | public static final String APPLICATION_VERSION = "1.0-beta2-dev"; |
0b0b2b0f | 29 | |
668268fc NR |
30 | static private Trans transService; |
31 | ||
32 | /** | |
33 | * Translate the given {@link StringId}. | |
34 | * | |
35 | * @param id | |
36 | * the ID to translate | |
37 | * | |
38 | * @return the translation | |
39 | */ | |
40 | static public String trans(StringId id) { | |
668268fc NR |
41 | if (transService == null) |
42 | return ""; | |
43 | ||
44 | return transService.trans(id); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Translate the given {@link KeyStroke}. | |
49 | * | |
50 | * @param key | |
51 | * the key to translate | |
52 | * | |
53 | * @return the translation | |
54 | */ | |
55 | static public String trans(KeyStroke key) { | |
56 | if (transService == null) | |
57 | return ""; | |
58 | ||
59 | return transService.trans(key); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Start the application. | |
64 | * | |
65 | * @param args | |
7f82bf68 NR |
66 | * the parameters (see <tt>--help</tt> to know which are |
67 | * supported) | |
668268fc | 68 | */ |
bcb54330 | 69 | public static void main(String[] args) { |
0b0b2b0f | 70 | Boolean textMode = null; |
bcb54330 NR |
71 | boolean noMoreParams = false; |
72 | boolean filesTried = false; | |
0b0b2b0f | 73 | |
668268fc NR |
74 | // get the "system default" language to help translate the --help |
75 | // message if needed | |
76 | String language = null; | |
7f82bf68 | 77 | transService = new Trans(language); |
668268fc | 78 | |
7f82bf68 | 79 | List<String> files = new LinkedList<String>(); |
668268fc NR |
80 | for (int index = 0; index < args.length; index++) { |
81 | String arg = args[index]; | |
bcb54330 NR |
82 | if (!noMoreParams && arg.equals("--")) { |
83 | noMoreParams = true; | |
84 | } else if (!noMoreParams && arg.equals("--help")) { | |
85 | System.out | |
86 | .println("TODO: implement some help text.\n" | |
87 | + "Usable switches:\n" | |
88 | + "\t--: stop looking for switches\n" | |
89 | + "\t--help: this here thingy\n" | |
668268fc | 90 | + "\t--lang LANGUAGE: choose the language, for instance en_GB\n" |
bcb54330 NR |
91 | + "\t--tui: force pure text mode even if swing treminal is available\n" |
92 | + "\t--gui: force swing terminal mode\n" | |
296a0b75 | 93 | + "\t--noutf: force non-utf8 mode if you need it\n" |
7f82bf68 | 94 | + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n" |
bcb54330 | 95 | + "everyhing else is either a file to open or a directory to open\n" |
7f82bf68 | 96 | + "(we will only open 1st level files in given directories)\n"); |
bcb54330 NR |
97 | return; |
98 | } else if (!noMoreParams && arg.equals("--tui")) { | |
99 | textMode = true; | |
100 | } else if (!noMoreParams && arg.equals("--gui")) { | |
101 | textMode = false; | |
296a0b75 NR |
102 | } else if (!noMoreParams && arg.equals("--noutf")) { |
103 | UiColors.getInstance().setUnicode(false); | |
668268fc NR |
104 | } else if (!noMoreParams && arg.equals("--lang")) { |
105 | index++; | |
106 | if (index < args.length) | |
107 | language = args[index]; | |
108 | transService = new Trans(language); | |
7f82bf68 NR |
109 | } else if (!noMoreParams && arg.equals("--config")) { |
110 | index++; | |
111 | if (index < args.length) { | |
112 | Bundles.setDirectory(args[index]); | |
113 | transService = new Trans(language); | |
114 | } | |
bcb54330 NR |
115 | } else { |
116 | filesTried = true; | |
117 | files.addAll(open(arg)); | |
118 | } | |
119 | } | |
120 | ||
d66e24cc NR |
121 | if (UiColors.getInstance().isUnicode()) { |
122 | utf8(); | |
123 | } | |
124 | ||
bcb54330 NR |
125 | if (files.size() == 0) { |
126 | if (filesTried) { | |
127 | System.exit(1); | |
128 | return; | |
129 | } | |
0b0b2b0f | 130 | |
bcb54330 NR |
131 | files.addAll(open(".")); |
132 | } | |
133 | ||
296a0b75 NR |
134 | Window win = new MainWindow(new FileList(files)); |
135 | ||
bcb54330 | 136 | try { |
296a0b75 | 137 | TuiLauncher.start(textMode, win); |
bcb54330 NR |
138 | } catch (IOException ioe) { |
139 | ioe.printStackTrace(); | |
140 | System.exit(2); | |
141 | } | |
0b0b2b0f NR |
142 | } |
143 | ||
bcb54330 NR |
144 | /** |
145 | * Open the given path and add all its files if it is a directory or just | |
146 | * this one if not to the returned list. | |
147 | * | |
148 | * @param path | |
149 | * the path to open | |
150 | * | |
151 | * @return the list of opened files | |
152 | */ | |
7f82bf68 NR |
153 | static private List<String> open(String path) { |
154 | List<String> files = new LinkedList<String>(); | |
bcb54330 NR |
155 | |
156 | File file = new File(path); | |
157 | if (file.exists()) { | |
158 | if (file.isDirectory()) { | |
159 | for (File subfile : file.listFiles()) { | |
160 | if (!subfile.isDirectory()) | |
7f82bf68 | 161 | files.add(subfile.getAbsolutePath()); |
bcb54330 NR |
162 | } |
163 | } else { | |
7f82bf68 | 164 | files.add(file.getAbsolutePath()); |
bcb54330 NR |
165 | } |
166 | } else { | |
167 | System.err.println("File or directory not found: \"" + path + "\""); | |
168 | } | |
169 | ||
170 | return files; | |
171 | } | |
172 | ||
d66e24cc NR |
173 | /** |
174 | * Really, really ask for UTF-8 encoding. | |
175 | */ | |
176 | static private void utf8() { | |
177 | try { | |
178 | System.setProperty("file.encoding", "UTF-8"); | |
179 | Field charset = Charset.class.getDeclaredField("defaultCharset"); | |
180 | charset.setAccessible(true); | |
181 | charset.set(null, null); | |
6b6a62ca NR |
182 | } catch (SecurityException e) { |
183 | } catch (NoSuchFieldException e) { | |
184 | } catch (IllegalArgumentException e) { | |
185 | } catch (IllegalAccessException e) { | |
d66e24cc NR |
186 | } |
187 | } | |
0b0b2b0f | 188 | } |