Fix --noutf, fix onAction being called to many times, lot of small fixes
[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;
5import java.util.LinkedList;
6import java.util.List;
7
0b0b2b0f
NR
8import be.nikiroo.jvcard.tui.panes.FileList;
9
0b0b2b0f
NR
10import com.googlecode.lanterna.TextColor;
11import com.googlecode.lanterna.gui2.BasicWindow;
0b0b2b0f
NR
12import com.googlecode.lanterna.gui2.DefaultWindowManager;
13import com.googlecode.lanterna.gui2.EmptySpace;
0b0b2b0f 14import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
0b0b2b0f
NR
15import com.googlecode.lanterna.gui2.Window;
16import com.googlecode.lanterna.gui2.table.Table;
17import com.googlecode.lanterna.screen.Screen;
18import com.googlecode.lanterna.screen.TerminalScreen;
19import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
20import com.googlecode.lanterna.terminal.Terminal;
21
bcb54330
NR
22/**
23 * This class contains the runnable Main method. It will parse the user supplied
24 * parameters and take action based upon those. Most of the time, it will start
25 * a MainWindow.
26 *
27 * @author niki
28 *
29 */
0b0b2b0f
NR
30public class Main {
31 public static final String APPLICATION_TITLE = "jVcard";
bcb54330 32 public static final String APPLICATION_VERSION = "1.0-beta1-dev";
0b0b2b0f 33
bcb54330 34 public static void main(String[] args) {
0b0b2b0f 35 Boolean textMode = null;
bcb54330
NR
36 boolean noMoreParams = false;
37 boolean filesTried = false;
0b0b2b0f 38
0b0b2b0f 39 List<File> files = new LinkedList<File>();
bcb54330
NR
40 for (String arg : args) {
41 if (!noMoreParams && arg.equals("--")) {
42 noMoreParams = true;
43 } else if (!noMoreParams && arg.equals("--help")) {
44 System.out
45 .println("TODO: implement some help text.\n"
46 + "Usable switches:\n"
47 + "\t--: stop looking for switches\n"
48 + "\t--help: this here thingy\n"
49 + "\t--tui: force pure text mode even if swing treminal is available\n"
50 + "\t--gui: force swing terminal mode\n"
296a0b75
NR
51 + "\t--noutf: force non-utf8 mode if you need it\n"
52 + "\t--noutfa: force non-utf8 and no accents mode if you need it\n"
bcb54330
NR
53 + "everyhing else is either a file to open or a directory to open\n"
54 + "(we will only open 1st level files in given directories)");
55 return;
56 } else if (!noMoreParams && arg.equals("--tui")) {
57 textMode = true;
58 } else if (!noMoreParams && arg.equals("--gui")) {
59 textMode = false;
296a0b75
NR
60 } else if (!noMoreParams && arg.equals("--noutf")) {
61 UiColors.getInstance().setUnicode(false);
bcb54330
NR
62 } else {
63 filesTried = true;
64 files.addAll(open(arg));
65 }
66 }
67
68 if (files.size() == 0) {
69 if (filesTried) {
70 System.exit(1);
71 return;
72 }
0b0b2b0f 73
bcb54330
NR
74 files.addAll(open("."));
75 }
76
296a0b75
NR
77 Window win = new MainWindow(new FileList(files));
78
bcb54330 79 try {
296a0b75 80 TuiLauncher.start(textMode, win);
bcb54330
NR
81 } catch (IOException ioe) {
82 ioe.printStackTrace();
83 System.exit(2);
84 }
0b0b2b0f
NR
85
86 /*
87 * String file = args.length > 0 ? args[0] : null; String file2 =
88 * args.length > 1 ? args[1] : null;
89 *
90 * if (file == null) file =
91 * "/home/niki/workspace/rcard/utils/CVcard/test.vcf"; if (file2 ==
92 * null) file2 = "/home/niki/workspace/rcard/utils/CVcard/test.abook";
93 *
94 * Card card = new Card(new File(file), Format.VCard21);
95 * System.out.println(card.toString());
96 *
97 * System.out.println("\n -- PINE -- \n");
98 *
99 * card = new Card(new File(file2), Format.Abook);
100 * System.out.println(card.toString(Format.Abook));
101 */
102 }
103
bcb54330
NR
104 /**
105 * Open the given path and add all its files if it is a directory or just
106 * this one if not to the returned list.
107 *
108 * @param path
109 * the path to open
110 *
111 * @return the list of opened files
112 */
113 static private List<File> open(String path) {
114 List<File> files = new LinkedList<File>();
115
116 File file = new File(path);
117 if (file.exists()) {
118 if (file.isDirectory()) {
119 for (File subfile : file.listFiles()) {
120 if (!subfile.isDirectory())
121 files.add(subfile);
122 }
123 } else {
124 files.add(file);
125 }
126 } else {
127 System.err.println("File or directory not found: \"" + path + "\"");
128 }
129
130 return files;
131 }
132
0b0b2b0f
NR
133 static private void fullTestTable() throws IOException {
134 final Table<String> table = new Table<String>("Column 1", "Column 2",
135 "Column 3");
136 table.getTableModel().addRow("1", "2", "3");
137 table.setSelectAction(new Runnable() {
138 @Override
139 public void run() {
140 List<String> data = table.getTableModel().getRow(
141 table.getSelectedRow());
142 for (int i = 0; i < data.size(); i++) {
143 System.out.println(data.get(i));
144 }
145 }
146 });
147
148 Window win = new BasicWindow();
149 win.setComponent(table);
bcb54330 150
0b0b2b0f 151 DefaultTerminalFactory factory = new DefaultTerminalFactory();
bcb54330 152 Terminal terminal = factory.createTerminal();
0b0b2b0f
NR
153
154 Screen screen = new TerminalScreen(terminal);
155 screen.startScreen();
156
157 // Create gui and start gui
158 MultiWindowTextGUI gui = new MultiWindowTextGUI(screen,
159 new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
160 gui.addWindowAndWait(win);
161
162 screen.stopScreen();
163 }
164}