001package org.nasdanika.html.ecore; 002 003import java.util.List; 004import java.util.function.BiFunction; 005import java.util.function.Predicate; 006 007import org.eclipse.emf.ecore.EClass; 008import org.eclipse.emf.ecore.EGenericType; 009import org.eclipse.emf.ecore.EModelElement; 010import org.eclipse.emf.ecore.ENamedElement; 011import org.eclipse.emf.ecore.EPackage; 012import org.eclipse.emf.ecore.ETypedElement; 013import org.nasdanika.common.Context; 014import org.nasdanika.common.ProgressMonitor; 015import org.nasdanika.html.bootstrap.BootstrapFactory; 016import org.nasdanika.html.bootstrap.RowContainer.Row; 017import org.nasdanika.html.bootstrap.RowContainer.Row.Cell; 018import org.nasdanika.html.bootstrap.Table; 019import org.nasdanika.html.model.app.Action; 020 021public class ETypedElementActionSupplier<T extends ETypedElement> extends ENamedElementActionSupplier<T> { 022 023 public ETypedElementActionSupplier( 024 T value, 025 Context context, 026 java.util.function.Function<EPackage,String> ePackagePathComputer, 027 Predicate<EModelElement> elementPredicate, 028 BiFunction<ENamedElement, String, String> labelProvider) { 029 super(value, context, ePackagePathComputer, elementPredicate, labelProvider); 030 } 031 032 @Override 033 public Action execute(EClass contextEClass, ProgressMonitor progressMonitor) { 034 Action action = super.execute(contextEClass, progressMonitor); 035 036 addContent(action, propertiesTable(contextEClass, progressMonitor).toString()); 037 return action; 038 } 039 040 @Override 041 protected String getDefaultLabel(ProgressMonitor progressMonitor) { 042 StringBuilder label = new StringBuilder(eObject.getName()); 043 EGenericType genericType = eObject.getEGenericType(); 044 if (genericType != null) { 045 label.append(" : "); 046 label.append(computeLabel(genericType, progressMonitor)); 047 if (eObject.isMany()) { 048 label.append("*"); 049 } 050 } 051 return label.toString(); 052 } 053 054 protected Table propertiesTable(EClass contextEClass, ProgressMonitor monitor) { 055 Table table = context.get(BootstrapFactory.class).table(); 056 table.toHTMLElement().style().width("auto"); 057 058 EGenericType genericType = eObject.getEGenericType(); 059 if (genericType != null) { 060 genericType(genericType, contextEClass, addRow(table, "Type")::add, monitor); 061 } 062 063 addRow(table, "Cardinality").add(cardinality(eObject)); 064 065 return table; 066 } 067 068 /** 069 * Adds a row to the table, returns cell content collection for adding content. 070 * @param table 071 * @param header 072 * @return 073 */ 074 protected static List<Object> addRow(Table table, String header) { 075 Row row = table.row(); 076 row.header(header); 077 Cell contentCell = row.cell(); 078 return contentCell.toHTMLElement().getContent(); 079 } 080 081}