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