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

Last change on this file since 210 was 210, checked in by sherbold, 13 years ago
  • renamed de.ugoe.cs.util.console.Console#printStacktrace() and de.ugoe.cs.util.console.listener.IExceptionListener#printStackstrace() to #logException
File size: 9.1 KB
Line 
1package de.ugoe.cs.util.console;
2
3import java.util.Collection;
4import java.util.LinkedHashSet;
5
6import de.ugoe.cs.util.StringTools;
7import de.ugoe.cs.util.console.listener.ICommandListener;
8import de.ugoe.cs.util.console.listener.IErrorListener;
9import de.ugoe.cs.util.console.listener.IExceptionListener;
10import de.ugoe.cs.util.console.listener.IOutputListener;
11import de.ugoe.cs.util.console.listener.ITraceListener;
12
13/**
14 * <p>
15 * This class provides an interface for communication with the user without have
16 * to rely on a specific user interface. Thus, it can be used to decouple the
17 * programs logic from its user interface.
18 * </p>
19 * <p>
20 * {@link Command} objects can be used to execute behavior.
21 * </p>
22 * <p>
23 * To send output to the user interface, the Observer pattern is used. The
24 * Console is an observable, the concrete user interfaces are the observers. The
25 * interface for the observers is {@link ConsoleObserver}.
26 * </p>
27 *
28 * @author Steffen Herbold
29 * @version 1.0
30 */
31public final class Console {
32
33        /**
34         * <p>
35         * Listeners for the output stream.
36         * </p>
37         */
38        private Collection<IOutputListener> outputListener;
39
40        /**
41         * <p>
42         * Listeners for the error stream.
43         * </p>
44         */
45        private Collection<IErrorListener> errorListener;
46
47        /**
48         * <p>
49         * Listeners for the trace stream.
50         * </p>
51         */
52        private Collection<ITraceListener> traceListener;
53
54        /**
55         * <p>
56         * Listeners for the command stream.
57         * </p>
58         */
59        private Collection<ICommandListener> commandListener;
60
61        /**
62         * <p>
63         * Listeners for the exception stream.
64         * </p>
65         */
66        private Collection<IExceptionListener> exceptionListener;
67
68        /**
69         * <p>
70         * Handle of the Console instance.
71         * </p>
72         */
73        private static Console theInstance = null;
74
75        /**
76         * <p>
77         * Returns the instance of Console. If no instances exists yet, a new one is
78         * created.
79         * </p>
80         *
81         * @return instance of this class
82         */
83        public static Console getInstance() {
84                if (theInstance == null) {
85                        theInstance = new Console();
86                }
87                return theInstance;
88        }
89
90        /**
91         * <p>
92         * Creates a new Console. Private to prevent multiple instances (Singleton).
93         * </p>
94         */
95        private Console() {
96                outputListener = new LinkedHashSet<IOutputListener>();
97                errorListener = new LinkedHashSet<IErrorListener>();
98                traceListener = new LinkedHashSet<ITraceListener>();
99                commandListener = new LinkedHashSet<ICommandListener>();
100                exceptionListener = new LinkedHashSet<IExceptionListener>();
101        }
102
103        /**
104         * <p>
105         * Register a new observer.
106         * </p>
107         *
108         * @deprecated use registerXYZListener instead
109         * @param observer
110         *            observer to be added
111         */
112        public void registerObserver(ConsoleObserver observer) {
113                registerOutputListener(observer);
114                registerErrorListener(observer);
115                registerTraceListener(observer);
116                registerCommandListener(observer);
117                registerExceptionListener(observer);
118        }
119
120        /**
121         * <p>
122         * Registers an output listener.
123         * </p>
124         *
125         * @param listener
126         *            listener that is registered
127         */
128        public void registerOutputListener(IOutputListener listener) {
129                outputListener.add(listener);
130        }
131
132        /**
133         * <p>
134         * Registers an error listener.
135         * </p>
136         *
137         * @param listener
138         *            listener that is registered
139         */
140        public void registerErrorListener(IErrorListener listener) {
141                errorListener.add(listener);
142        }
143
144        /**
145         * <p>
146         * Registers a trace listener.
147         * </p>
148         *
149         * @param listener
150         *            listener that is registered
151         */
152        public void registerTraceListener(ITraceListener listener) {
153                traceListener.add(listener);
154        }
155
156        /**
157         * <p>
158         * Registers a command listener.
159         * </p>
160         *
161         * @param listener
162         *            listener that is registered
163         */
164        public void registerCommandListener(ICommandListener listener) {
165                commandListener.add(listener);
166        }
167
168        /**
169         * <p>
170         * Registers an exception listener.
171         * </p>
172         *
173         * @param listener
174         *            listener that is registered
175         */
176        public void registerExceptionListener(IExceptionListener listener) {
177                exceptionListener.add(listener);
178        }
179
180        /**
181         * <p>
182         * Remove an observer. If the observer is not found, nothing is done.
183         * </p>
184         *
185         * @deprecated use removeXYZListener instead
186         * @param observer
187         *            observer to be removed
188         */
189        public void deleteObserver(ConsoleObserver observer) {
190                removeOutputListener(observer);
191                removeErrorListener(observer);
192                removeTraceListener(observer);
193                removeCommandListener(observer);
194                removeExceptionListener(observer);
195        }
196
197        /**
198         * <p>
199         * Removes an output listener.
200         * </p>
201         *
202         * @param listener
203         *            listener that is removed
204         */
205        public void removeOutputListener(IOutputListener listener) {
206                outputListener.remove(listener);
207        }
208
209        /**
210         * <p>
211         * Removes an error listener.
212         * </p>
213         *
214         * @param listener
215         *            listener that is removed
216         */
217        public void removeErrorListener(IErrorListener listener) {
218                errorListener.remove(listener);
219        }
220
221        /**
222         * <p>
223         * Removes an trace listener.
224         * </p>
225         *
226         * @param listener
227         *            listener that is removed
228         */
229        public void removeTraceListener(ITraceListener listener) {
230                traceListener.remove(listener);
231        }
232
233        /**
234         * <p>
235         * Removes a command listener.
236         * </p>
237         *
238         * @param listener
239         *            listener that is removed
240         */
241        public void removeCommandListener(ICommandListener listener) {
242                commandListener.remove(listener);
243        }
244
245        /**
246         * <p>
247         * Removes an exception listener.
248         * </p>
249         *
250         * @param listener
251         *            listener that is removed
252         */
253        public void removeExceptionListener(IExceptionListener listener) {
254                exceptionListener.remove(listener);
255        }
256
257        /**
258         * <p>
259         * Sends a message to all observers containing the message that was passed
260         * to this function.
261         * </p>
262         *
263         * @param msg
264         *            message that is send to the console
265         */
266        public static void print(String msg) {
267                if (theInstance == null) {
268                        getInstance();
269                }
270                for (IOutputListener observer : theInstance.outputListener) {
271                        observer.outputMsg(msg);
272                }
273        }
274
275        /**
276         * <p>
277         * Sends a message to all observers containing the message that was passed
278         * to this function and adds an endline to the message.
279         * </p>
280         *
281         * @param msg
282         *            message that is send to the observers
283         */
284        public static void println(String msg) {
285                if (theInstance == null) {
286                        getInstance();
287                }
288                for (IOutputListener observer : theInstance.outputListener) {
289                        observer.outputMsg(msg + StringTools.ENDLINE);
290                }
291        }
292
293        /**
294         * <p>
295         * Sends an error message to all observers containing the message that was
296         * passed to this function.
297         * </p>
298         *
299         * @param errMsg
300         *            message that is send to the observers
301         */
302        public static void printerr(String errMsg) {
303                if (theInstance == null) {
304                        getInstance();
305                }
306                for (IErrorListener observer : theInstance.errorListener) {
307                        observer.errorMsg(errMsg);
308                }
309        }
310
311        /**
312         * <p>
313         * Sends an error message to all observers containing the message that was
314         * passed to this function and adds an endline to the message.
315         * </p>
316         *
317         * @param errMsg
318         *            message that is send to the observers
319         */
320        public static void printerrln(String errMsg) {
321                if (theInstance == null) {
322                        getInstance();
323                }
324                for (IErrorListener observer : theInstance.errorListener) {
325                        observer.errorMsg(errMsg + StringTools.ENDLINE);
326                }
327        }
328
329        /**
330         * <p>
331         * Sends an exception to all observers to print its stack trace.
332         * </p>
333         *
334         * @param e
335         *            exception whose stack trace is to be printed
336         */
337        public static void logException(Exception e) {
338                if (theInstance == null) {
339                        getInstance();
340                }
341                for (IExceptionListener observer : theInstance.exceptionListener) {
342                        observer.logException(e);
343                }
344        }
345
346        /**
347         * <p>
348         * Sends a debug message to all observers containing the message that was
349         * passed to this function.
350         * </p>
351         *
352         * @param traceMsg
353         *            message that is send to the observers
354         */
355        public static void trace(String traceMsg) {
356                if (theInstance == null) {
357                        getInstance();
358                }
359                for (ITraceListener observer : theInstance.traceListener) {
360                        observer.traceMsg(traceMsg);
361                }
362        }
363
364        /**
365         * <p>
366         * Sends a debug message to all observers containing the message that was
367         * passed to this function and adds an {@link StringTools#ENDLINE} to the
368         * message.
369         * </p>
370         *
371         * @param traceMsg
372         *            message that is send to the observers
373         */
374        public static void traceln(String traceMsg) {
375                if (theInstance == null) {
376                        getInstance();
377                }
378                for (ITraceListener observer : theInstance.traceListener) {
379                        observer.traceMsg(traceMsg + StringTools.ENDLINE);
380                }
381        }
382
383        /**
384         * <p>
385         * Called by {@link CommandExecuter#exec(String)}.
386         * </p>
387         *
388         * @param command
389         *            command that is executed
390         */
391        static void commandNotification(String command) {
392                if (theInstance == null) {
393                        getInstance();
394                }
395                for (ICommandListener observer : theInstance.commandListener) {
396                        observer.commandNotification(command);
397                }
398        }
399
400}
Note: See TracBrowser for help on using the repository browser.