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