package de.ugoe.cs.util.console; import java.util.Collection; import java.util.LinkedHashSet; import java.util.logging.Level; import de.ugoe.cs.util.StringTools; import de.ugoe.cs.util.console.listener.ICommandListener; import de.ugoe.cs.util.console.listener.IErrorListener; import de.ugoe.cs.util.console.listener.IExceptionListener; import de.ugoe.cs.util.console.listener.IOutputListener; import de.ugoe.cs.util.console.listener.ITraceListener; /** *

* This class provides an interface for communication with the user without have * to rely on a specific user interface. Thus, it can be used to decouple the * programs logic from its user interface. *

*

* {@link Command} objects can be used to execute behavior. *

*

* To send output to the user interface, the Observer pattern is used. The * Console is an observable, the concrete user interfaces are the observers. The * interface for the observers is {@link ConsoleObserver}. *

* * @author Steffen Herbold * @version 1.0 */ public final class Console { /** *

* Listeners for the output stream. *

*/ private Collection outputListener; /** *

* Listeners for the error stream. *

*/ private Collection errorListener; /** *

* Listeners for the trace stream. *

*/ private Collection traceListener; /** *

* Listeners for the command stream. *

*/ private Collection commandListener; /** *

* Listeners for the exception stream. *

*/ private Collection exceptionListener; /** *

* Handle of the Console instance. *

*/ private static Console theInstance = new Console(); /** *

* Returns the instance of Console. If no instances exists yet, a new one is * created. *

* * @return instance of this class */ public static Console getInstance() { return theInstance; } /** *

* Resets the Console by creating a new instance that has no registered * observers. *

*/ public static void reset() { theInstance.init(); } /** *

* Creates a new Console. Private to prevent multiple instances (Singleton). *

*/ private Console() { init(); } /** *

* Initializes the console. *

*/ private void init() { outputListener = new LinkedHashSet(); errorListener = new LinkedHashSet(); traceListener = new LinkedHashSet(); commandListener = new LinkedHashSet(); exceptionListener = new LinkedHashSet(); } /** *

* Register a new observer. *

* * @deprecated use registerXYZListener instead * @param observer * observer to be added */ public void registerObserver(ConsoleObserver observer) { registerOutputListener(observer); registerErrorListener(observer); registerTraceListener(observer); registerCommandListener(observer); registerExceptionListener(observer); } /** *

* Registers an output listener. *

* * @param listener * listener that is registered */ public void registerOutputListener(IOutputListener listener) { outputListener.add(listener); } /** *

* Registers an error listener. *

* * @param listener * listener that is registered */ public void registerErrorListener(IErrorListener listener) { errorListener.add(listener); } /** *

* Registers a trace listener. *

* * @param listener * listener that is registered */ public void registerTraceListener(ITraceListener listener) { traceListener.add(listener); } /** *

* Registers a command listener. *

* * @param listener * listener that is registered */ public void registerCommandListener(ICommandListener listener) { commandListener.add(listener); } /** *

* Registers an exception listener. *

* * @param listener * listener that is registered */ public void registerExceptionListener(IExceptionListener listener) { exceptionListener.add(listener); } /** *

* Remove an observer. If the observer is not found, nothing is done. *

* * @deprecated use removeXYZListener instead * @param observer * observer to be removed */ public void deleteObserver(ConsoleObserver observer) { removeOutputListener(observer); removeErrorListener(observer); removeTraceListener(observer); removeCommandListener(observer); removeExceptionListener(observer); } /** *

* Removes an output listener. *

* * @param listener * listener that is removed */ public void removeOutputListener(IOutputListener listener) { outputListener.remove(listener); } /** *

* Removes an error listener. *

* * @param listener * listener that is removed */ public void removeErrorListener(IErrorListener listener) { errorListener.remove(listener); } /** *

* Removes an trace listener. *

* * @param listener * listener that is removed */ public void removeTraceListener(ITraceListener listener) { traceListener.remove(listener); } /** *

* Removes a command listener. *

* * @param listener * listener that is removed */ public void removeCommandListener(ICommandListener listener) { commandListener.remove(listener); } /** *

* Removes an exception listener. *

* * @param listener * listener that is removed */ public void removeExceptionListener(IExceptionListener listener) { exceptionListener.remove(listener); } /** *

* Checks if a listener is registered. *

* * @param listener * listener that is checked * @return true, is listener is registered; false, otherwise */ public boolean hasOutputListener(IOutputListener listener) { return outputListener.contains(listener); } /** *

* Checks if a listener is registered. *

* * @param listener * listener that is checked * @return true, is listener is registered; false, otherwise */ public boolean hasErrorListener(IErrorListener listener) { return errorListener.contains(listener); } /** *

* Checks if a listener is registered. *

* * @param listener * listener that is checked * @return true, is listener is registered; false, otherwise */ public boolean hasTraceListener(ITraceListener listener) { return traceListener.contains(listener); } /** *

* Checks if a listener is registered. *

* * @param listener * listener that is checked * @return true, is listener is registered; false, otherwise */ public boolean hasCommandListener(ICommandListener listener) { return commandListener.contains(listener); } /** *

* Checks if a listener is registered. *

* * @param listener * listener that is checked * @return true, is listener is registered; false, otherwise */ public boolean hasExceptionListener(IExceptionListener listener) { return exceptionListener.contains(listener); } /** *

* Sends a message to all observers containing the message that was passed * to this function. *

* * @param msg * message that is send to the console */ public static void print(String msg) { for (IOutputListener observer : theInstance.outputListener) { observer.outputMsg(msg); } } /** *

* Sends a message to all observers containing the message that was passed * to this function and adds an endline to the message. *

* * @param msg * message that is send to the observers */ public static void println(String msg) { for (IOutputListener observer : theInstance.outputListener) { observer.outputMsg(msg + StringTools.ENDLINE); } } /** *

* Sends an error message to all observers containing the message that was * passed to this function. *

* * @param errMsg * message that is send to the observers */ public static void printerr(String errMsg) { for (IErrorListener observer : theInstance.errorListener) { observer.errorMsg(errMsg); } } /** *

* Sends an error message to all observers containing the message that was * passed to this function and adds an endline to the message. *

* * @param errMsg * message that is send to the observers */ public static void printerrln(String errMsg) { for (IErrorListener observer : theInstance.errorListener) { observer.errorMsg(errMsg + StringTools.ENDLINE); } } /** *

* Sends an exception to all observers to print its stack trace. *

* * @param e * exception whose stack trace is to be printed */ public static void logException(Exception e) { for (IExceptionListener observer : theInstance.exceptionListener) { observer.logException(e); } } /** *

* Sends a debug message to all observers containing the message that was * passed to this function. The default log {@link Level} is {@link Level#INFO}. *

* * @param traceMsg * message that is send to the observers */ @Deprecated public static void trace(String traceMsg) { for (ITraceListener observer : theInstance.traceListener) { observer.traceMsg(traceMsg, Level.INFO); } } /** * *

* Sends a debug message to all trace listeners containing the message that was passed to this function. *

* * @param traceMsg message that is send to the trace listener * @param logLevel log level of the message */ public static void trace(String traceMsg, Level logLevel) { for( ITraceListener observer : theInstance.traceListener) { observer.traceMsg(traceMsg, logLevel); } } /** *

* Sends a debug message to all observers containing the message that was * passed to this function and adds an {@link StringTools#ENDLINE} to the * message. The default log {@link Level} is {@link Level#INFO}. *

* * @param traceMsg * message that is send to the observers */ @Deprecated public static void traceln(String traceMsg) { for (ITraceListener observer : theInstance.traceListener) { observer.traceMsg(traceMsg + StringTools.ENDLINE, Level.INFO); } } /** *

* Sends a debug message to all observers containing the message that was * passed to this function and adds an {@link StringTools#ENDLINE} to the * message. *

* * @param traceMsg message that is send to the observers * @param logLevel log levle of the message */ public static void traceln(String traceMsg, Level logLevel) { for( ITraceListener observer : theInstance.traceListener) { observer.traceMsg(traceMsg + StringTools.ENDLINE, logLevel); } } /** *

* Called by {@link CommandExecuter#exec(String)}. *

* * @param command * command that is executed */ static void commandNotification(String command) { for (ICommandListener observer : theInstance.commandListener) { observer.commandNotification(command); } } }