001    /*
002     * Copyright (C) 2010 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.shell.ui;
021    
022    import org.crsh.command.InvocationContext;
023    import org.crsh.shell.io.ShellWriter;
024    import org.crsh.shell.io.ShellWriterContext;
025    import org.crsh.text.Style;
026    
027    import java.io.IOException;
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.EnumMap;
031    
032    /**
033     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
034     * @version $Revision$
035     */
036    class UIWriterContext implements ShellWriterContext {
037    
038      /** . */
039      final ArrayList<Pad> stack;
040    
041      /** . */
042      Style padStyle;
043    
044      /** . */
045      boolean needLF;
046    
047      /** . */
048      boolean needLine;
049    
050      /** . */
051      private final InvocationContext context;
052    
053      /** . */
054      String leftLinePadding = "";
055    
056      /** . */
057      String rightLinePadding = "";
058    
059      /** . */
060      UIWriterContext parentUIContext;
061    
062      UIWriterContext(InvocationContext context) {
063        this.context = context;
064        this.stack = new ArrayList<Pad>();
065        this.needLF = false;
066      }
067    
068      UIWriterContext(UIWriterContext parentUIContext) {
069        this(parentUIContext.getInvocationContext());
070        this.parentUIContext = parentUIContext;
071      }
072    
073      private static EnumMap<Pad, String> charMap = new EnumMap<Pad, String>(Pad.class);
074      private static EnumMap<Pad, Pad> nextMap = new EnumMap<Pad, Pad>(Pad.class);
075    
076      static {
077        charMap.put(Pad.BRANCH, "+-");
078        charMap.put(Pad.LAST_BRANCH, "+-");
079        charMap.put(Pad.CONTINUE_BRANCH, "| ");
080        charMap.put(Pad.STOP_BRANCH, "  ");
081        charMap.put(Pad.SPACE, " ");
082        nextMap.put(Pad.BRANCH, Pad.CONTINUE_BRANCH);
083        nextMap.put(Pad.CONTINUE_BRANCH, Pad.CONTINUE_BRANCH);
084        nextMap.put(Pad.LAST_BRANCH, Pad.STOP_BRANCH);
085        nextMap.put(Pad.STOP_BRANCH, Pad.STOP_BRANCH);
086      }
087    
088      public void pad(ShellWriter writer) throws IOException {
089        for (int i = 0;i < stack.size();i++) {
090          Pad abc = stack.get(i);
091          
092          //
093          if (padStyle != null) {
094            new FormattingElement(padStyle).print(this, writer);
095          }
096    
097          //
098          writer.append(charMap.get(abc));
099    
100          //
101          if (padStyle != null) {
102            new FormattingElement(Style.reset).print(this, writer);
103          }
104    
105          Pad next = nextMap.get(abc);
106          stack.set(i, next);
107        }
108    
109        stack.removeAll(Collections.singleton(null));
110    
111      }
112    
113      public void printLine(int length, ShellWriter writer) throws IOException {
114    
115        writer.append(" ");
116        for (int i = 0; i < length; ++i ) {
117          writer.append("-");
118        }
119        writer.append("\n");
120        
121      }
122    
123      public void text(CharSequence csq, int off, int end) {
124        needLF = true;
125      }
126    
127      public void lineFeed() {
128        needLF = false;
129      }
130    
131      public int getConsoleWidth() {
132        return context.getWidth();
133      }
134    
135      public InvocationContext getInvocationContext() {
136        return context;
137      }
138    
139      public int padWidth() {
140    
141        int width = leftLinePadding.length() + rightLinePadding.length();
142        for (Pad pad : stack) {
143          String p = charMap.get(pad);
144          if (p != null) {
145            width += p.length();
146          }
147        }
148    
149        return width;
150    
151      }
152      
153    }