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