View Javadoc

1   /*
2    * db2sa: DB2 Syntax Assistant
3    * Copyright (C) Andres Gomez Casanova
4    *
5    * This file is part of db2sa.
6    *
7    * db2sa is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * db2sa is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2009-06-29 23:12:30 +0200 (Mon, 29 Jun 2009) $:
26   * Revision: $LastChangedRevision: 258 $:
27   * URL:      $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/cli/jline/JlineInputReader.java $:
28   */
29  package name.angoca.db2sa.cli.jline;
30  
31  import java.io.IOException;
32  
33  import jline.ConsoleReader;
34  import name.angoca.db2sa.cli.InputReader;
35  import name.angoca.db2sa.cli.exceptions.InputReaderException;
36  
37  /**
38   * Implementation of the input reader using JLine.<br/>
39   * <b>Control Version</b><br />
40   * <ul>
41   * <li>0.0.1 Class creation.</li>
42   * <li>0.0.2 'this' to access attributes.</li>
43   * <li>0.0.3 No override.</li>
44   * <li>0.0.4 final.</li>
45   * <li>1.0.0 Moved to version 1.</li>
46   * </ul>
47   * 
48   * @author Andres Gomez Casanova <a
49   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
50   * @version 1.0.0 2009-07-19
51   */
52  public final class JlineInputReader implements InputReader {
53      /**
54       * JLine console reader.
55       */
56      private final transient ConsoleReader m_reader;
57      /**
58       * Prompt to show in the console.
59       */
60      private final transient String m_prompt;
61  
62      /**
63       * Constructor that flushes the stream.
64       * 
65       * @param prompt
66       *            Prompt to show in each line of the console.
67       * @throws InputReaderException
68       *             If there is a problem flushing the stream.
69       */
70      public JlineInputReader(final String/* ! */prompt)
71              throws InputReaderException {
72          this.m_prompt = prompt;
73          try {
74              this.m_reader = new ConsoleReader();
75  
76              // Normally, each time a tab is pressed, a beep sounds. It blocks
77              // that.
78              this.m_reader.setBellEnabled(false);
79  
80              this.m_reader.addCompletor(new DB2Completor());
81          } catch (IOException exception) {
82              throw new InputReaderException(exception);
83          }
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see name.angoca.db2sa.cli.InputReader#readString()
90       */
91      public String/* ! */readString() throws InputReaderException {
92          try {
93              return this.m_reader.readLine(this.m_prompt);
94          } catch (final IOException exception) {
95              throw new InputReaderException(exception);
96          }
97      }
98  }