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
.tui
.panes
.FileList
;
14 import com
.googlecode
.lanterna
.TextColor
;
15 import com
.googlecode
.lanterna
.gui2
.BasicWindow
;
16 import com
.googlecode
.lanterna
.gui2
.DefaultWindowManager
;
17 import com
.googlecode
.lanterna
.gui2
.EmptySpace
;
18 import com
.googlecode
.lanterna
.gui2
.MultiWindowTextGUI
;
19 import com
.googlecode
.lanterna
.gui2
.Window
;
20 import com
.googlecode
.lanterna
.gui2
.table
.Table
;
21 import com
.googlecode
.lanterna
.input
.KeyStroke
;
22 import com
.googlecode
.lanterna
.screen
.Screen
;
23 import com
.googlecode
.lanterna
.screen
.TerminalScreen
;
24 import com
.googlecode
.lanterna
.terminal
.DefaultTerminalFactory
;
25 import com
.googlecode
.lanterna
.terminal
.Terminal
;
28 * This class contains the runnable Main method. It will parse the user supplied
29 * parameters and take action based upon those. Most of the time, it will start
36 public static final String APPLICATION_TITLE
= "jVcard";
37 public static final String APPLICATION_VERSION
= "1.0-beta2-dev";
39 static private Trans transService
;
42 * Translate the given {@link StringId}.
47 * @return the translation
49 static public String
trans(StringId id
) {
50 if (transService
== null)
53 return transService
.trans(id
);
57 * Translate the given {@link KeyStroke}.
60 * the key to translate
62 * @return the translation
64 static public String
trans(KeyStroke key
) {
65 if (transService
== null)
68 return transService
.trans(key
);
72 * Start the application.
75 * the parameters (see --help to know hich are supported)
77 public static void main(String
[] args
) {
78 Boolean textMode
= null;
79 boolean noMoreParams
= false;
80 boolean filesTried
= false;
82 // get the "system default" language to help translate the --help
84 String language
= null;
85 transService
= new Trans(null);
87 List
<File
> files
= new LinkedList
<File
>();
88 for (int index
= 0; index
< args
.length
; index
++) {
89 String arg
= args
[index
];
90 if (!noMoreParams
&& arg
.equals("--")) {
92 } else if (!noMoreParams
&& arg
.equals("--help")) {
94 .println("TODO: implement some help text.\n"
95 + "Usable switches:\n"
96 + "\t--: stop looking for switches\n"
97 + "\t--help: this here thingy\n"
98 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
99 + "\t--tui: force pure text mode even if swing treminal is available\n"
100 + "\t--gui: force swing terminal mode\n"
101 + "\t--noutf: force non-utf8 mode if you need it\n"
102 + "\t--noutfa: force non-utf8 and no accents mode if you need it\n"
103 + "everyhing else is either a file to open or a directory to open\n"
104 + "(we will only open 1st level files in given directories)");
106 } else if (!noMoreParams
&& arg
.equals("--tui")) {
108 } else if (!noMoreParams
&& arg
.equals("--gui")) {
110 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
111 UiColors
.getInstance().setUnicode(false);
112 } else if (!noMoreParams
&& arg
.equals("--lang")) {
114 if (index
< args
.length
)
115 language
= args
[index
];
116 transService
= new Trans(language
);
119 files
.addAll(open(arg
));
123 if (UiColors
.getInstance().isUnicode()) {
127 if (files
.size() == 0) {
133 files
.addAll(open("."));
136 Window win
= new MainWindow(new FileList(files
));
139 TuiLauncher
.start(textMode
, win
);
140 } catch (IOException ioe
) {
141 ioe
.printStackTrace();
147 * Open the given path and add all its files if it is a directory or just
148 * this one if not to the returned list.
153 * @return the list of opened files
155 static private List
<File
> open(String path
) {
156 List
<File
> files
= new LinkedList
<File
>();
158 File file
= new File(path
);
160 if (file
.isDirectory()) {
161 for (File subfile
: file
.listFiles()) {
162 if (!subfile
.isDirectory())
169 System
.err
.println("File or directory not found: \"" + path
+ "\"");
176 * Really, really ask for UTF-8 encoding.
178 static private void utf8() {
180 System
.setProperty("file.encoding", "UTF-8");
181 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
182 charset
.setAccessible(true);
183 charset
.set(null, null);
184 } catch (SecurityException
| NoSuchFieldException
185 | IllegalArgumentException
| IllegalAccessException e
) {
189 static private void fullTestTable() throws IOException
{
190 final Table
<String
> table
= new Table
<String
>("Column 1", "Column 2",
192 table
.getTableModel().addRow("1", "2", "3");
193 table
.setSelectAction(new Runnable() {
196 List
<String
> data
= table
.getTableModel().getRow(
197 table
.getSelectedRow());
198 for (int i
= 0; i
< data
.size(); i
++) {
199 System
.out
.println(data
.get(i
));
204 Window win
= new BasicWindow();
205 win
.setComponent(table
);
207 DefaultTerminalFactory factory
= new DefaultTerminalFactory();
208 Terminal terminal
= factory
.createTerminal();
210 Screen screen
= new TerminalScreen(terminal
);
211 screen
.startScreen();
213 // Create gui and start gui
214 MultiWindowTextGUI gui
= new MultiWindowTextGUI(screen
,
215 new DefaultWindowManager(), new EmptySpace(TextColor
.ANSI
.BLUE
));
216 gui
.addWindowAndWait(win
);