1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
63: public class ConsoleCallbackHandler extends AbstractCallbackHandler
64: {
65:
66:
67:
68:
69: private final PrintStream out;
70:
71:
72:
73:
74: public ConsoleCallbackHandler()
75: {
76: this (System.out);
77: }
78:
79: public ConsoleCallbackHandler (final PrintStream out)
80: {
81: super ("CONSOLE");
82: this.out = out;
83: }
84:
85:
86:
87:
88: protected void handleChoice(ChoiceCallback c) throws IOException
89: {
90: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
91: out.println(c.getPrompt());
92: out.print('(');
93: String[] choices = c.getChoices();
94: for (int i = 0; i < choices.length; i++)
95: {
96: out.print(choices[i]);
97: if (i != choices.length - 1)
98: out.print(", ");
99: }
100: out.print(") ");
101: if (c.getDefaultChoice() >= 0 && c.getDefaultChoice() < choices.length)
102: {
103: out.print('[');
104: out.print(choices[c.getDefaultChoice()]);
105: out.print("] ");
106: }
107: String reply = in.readLine();
108: if (reply == null || reply.length() == 0)
109: {
110: c.setSelectedIndex(c.getDefaultChoice());
111: return;
112: }
113: if (!c.allowMultipleSelections())
114: {
115: for (int i = 0; i < choices.length; i++)
116: {
117: if (reply.trim().equals(choices[i]))
118: {
119: c.setSelectedIndex(i);
120: return;
121: }
122: }
123: c.setSelectedIndex(c.getDefaultChoice());
124: }
125: else
126: {
127: TreeSet indices = new TreeSet();
128: StringTokenizer tok = new StringTokenizer(reply, ",");
129: String[] replies = new String[tok.countTokens()];
130: int idx = 0;
131: while (tok.hasMoreTokens())
132: {
133: replies[idx++] = tok.nextToken().trim();
134: }
135: for (int i = 0; i < choices.length; i++)
136: for (int j = 0; j < replies.length; i++)
137: {
138: if (choices[i].equals(replies[j]))
139: {
140: indices.add(Integer.valueOf(i));
141: }
142: }
143: if (indices.size() == 0)
144: c.setSelectedIndex(c.getDefaultChoice());
145: else
146: {
147: int[] ii = new int[indices.size()];
148: int i = 0;
149: for (Iterator it = indices.iterator(); it.hasNext(); )
150: ii[i++] = ((Integer) it.next()).intValue();
151: c.setSelectedIndexes(ii);
152: }
153: }
154: }
155:
156: protected void handleConfirmation(ConfirmationCallback c) throws IOException
157: {
158: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
159: if (c.getPrompt() != null)
160: out.print(c.getPrompt());
161:
162: String[] choices = null;
163: int[] values = null;
164: switch (c.getOptionType())
165: {
166: case ConfirmationCallback.OK_CANCEL_OPTION:
167: out.print(messages.getString("callback.okCancel"));
168: choices = new String[] {
169: messages.getString("callback.ok"),
170: messages.getString("callback.cancel"),
171: messages.getString("callback.shortOk"),
172: messages.getString("callback.shortCancel")
173: };
174: values = new int[] {
175: ConfirmationCallback.OK, ConfirmationCallback.CANCEL,
176: ConfirmationCallback.OK, ConfirmationCallback.CANCEL
177: };
178: break;
179:
180: case ConfirmationCallback.YES_NO_CANCEL_OPTION:
181: out.print(messages.getString("callback.yesNoCancel"));
182: choices = new String[] {
183: messages.getString("callback.yes"),
184: messages.getString("callback.no"),
185: messages.getString("callback.cancel"),
186: messages.getString("callback.shortYes"),
187: messages.getString("callback.shortNo"),
188: messages.getString("callback.shortCancel")
189: };
190: values = new int[] {
191: ConfirmationCallback.YES, ConfirmationCallback.NO,
192: ConfirmationCallback.CANCEL, ConfirmationCallback.YES,
193: ConfirmationCallback.NO, ConfirmationCallback.CANCEL
194: };
195: break;
196:
197: case ConfirmationCallback.YES_NO_OPTION:
198: out.print(messages.getString("callback.yesNo"));
199: choices = new String[] { messages.getString("callback.yes"),
200: messages.getString("callback.no"),
201: messages.getString("callback.shortYes"),
202: messages.getString("callback.shortNo") };
203: values = new int[] { ConfirmationCallback.YES,
204: ConfirmationCallback.NO,
205: ConfirmationCallback.YES,
206: ConfirmationCallback.NO };
207: int defaultOption = c.getDefaultOption();
208: if (defaultOption > -1 && defaultOption < choices.length)
209: {
210: out.print("[");
211: out.print(choices[defaultOption]);
212: out.print("] ");
213: }
214: break;
215:
216: case ConfirmationCallback.UNSPECIFIED_OPTION:
217: choices = c.getOptions();
218: values = new int[choices.length];
219: for (int i = 0; i < values.length; i++)
220: values[i] = i;
221: out.print('(');
222: for (int i = 0; i < choices.length; i++)
223: {
224: out.print(choices[i]);
225: if (i != choices.length - 1)
226: out.print(", ");
227: }
228: out.print(") [");
229: out.print(choices[c.getDefaultOption()]);
230: out.print("] ");
231: break;
232:
233: default:
234: throw new IllegalArgumentException();
235: }
236: String reply = in.readLine();
237: if (reply == null)
238: {
239: c.setSelectedIndex(c.getDefaultOption());
240: return;
241: }
242: reply = reply.trim();
243: for (int i = 0; i < choices.length; i++)
244: if (reply.equalsIgnoreCase(choices[i]))
245: {
246: c.setSelectedIndex(values[i]);
247: return;
248: }
249: c.setSelectedIndex(c.getDefaultOption());
250: }
251:
252: protected void handleLanguage(LanguageCallback c) throws IOException
253: {
254: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
255: out.print(messages.getString("callback.language"));
256: String reply = null;
257: reply = in.readLine();
258: if (reply == null)
259: {
260: c.setLocale(Locale.getDefault());
261: }
262: else
263: {
264: c.setLocale(new Locale(reply.trim()));
265: }
266: }
267:
268: protected void handleName(NameCallback c) throws IOException
269: {
270: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
271: out.print(c.getPrompt());
272: String name = in.readLine();
273: if (name != null)
274: c.setName(name.trim());
275: }
276:
277: protected void handlePassword(PasswordCallback c) throws IOException
278: {
279: out.print(c.getPrompt());
280: BufferedReader in =
281: new BufferedReader(new InputStreamReader(System.in));
282: String pass = in.readLine();
283: c.setPassword(pass.toCharArray());
284: }
285:
286: protected void handleTextInput(TextInputCallback c) throws IOException
287: {
288: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
289: out.print(c.getPrompt());
290: String text = in.readLine();
291: if (text != null)
292: c.setText(text);
293: }
294:
295: protected void handleTextOutput(TextOutputCallback c)
296: {
297: out.print(c.getMessage());
298: }
299: }