Fix UTF8 bug, create first executable JAR file
[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
NR
14import com.googlecode.lanterna.TextColor;
15import com.googlecode.lanterna.gui2.BasicWindow;
0b0b2b0f
NR
16import com.googlecode.lanterna.gui2.DefaultWindowManager;
17import com.googlecode.lanterna.gui2.EmptySpace;
0b0b2b0f 18import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
0b0b2b0f
NR
19import com.googlecode.lanterna.gui2.Window;
20import com.googlecode.lanterna.gui2.table.Table;
668268fc 21import com.googlecode.lanterna.input.KeyStroke;
0b0b2b0f
NR
22import com.googlecode.lanterna.screen.Screen;
23import com.googlecode.lanterna.screen.TerminalScreen;
24import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
25import com.googlecode.lanterna.terminal.Terminal;
26
bcb54330
NR
27/**
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
30 * a MainWindow.
31 *
32 * @author niki
33 *
34 */
0b0b2b0f
NR
35public class Main {
36 public static final String APPLICATION_TITLE = "jVcard";
d66e24cc 37 public static final String APPLICATION_VERSION = "1.0-beta2-dev";
0b0b2b0f 38
668268fc
NR
39 static private Trans transService;
40
41 /**
42 * Translate the given {@link StringId}.
43 *
44 * @param id
45 * the ID to translate
46 *
47 * @return the translation
48 */
49 static public String trans(StringId id) {
668268fc
NR
50 if (transService == null)
51 return "";
52
53 return transService.trans(id);
54 }
55
56 /**
57 * Translate the given {@link KeyStroke}.
58 *
59 * @param key
60 * the key to translate
61 *
62 * @return the translation
63 */
64 static public String trans(KeyStroke key) {
65 if (transService == null)
66 return "";
67
68 return transService.trans(key);
69 }
70
71 /**
72 * Start the application.
73 *
74 * @param args
75 * the parameters (see --help to know hich are supported)
76 */
bcb54330 77 public static void main(String[] args) {
0b0b2b0f 78 Boolean textMode = null;
bcb54330
NR
79 boolean noMoreParams = false;
80 boolean filesTried = false;
0b0b2b0f 81
668268fc
NR
82 // get the "system default" language to help translate the --help
83 // message if needed
84 String language = null;
85 transService = new Trans(null);
86
0b0b2b0f 87 List<File> files = new LinkedList<File>();
668268fc
NR
88 for (int index = 0; index < args.length; index++) {
89 String arg = args[index];
bcb54330
NR
90 if (!noMoreParams && arg.equals("--")) {
91 noMoreParams = true;
92 } else if (!noMoreParams && arg.equals("--help")) {
93 System.out
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"
668268fc 98 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
bcb54330
NR
99 + "\t--tui: force pure text mode even if swing treminal is available\n"
100 + "\t--gui: force swing terminal mode\n"
296a0b75
NR
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"
bcb54330
NR
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)");
105 return;
106 } else if (!noMoreParams && arg.equals("--tui")) {
107 textMode = true;
108 } else if (!noMoreParams && arg.equals("--gui")) {
109 textMode = false;
296a0b75
NR
110 } else if (!noMoreParams && arg.equals("--noutf")) {
111 UiColors.getInstance().setUnicode(false);
668268fc
NR
112 } else if (!noMoreParams && arg.equals("--lang")) {
113 index++;
114 if (index < args.length)
115 language = args[index];
116 transService = new Trans(language);
bcb54330
NR
117 } else {
118 filesTried = true;
119 files.addAll(open(arg));
120 }
121 }
122
d66e24cc
NR
123 if (UiColors.getInstance().isUnicode()) {
124 utf8();
125 }
126
bcb54330
NR
127 if (files.size() == 0) {
128 if (filesTried) {
129 System.exit(1);
130 return;
131 }
0b0b2b0f 132
bcb54330
NR
133 files.addAll(open("."));
134 }
135
296a0b75
NR
136 Window win = new MainWindow(new FileList(files));
137
bcb54330 138 try {
296a0b75 139 TuiLauncher.start(textMode, win);
bcb54330
NR
140 } catch (IOException ioe) {
141 ioe.printStackTrace();
142 System.exit(2);
143 }
0b0b2b0f
NR
144 }
145
bcb54330
NR
146 /**
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.
149 *
150 * @param path
151 * the path to open
152 *
153 * @return the list of opened files
154 */
155 static private List<File> open(String path) {
156 List<File> files = new LinkedList<File>();
157
158 File file = new File(path);
159 if (file.exists()) {
160 if (file.isDirectory()) {
161 for (File subfile : file.listFiles()) {
162 if (!subfile.isDirectory())
163 files.add(subfile);
164 }
165 } else {
166 files.add(file);
167 }
168 } else {
169 System.err.println("File or directory not found: \"" + path + "\"");
170 }
171
172 return files;
173 }
174
d66e24cc
NR
175 /**
176 * Really, really ask for UTF-8 encoding.
177 */
178 static private void utf8() {
179 try {
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) {
186 }
187 }
188
0b0b2b0f
NR
189 static private void fullTestTable() throws IOException {
190 final Table<String> table = new Table<String>("Column 1", "Column 2",
191 "Column 3");
192 table.getTableModel().addRow("1", "2", "3");
193 table.setSelectAction(new Runnable() {
194 @Override
195 public void run() {
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));
200 }
201 }
202 });
203
204 Window win = new BasicWindow();
205 win.setComponent(table);
bcb54330 206
0b0b2b0f 207 DefaultTerminalFactory factory = new DefaultTerminalFactory();
bcb54330 208 Terminal terminal = factory.createTerminal();
0b0b2b0f
NR
209
210 Screen screen = new TerminalScreen(terminal);
211 screen.startScreen();
212
213 // Create gui and start gui
214 MultiWindowTextGUI gui = new MultiWindowTextGUI(screen,
215 new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
216 gui.addWindowAndWait(win);
217
218 screen.stopScreen();
219 }
220}