Update lanterna, fix bugs, implement save...
[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.tui.panes.FileList;
9
10 import com.googlecode.lanterna.TextColor;
11 import com.googlecode.lanterna.gui2.BasicWindow;
12 import com.googlecode.lanterna.gui2.DefaultWindowManager;
13 import com.googlecode.lanterna.gui2.EmptySpace;
14 import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
15 import com.googlecode.lanterna.gui2.Window;
16 import com.googlecode.lanterna.gui2.table.Table;
17 import com.googlecode.lanterna.screen.Screen;
18 import com.googlecode.lanterna.screen.TerminalScreen;
19 import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
20 import com.googlecode.lanterna.terminal.Terminal;
21
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 */
30 public class Main {
31 public static final String APPLICATION_TITLE = "jVcard";
32 public static final String APPLICATION_VERSION = "1.0-beta1-dev";
33
34 public static void main(String[] args) {
35 Boolean textMode = null;
36 boolean noMoreParams = false;
37 boolean filesTried = false;
38
39 List<File> files = new LinkedList<File>();
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"
51 + "everyhing else is either a file to open or a directory to open\n"
52 + "(we will only open 1st level files in given directories)");
53 return;
54 } else if (!noMoreParams && arg.equals("--tui")) {
55 textMode = true;
56 } else if (!noMoreParams && arg.equals("--gui")) {
57 textMode = false;
58 } else {
59 filesTried = true;
60 files.addAll(open(arg));
61 }
62 }
63
64 if (files.size() == 0) {
65 if (filesTried) {
66 System.exit(1);
67 return;
68 }
69
70 files.addAll(open("."));
71 }
72
73 try {
74 TuiLauncher.start(textMode, new MainWindow(new FileList(files)));
75 } catch (IOException ioe) {
76 ioe.printStackTrace();
77 System.exit(2);
78 }
79
80 /*
81 * String file = args.length > 0 ? args[0] : null; String file2 =
82 * args.length > 1 ? args[1] : null;
83 *
84 * if (file == null) file =
85 * "/home/niki/workspace/rcard/utils/CVcard/test.vcf"; if (file2 ==
86 * null) file2 = "/home/niki/workspace/rcard/utils/CVcard/test.abook";
87 *
88 * Card card = new Card(new File(file), Format.VCard21);
89 * System.out.println(card.toString());
90 *
91 * System.out.println("\n -- PINE -- \n");
92 *
93 * card = new Card(new File(file2), Format.Abook);
94 * System.out.println(card.toString(Format.Abook));
95 */
96 }
97
98 /**
99 * Open the given path and add all its files if it is a directory or just
100 * this one if not to the returned list.
101 *
102 * @param path
103 * the path to open
104 *
105 * @return the list of opened files
106 */
107 static private List<File> open(String path) {
108 List<File> files = new LinkedList<File>();
109
110 File file = new File(path);
111 if (file.exists()) {
112 if (file.isDirectory()) {
113 for (File subfile : file.listFiles()) {
114 if (!subfile.isDirectory())
115 files.add(subfile);
116 }
117 } else {
118 files.add(file);
119 }
120 } else {
121 System.err.println("File or directory not found: \"" + path + "\"");
122 }
123
124 return files;
125 }
126
127 static private void fullTestTable() throws IOException {
128 final Table<String> table = new Table<String>("Column 1", "Column 2",
129 "Column 3");
130 table.getTableModel().addRow("1", "2", "3");
131 table.setSelectAction(new Runnable() {
132 @Override
133 public void run() {
134 List<String> data = table.getTableModel().getRow(
135 table.getSelectedRow());
136 for (int i = 0; i < data.size(); i++) {
137 System.out.println(data.get(i));
138 }
139 }
140 });
141
142 Window win = new BasicWindow();
143 win.setComponent(table);
144
145 DefaultTerminalFactory factory = new DefaultTerminalFactory();
146 Terminal terminal = factory.createTerminal();
147
148 Screen screen = new TerminalScreen(terminal);
149 screen.startScreen();
150
151 // Create gui and start gui
152 MultiWindowTextGUI gui = new MultiWindowTextGUI(screen,
153 new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
154 gui.addWindowAndWait(win);
155
156 screen.stopScreen();
157 }
158 }