1 package be
.nikiroo
.jvcard
.tui
;
4 import java
.io
.IOException
;
5 import java
.lang
.reflect
.Field
;
6 import java
.nio
.charset
.Charset
;
7 import java
.util
.LinkedList
;
10 import be
.nikiroo
.jvcard
.i18n
.Trans
;
11 import be
.nikiroo
.jvcard
.i18n
.Trans
.StringId
;
12 import be
.nikiroo
.jvcard
.resources
.Bundles
;
13 import be
.nikiroo
.jvcard
.tui
.panes
.FileList
;
15 import com
.googlecode
.lanterna
.gui2
.Window
;
16 import com
.googlecode
.lanterna
.input
.KeyStroke
;
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
27 public static final String APPLICATION_TITLE
= "jVcard";
28 public static final String APPLICATION_VERSION
= "1.0-beta2-dev";
30 static private Trans transService
;
33 * Translate the given {@link StringId}.
38 * @return the translation
40 static public String
trans(StringId id
) {
41 if (transService
== null)
44 return transService
.trans(id
);
48 * Translate the given {@link KeyStroke}.
51 * the key to translate
53 * @return the translation
55 static public String
trans(KeyStroke key
) {
56 if (transService
== null)
59 return transService
.trans(key
);
63 * Start the application.
66 * the parameters (see <tt>--help</tt> to know which are
69 public static void main(String
[] args
) {
70 Boolean textMode
= null;
71 boolean noMoreParams
= false;
72 boolean filesTried
= false;
74 // get the "system default" language to help translate the --help
76 String language
= null;
77 transService
= new Trans(language
);
79 List
<String
> files
= new LinkedList
<String
>();
80 for (int index
= 0; index
< args
.length
; index
++) {
81 String arg
= args
[index
];
82 if (!noMoreParams
&& arg
.equals("--")) {
84 } else if (!noMoreParams
&& arg
.equals("--help")) {
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"
90 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
91 + "\t--tui: force pure text mode even if swing treminal is available\n"
92 + "\t--gui: force swing terminal mode\n"
93 + "\t--noutf: force non-utf8 mode if you need it\n"
94 + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
95 + "everyhing else is either a file to open or a directory to open\n"
96 + "(we will only open 1st level files in given directories)\n");
98 } else if (!noMoreParams
&& arg
.equals("--tui")) {
100 } else if (!noMoreParams
&& arg
.equals("--gui")) {
102 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
103 UiColors
.getInstance().setUnicode(false);
104 } else if (!noMoreParams
&& arg
.equals("--lang")) {
106 if (index
< args
.length
)
107 language
= args
[index
];
108 transService
= new Trans(language
);
109 } else if (!noMoreParams
&& arg
.equals("--config")) {
111 if (index
< args
.length
) {
112 Bundles
.setDirectory(args
[index
]);
113 transService
= new Trans(language
);
117 files
.addAll(open(arg
));
121 if (UiColors
.getInstance().isUnicode()) {
125 if (files
.size() == 0) {
131 files
.addAll(open("."));
134 Window win
= new MainWindow(new FileList(files
));
137 TuiLauncher
.start(textMode
, win
);
138 } catch (IOException ioe
) {
139 ioe
.printStackTrace();
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.
151 * @return the list of opened files
153 static private List
<String
> open(String path
) {
154 List
<String
> files
= new LinkedList
<String
>();
156 File file
= new File(path
);
158 if (file
.isDirectory()) {
159 for (File subfile
: file
.listFiles()) {
160 if (!subfile
.isDirectory())
161 files
.add(subfile
.getAbsolutePath());
164 files
.add(file
.getAbsolutePath());
167 System
.err
.println("File or directory not found: \"" + path
+ "\"");
174 * Really, really ask for UTF-8 encoding.
176 static private void utf8() {
178 System
.setProperty("file.encoding", "UTF-8");
179 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
180 charset
.setAccessible(true);
181 charset
.set(null, null);
182 } catch (SecurityException e
) {
183 } catch (NoSuchFieldException e
) {
184 } catch (IllegalArgumentException e
) {
185 } catch (IllegalAccessException e
) {