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
036 @Override
037 public Class<BindingData> getType() {
038 return BindingData.class;
039 }
040
041 @Override
042 public Renderer renderer(Iterator<BindingData> stream) {
043
044 TableElement table = new TableElement();
045 table.setRightCellPadding(1);
046 RowElement header = new RowElement(true);
047 header.add(new LabelElement("NAME"));
048 table.add(header);
049
050 while (stream.hasNext()) {
051 BindingData binding = stream.next();
052
053 RowElement row = new RowElement();
054
055 row.add(new LabelElement(binding.name));
056
057 if (binding.verbose) {
058 row.add(new LabelElement(binding.type));
059 if (header.getSize() == 1) {
060 header.add(new LabelElement("CLASS"));
061 }
062 }
063
064 table.add(row);
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 Object instance;
076 public final Boolean verbose;
077
078 public BindingData(String name, String type, Object instance, Boolean verbose) {
079 this.name = name;
080 this.type = type;
081 this.instance = instance;
082 this.verbose = (verbose != null ? verbose : false);
083 }
084 }
085
086 }