Java 1.6+ compatibility (at least) instead of 1.8+ only
[jvcard.git] / src / be / nikiroo / jvcard / tui / Main.java
CommitLineData
0b0b2b0f
NR
1package be.nikiroo.jvcard.tui;
2
3import java.io.File;
4import java.io.IOException;
d66e24cc
NR
5import java.lang.reflect.Field;
6import java.nio.charset.Charset;
0b0b2b0f
NR
7import java.util.LinkedList;
8import java.util.List;
9
668268fc
NR
10import be.nikiroo.jvcard.i18n.Trans;
11import be.nikiroo.jvcard.i18n.Trans.StringId;
0b0b2b0f
NR
12import be.nikiroo.jvcard.tui.panes.FileList;
13
0b0b2b0f 14import com.googlecode.lanterna.gui2.Window;
668268fc 15import 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
25public class Main {
26 public static final String APPLICATION_TITLE = "jVcard";
176a8327 27 public static final String APPLICATION_VERSION = "1.0-beta2";
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 91 + "\t--noutf: force non-utf8 mode if you need it\n"
bcb54330
NR
92 + "everyhing else is either a file to open or a directory to open\n"
93 + "(we will only open 1st level files in given directories)");
94 return;
95 } else if (!noMoreParams && arg.equals("--tui")) {
96 textMode = true;
97 } else if (!noMoreParams && arg.equals("--gui")) {
98 textMode = false;
296a0b75
NR
99 } else if (!noMoreParams && arg.equals("--noutf")) {
100 UiColors.getInstance().setUnicode(false);
668268fc
NR
101 } else if (!noMoreParams && arg.equals("--lang")) {
102 index++;
103 if (index < args.length)
104 language = args[index];
105 transService = new Trans(language);
bcb54330
NR
106 } else {
107 filesTried = true;
108 files.addAll(open(arg));
109 }
110 }
111
d66e24cc
NR
112 if (UiColors.getInstance().isUnicode()) {
113 utf8();
114 }
115
bcb54330
NR
116 if (files.size() == 0) {
117 if (filesTried) {
118 System.exit(1);
119 return;
120 }
0b0b2b0f 121
bcb54330
NR
122 files.addAll(open("."));
123 }
124
296a0b75
NR
125 Window win = new MainWindow(new FileList(files));
126
bcb54330 127 try {
296a0b75 128 TuiLauncher.start(textMode, win);
bcb54330
NR
129 } catch (IOException ioe) {
130 ioe.printStackTrace();
131 System.exit(2);
132 }
0b0b2b0f
NR
133 }
134
bcb54330
NR
135 /**
136 * Open the given path and add all its files if it is a directory or just
137 * this one if not to the returned list.
138 *
139 * @param path
140 * the path to open
141 *
142 * @return the list of opened files
143 */
144 static private List<File> open(String path) {
145 List<File> files = new LinkedList<File>();
146
147 File file = new File(path);
148 if (file.exists()) {
149 if (file.isDirectory()) {
150 for (File subfile : file.listFiles()) {
151 if (!subfile.isDirectory())
152 files.add(subfile);
153 }
154 } else {
155 files.add(file);
156 }
157 } else {
158 System.err.println("File or directory not found: \"" + path + "\"");
159 }
160
161 return files;
162 }
163
d66e24cc
NR
164 /**
165 * Really, really ask for UTF-8 encoding.
166 */
167 static private void utf8() {
168 try {
169 System.setProperty("file.encoding", "UTF-8");
170 Field charset = Charset.class.getDeclaredField("defaultCharset");
171 charset.setAccessible(true);
172 charset.set(null, null);
6b6a62ca
NR
173 } catch (SecurityException e) {
174 } catch (NoSuchFieldException e) {
175 } catch (IllegalArgumentException e) {
176 } catch (IllegalAccessException e) {
d66e24cc
NR
177 }
178 }
0b0b2b0f 179}