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 */
019package org.crsh.text.renderers;
020
021import org.crsh.text.LineRenderer;
022import org.crsh.text.Renderer;
023import org.crsh.text.ui.LabelElement;
024import org.crsh.text.ui.RowElement;
025import org.crsh.text.ui.TableElement;
026
027import java.util.Iterator;
028
029/**
030 * @author <a href="mailto:alain.defrance@exoplatform.com">Alain Defrance</a>
031 */
032public class BindingRenderer extends Renderer<BindingRenderer.BindingData> {
033  
034  @Override
035  public Class<BindingData> getType() {
036    return BindingData.class;
037  }
038
039  @Override
040  public LineRenderer renderer(Iterator<BindingData> stream) {
041
042    TableElement table = new TableElement();
043    table.setRightCellPadding(1);
044    RowElement header = new RowElement(true);
045    header.add("NAME");
046    table.add(header);
047
048    while (stream.hasNext()) {
049      BindingData binding = stream.next();
050
051      RowElement row = new RowElement();
052
053       row.add(binding.name);
054
055      if (binding.verbose) {
056        row.add(new LabelElement(binding.type));
057        if (header.getSize() == 1) {
058          header.add("CLASS");
059        }
060      }
061      
062      table.add(row);
063
064    }
065
066    return table.renderer();
067  }
068
069  public static class BindingData {
070
071    public final String name;
072    public final String type;
073    public final Object instance;
074    public final Boolean verbose;
075
076    public BindingData(String name, String type, Object instance, Boolean verbose) {
077      this.name = name;
078      this.type = type;
079      this.instance = instance;
080      this.verbose = (verbose != null ? verbose : false);
081    }
082  }
083
084}