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 print(UIWriterContext ctx, ShellWriter writer) throws IOException { 067 if (ctx == null) { 068 ctx = new UIWriterContext(); 069 } 070 071 // 072 if (value != null) { 073 value.print(ctx, writer); 074 writer.append(ctx, '\n'); 075 } 076 077 // 078 for (int i = 0;i < nodes.size();i++) { 079 080 // Add the padding ? 081 ctx.stack.add(i == nodes.size() - 1 ? Pad.LAST_BRANCH : Pad.BRANCH); 082 // ctx.stack.add(Foo.TRUE); 083 084 // 085 Element node = nodes.get(i); 086 node.print(ctx, writer); 087 if (ctx.needLF) { 088 writer.append(ctx, '\n'); 089 } 090 ctx.stack.remove(ctx.stack.size() - 1); 091 } 092 } 093 }