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