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.shell.ScreenContext;
023    import org.crsh.text.ui.Element;
024    import org.crsh.text.ui.UIBuilder;
025    import org.crsh.text.ui.UIBuilderRenderable;
026    
027    import java.io.Closeable;
028    import java.io.IOException;
029    import java.io.InterruptedIOException;
030    import java.io.PrintWriter;
031    import java.util.Collections;
032    
033    public class RenderPrintWriter extends PrintWriter {
034    
035      /** . */
036      private final RenderWriter out;
037    
038      public RenderPrintWriter(ScreenContext out) {
039        super(new RenderWriter(out));
040    
041        //
042        this.out = (RenderWriter)super.out;
043      }
044    
045      public RenderPrintWriter(ScreenContext out, Closeable closeable) {
046        super(new RenderWriter(out, closeable));
047    
048        //
049        this.out = (RenderWriter)super.out;
050      }
051    
052      public final boolean isEmpty() {
053        return out.isEmpty();
054      }
055    
056      public final void print(Object obj, Color foreground) {
057        try {
058          out.provide(Style.style(foreground));
059        }
060        catch (InterruptedIOException x) {
061          Thread.currentThread().interrupt();
062        }
063        catch (IOException x) {
064          setError();
065        }
066        print(obj);
067        try {
068          out.provide(Style.reset);
069        }
070        catch (InterruptedIOException x) {
071          Thread.currentThread().interrupt();
072        }
073        catch (IOException x) {
074          setError();
075        }
076      }
077    
078      public final void println(Object obj, Color foreground) {
079        print(obj, Style.style(foreground));
080        println();
081      }
082    
083      public final void print(Object obj, Color foreground, Color background) {
084        try {
085          out.provide(Style.style(foreground, background));
086        }
087        catch (InterruptedIOException x) {
088          Thread.currentThread().interrupt();
089        }
090        catch (IOException x) {
091          setError();
092        }
093        print(obj);
094        try {
095          out.provide(Style.reset);
096        }
097        catch (InterruptedIOException x) {
098          Thread.currentThread().interrupt();
099        }
100        catch (IOException x) {
101          setError();
102        }
103      }
104    
105      public final void println(Object obj, Color foreground, Color background) {
106        print(obj, Style.style(foreground, background));
107        println();
108      }
109    
110      public final void print(Object obj, Decoration decoration) {
111        try {
112          out.provide(Style.style(decoration));
113        }
114        catch (InterruptedIOException x) {
115          Thread.currentThread().interrupt();
116        }
117        catch (IOException x) {
118          setError();
119        }
120        print(obj);
121        try {
122          out.provide(Style.reset);
123        }
124        catch (InterruptedIOException x) {
125          Thread.currentThread().interrupt();
126        }
127        catch (IOException x) {
128          setError();
129        }
130      }
131    
132      public final void println(Object obj, Decoration decoration) {
133        print(obj, Style.style(decoration));
134        println();
135      }
136    
137      public final void print(Object obj, Decoration decoration, Color foreground) {
138        print(obj, Style.style(decoration, foreground));
139        println();
140      }
141    
142      public final void println(Object obj, Decoration decoration, Color foreground) {
143        print(obj, Style.style(decoration, foreground, null));
144        println();
145      }
146    
147      public final void print(Object obj, Decoration decoration, Color foreground, Color background) {
148        print(obj, Style.style(decoration, foreground, background));
149        println();
150      }
151    
152      public final void println(Object obj, Decoration decoration, Color foreground, Color background) {
153        print(obj, Style.style(decoration, foreground, background));
154        println();
155      }
156    
157      public final void print(Object obj, Style style) {
158        try {
159          out.provide(style);
160        }
161        catch (InterruptedIOException x) {
162          Thread.currentThread().interrupt();
163        }
164        catch (IOException x) {
165          setError();
166        }
167        print(obj);
168        try {
169          out.provide(Style.reset);
170        }
171        catch (InterruptedIOException x) {
172          Thread.currentThread().interrupt();
173        }
174        catch (IOException x) {
175          setError();
176        }
177      }
178    
179      public final void println(Object obj, Style style) {
180        print(obj, style);
181        println();
182      }
183    
184      /**
185       * Groovy left shift operator.
186       *
187       * @param o the appended
188       * @return this object
189       */
190      public final RenderPrintWriter leftShift(Object o) {
191        if (o instanceof Style) {
192          try {
193            out.provide((Style)o);
194          }
195          catch (InterruptedIOException x) {
196            Thread.currentThread().interrupt();
197          }
198          catch (IOException x) {
199            setError();
200          }
201        } else if (o instanceof Decoration) {
202          try {
203            out.provide((Style.style((Decoration)o)));
204          }
205          catch (InterruptedIOException x) {
206            Thread.currentThread().interrupt();
207          }
208          catch (IOException x) {
209            setError();
210          }
211        } else if (o instanceof Color) {
212          try {
213            out.provide(Style.style((Color)o));
214          }
215          catch (InterruptedIOException x) {
216            Thread.currentThread().interrupt();
217          }
218          catch (IOException x) {
219            setError();
220          }
221        } else {
222          print(o);
223        }
224        return this;
225      }
226    
227      public final RenderPrintWriter cls() {
228        try {
229          out.provide(CLS.INSTANCE);
230        }
231        catch (InterruptedIOException x) {
232          Thread.currentThread().interrupt();
233        }
234        catch (IOException x) {
235          setError();
236        }
237        return this;
238      }
239    
240      @Override
241      public void println(Object x) {
242        print(x);
243        println();
244      }
245    
246      public void show(Element element) {
247        element.render(new RenderAppendable(this.out));
248      }
249    
250      @Override
251      public void print(Object obj) {
252        if (obj instanceof UIBuilder) {
253          RenderAppendable out = new RenderAppendable(this.out);
254          new UIBuilderRenderable().renderer(Collections.singleton((UIBuilder)obj).iterator()).render(out);
255        } else if (obj instanceof Element) {
256          RenderAppendable out = new RenderAppendable(this.out);
257          ((Element)obj).renderer().render(out);
258        } else {
259          super.print(obj);
260        }
261      }
262    }