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.shell.io.ShellWriter;
023
024 import java.io.IOException;
025 import java.util.ArrayList;
026 import java.util.List;
027
028 /**
029 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030 * @version $Revision$
031 */
032 public class TreeElement extends Element {
033
034 /** An optional value element. */
035 private Element value;
036
037 /** . */
038 private List<Element> nodes = new ArrayList<Element>();
039
040 public TreeElement() {
041 this(null);
042 }
043
044 public TreeElement(Element value) {
045 this.value = value;
046 }
047
048 public TreeElement addNode(Element node) {
049 nodes.add(node);
050 return this;
051 }
052
053 public int getSize() {
054 return nodes.size();
055 }
056
057 public Element getValue() {
058 return value;
059 }
060
061 public Element getNode(int index) {
062 return nodes.get(index);
063 }
064
065 @Override
066 void doPrint(UIWriterContext ctx, ShellWriter writer) throws IOException {
067
068 //
069 if (value != null) {
070 value.print(ctx, writer);
071 writer.append(ctx, '\n');
072 }
073
074 //
075 for (int i = 0;i < nodes.size();i++) {
076
077 // Add the padding ?
078 ctx.stack.add(i == nodes.size() - 1 ? Pad.LAST_BRANCH : Pad.BRANCH);
079 // ctx.stack.add(Foo.TRUE);
080
081 //
082 Element node = nodes.get(i);
083 node.print(ctx, writer);
084 if (ctx.needLF) {
085 writer.append(ctx, '\n');
086 }
087 ctx.stack.remove(ctx.stack.size() - 1);
088 }
089 }
090
091 @Override
092 int width() {
093 return 0;
094 }
095 }