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/system/SystemInterfaceController.java $:
28   */
29  package name.angoca.db2sa.cli.system;
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.core.InterfaceCore;
37  import name.angoca.db2sa.core.ReturnOptions;
38  import name.angoca.db2sa.exceptions.AbstractDB2SAException;
39  
40  /**
41   * This is the interface controller that manage the read and the printer. This
42   * is just a draft, because the functionality is restricted and the interaction
43   * with the user can't be the desired.<br/>
44   * <b>Control Version</b><br />
45   * <ul>
46   * <li>0.0.1 Class creation.</li>
47   * <li>0.1.0 Use of InterfaceCore.</li>
48   * <li>0.1.1 Execution state.</li>
49   * <li>0.1.2 enum.</li>
50   * <li>0.1.3 Name of a state.</li>
51   * <li>0.1.4 final.</li>
52   * <li>1.0.0 Moved to version 1.</li>
53   * </ul>
54   * 
55   * @author Andres Gomez Casanova <a
56   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
57   * @version 1.0.0 2009-07-19
58   */
59  public class SystemInterfaceController extends AbstractInterfaceController {
60      /**
61       * Input processor.
62       */
63      private final InputReader m_input;
64      /**
65       * Screen printer.
66       */
67      private final OutputWriter m_output;
68  
69      /**
70       * Constructor that creates a reader and a printer.
71       * 
72       * @param prompt
73       *            Prompt to show in each line of the console.
74       * @throws InputReaderException
75       *             When there is a problem establishing the input or the output.
76       */
77      public SystemInterfaceController(final String/* ! */prompt)
78              throws InputReaderException {
79          super();
80          this.m_output = new SystemOutputWriter();
81          this.m_input = new SystemInputReader(this.m_output, prompt);
82      }
83  
84      /**
85       * Start to read commands from the user and process them.
86       * 
87       * @throws AbstractDB2SAException
88       *             When there is a IO problem.
89       */
90      @Override
91      public final void start() throws AbstractDB2SAException {
92          ExecutionState execState = ExecutionState.UNKNOWN;
93          this.m_output
94                  .writeLine("Write a command with a '?' to search the options.");
95          while (execState != ExecutionState.APPLICATION_EXIT) {
96              final String phrase = this.m_input.readString();
97  
98              final int index = phrase.indexOf('?');
99              if (index < 0) {
100                 execState = InterfaceCore.executeCommand(phrase, this.m_output);
101             } else {
102                 final String newPhrase = phrase.substring(0, index);
103                 final ReturnOptions actual = InterfaceCore
104                         .analyzePhrase(newPhrase);
105                 // The output can be confusing, but this implementation is just
106                 // a draft.
107                 this.m_output.writeLine(actual.toString());
108             }
109         }
110         // TODO Cerrar todo y finalizar todo ya que es el fin del programa.
111     }
112 }