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.EnumMap;
030    
031    /**
032     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
033     * @version $Revision$
034     */
035    class UIWriterContext implements ShellWriterContext {
036    
037      /** . */
038      final ArrayList<Pad> stack;
039    
040      /** . */
041      Style padStyle;
042    
043      /** . */
044      boolean needLF;
045    
046      /** . */
047      private final InvocationContext context;
048    
049      /** . */
050      UIWriterContext parentUIContext;
051    
052      UIWriterContext(InvocationContext context) {
053        this.context = context;
054        this.stack = new ArrayList<Pad>();
055        this.needLF = false;
056      }
057    
058      UIWriterContext(UIWriterContext parentUIContext) {
059        this(parentUIContext.getInvocationContext());
060        this.parentUIContext = parentUIContext;
061      }
062    
063      private static EnumMap<Pad, String> charMap = new EnumMap<Pad, String>(Pad.class);
064      private static EnumMap<Pad, Pad> nextMap = new EnumMap<Pad, Pad>(Pad.class);
065    
066      static {
067        charMap.put(Pad.BRANCH, "+-");
068        charMap.put(Pad.LAST_BRANCH, "+-");
069        charMap.put(Pad.CONTINUE_BRANCH, "| ");
070        charMap.put(Pad.STOP_BRANCH, "  ");
071        charMap.put(Pad.SPACE, " ");
072        nextMap.put(Pad.BRANCH, Pad.CONTINUE_BRANCH);
073        nextMap.put(Pad.CONTINUE_BRANCH, Pad.CONTINUE_BRANCH);
074        nextMap.put(Pad.LAST_BRANCH, Pad.STOP_BRANCH);
075        nextMap.put(Pad.STOP_BRANCH, Pad.STOP_BRANCH);
076      }
077    
078      public void pad(ShellWriter writer) throws IOException {
079        for (int i = 0;i < stack.size();i++) {
080          Pad abc = stack.get(i);
081          
082          //
083          if (padStyle != null) {
084            new FormattingElement(padStyle).print(this, writer);
085          }
086    
087          //
088          writer.append(charMap.get(abc));
089    
090          //
091          if (padStyle != null) {
092            new FormattingElement(Style.reset).print(this, writer);
093          }
094    
095          Pad next = nextMap.get(abc);
096          stack.set(i, next);
097        }
098      }
099    
100      public void text(CharSequence csq, int off, int end) {
101        needLF = true;
102      }
103    
104      public void lineFeed() {
105        needLF = false;
106      }
107    
108      public int getConsoleWidth() {
109        return context.getWidth();
110      }
111    
112      public InvocationContext getInvocationContext() {
113        return context;
114      }
115      
116    }