| 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 | + "\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" |
| 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; |
| 60 | } else if (!noMoreParams && arg.equals("--noutf")) { |
| 61 | UiColors.getInstance().setUnicode(false); |
| 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 | } |
| 73 | |
| 74 | files.addAll(open(".")); |
| 75 | } |
| 76 | |
| 77 | Window win = new MainWindow(new FileList(files)); |
| 78 | |
| 79 | try { |
| 80 | TuiLauncher.start(textMode, win); |
| 81 | } catch (IOException ioe) { |
| 82 | ioe.printStackTrace(); |
| 83 | System.exit(2); |
| 84 | } |
| 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 | |
| 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 | |
| 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); |
| 150 | |
| 151 | DefaultTerminalFactory factory = new DefaultTerminalFactory(); |
| 152 | Terminal terminal = factory.createTerminal(); |
| 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 | } |