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.io.IOContext;
023
024 import java.io.IOException;
025 import java.util.LinkedList;
026
027 public class RenderAppendable implements Appendable, IOContext<Chunk> {
028
029 /** . */
030 private final IOContext<Chunk> context;
031
032 /** . */
033 private LinkedList<Style.Composite> stack;
034
035 public RenderAppendable(IOContext<Chunk> context) {
036 this.context = context;
037 }
038
039 private void safeAppend(Chunk chunk) {
040 try {
041 context.provide(chunk);
042 }
043 catch (java.io.IOException e) {
044 // e.printStackTrace();
045 }
046 }
047
048 public int getWidth() {
049 return context.getWidth();
050 }
051
052 public int getHeight() {
053 return context.getHeight();
054 }
055
056 public void provide(Chunk element) throws IOException {
057 context.provide(element);
058 }
059
060 public void flush() throws IOException {
061 context.flush();
062 }
063
064 public RenderAppendable append(CharSequence csq) {
065 safeAppend(Text.create(csq));
066 return this;
067 }
068
069 public void enterStyle(Style.Composite style) {
070 if (stack == null) {
071 stack = new LinkedList<Style.Composite>();
072 }
073 safeAppend(style);
074 stack.addLast(style);
075 }
076
077 public Style.Composite leaveStyle() {
078 if (stack == null || stack.isEmpty()) {
079 throw new IllegalStateException("Cannot leave non existing style");
080 }
081 Style.Composite last = stack.removeLast();
082 if (stack.size() > 0) {
083
084 // Compute merged
085 Style.Composite merged = getMerged();
086
087 // Compute diff with removed
088 Boolean bold = foo(last.getBold(), merged.getBold());
089 Boolean underline = foo(last.getUnderline(), merged.getUnderline());
090 Boolean blink = foo(last.getBlink(), merged.getBlink());
091
092 // For now we assume that black is the default background color
093 // and white is the default foreground color
094 Color fg = foo(last.getForeground(), merged.getForeground(), Color.def);
095 Color bg = foo(last.getBackground(), merged.getBackground(), Color.def);
096
097 //
098 Style.Composite bilto = Style.style(bold, underline, blink, fg, bg);
099
100 //
101 safeAppend(bilto);
102 } else {
103 safeAppend(Style.reset);
104 }
105 return last;
106 }
107
108 /**
109 * Compute the current merged style.
110 *
111 * @return the merged style
112 */
113 private Style.Composite getMerged() {
114 Style.Composite merged = Style.style();
115 for (Style s : stack) {
116 merged = (Style.Composite)merged.merge(s);
117 }
118 return merged;
119 }
120
121 private Boolean foo(Boolean last, Boolean merged) {
122 if (last != null) {
123 if (merged != null) {
124 return merged;
125 } else {
126 return !last;
127 }
128 } else {
129 return null;
130 }
131 }
132
133 private Color foo(Color last, Color merged, Color def) {
134 if (last != null) {
135 if (merged != null) {
136 return merged;
137 } else {
138 return def;
139 }
140 } else {
141 return null;
142 }
143 }
144
145 public void styleOff() {
146 if (stack != null && stack.size() > 0) {
147 safeAppend(Style.reset);
148 }
149 }
150
151 public void styleOn() {
152 if (stack != null && stack.size() > 0) {
153 safeAppend(getMerged());
154 }
155 }
156
157 public RenderAppendable append(CharSequence csq, int start, int end) {
158 safeAppend(Text.create(csq.subSequence(start, end)));
159 return this;
160 }
161
162 public RenderAppendable append(char c) {
163 safeAppend(Text.create(Character.toString(c)));
164 return this;
165 }
166 }