Fix UTF8 bug, create first executable JAR 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.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;
26
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 */
35 public class Main {
36 public static final String APPLICATION_TITLE = "jVcard";
37 public static final String APPLICATION_VERSION = "1.0-beta2-dev";
38
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) {
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 */
77 public static void main(String[] args) {
78 Boolean textMode = null;
79 boolean noMoreParams = false;
80 boolean filesTried = false;
81
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
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("--")) {
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"
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)");
105 return;
106 } else if (!noMoreParams && arg.equals("--tui")) {
107 textMode = true;
108 } else if (!noMoreParams && arg.equals("--gui")) {
109 textMode = false;
110 } else if (!noMoreParams && arg.equals("--noutf")) {
111 UiColors.getInstance().setUnicode(false);
112 } else if (!noMoreParams && arg.equals("--lang")) {
113 index++;
114 if (index < args.length)
115 language = args[index];
116 transService = new Trans(language);
117 } else {
118 filesTried = true;
119 files.addAll(open(arg));
120 }
121 }
122
123 if (UiColors.getInstance().isUnicode()) {
124 utf8();
125 }
126
127 if (files.size() == 0) {
128 if (filesTried) {
129 System.exit(1);
130 return;
131 }
132
133 files.addAll(open("."));
134 }
135
136 Window win = new MainWindow(new FileList(files));
137
138 try {
139 TuiLauncher.start(textMode, win);
140 } catch (IOException ioe) {
141 ioe.printStackTrace();
142 System.exit(2);
143 }
144 }
145
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
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
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);
206
207 DefaultTerminalFactory factory = new DefaultTerminalFactory();
208 Terminal terminal = factory.createTerminal();
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 }