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