source: trunk/JavaHelperLib/src/de/ugoe/cs/util/console/Console.java @ 195

Last change on this file since 195 was 186, checked in by sherbold, 13 years ago
  • modified CommandExecuter? and Console make use of the command notification for observers
File size: 5.3 KB
Line 
1package de.ugoe.cs.util.console;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import de.ugoe.cs.util.StringTools;
7
8/**
9 * <p>
10 * This class provides an interface for communication with the user without have
11 * to rely on a specific user interface. Thus, it can be used to decouple the
12 * programs logic from its user interface.
13 * </p>
14 * <p>
15 * {@link Command} objects can be used to execute behavior.
16 * </p>
17 * <p>
18 * To send output to the user interface, the Observer pattern is used. The
19 * Console is an observable, the concrete user interfaces are the observers. The
20 * interface for the observers is {@link ConsoleObserver}.
21 * </p>
22 *
23 * @author Steffen Herbold
24 * @version 1.0
25 */
26public final class Console {
27
28        /**
29         * <p>
30         * List of observers.
31         * </p>
32         */
33        private List<ConsoleObserver> observers;
34
35        /**
36         * <p>
37         * Handle of the Console instance.
38         * </p>
39         */
40        private static Console theInstance = null;
41
42        /**
43         * <p>
44         * Returns the instance of Console. If no instances exists yet, a new one is
45         * created.
46         * </p>
47         *
48         * @return instance of this class
49         */
50        public static Console getInstance() {
51                if (theInstance == null) {
52                        theInstance = new Console();
53                }
54                return theInstance;
55        }
56
57        /**
58         * <p>
59         * Creates a new Console. Private to prevent multiple instances (Singleton).
60         * </p>
61         */
62        private Console() {
63                observers = new ArrayList<ConsoleObserver>();
64        }
65
66        /**
67         * <p>
68         * Register a new observer.
69         * </p>
70         *
71         * @param observer
72         *            observer to be added
73         */
74        public void registerObserver(ConsoleObserver observer) {
75                observers.add(observer);
76        }
77
78        /**
79         * <p>
80         * Remove an observer. If the observer is not found, nothing is done.
81         * </p>
82         *
83         * @param observer
84         *            observer to be removed
85         */
86        public void deleteObserver(ConsoleObserver observer) {
87                observers.remove(observer);
88        }
89
90        /**
91         * <p>
92         * Sends a message to all observers containing the message that was passed
93         * to this function.
94         * </p>
95         *
96         * @param msg
97         *            message that is send to the console
98         */
99        public static void print(String msg) {
100                if (theInstance == null) {
101                        getInstance();
102                }
103                for (ConsoleObserver observer : theInstance.observers) {
104                        observer.updateText(msg);
105                }
106        }
107
108        /**
109         * <p>
110         * Sends a message to all observers containing the message that was passed
111         * to this function and adds an endline to the message.
112         * </p>
113         *
114         * @param msg
115         *            message that is send to the observers
116         */
117        public static void println(String msg) {
118                if (theInstance == null) {
119                        getInstance();
120                }
121                for (ConsoleObserver observer : theInstance.observers) {
122                        observer.updateText(msg + StringTools.ENDLINE);
123                }
124        }
125
126        /**
127         * <p>
128         * Sends an error message to all observers containing the message that was
129         * passed to this function.
130         * </p>
131         *
132         * @param errMsg
133         *            message that is send to the observers
134         */
135        public static void printerr(String errMsg) {
136                if (theInstance == null) {
137                        getInstance();
138                }
139                for (ConsoleObserver observer : theInstance.observers) {
140                        observer.errStream(errMsg);
141                }
142        }
143
144        /**
145         * <p>
146         * Sends an error message to all observers containing the message that was
147         * passed to this function and adds an endline to the message.
148         * </p>
149         *
150         * @param errMsg
151         *            message that is send to the observers
152         */
153        public static void printerrln(String errMsg) {
154                if (theInstance == null) {
155                        getInstance();
156                }
157                for (ConsoleObserver observer : theInstance.observers) {
158                        observer.errStream(errMsg + StringTools.ENDLINE);
159                }
160        }
161
162        /**
163         * <p>
164         * Sends an exception to all observers to print its stack trace.
165         * </p>
166         *
167         * @param e
168         *            exception whose stack trace is to be printed
169         */
170        public static void printStacktrace(Exception e) {
171                if (theInstance == null) {
172                        getInstance();
173                }
174                for (ConsoleObserver observer : theInstance.observers) {
175                        observer.printStacktrace(e);
176                }
177        }
178
179        /**
180         * <p>
181         * Sends a debug message to all observers containing the message that was
182         * passed to this function.
183         * </p>
184         *
185         * @param traceMsg
186         *            message that is send to the observers
187         */
188        public static void trace(String traceMsg) {
189                if (theInstance == null) {
190                        getInstance();
191                }
192                for (ConsoleObserver observer : theInstance.observers) {
193                        observer.trace(traceMsg);
194                }
195        }
196
197        /**
198         * <p>
199         * Sends a debug message to all observers containing the message that was
200         * passed to this function and adds an {@link StringTools#ENDLINE} to the
201         * message.
202         * </p>
203         *
204         * @param traceMsg
205         *            message that is send to the observers
206         */
207        public static void traceln(String traceMsg) {
208                if (theInstance == null) {
209                        getInstance();
210                }
211                for (ConsoleObserver observer : theInstance.observers) {
212                        observer.trace(traceMsg + StringTools.ENDLINE);
213                }
214        }
215
216        /**
217         * <p>
218         * Called by {@link CommandExecuter#exec(String)}.
219         * </p>
220         *
221         * @param command
222         *            command that is executed
223         */
224        static void commandNotification(String command) {
225                if (theInstance == null) {
226                        getInstance();
227                }
228                for (ConsoleObserver observer : theInstance.observers) {
229                        observer.commandNotification(command);
230                }
231        }
232
233}
Note: See TracBrowser for help on using the repository browser.