View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan 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   * Zemucan 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: 2011-03-06 23:10:20 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1917 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/uiImplSystem/src/main/java/name/angoca/zemucan/ui/impl/system/ImplementationSystemInterfaceController.java $:
28   */
29  package name.angoca.zemucan.ui.impl.system;
30  
31  import name.angoca.zemucan.AbstractZemucanException;
32  import name.angoca.zemucan.core.graph.model.AboutNode;
33  import name.angoca.zemucan.executer.api.ExecutionState;
34  import name.angoca.zemucan.executer.impl.ImplementationExecuter;
35  import name.angoca.zemucan.interfaze.InterfaceCore;
36  import name.angoca.zemucan.interfaze.model.ReturnOptions;
37  import name.angoca.zemucan.tools.configurator.Configurator;
38  import name.angoca.zemucan.tools.messages.system.Messages;
39  import name.angoca.zemucan.ui.api.AbstractInterfaceController;
40  import name.angoca.zemucan.ui.api.InputReader;
41  import name.angoca.zemucan.ui.api.InputReaderException;
42  import name.angoca.zemucan.ui.api.OutputWriter;
43  import name.angoca.zemucan.ui.api.SynchronizedLoad;
44  
45  /**
46   * Interface controller that manage the reader and the printer. This is just a
47   * draft, because the functionality is restricted and the interaction with the
48   * user is not the desired.
49   * <p>
50   * <b>Control Version</b>
51   * <p>
52   * <ul>
53   * <li>0.0.1 Class creation.</li>
54   * <li>0.1.0 Use of InterfaceCore.</li>
55   * <li>0.1.1 Execution state.</li>
56   * <li>0.1.2 enum.</li>
57   * <li>0.1.3 Name of a state.</li>
58   * <li>0.1.4 final.</li>
59   * <li>1.0.0 Moved to version 1.</li>
60   * <li>1.0.1 Show intro.</li>
61   * <li>1.0.2 Show intro.</li>
62   * <li>1.0.3 Strings externalized.</li>
63   * <li>1.1.0 final.</li>
64   * <li>1.1.1 Assert.</li>
65   * <li>1.1.2 Executer not in interface.</li>
66   * <li>1.2.0 Load synchronization.</li>
67   * <li>1.2.1 Synchronization optional.</li>
68   * <li>1.2.2 Show license.</li>
69   * </ul>
70   *
71   * @author Andres Gomez Casanova <a
72   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
73   * @version 1.2.2 2010-06-01
74   */
75  public final class ImplementationSystemInterfaceController extends
76          AbstractInterfaceController {
77  
78      /**
79       * Input reader.
80       */
81      private final InputReader input;
82      /**
83       * Screen printer.
84       */
85      private final OutputWriter output;
86  
87      /**
88       * Constructor that creates a reader and a printer.
89       *
90       * @param prompt
91       *            Prompt to show in each line of the console.
92       * @throws InputReaderException
93       *             When there is a problem establishing the input or the output.
94       */
95      public ImplementationSystemInterfaceController(final String/* ! */prompt)
96              throws InputReaderException {
97          super();
98          assert prompt != null;
99  
100         this.output = new SystemOutputWriter();
101         this.input = new SystemInputReader(this.output, prompt);
102     }
103 
104     /*
105      * (non-Javadoc)
106      *
107      * @see
108      * name.angoca.zemucan.ui.api.AbstractInterfaceController#start(java.lang
109      * .Thread, name.angoca.zemucan.ui.api.SynchronizedLoad)
110      */
111     @Override
112     public void start(final Thread/* ? */synchro,
113             final SynchronizedLoad/* ? */synchronizedLoad)
114             throws AbstractZemucanException {
115         ExecutionState execState = ExecutionState.INITIATED;
116 
117         // Show the application's license.
118         if (Boolean.parseBoolean(Configurator.getInstance().getProperty(
119                 AbstractInterfaceController.SHOW_LICENSE))) {
120             this.output.writeLine(AboutNode.readAboutInformation());
121         }
122 
123         // Shows a introductory help.
124         if (Boolean.parseBoolean(Configurator.getInstance().getProperty(
125                 AbstractInterfaceController.SHOW_INTRO_PROPERTY))) {
126             this.output
127                     .writeLine(Messages
128                             .getString("ImplementationSystemInterfaceController.Instructions")); //$NON-NLS-1$
129         }
130 
131         if (synchro != null) {
132             synchronized (synchro) {
133                 synchronizedLoad.alreadyLoaded();
134                 synchro.notifyAll();
135             }
136         }
137 
138         while (execState != ExecutionState.APPLICATION_EXIT) {
139             final String phrase = this.input.readString();
140 
141             final int index = phrase.indexOf('?');
142             if (index < 0) {
143                 execState = ImplementationExecuter.getInstance().execute(
144                         phrase, this.output);
145             } else {
146                 final String newPhrase = phrase.substring(0, index);
147                 final ReturnOptions actual = InterfaceCore
148                         .analyzePhrase(newPhrase);
149                 // The output can be confusing, but this implementation is just
150                 // for testing purposes.
151                 this.output.writeLine(actual.toString());
152             }
153         }
154     }
155 }