001package org.nasdanika.html.ecore;
002
003import org.eclipse.emf.ecore.EAttribute;
004import org.eclipse.emf.ecore.EClass;
005import org.eclipse.emf.ecore.EClassifier;
006import org.eclipse.emf.ecore.EDataType;
007import org.eclipse.emf.ecore.EEnum;
008import org.eclipse.emf.ecore.EEnumLiteral;
009import org.eclipse.emf.ecore.EModelElement;
010import org.eclipse.emf.ecore.ENamedElement;
011import org.eclipse.emf.ecore.EOperation;
012import org.eclipse.emf.ecore.EPackage;
013import org.eclipse.emf.ecore.EParameter;
014import org.eclipse.emf.ecore.EReference;
015import org.eclipse.emf.ecore.EStructuralFeature;
016import org.eclipse.emf.ecore.ETypedElement;
017import org.eclipse.emf.ecore.EcorePackage;
018import org.nasdanika.common.Context;
019import org.nasdanika.emf.ComposedAdapterFactory;
020import org.nasdanika.emf.FunctionAdapterFactory;
021import org.nasdanika.html.model.app.AppPackage;
022import org.nasdanika.ncore.util.NcoreUtil;
023
024/**
025 * Provides adapters for the Ecore types - {@link EPackage}, {@link EClass}, {@link EStructuralFeature}, {@link EOperation}, ...
026 * @author Pavel
027 *
028 */
029public class EcoreActionSupplierAdapterFactory extends ComposedAdapterFactory {
030        
031        public EcoreActionSupplierAdapterFactory(
032                        Context context, 
033                        java.util.function.Function<EPackage,String> ePackagePathComputer,
034                        java.util.function.Function<String, String> javadocResolver) {
035                
036                // Registering adapter factories.
037                registerAdapterFactory(
038                        new FunctionAdapterFactory<EcoreActionSupplier, EPackage>(
039                                EcorePackage.Literals.EPACKAGE, 
040                                EcoreActionSupplier.class, 
041                                this.getClass().getClassLoader(), 
042                                e -> new EPackageActionSupplier(
043                                                e, 
044                                                context, 
045                                                ePackagePathComputer, 
046                                                this::getEPackage, 
047                                                this::shallDocument,
048                                                this::getLabel,
049                                                this::getDiagramDialect,
050                                                this::getEClassifierRole)));    
051
052                registerAdapterFactory(
053                        new FunctionAdapterFactory<EcoreActionSupplier, EClass>(
054                                EcorePackage.Literals.ECLASS, 
055                                EcoreActionSupplier.class, 
056                                this.getClass().getClassLoader(), 
057                                e -> new EClassActionSupplier(
058                                                e, 
059                                                context, 
060                                                ePackagePathComputer, 
061                                                javadocResolver, 
062                                                this::getEPackage, 
063                                                this::shallDocument,
064                                                this::getLabel,
065                                                this::isGenerateLoadSpecification, 
066                                                this::getDiagramDialect)));             
067
068                registerAdapterFactory(
069                        new FunctionAdapterFactory<EcoreActionSupplier, EDataType>(
070                                EcorePackage.Literals.EDATA_TYPE, 
071                                EcoreActionSupplier.class, 
072                                this.getClass().getClassLoader(), 
073                                e -> new EDataTypeActionSupplier(
074                                                e, 
075                                                context, 
076                                                ePackagePathComputer, 
077                                                javadocResolver, 
078                                                this::getEPackage,
079                                                this::shallDocument,
080                                                this::getLabel)));              
081
082                registerAdapterFactory(
083                        new FunctionAdapterFactory<EcoreActionSupplier, EEnum>(
084                                EcorePackage.Literals.EENUM, 
085                                EcoreActionSupplier.class, 
086                                this.getClass().getClassLoader(), 
087                                e -> new EEnumActionSupplier(
088                                                e, 
089                                                context, 
090                                                ePackagePathComputer, 
091                                                javadocResolver,
092                                                this::getEPackage,
093                                                this::shallDocument,
094                                                this::getLabel)));              
095
096                registerAdapterFactory(
097                        new FunctionAdapterFactory<EcoreActionSupplier, EEnumLiteral>(
098                                EcorePackage.Literals.EENUM_LITERAL, 
099                                EcoreActionSupplier.class, 
100                                this.getClass().getClassLoader(), 
101                                e -> new EEnumLiteralActionSupplier(
102                                                e, 
103                                                context, 
104                                                ePackagePathComputer,
105                                                this::shallDocument,
106                                                this::getLabel)));              
107
108                registerAdapterFactory(
109                        new FunctionAdapterFactory<EcoreActionSupplier, EAttribute>(
110                                EcorePackage.Literals.EATTRIBUTE, 
111                                EcoreActionSupplier.class, 
112                                this.getClass().getClassLoader(), 
113                                e -> new EAttributeActionSupplier(
114                                                e,
115                                                context,
116                                                ePackagePathComputer,
117                                                this::shallDocument,
118                                                this::getLabel)));              
119
120                registerAdapterFactory(
121                        new FunctionAdapterFactory<EcoreActionSupplier, EReference>(
122                                EcorePackage.Literals.EREFERENCE, 
123                                EcoreActionSupplier.class, 
124                                this.getClass().getClassLoader(), 
125                                e -> new EReferenceActionSupplier(
126                                                e, 
127                                                context,
128                                                ePackagePathComputer,
129                                                this::shallDocument,
130                                                this::getLabel)));              
131
132                registerAdapterFactory(
133                        new FunctionAdapterFactory<EcoreActionSupplier, EOperation>(
134                                EcorePackage.Literals.EOPERATION, 
135                                EcoreActionSupplier.class, 
136                                this.getClass().getClassLoader(), 
137                                e -> new EOperationActionSupplier(
138                                                e, 
139                                                context, 
140                                                ePackagePathComputer,
141                                                this::shallDocument,
142                                                this::getLabel)));              
143
144                registerAdapterFactory(
145                        new FunctionAdapterFactory<EcoreActionSupplier, EParameter>(
146                                EcorePackage.Literals.EPARAMETER, 
147                                EcoreActionSupplier.class, 
148                                this.getClass().getClassLoader(), 
149                                e -> new EParameterActionSupplier(
150                                                e,
151                                                context,
152                                                ePackagePathComputer,
153                                                this::shallDocument,
154                                                this::getLabel)));      
155        }
156        
157        /**
158         * Override to return false to suppress generation of load specification.
159         * @return
160         */
161        protected boolean isGenerateLoadSpecification() {
162                return true;
163        }
164        
165        protected String getDiagramDialect() {
166                return null;
167        }
168        
169        protected Object getEPackage(String nsURI) {
170                return EPackage.Registry.INSTANCE.get(nsURI);
171        }
172        
173        /**
174         * @param modelElement
175         * @return true if the element is documentable, its container shall be documented and for {@link ETypedElement}'s the type should be documented. 
176         */
177        protected boolean shallDocument(EModelElement modelElement) {
178                if (!isDocumentable(modelElement)) {
179                        return false;
180                }
181                if (modelElement instanceof EPackage) {
182                        EPackage sp = ((EPackage) modelElement).getESuperPackage();
183                        return sp == null || shallDocument(sp);
184                }
185                if (modelElement instanceof EClass) {
186                        EPackage p = ((EClass) modelElement).getEPackage();
187                        return shallDocument(p);
188                }
189                if (modelElement instanceof EStructuralFeature) {
190                        EStructuralFeature sf = (EStructuralFeature) modelElement;
191                        EClass c = sf.getEContainingClass();
192                        EClassifier t = sf.getEType();
193                        return shallDocument(c) && shallDocument(t);
194                }
195                if (modelElement instanceof EOperation) {
196                        EOperation op = (EOperation) modelElement;
197                        EClass c = op.getEContainingClass();
198                        EClassifier t = op.getEType();
199                        return shallDocument(c) && shallDocument(t);
200                }
201                if (modelElement instanceof EEnumLiteral) {
202                        EEnum e = ((EEnumLiteral) modelElement).getEEnum();
203                        return shallDocument(e);
204                }
205                
206                return true;
207        }
208        
209        /**
210         * Override to selectively document models, i.e. provide a specific view.
211         * @param modelElement
212         * @return
213         */
214        protected boolean isDocumentable(EModelElement modelElement) {
215                return true;
216        }
217        
218        protected EReference getEClassifierRole(EClassifier eClassifier) {
219                String roleName = NcoreUtil.getNasdanikaAnnotationDetail(eClassifier, "role", "children");
220                return (EReference) AppPackage.Literals.ACTION.getEStructuralFeature(roleName);
221        }
222
223        protected String getLabel(ENamedElement eNamedElement, String defaultLabel) {
224                return NcoreUtil.getNasdanikaAnnotationDetail(eNamedElement, "label", defaultLabel);
225        }
226        
227}