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