001: package com.memoire.vainstall.tui;
002:
003: import java.io.*;
004: import com.memoire.vainstall.VAGlobals;
005: import com.memoire.vainstall.VAStep;
006: import com.memoire.vainstall.VAWizardInterface;
007:
008: public class TuiWizard implements VAWizardInterface // , ActionListener
009: {
010: private static TuiWizard UNIQUE_WIZARD = null;
011:
012: private VAStep step_;
013: private int actions_;
014:
015: protected TuiWizard() {
016: super ();
017: init();
018: }
019:
020: // PROTECTED
021:
022: protected void init() {
023: }
024:
025: public static int key() {
026: int r = 0;
027: boolean again = true;
028:
029: while (again) {
030: again = false;
031: try {
032: System.in.skip(System.in.available());
033:
034: r = System.in.read();
035:
036: if (r == '\r')
037: r = '\n';
038:
039: if (r == 27) {
040: if (System.in.available() > 0) {
041: r = System.in.read();
042: if ((r == '[') && (System.in.available() > 0)) {
043: r = System.in.read();
044: if (r == 'C')
045: r = '\006'; // next
046: if (r == 'D')
047: r = '\002'; // next
048: }
049: }
050: }
051:
052: System.in.skip(System.in.available());
053: } catch (IOException ex) {
054: again = true;
055: }
056: }
057:
058: return r;
059: }
060:
061: public static void enter() {
062: println("");
063: separator();
064: title();
065: print(VAGlobals.i18n("TuiWizard_Enter"));
066: user();
067:
068: while (key() != '\n') {
069: print("\007");
070: if (ansi)
071: print("\033[24;11H\033[K");
072: }
073: }
074:
075: public static String input() {
076: String r = "";
077:
078: try {
079: System.in.skip(System.in.available());
080: } catch (IOException ex) {
081: }
082:
083: while (true) {
084: try {
085: int c = System.in.read();
086:
087: if ((c == '\n') || (c == '\r'))
088: break;
089:
090: if ((c == 128) || (c == 8))
091: r = (r.length() == 0 ? "" : r.substring(0, r
092: .length() - 1));
093: else if (Character.isLetter((char) c)
094: || Character.isDigit((char) c) || (c == '_')
095: || (c == '-') || (c == '.') || (c == ':')
096: || (c == File.separatorChar))
097: r += (char) c;
098: else
099: print("\007");
100:
101: if (ansi)
102: print("\033[24;3H\033[K" + r);
103: } catch (IOException ex) {
104: }
105: }
106:
107: return r;
108: }
109:
110: // PRINT
111:
112: public static boolean ansi = (System.getProperty("ANSI") != null);
113: public static boolean skip = (System.getProperty("SKIP") != null);
114:
115: public static void print(String _s) {
116: String s;
117:
118: if (!ansi) {
119: boolean ok = true;
120:
121: s = "";
122: for (int i = 0; i < _s.length(); i++) {
123: char c = _s.charAt(i);
124: if (c == '\033')
125: ok = false;
126: if (ok)
127: s += c;
128: else if (Character.isLetterOrDigit(c)
129: || (c == File.separatorChar) || (c == ':')
130: || (c == '-'))
131: ok = true;
132: }
133: } else
134: s = _s;
135:
136: System.out.print(s);
137: System.out.flush();
138: }
139:
140: public static void clear() {
141: if (ansi)
142: print("\033[34m\033[2J\r");
143: else
144: println("");
145: }
146:
147: public static void normal() {
148: if (ansi)
149: print("\033[34m");
150: }
151:
152: public static void info() {
153: if (ansi)
154: print("\033[31m");
155: }
156:
157: public static void title() {
158: if (ansi)
159: print("\033[35m");
160: }
161:
162: public static void user() {
163: print("> ");
164: if (ansi)
165: print("\033[00m");
166: }
167:
168: public static void println(String _s) {
169: print(_s + System.getProperty("line.separator"));
170: }
171:
172: public static void separator() {
173: if (ansi)
174: print("\033[33m\033[K");
175: print("................................................................................");
176: if (ansi)
177: println("\033[34m");
178: else
179: println("");
180: }
181:
182: // PUBLIC
183:
184: public static VAWizardInterface createWizard() {
185: if (UNIQUE_WIZARD == null)
186: UNIQUE_WIZARD = new TuiWizard();
187: else
188: UNIQUE_WIZARD.init();
189: return UNIQUE_WIZARD;
190: }
191:
192: public void setActionEnabled(int _actions) {
193: actions_ = _actions;
194: }
195:
196: public void setStep(VAStep _step) {
197: step_ = _step;
198: }
199:
200: public void show() {
201: separator();
202:
203: String s = "";
204:
205: if ((actions_ & FINISH) != FINISH)
206: s += VAGlobals.i18n("TuiWizard_Again");
207:
208: if ((actions_ & CANCEL) == CANCEL)
209: s += VAGlobals.i18n("TuiWizard_Cancel");
210: if ((actions_ & BACK) == BACK)
211: s += VAGlobals.i18n("TuiWizard_Back");
212: if ((actions_ & NEXT) == NEXT)
213: s += VAGlobals.i18n("TuiWizard_Next");
214: if ((actions_ & FINISH) == FINISH)
215: s += VAGlobals.i18n("TuiWizard_Finish");
216:
217: title();
218: print(s);
219: user();
220:
221: while (true) {
222: int c = key();
223: c = Character.toLowerCase((char) c);
224:
225: // System.out.println("["+c+"]");
226:
227: if (((actions_ & CANCEL) == CANCEL)
228: && ((c == VAGlobals.getResourceInt(
229: "com.memoire.vainstall.tui.Language",
230: "TuiWizard_KeyCancel")) || (c == 27))) {
231: step_.cancelAction();
232: break;
233: } else if (((actions_ & BACK) == BACK)
234: && ((c == VAGlobals.getResourceInt(
235: "com.memoire.vainstall.tui.Language",
236: "TuiWizard_KeyBack")) || (c == '\002'))) {
237: step_.backAction();
238: break;
239: } else if (((actions_ & NEXT) == NEXT)
240: && ((c == VAGlobals.getResourceInt(
241: "com.memoire.vainstall.tui.Language",
242: "TuiWizard_KeyNext")) || (c == '\006'))) {
243: step_.nextAction();
244: break;
245: } else if (((actions_ & FINISH) == FINISH)
246: && ((c == VAGlobals.getResourceInt(
247: "com.memoire.vainstall.tui.Language",
248: "TuiWizard_KeyFinish")) || (c == '\006'))) {
249: step_.nextAction();
250: break;
251: } else if (((actions_ & FINISH) != FINISH)
252: && (c == VAGlobals.getResourceInt(
253: "com.memoire.vainstall.tui.Language",
254: "TuiWizard_KeyAgain"))) {
255: ((TuiDefaultStep) step_).againAction();
256: break;
257: } else {
258: print("\007");
259: if (ansi)
260: print("\033[24;" + (s.length() + 3) + "H\033[K");
261: }
262: }
263: }
264:
265: public void dispose(boolean _confirm) {
266: if (_confirm) {
267: println(VAGlobals.i18n("TuiWizard_WantAbort"));
268: confirm();
269: }
270: }
271:
272: public static boolean confirm() {
273: title();
274: print(VAGlobals.i18n("TuiWizard_YesNo"));
275: user();
276:
277: while (true) {
278: int c = key();
279: c = Character.toLowerCase((char) c);
280:
281: if (c == VAGlobals.getResourceInt(
282: "com.memoire.vainstall.tui.Language",
283: "TuiWizard_KeyYes")) {
284: println("");
285: return true;
286: }
287: if (c == VAGlobals.getResourceInt(
288: "com.memoire.vainstall.tui.Language",
289: "TuiWizard_KeyNo")) {
290: println("");
291: return false;
292: }
293:
294: print("\007");
295: if (ansi)
296: print("\033[24;14H\033[K");
297: }
298: }
299:
300: public static void error(String _s) {
301: TuiWizard.clear();
302: TuiWizard.title();
303: TuiWizard.println(VAGlobals.i18n("TuiWizard_Error"));
304: TuiWizard.separator();
305: TuiWizard.println("");
306: TuiWizard.println(_s);
307: TuiWizard.println("");
308: for (int i = 6; i <= 21; i++)
309: TuiWizard.println("");
310: TuiWizard.enter();
311: }
312: }
|