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 */
019
020package org.crsh.text.renderers;
021
022import org.crsh.text.Color;
023import org.crsh.text.Decoration;
024import org.crsh.text.LineRenderer;
025import org.crsh.text.Renderer;
026import org.crsh.text.ui.LabelElement;
027import org.crsh.text.ui.RowElement;
028import org.crsh.text.ui.TableElement;
029
030import java.util.ArrayList;
031import java.util.Iterator;
032import java.util.LinkedHashSet;
033import java.util.Map;
034
035public class MapRenderer extends Renderer<Map<?, ?>> {
036
037  @Override
038  public Class<Map<?, ?>> getType() {
039    Object mapClass = Map.class;
040    return (Class<Map<?,?>>)mapClass;
041  }
042
043  @Override
044  public LineRenderer renderer(Iterator<Map<?, ?>> stream) {
045
046    TableElement table = new TableElement();
047    LinkedHashSet<String> current = new LinkedHashSet<String>();
048    LinkedHashSet<String> bilto = new LinkedHashSet<String>();
049
050    ArrayList<LineRenderer> renderers = new ArrayList<LineRenderer>();
051
052    while (stream.hasNext()) {
053
054      Map<?, ?> row = stream.next();
055
056      if (row.size() >  0) {
057
058        bilto.clear();
059        for (Map.Entry<?, ?> entry : row.entrySet()) {
060          bilto.add(String.valueOf(entry.getKey()));
061        }
062
063        // Create a new table if needed
064        if (!current.equals(bilto)) {
065          if (table.getRows().size() > 0) {
066            renderers.add(table.renderer());
067          }
068          table = new TableElement().rightCellPadding(1);
069          RowElement header = new RowElement(true);
070          header.style(Decoration.bold.fg(Color.black).bg(Color.white));
071          for (String s : bilto) {
072            header.add(s);
073          }
074          table.add(header);
075          current = bilto;
076        }
077
078        //
079        RowElement r = new RowElement();
080        for (String s : bilto) {
081          r.add(String.valueOf(row.get(s)));
082        }
083        table.add(r);
084      }
085    }
086
087    //
088    if (table.getRows().size() > 0) {
089      renderers.add(table.renderer());
090    }
091
092    //
093    return LineRenderer.vertical(renderers);
094  }
095}