]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/ConsoleUserAgent.java
Support using a DB baseline for faster program startup.
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / ConsoleUserAgent.java
1 /*******************************************************************************
2  * Copyright (c) 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics;
13
14 import java.io.IOException;
15
16 import org.simantics.databoard.util.binary.InputStreamReadable;
17
18 /**
19  * @author Tuukka Lehtonen
20  */
21 public class ConsoleUserAgent implements PlatformUserAgent {
22
23     @Override
24     public int showPrompt(String title, String message, String[] options, int defaultChoice) {
25         try {
26             boolean ok = false;
27             while (!ok) {
28                 System.out.println("== " + title + " ==\n");
29                 System.out.println(message + "\n");
30                 for (int i = 0; i < options.length; ++i)
31                     System.out.println("[" + i + "] " + options[i]);
32                 System.out.print("Select [");
33                 for (int i = 0; i < options.length; ++i)
34                     System.out.print(i);
35                 System.out.println("] (default = " + defaultChoice + ") ? ");
36
37                 InputStreamReadable rdr  = new InputStreamReadable(System.in, 1);
38                 String str = rdr.readLine();
39                 //System.out.println("READ: '" + str + "'");
40                 try {
41                     int choice = Integer.parseInt(str);
42                     if (choice >= 0 && choice < options.length) {
43                         ok = true;
44                         return choice;
45                     }
46                 } catch (NumberFormatException e) {
47                     // Just repeat the question if input is unintelligible
48                 }
49             }
50         } catch (IOException e) {
51             e.printStackTrace();
52         }
53         return defaultChoice;
54     }
55
56 }