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.formatter; 021 022 import org.crsh.text.Renderable; 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 EntityTypeRenderable extends Renderable<EntityTypeRenderable.EntityTypeData> { 032 033 @Override 034 public Class<EntityTypeRenderable.EntityTypeData> getType() { 035 return EntityTypeRenderable.EntityTypeData.class; 036 } 037 038 @Override 039 public Renderer renderer(Iterator<EntityTypeRenderable.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(new LabelElement("NAME"), new LabelElement("TYPE")); 052 table.add(header); 053 } 054 RowElement row = new RowElement(); 055 row.add(new LabelElement(entityTypeData.name), new LabelElement(entityTypeData.type)); 056 table.add(row); 057 } else { 058 table.setColumnLayout(Layout.weighted(1)); 059 RowElement name = new RowElement(); 060 name.add(new LabelElement("Name : " + entityTypeData.name)); 061 table.add(name); 062 RowElement type = new RowElement(); 063 type.add(new LabelElement("Type : " + entityTypeData.type)); 064 table.add(type); 065 RowElement mapping = new RowElement(); 066 mapping.add(new LabelElement("Mapping : " + entityTypeData.mapping)); 067 table.add(mapping); 068 069 if (entityTypeData.attributes.size() > 0) { 070 RowElement attributesLabel = new RowElement(); 071 attributesLabel.add(new LabelElement("Attributes : ")); 072 table.add(attributesLabel); 073 074 TableElement attributeTable = new TableElement(); 075 attributeTable.setRightCellPadding(1); 076 RowElement attributeRowHeader = new RowElement(true); 077 attributeRowHeader.add(new LabelElement("NAME"), new LabelElement("TYPE"), new LabelElement("ASSOCIATION"), new LabelElement("COLLECTION"), new LabelElement("MAPPING")); 078 attributeTable.add(attributeRowHeader); 079 080 for (AttributeData attributes : entityTypeData.attributes) { 081 RowElement row = new RowElement(); 082 row.add(new LabelElement(attributes.name), new LabelElement(attributes.type), new LabelElement(attributes.association), new LabelElement(attributes.collection), new LabelElement(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 }