source: trunk/JavaHelperLib/src/de/ugoe/cs/util/console/TextConsole.java @ 310

Last change on this file since 310 was 250, checked in by sherbold, 13 years ago
  • various changed to de.ugoe.cs.util.Console:
    • changed instantiation method of the instance, i.e., how the Singleton property is guaranteed
    • added method reset() to remove all listeners at once
    • added functions to check is a given listener is registered
  • various changes to de.ugoe.cs.util.FileOutputListener?:
    • added null-checks for writer to improve behavior in case of improper usage
    • added method getFilename() to check to which file the listener writes.
  • added method setDebug() to de.ugoe.cs.util.TextConsole? to define whether trace stream is displayed or not.
File size: 3.1 KB
Line 
1package de.ugoe.cs.util.console;
2
3import java.io.IOException;
4
5import de.ugoe.cs.util.console.listener.IErrorListener;
6import de.ugoe.cs.util.console.listener.IExceptionListener;
7import de.ugoe.cs.util.console.listener.IOutputListener;
8import de.ugoe.cs.util.console.listener.ITraceListener;
9
10/**
11 * <p>
12 * Implements a simple console observer that prints normal text to
13 * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}.
14 * </p>
15 *
16 * @author Steffen Herbold
17 * @version 1.0
18 */
19public class TextConsole implements IOutputListener, IErrorListener,
20                ITraceListener, IExceptionListener {
21
22        /**
23         * <p>
24         * In the debug mode, trace messages will be printed.
25         * </p>
26         */
27        private boolean debugMode = true;
28
29        /**
30         * <p>
31         * Creates a new text console and automatically registers it as observer.
32         * </p>
33         */
34        public TextConsole() {
35                Console.getInstance().registerOutputListener(this);
36                Console.getInstance().registerErrorListener(this);
37                Console.getInstance().registerTraceListener(this);
38                Console.getInstance().registerExceptionListener(this);
39        }
40
41        /**
42         * <p>
43         * Prints messages to {@code stdout}.
44         * </p>
45         *
46         * @see ConsoleObserver#outputMsg(java.lang.String)
47         */
48        public void outputMsg(String newMessage) {
49                System.out.print(newMessage);
50        }
51
52        /**
53         * <p>
54         * Prints messages to {@code stderr}.
55         * </p>
56         *
57         * @see ConsoleObserver#errorMsg(String)
58         */
59        @Override
60        public void errorMsg(String errMessage) {
61                System.err.print(errMessage);
62        }
63
64        /**
65         * <p>
66         * Prints the stacktrace of an exception to {@code stderr}.
67         * </p>
68         *
69         * @see ConsoleObserver#logException(Exception)
70         */
71        @Override
72        public void logException(Exception e) {
73                System.err.println(e.getMessage());
74        }
75
76        /**
77         * <p>
78         * Prints messages to {@code stdout}. These messages are only printed, if
79         * the console is run in debug mode.
80         * </p>
81         */
82        @Override
83        public void traceMsg(String traceMessage) {
84                if (debugMode) {
85                        System.out.print(traceMessage);
86                }
87        }
88
89        /**
90         * <p>
91         * Starts a new TextConsole. If the text console is started, it can be used
92         * not only to print message, but also to execute commands by reading
93         * {@code stdin}.
94         * </p>
95         *
96         * @param debugMode
97         *            true, if the application is to run in debug mode, i.e. trace
98         *            messages will be printed
99         */
100        public void run(boolean debugMode) {
101                this.debugMode = debugMode;
102                CommandExecuter exec = CommandExecuter.getInstance();
103                while (true) {
104                        System.out.print("> ");
105                        String command = getCommand().trim();
106                        if (!command.equals("")) {
107                                exec.exec(command);
108                        }
109                }
110        }
111
112        /**
113         * <p>
114         * Reads a new command from {@code stdin}.
115         * </p>
116         *
117         * @return a string with a command
118         */
119        protected String getCommand() {
120                byte[] buffer = new byte[1024];
121                try {
122                        System.in.read(buffer);
123                } catch (IOException e) {
124
125                }
126                return new String(buffer);
127        }
128       
129        /**
130         * <p>
131         * Configures if the debug mode of the text console is enabled.
132         * </p>
133         * @param debug if true, debug mode is enabled.
134         */
135        public void setDebug(boolean debug) {
136                debugMode = debug;
137        }
138
139}
Note: See TracBrowser for help on using the repository browser.