i18n: rework of the system + French translation
[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.util.LinkedList;
6 import java.util.List;
7
8 import be.nikiroo.jvcard.i18n.Trans;
9 import be.nikiroo.jvcard.i18n.Trans.StringId;
10 import be.nikiroo.jvcard.tui.panes.FileList;
11
12 import com.googlecode.lanterna.TextColor;
13 import com.googlecode.lanterna.gui2.BasicWindow;
14 import com.googlecode.lanterna.gui2.DefaultWindowManager;
15 import com.googlecode.lanterna.gui2.EmptySpace;
16 import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
17 import com.googlecode.lanterna.gui2.Window;
18 import com.googlecode.lanterna.gui2.table.Table;
19 import com.googlecode.lanterna.input.KeyStroke;
20 import com.googlecode.lanterna.screen.Screen;
21 import com.googlecode.lanterna.screen.TerminalScreen;
22 import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
23 import com.googlecode.lanterna.terminal.Terminal;
24
25 /**
26 * This class contains the runnable Main method. It will parse the user supplied
27 * parameters and take action based upon those. Most of the time, it will start
28 * a MainWindow.
29 *
30 * @author niki
31 *
32 */
33 public class Main {
34 public static final String APPLICATION_TITLE = "jVcard";
35 public static final String APPLICATION_VERSION = "1.0-beta1-dev";
36
37 static private Trans transService;
38
39 /**
40 * Translate the given {@link StringId}.
41 *
42 * @param id
43 * the ID to translate
44 *
45 * @return the translation
46 */
47 static public String trans(StringId id) {
48
49 if (transService == null)
50 return "";
51
52 return transService.trans(id);
53 }
54
55 /**
56 * Translate the given {@link KeyStroke}.
57 *
58 * @param key
59 * the key to translate
60 *
61 * @return the translation
62 */
63 static public String trans(KeyStroke key) {
64 if (transService == null)
65 return "";
66
67 return transService.trans(key);
68 }
69
70 /**
71 * Start the application.
72 *
73 * @param args
74 * the parameters (see --help to know hich are supported)
75 */
76 public static void main(String[] args) {
77 Boolean textMode = null;
78 boolean noMoreParams = false;
79 boolean filesTried = false;
80
81 // get the "system default" language to help translate the --help
82 // message if needed
83 String language = null;
84 transService = new Trans(null);
85
86 List<File> files = new LinkedList<File>();
87 for (int index = 0; index < args.length; index++) {
88 String arg = args[index];
89 if (!noMoreParams && arg.equals("--")) {
90 noMoreParams = true;
91 } else if (!noMoreParams && arg.equals("--help")) {
92 System.out
93 .println("TODO: implement some help text.\n"
94 + "Usable switches:\n"
95 + "\t--: stop looking for switches\n"
96 + "\t--help: this here thingy\n"
97 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
98 + "\t--tui: force pure text mode even if swing treminal is available\n"
99 + "\t--gui: force swing terminal mode\n"
100 + "\t--noutf: force non-utf8 mode if you need it\n"
101 + "\t--noutfa: force non-utf8 and no accents mode if you need it\n"
102 + "everyhing else is either a file to open or a directory to open\n"
103 + "(we will only open 1st level files in given directories)");
104 return;
105 } else if (!noMoreParams && arg.equals("--tui")) {
106 textMode = true;
107 } else if (!noMoreParams && arg.equals("--gui")) {
108 textMode = false;
109 } else if (!noMoreParams && arg.equals("--noutf")) {
110 UiColors.getInstance().setUnicode(false);
111 } else if (!noMoreParams && arg.equals("--lang")) {
112 index++;
113 if (index < args.length)
114 language = args[index];
115 transService = new Trans(language);
116 } else {
117 filesTried = true;
118 files.addAll(open(arg));
119 }
120 }
121
122 if (files.size() == 0) {
123 if (filesTried) {
124 System.exit(1);
125 return;
126 }
127
128 files.addAll(open("."));
129 }
130
131 Window win = new MainWindow(new FileList(files));
132
133 try {
134 TuiLauncher.start(textMode, win);
135 } catch (IOException ioe) {
136 ioe.printStackTrace();
137 System.exit(2);
138 }
139 }
140
141 /**
142 * Open the given path and add all its files if it is a directory or just
143 * this one if not to the returned list.
144 *
145 * @param path
146 * the path to open
147 *
148 * @return the list of opened files
149 */
150 static private List<File> open(String path) {
151 List<File> files = new LinkedList<File>();
152
153 File file = new File(path);
154 if (file.exists()) {
155 if (file.isDirectory()) {
156 for (File subfile : file.listFiles()) {
157 if (!subfile.isDirectory())
158 files.add(subfile);
159 }
160 } else {
161 files.add(file);
162 }
163 } else {
164 System.err.println("File or directory not found: \"" + path + "\"");
165 }
166
167 return files;
168 }
169
170 static private void fullTestTable() throws IOException {
171 final Table<String> table = new Table<String>("Column 1", "Column 2",
172 "Column 3");
173 table.getTableModel().addRow("1", "2", "3");
174 table.setSelectAction(new Runnable() {
175 @Override
176 public void run() {
177 List<String> data = table.getTableModel().getRow(
178 table.getSelectedRow());
179 for (int i = 0; i < data.size(); i++) {
180 System.out.println(data.get(i));
181 }
182 }
183 });
184
185 Window win = new BasicWindow();
186 win.setComponent(table);
187
188 DefaultTerminalFactory factory = new DefaultTerminalFactory();
189 Terminal terminal = factory.createTerminal();
190
191 Screen screen = new TerminalScreen(terminal);
192 screen.startScreen();
193
194 // Create gui and start gui
195 MultiWindowTextGUI gui = new MultiWindowTextGUI(screen,
196 new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
197 gui.addWindowAndWait(win);
198
199 screen.stopScreen();
200 }
201 }