Colours are now taken from a .properties file
[jvcard.git] / src / be / nikiroo / jvcard / tui / Main.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.Field;
6 import java.nio.charset.Charset;
7 import java.util.LinkedList;
8 import java.util.List;
9
10 import be.nikiroo.jvcard.i18n.Trans;
11 import be.nikiroo.jvcard.i18n.Trans.StringId;
12 import be.nikiroo.jvcard.tui.panes.FileList;
13
14 import com.googlecode.lanterna.gui2.Window;
15 import com.googlecode.lanterna.input.KeyStroke;
16
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 */
25 public class Main {
26 public static final String APPLICATION_TITLE = "jVcard";
27 public static final String APPLICATION_VERSION = "1.0-beta2-dev";
28
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) {
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 */
67 public static void main(String[] args) {
68 Boolean textMode = null;
69 boolean noMoreParams = false;
70 boolean filesTried = false;
71
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
77 List<File> files = new LinkedList<File>();
78 for (int index = 0; index < args.length; index++) {
79 String arg = args[index];
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"
88 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
89 + "\t--tui: force pure text mode even if swing treminal is available\n"
90 + "\t--gui: force swing terminal mode\n"
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"
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;
100 } else if (!noMoreParams && arg.equals("--noutf")) {
101 UiColors.getInstance().setUnicode(false);
102 } else if (!noMoreParams && arg.equals("--lang")) {
103 index++;
104 if (index < args.length)
105 language = args[index];
106 transService = new Trans(language);
107 } else {
108 filesTried = true;
109 files.addAll(open(arg));
110 }
111 }
112
113 if (UiColors.getInstance().isUnicode()) {
114 utf8();
115 }
116
117 if (files.size() == 0) {
118 if (filesTried) {
119 System.exit(1);
120 return;
121 }
122
123 files.addAll(open("."));
124 }
125
126 Window win = new MainWindow(new FileList(files));
127
128 try {
129 TuiLauncher.start(textMode, win);
130 } catch (IOException ioe) {
131 ioe.printStackTrace();
132 System.exit(2);
133 }
134 }
135
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
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 }
178 }