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    
020    package org.crsh.text.renderers;
021    
022    import org.crsh.text.LineRenderer;
023    import org.crsh.text.Renderer;
024    import org.crsh.text.ui.*;
025    
026    import java.util.*;
027    
028    /**
029     * @author <a href="mailto:alain.defrance@exoplatform.com">Alain Defrance</a>
030     */
031    public class EntityTypeRenderer extends Renderer<EntityTypeRenderer.EntityTypeData> {
032    
033      @Override
034      public Class<EntityTypeRenderer.EntityTypeData> getType() {
035        return EntityTypeRenderer.EntityTypeData.class;
036      }
037    
038      @Override
039      public LineRenderer renderer(Iterator<EntityTypeRenderer.EntityTypeData> stream) {
040    
041        TableElement table = new TableElement();
042    
043        table.setRightCellPadding(1);
044    
045        while (stream.hasNext()) {
046          EntityTypeData entityTypeData = stream.next();
047    
048          if (!entityTypeData.verbose) {
049            if (table.getRows().size() == 0) {
050              RowElement header = new RowElement(true);
051              header.add("NAME", "TYPE");
052              table.add(header);
053            }
054            RowElement row = new RowElement();
055            row.add(entityTypeData.name, entityTypeData.type);
056            table.add(row);
057          } else {
058            table.setColumnLayout(Layout.weighted(1));
059            RowElement name = new RowElement();
060            name.add("Name : " + entityTypeData.name);
061            table.add(name);
062            RowElement type = new RowElement();
063            type.add("Type : " + entityTypeData.type);
064            table.add(type);
065            RowElement mapping = new RowElement();
066            mapping.add("Mapping : " + entityTypeData.mapping);
067            table.add(mapping);
068    
069            if (entityTypeData.attributes.size() > 0) {
070              RowElement attributesLabel = new RowElement();
071              attributesLabel.add("Attributes : ");
072              table.add(attributesLabel);
073    
074              TableElement attributeTable = new TableElement();
075              attributeTable.setRightCellPadding(1);
076              RowElement attributeRowHeader = new RowElement(true);
077              attributeRowHeader.add("NAME", "TYPE", "ASSOCIATION", "COLLECTION", "MAPPING");
078              attributeTable.add(attributeRowHeader);
079    
080              for (AttributeData attributes : entityTypeData.attributes) {
081                RowElement row = new RowElement();
082                row.add(attributes.name, attributes.type, "" + attributes.association, "" + attributes.collection, attributes.mapping);
083                attributeTable.add(row);
084              }
085    
086              RowElement attributesRow = new RowElement();
087              attributesRow.add(attributeTable);
088              table.add(attributesRow);
089    
090            }
091          }
092    
093        }
094    
095        return table.renderer();
096      
097      }
098    
099      public static class EntityTypeData {
100    
101        public final String name;
102        public final String type;
103        public final String mapping;
104        public final boolean verbose;
105        public final List<AttributeData> attributes;
106    
107        public EntityTypeData(String name, String type, String mapping) {
108          this(name, type, mapping, false);
109        }
110    
111        public EntityTypeData(String name, String type, String mapping, boolean verbose) {
112          this.name = name;
113          this.type = type;
114          this.mapping = mapping;
115          this.verbose = verbose;
116          this.attributes = new ArrayList<AttributeData>();
117        }
118    
119        public void add(AttributeData d) {
120          attributes.add(d);
121        }
122        
123      }
124    
125      public static class AttributeData {
126        public final String name;
127        public final String type;
128        public final Boolean association;
129        public final Boolean collection;
130        public final String mapping;
131    
132        public AttributeData(String name, String type, Boolean association, Boolean collection, String mapping) {
133          this.name = name;
134          this.type = type;
135          this.association = (association != null ? association : false);
136          this.collection = (collection != null ? collection : false);
137          this.mapping = mapping;
138        }
139        
140      }
141    }