001 /* 002 * Copyright (C) 2012 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020 package org.crsh.text; 021 022 import org.crsh.text.ui.Element; 023 024 import java.io.Closeable; 025 import java.io.IOException; 026 import java.io.InterruptedIOException; 027 import java.io.PrintWriter; 028 029 public class RenderPrintWriter extends PrintWriter { 030 031 /** . */ 032 private final RenderWriter out; 033 034 public RenderPrintWriter(ScreenContext out) { 035 super(new RenderWriter(out)); 036 037 // 038 this.out = (RenderWriter)super.out; 039 } 040 041 public RenderPrintWriter(ScreenContext out, Closeable closeable) { 042 super(new RenderWriter(out, closeable)); 043 044 // 045 this.out = (RenderWriter)super.out; 046 } 047 048 public final boolean isEmpty() { 049 return out.isEmpty(); 050 } 051 052 public final void print(Object obj, Color foreground) { 053 try { 054 out.append(Style.style(foreground)); 055 } 056 catch (InterruptedIOException x) { 057 Thread.currentThread().interrupt(); 058 } 059 catch (IOException x) { 060 setError(); 061 } 062 print(obj); 063 try { 064 out.append(Style.reset); 065 } 066 catch (InterruptedIOException x) { 067 Thread.currentThread().interrupt(); 068 } 069 catch (IOException x) { 070 setError(); 071 } 072 } 073 074 public final void println(Object obj, Color foreground) { 075 print(obj, Style.style(foreground)); 076 println(); 077 } 078 079 public final void print(Object obj, Color foreground, Color background) { 080 try { 081 out.append(Style.style(foreground, background)); 082 } 083 catch (InterruptedIOException x) { 084 Thread.currentThread().interrupt(); 085 } 086 catch (IOException x) { 087 setError(); 088 } 089 print(obj); 090 try { 091 out.append(Style.reset); 092 } 093 catch (InterruptedIOException x) { 094 Thread.currentThread().interrupt(); 095 } 096 catch (IOException x) { 097 setError(); 098 } 099 } 100 101 public final void println(Object obj, Color foreground, Color background) { 102 print(obj, Style.style(foreground, background)); 103 println(); 104 } 105 106 public final void print(Object obj, Decoration decoration) { 107 try { 108 out.append(Style.style(decoration)); 109 } 110 catch (InterruptedIOException x) { 111 Thread.currentThread().interrupt(); 112 } 113 catch (IOException x) { 114 setError(); 115 } 116 print(obj); 117 try { 118 out.append(Style.reset); 119 } 120 catch (InterruptedIOException x) { 121 Thread.currentThread().interrupt(); 122 } 123 catch (IOException x) { 124 setError(); 125 } 126 } 127 128 public final void println(Object obj, Decoration decoration) { 129 print(obj, Style.style(decoration)); 130 println(); 131 } 132 133 public final void print(Object obj, Decoration decoration, Color foreground) { 134 print(obj, Style.style(decoration, foreground)); 135 println(); 136 } 137 138 public final void println(Object obj, Decoration decoration, Color foreground) { 139 print(obj, Style.style(decoration, foreground, null)); 140 println(); 141 } 142 143 public final void print(Object obj, Decoration decoration, Color foreground, Color background) { 144 print(obj, Style.style(decoration, foreground, background)); 145 println(); 146 } 147 148 public final void println(Object obj, Decoration decoration, Color foreground, Color background) { 149 print(obj, Style.style(decoration, foreground, background)); 150 println(); 151 } 152 153 public final void print(Object obj, Style style) { 154 try { 155 out.append(style); 156 } 157 catch (InterruptedIOException x) { 158 Thread.currentThread().interrupt(); 159 } 160 catch (IOException x) { 161 setError(); 162 } 163 print(obj); 164 try { 165 out.append(Style.reset); 166 } 167 catch (InterruptedIOException x) { 168 Thread.currentThread().interrupt(); 169 } 170 catch (IOException x) { 171 setError(); 172 } 173 } 174 175 public final void println(Object obj, Style style) { 176 print(obj, style); 177 println(); 178 } 179 180 /** 181 * Groovy left shift operator. 182 * 183 * @param o the appended 184 * @return this object 185 */ 186 public final RenderPrintWriter leftShift(Object o) { 187 if (o instanceof Style) { 188 try { 189 out.append((Style)o); 190 } 191 catch (InterruptedIOException x) { 192 Thread.currentThread().interrupt(); 193 } 194 catch (IOException x) { 195 setError(); 196 } 197 } else if (o instanceof Decoration) { 198 try { 199 out.append((Style.style((Decoration)o))); 200 } 201 catch (InterruptedIOException x) { 202 Thread.currentThread().interrupt(); 203 } 204 catch (IOException x) { 205 setError(); 206 } 207 } else if (o instanceof Color) { 208 try { 209 out.append(Style.style((Color)o)); 210 } 211 catch (InterruptedIOException x) { 212 Thread.currentThread().interrupt(); 213 } 214 catch (IOException x) { 215 setError(); 216 } 217 } else { 218 print(o); 219 } 220 return this; 221 } 222 223 public final RenderPrintWriter cls() { 224 try { 225 out.cls(); 226 } 227 catch (InterruptedIOException x) { 228 Thread.currentThread().interrupt(); 229 } 230 catch (IOException x) { 231 setError(); 232 } 233 return this; 234 } 235 236 @Override 237 public void println(Object x) { 238 print(x); 239 println(); 240 } 241 242 public void show(Element element) { 243 element.render(new RenderAppendable(this.out.out)); 244 } 245 246 @Override 247 public void print(Object obj) { 248 if (obj instanceof Element) { 249 RenderAppendable out = new RenderAppendable(this.out.out); 250 ((Element)obj).renderer().render(out); 251 } else { 252 super.print(obj); 253 } 254 } 255 }