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-07-08 19:56:43 +0200 (Wed, 08 Jul 2009) $:
26   * Revision: $LastChangedRevision: 357 $:
27   * URL:      $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/cli/jline/JlineInterfaceController.java $:
28   */
29  package name.angoca.db2sa.cli.jline;
30  
31  import name.angoca.db2sa.ExecutionState;
32  import name.angoca.db2sa.cli.AbstractInterfaceController;
33  import name.angoca.db2sa.cli.InputReader;
34  import name.angoca.db2sa.cli.OutputWriter;
35  import name.angoca.db2sa.cli.exceptions.InputReaderException;
36  import name.angoca.db2sa.cli.system.SystemOutputWriter;
37  import name.angoca.db2sa.core.InterfaceCore;
38  import name.angoca.db2sa.exceptions.AbstractDB2SAException;
39  import name.angoca.db2sa.messages.Messages;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * This is the interface controller that manage the read and the printer.<br/>
46   * <b>Control Version</b><br />
47   * <ul>
48   * <li>0.0.1 Class creation.</li>
49   * <li>0.0.2 Annotations.</li>
50   * <li>0.0.3 Execution state.</li>
51   * <li>0.0.4 Enum.</li>
52   * <li>0.0.5 Read string just once.</li>
53   * <li>0.0.6 Name of a state.</li>
54   * <li>1.0.0 Moved to version 1.</li>
55   * </ul>
56   * 
57   * @author Andres Gomez Casanova <a
58   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
59   * @version 1.0.0 2009-07-19
60   */
61  public class JlineInterfaceController extends AbstractInterfaceController {
62  
63      /**
64       * Logger.
65       */
66      private static final Logger LOGGER = LoggerFactory
67              .getLogger(JlineInterfaceController.class);
68  
69      /**
70       * Input processor.
71       */
72      private final InputReader m_input;
73      /**
74       * Screen printer.
75       */
76      private final OutputWriter m_output;
77  
78      /**
79       * Constructor that creates a reader and a printer.
80       * 
81       * @param prompt
82       *            Prompt to show in each line of the console.@throws
83       *            InputReaderException When there is a problem establishing the
84       *            input or the output.
85       * @throws InputReaderException
86       *             When there is a problem establishing the input or the output.
87       */
88      public JlineInterfaceController(final String/* ! */prompt)
89              throws InputReaderException {
90          super();
91          this.m_output = new SystemOutputWriter();
92          this.m_input = new JlineInputReader(prompt);
93      }
94  
95      /**
96       * Start to read commands from the user and process them.
97       * 
98       * @throws AbstractDB2SAException
99       *             When there is a IO problem.
100      */
101     @Override
102     public final void start() throws AbstractDB2SAException {
103         String phrase = null;
104 
105         // Shows a introductory help.
106         // TODO optional from configuration file
107         // TODO configure the assitant call key desde el archivo de
108         // configuración
109         this.m_output.writeLine(Messages.getString("JlineInterfaceController"//$NON-NLS-1$
110                 + ".introduction")); //$NON-NLS-1$
111 
112         // TODO Change the name of the constant unknown
113         ExecutionState execState = ExecutionState.UNKNOWN;
114 
115         JlineInterfaceController.LOGGER.debug("User interaction start"); //$NON-NLS-1$
116 
117         while (execState != ExecutionState.APPLICATION_EXIT) {
118             phrase = this.m_input.readString();
119             execState = InterfaceCore.executeCommand(phrase, this.m_output);
120         }
121         // TODO Cerrar todo y finalizar todo ya que es el fin del programa.
122     }
123 }