Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ColorPrinter.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.text;
00026 
00027 import java.io.PrintStream;
00028 
00029 /**
00030  * This class defines a ColorPrinter
00031  * 
00032  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00033  * @version 1.0
00034  */
00035 public class ColorPrinter
00036 {
00037   //  private static final int ATTR_NORMAL = 0;
00038   private static final int    ATTR_BRIGHT  = 1;
00039   private static final int    ATTR_DIM     = 2;
00040   //  private static final int ATTR_UNDERLINE = 3;
00041   //  private static final int ATTR_BLINK = 5;
00042   // private static final int ATTR_REVERSE = 7;
00043   //  private static final int ATTR_HIDDEN = 8;
00044 
00045   private static final int    FG_BLACK     = 30;
00046   private static final int    FG_RED       = 31;
00047   private static final int    FG_GREEN     = 32;
00048   //  private static final int FG_YELLOW = 33;
00049   private static final int    FG_BLUE      = 34;
00050   private static final int    FG_MAGENTA   = 35;
00051   //  private static final int FG_CYAN = 36;
00052   //  private static final int FG_WHITE = 37;
00053 
00054   //  private static final int BG_BLACK = 40;
00055   //  private static final int BG_RED = 41;
00056   //  private static final int BG_GREEN = 42;
00057   //  private static final int BG_YELLOW = 44;
00058   //  private static final int BG_BLUE = 44;
00059   //  private static final int BG_MAGENTA = 45;
00060   //  private static final int BG_CYAN = 46;
00061   //  private static final int BG_WHITE = 47;
00062 
00063   private static final String PREFIX       = "\u001b[";
00064   private static final String SUFFIX       = "m";
00065   private static final char   SEPARATOR    = ';';
00066   private static final String END_COLOR    = PREFIX + SUFFIX;
00067 
00068   private static final String STD_COLOR    = PREFIX + ATTR_DIM + SEPARATOR
00069                                                + FG_BLACK + SUFFIX;
00070   private static final String ERR_COLOR    = PREFIX + ATTR_BRIGHT + SEPARATOR
00071                                                + FG_RED + SUFFIX;
00072   //  private static final String verboseColor = PREFIX + ATTR_BRIGHT + SEPARATOR
00073   //                                               + FG_BLACK + SUFFIX;
00074   private static final String INFO_COLOR   = PREFIX + ATTR_BRIGHT + SEPARATOR
00075                                                + FG_GREEN + SUFFIX;
00076   private static final String STATUS_COLOR = PREFIX + ATTR_DIM + SEPARATOR
00077                                                + FG_MAGENTA + SUFFIX;
00078   private static final String PROMPT_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR
00079                                                + FG_BLUE + SUFFIX;
00080 
00081   //  private static final String warnColor = PREFIX + ATTR_DIM + SEPARATOR
00082   //                                               + FG_MAGENTA + SUFFIX;
00083   //  private static final String INFO_COLOR = PREFIX + ATTR_DIM + SEPARATOR
00084   //                                               + FG_CYAN + SUFFIX;
00085   //  private static final String debugColor = PREFIX + ATTR_DIM + SEPARATOR
00086   //                                               + FG_BLUE + SUFFIX;
00087 
00088   /**
00089    * Standard color
00090    */
00091   public static final int     STD          = 0;
00092   /**
00093    * Error color
00094    */
00095   public static final int     ERROR        = 1;
00096   /**
00097    * Info color for commands
00098    */
00099   public static final int     INFO         = 2;
00100   /**
00101    * status color for commands
00102    */
00103   public static final int     STATUS       = 3;
00104   /**
00105    * status color for commands
00106    */
00107   public static final int     PROMPT       = 4;
00108 
00109   /**
00110    * Print a message in color
00111    * 
00112    * @param message Message to print
00113    * @param stream Stream where to send the message
00114    * @param color Color for the message
00115    */
00116   public static final void printMessage(final String message,
00117       final PrintStream stream, final int color)
00118   {
00119     printMessage(message, stream, color, true);
00120   }
00121 
00122   /**
00123    * Print a message in color
00124    * 
00125    * @param message Message to print
00126    * @param stream Stream where to send the message
00127    * @param color Color for the message
00128    * @param endline true if a carriage return should be appended to the message
00129    */
00130   public static final void printMessage(final String message,
00131       final PrintStream stream, final int color, boolean endline)
00132   {
00133 
00134     final StringBuffer msg = new StringBuffer(message);
00135     switch (color)
00136     {
00137       default : // Use STD as default
00138       case STD :
00139         msg.insert(0, STD_COLOR);
00140         msg.append(END_COLOR);
00141         break;
00142       case ERROR :
00143         msg.insert(0, ERR_COLOR);
00144         msg.append(END_COLOR);
00145         break;
00146       case INFO :
00147         msg.insert(0, INFO_COLOR);
00148         msg.append(END_COLOR);
00149         break;
00150       case STATUS :
00151         msg.insert(0, STATUS_COLOR);
00152         msg.append(END_COLOR);
00153         break;
00154       case PROMPT :
00155         msg.insert(0, PROMPT_COLOR);
00156         msg.append(END_COLOR);
00157         break;
00158     }
00159     final String strmessage = msg.toString();
00160     if (endline)
00161       stream.println(strmessage);
00162     else
00163       stream.print(strmessage);
00164   }
00165 }

Generated on Mon Apr 11 22:01:30 2005 for C-JDBC by  doxygen 1.3.9.1