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/uiImplJLine/src/main/java/name/angoca/zemucan/ui/impl/jline/ImplementationJlineInterfaceController.java $:
28 */
29 package name.angoca.zemucan.ui.impl.jline;
30
31 import name.angoca.zemucan.AbstractZemucanException;
32 import name.angoca.zemucan.core.graph.model.AboutNode;
33 import name.angoca.zemucan.executer.api.ExecutingCommandException;
34 import name.angoca.zemucan.executer.api.ExecutionState;
35 import name.angoca.zemucan.executer.impl.ImplementationExecuter;
36 import name.angoca.zemucan.tools.configurator.Configurator;
37 import name.angoca.zemucan.tools.messages.jline.Messages;
38 import name.angoca.zemucan.ui.api.AbstractInterfaceController;
39 import name.angoca.zemucan.ui.api.InputReader;
40 import name.angoca.zemucan.ui.api.InputReaderException;
41 import name.angoca.zemucan.ui.api.OutputWriter;
42 import name.angoca.zemucan.ui.api.SynchronizedLoad;
43 import name.angoca.zemucan.ui.impl.system.SystemOutputWriter;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49 * This is the interface controller that manage the read and the printer.
50 * <p>
51 * TODO v2.0 mostrar un mensaje cuando se puede ejecutar un comando (ending
52 * node)
53 * <p>
54 * <b>Control Version</b>
55 * <p>
56 * <ul>
57 * <li>0.0.1 Class creation.</li>
58 * <li>0.0.2 Annotations.</li>
59 * <li>0.0.3 Execution state.</li>
60 * <li>0.0.4 Enum.</li>
61 * <li>0.0.5 Read string just once.</li>
62 * <li>0.0.6 Name of a state.</li>
63 * <li>1.0.0 Moved to version 1.</li>
64 * <li>1.0.1 Show intro.</li>
65 * <li>1.0.2 Show intro.</li>
66 * <li>1.0.3 Code changed and final.</li>
67 * <li>1.0.4 Assert.</li>
68 * <li>1.0.5 Executer not in interface.</li>
69 * <li>1.1.0 Load synchronization.</li>
70 * <li>1.2.0 Synchronized version with abstract part.</li>
71 * <li>1.2.1 Synchronization optional.</li>
72 * <li>1.2.2 Show license.</li>
73 * <li>1.2.3 Show execution error.</li>
74 * </ul>
75 *
76 * @author Andres Gomez Casanova <a
77 * href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
78 * @version 1.2.3 2010-06-04
79 */
80 public final class ImplementationJlineInterfaceController extends
81 AbstractInterfaceController {
82
83 /**
84 * Property max options to print.
85 */
86 static final String AUTO_PRINT_THRESHOLD_KEY = "AutoPrintThreshold";
87 /**
88 * Value max options to print.
89 */
90 static final int AUTO_PRINT_THRESHOLD_VALUE = 50;
91 /**
92 * Logger.
93 */
94 private static final Logger LOGGER = LoggerFactory
95 .getLogger(ImplementationJlineInterfaceController.class);
96 /**
97 * Input processor.
98 */
99 private final InputReader input;
100 /**
101 * Screen printer.
102 */
103 private final OutputWriter output;
104
105 /**
106 * Constructor that creates a reader and a printer.
107 *
108 * @param prompt
109 * Prompt to show in each line of the console.@throws
110 * InputReaderException When there is a problem establishing the
111 * input or the output.
112 * @throws InputReaderException
113 * When there is a problem establishing the input or the output.
114 */
115 public ImplementationJlineInterfaceController(final String/* ! */prompt)
116 throws InputReaderException {
117 super();
118 assert prompt != null;
119
120 this.output = new SystemOutputWriter();
121 this.input = new JlineInputReader(prompt);
122 }
123
124 /*
125 * (non-Javadoc)
126 *
127 * @see
128 * name.angoca.zemucan.ui.api.AbstractInterfaceController#start(java.lang
129 * .Thread, name.angoca.zemucan.ui.api.SynchronizedLoad)
130 */
131 @Override
132 public void start(final Thread/* ? */synchro,
133 final SynchronizedLoad/* ? */synchronizedLoad)
134 throws AbstractZemucanException {
135 String phrase = null;
136
137 // Show the application's license.
138 if (Boolean.parseBoolean(Configurator.getInstance().getProperty(
139 AbstractInterfaceController.SHOW_LICENSE))) {
140 this.output.writeLine(AboutNode.readAboutInformation());
141 }
142
143 // Shows a introductory help.
144 if (Boolean.parseBoolean(Configurator.getInstance().getProperty(
145 AbstractInterfaceController.SHOW_INTRO_PROPERTY))) {
146 this.output.writeLine(Messages
147 .getString("ImplementationJlineInterfaceController"//$NON-NLS-1$
148 + ".Instructions")); //$NON-NLS-1$
149 }
150 ExecutionState execState = ExecutionState.INITIATED;
151
152 ImplementationJlineInterfaceController.LOGGER
153 .debug("User interaction start"); //$NON-NLS-1$
154
155 if (synchro != null) {
156 synchronized (synchro) {
157 synchronizedLoad.alreadyLoaded();
158 synchro.notifyAll();
159 }
160 }
161
162 while (execState != ExecutionState.APPLICATION_EXIT) {
163 phrase = this.input.readString();
164 try {
165 execState = ImplementationExecuter.getInstance().execute(
166 phrase, this.output);
167 } catch (final ExecutingCommandException e) {
168 ImplementationJlineInterfaceController.LOGGER.error(e
169 .getMessage());
170 ImplementationJlineInterfaceController.LOGGER.error(e
171 .getCause().getMessage());
172 }
173 }
174 }
175 }