KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > tui > TuiWizard


1 package com.memoire.vainstall.tui;
2
3 import java.io.*;
4 import com.memoire.vainstall.VAGlobals;
5 import com.memoire.vainstall.VAStep;
6 import com.memoire.vainstall.VAWizardInterface;
7
8 public class TuiWizard
9        implements VAWizardInterface // , ActionListener
10
{
11   private static TuiWizard UNIQUE_WIZARD=null;
12
13   private VAStep step_;
14   private int actions_;
15
16   protected TuiWizard()
17   {
18     super();
19     init();
20   }
21
22   // PROTECTED
23

24   protected void init()
25   {
26   }
27
28   public static int key()
29   {
30     int r=0;
31     boolean again=true;
32
33     while(again)
34     {
35       again=false;
36       try
37       {
38     System.in.skip(System.in.available());
39
40     r=System.in.read();
41
42     if(r=='\r') r='\n';
43
44     if(r==27)
45     {
46       if(System.in.available()>0)
47       {
48         r=System.in.read();
49         if((r=='[')&&(System.in.available()>0))
50         {
51           r=System.in.read();
52           if(r=='C') r='\006'; // next
53
if(r=='D') r='\002'; // next
54
}
55       }
56     }
57
58     System.in.skip(System.in.available());
59       }
60       catch(IOException ex) { again=true; }
61     }
62
63     return r;
64   }
65
66   public static void enter()
67   {
68     println("");
69     separator();
70     title();
71     print(VAGlobals.i18n("TuiWizard_Enter"));
72     user();
73
74     while(key()!='\n')
75     {
76       print("\007");
77       if(ansi) print("\033[24;11H\033[K");
78     }
79   }
80
81   public static String JavaDoc input()
82   {
83     String JavaDoc r="";
84
85     try { System.in.skip(System.in.available()); }
86     catch(IOException ex) { }
87
88     while(true)
89     {
90       try
91       {
92     int c=System.in.read();
93
94     if((c=='\n')||(c=='\r')) break;
95
96     if((c==128)||(c==8))
97       r=(r.length()==0 ? "" : r.substring(0,r.length()-1));
98     else
99     if(Character.isLetter((char)c)
100        ||Character.isDigit((char)c)
101        ||(c=='_')||(c=='-')||(c=='.')||(c==':')||(c==File.separatorChar))
102       r+=(char)c;
103     else
104       print("\007");
105
106     if(ansi) print("\033[24;3H\033[K"+r);
107       }
108       catch(IOException ex) { }
109     }
110
111     return r;
112   }
113
114   // PRINT
115

116   public static boolean ansi=(System.getProperty("ANSI")!=null);
117   public static boolean skip=(System.getProperty("SKIP")!=null);
118
119   public static void print(String JavaDoc _s)
120   {
121     String JavaDoc s;
122
123     if(!ansi)
124     {
125       boolean ok=true;
126
127       s="";
128       for(int i=0;i<_s.length();i++)
129       {
130     char c=_s.charAt(i);
131     if(c=='\033') ok=false;
132     if(ok) s+=c;
133     else if(Character.isLetterOrDigit(c)||
134              (c==File.separatorChar)||
135              (c==':')||
136              (c=='-')) ok=true;
137       }
138     }
139     else s=_s;
140
141     System.out.print(s);
142     System.out.flush();
143   }
144
145   public static void clear()
146   {
147     if(ansi) print("\033[34m\033[2J\r");
148     else println("");
149   }
150
151   public static void normal()
152   {
153     if(ansi) print("\033[34m");
154   }
155
156   public static void info()
157   {
158     if(ansi) print("\033[31m");
159   }
160
161   public static void title()
162   {
163     if(ansi) print("\033[35m");
164   }
165
166   public static void user()
167   {
168     print("> ");
169     if(ansi) print("\033[00m");
170   }
171
172   public static void println(String JavaDoc _s)
173   {
174     print(_s+System.getProperty("line.separator"));
175   }
176
177   public static void separator()
178   {
179     if(ansi) print("\033[33m\033[K");
180     print("................................................................................");
181     if(ansi) println("\033[34m");
182     else println("");
183   }
184
185   // PUBLIC
186

187   public static VAWizardInterface createWizard()
188   {
189     if(UNIQUE_WIZARD==null) UNIQUE_WIZARD=new TuiWizard();
190     else UNIQUE_WIZARD.init();
191     return UNIQUE_WIZARD;
192   }
193   
194   public void setActionEnabled(int _actions)
195   {
196     actions_=_actions;
197   }
198
199   public void setStep(VAStep _step)
200   {
201     step_=_step;
202   }
203
204   public void show()
205   {
206     separator();
207
208     String JavaDoc s="";
209
210     if((actions_&FINISH)!=FINISH) s += VAGlobals.i18n("TuiWizard_Again");
211
212     if((actions_&CANCEL)==CANCEL) s += VAGlobals.i18n("TuiWizard_Cancel");
213     if((actions_&BACK )==BACK ) s += VAGlobals.i18n("TuiWizard_Back");
214     if((actions_&NEXT )==NEXT ) s += VAGlobals.i18n("TuiWizard_Next");
215     if((actions_&FINISH)==FINISH) s += VAGlobals.i18n("TuiWizard_Finish");
216
217     title();
218     print(s);
219     user();
220
221     while(true)
222     {
223       int c=key();
224       c=Character.toLowerCase((char)c);
225
226       // System.out.println("["+c+"]");
227

228       if(((actions_&CANCEL)==CANCEL)&&((c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyCancel"))||(c==27)))
229       {
230     step_.cancelAction();
231     break;
232       }
233       else
234       if(((actions_&BACK)==BACK)&&((c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyBack"))||(c=='\002')))
235       {
236     step_.backAction();
237     break;
238       }
239       else
240       if(((actions_&NEXT)==NEXT)&&((c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyNext"))||(c=='\006')))
241       {
242     step_.nextAction();
243     break;
244       }
245       else
246       if(((actions_&FINISH)==FINISH)&&((c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyFinish"))||(c=='\006')))
247       {
248     step_.nextAction();
249     break;
250       }
251       else
252       if(((actions_&FINISH)!=FINISH)&&(c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyAgain")))
253       {
254     ((TuiDefaultStep)step_).againAction();
255     break;
256       }
257       else
258       {
259     print("\007");
260     if(ansi) print("\033[24;"+(s.length()+3)+"H\033[K");
261       }
262     }
263   }
264   
265   public void dispose(boolean _confirm)
266   {
267     if(_confirm)
268     {
269       println(VAGlobals.i18n("TuiWizard_WantAbort"));
270       confirm();
271     }
272   }
273
274   public static boolean confirm()
275   {
276     title();
277     print(VAGlobals.i18n("TuiWizard_YesNo"));
278     user();
279     
280     while(true)
281     {
282       int c=key();
283       c=Character.toLowerCase((char)c);
284
285       if(c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyYes")) {
286         println("");
287         return true;
288       }
289       if(c == VAGlobals.getResourceInt("com.memoire.vainstall.tui.Language","TuiWizard_KeyNo")) {
290         println("");
291         return false;
292       }
293
294       print("\007");
295       if(ansi) print("\033[24;14H\033[K");
296     }
297   }
298
299   public static void error(String JavaDoc _s)
300   {
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++) TuiWizard.println("");
309     TuiWizard.enter();
310   }
311 }
312
Popular Tags