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::shallDocument,
047                                                this::getLabel,
048                                                this::getDiagramDialect,
049                                                this::getEClassifierRole)));    
050
051                registerAdapterFactory(
052                        new FunctionAdapterFactory<EcoreActionSupplier, EClass>(
053                                EcorePackage.Literals.ECLASS, 
054                                EcoreActionSupplier.class, 
055                                this.getClass().getClassLoader(), 
056                                e -> new EClassActionSupplier(
057                                                e, 
058                                                context, 
059                                                ePackagePathComputer, 
060                                                javadocResolver, 
061                                                this::getEPackage, 
062                                                this::shallDocument,
063                                                this::getLabel,
064                                                this::isGenerateLoadSpecification, 
065                                                this::getDiagramDialect)));             
066
067                registerAdapterFactory(
068                        new FunctionAdapterFactory<EcoreActionSupplier, EDataType>(
069                                EcorePackage.Literals.EDATA_TYPE, 
070                                EcoreActionSupplier.class, 
071                                this.getClass().getClassLoader(), 
072                                e -> new EDataTypeActionSupplier(
073                                                e, 
074                                                context, 
075                                                ePackagePathComputer, 
076                                                javadocResolver, 
077                                                this::getEPackage,
078                                                this::shallDocument,
079                                                this::getLabel)));              
080
081                registerAdapterFactory(
082                        new FunctionAdapterFactory<EcoreActionSupplier, EEnum>(
083                                EcorePackage.Literals.EENUM, 
084                                EcoreActionSupplier.class, 
085                                this.getClass().getClassLoader(), 
086                                e -> new EEnumActionSupplier(
087                                                e, 
088                                                context, 
089                                                ePackagePathComputer, 
090                                                javadocResolver,
091                                                this::getEPackage,
092                                                this::shallDocument,
093                                                this::getLabel)));              
094
095                registerAdapterFactory(
096                        new FunctionAdapterFactory<EcoreActionSupplier, EEnumLiteral>(
097                                EcorePackage.Literals.EENUM_LITERAL, 
098                                EcoreActionSupplier.class, 
099                                this.getClass().getClassLoader(), 
100                                e -> new EEnumLiteralActionSupplier(
101                                                e, 
102                                                context, 
103                                                ePackagePathComputer,
104                                                this::shallDocument,
105                                                this::getLabel)));              
106
107                registerAdapterFactory(
108                        new FunctionAdapterFactory<EcoreActionSupplier, EAttribute>(
109                                EcorePackage.Literals.EATTRIBUTE, 
110                                EcoreActionSupplier.class, 
111                                this.getClass().getClassLoader(), 
112                                e -> new EAttributeActionSupplier(
113                                                e,
114                                                context,
115                                                ePackagePathComputer,
116                                                this::shallDocument,
117                                                this::getLabel)));              
118
119                registerAdapterFactory(
120                        new FunctionAdapterFactory<EcoreActionSupplier, EReference>(
121                                EcorePackage.Literals.EREFERENCE, 
122                                EcoreActionSupplier.class, 
123                                this.getClass().getClassLoader(), 
124                                e -> new EReferenceActionSupplier(
125                                                e, 
126                                                context,
127                                                ePackagePathComputer,
128                                                this::shallDocument,
129                                                this::getLabel)));              
130
131                registerAdapterFactory(
132                        new FunctionAdapterFactory<EcoreActionSupplier, EOperation>(
133                                EcorePackage.Literals.EOPERATION, 
134                                EcoreActionSupplier.class, 
135                                this.getClass().getClassLoader(), 
136                                e -> new EOperationActionSupplier(
137                                                e, 
138                                                context, 
139                                                ePackagePathComputer,
140                                                this::shallDocument,
141                                                this::getLabel)));              
142
143                registerAdapterFactory(
144                        new FunctionAdapterFactory<EcoreActionSupplier, EParameter>(
145                                EcorePackage.Literals.EPARAMETER, 
146                                EcoreActionSupplier.class, 
147                                this.getClass().getClassLoader(), 
148                                e -> new EParameterActionSupplier(
149                                                e,
150                                                context,
151                                                ePackagePathComputer,
152                                                this::shallDocument,
153                                                this::getLabel)));      
154        }
155        
156        /**
157         * Override to return false to suppress generation of load specification.
158         * @return
159         */
160        protected boolean isGenerateLoadSpecification() {
161                return true;
162        }
163        
164        protected String getDiagramDialect() {
165                return null;
166        }
167        
168        protected Object getEPackage(String nsURI) {
169                return EPackage.Registry.INSTANCE.get(nsURI);
170        }
171        
172        /**
173         * @param modelElement
174         * @return true if the element is documentable, its container shall be documented and for {@link ETypedElement}'s the type should be documented. 
175         */
176        protected boolean shallDocument(EModelElement modelElement) {
177                if (!isDocumentable(modelElement)) {
178                        return false;
179                }
180                if (modelElement instanceof EPackage) {
181                        EPackage sp = ((EPackage) modelElement).getESuperPackage();
182                        return sp == null || shallDocument(sp);
183                }
184                if (modelElement instanceof EClass) {
185                        EPackage p = ((EClass) modelElement).getEPackage();
186                        return shallDocument(p);
187                }
188                if (modelElement instanceof EStructuralFeature) {
189                        EStructuralFeature sf = (EStructuralFeature) modelElement;
190                        EClass c = sf.getEContainingClass();
191                        EClassifier t = sf.getEType();
192                        return shallDocument(c) && shallDocument(t);
193                }
194                if (modelElement instanceof EOperation) {
195                        EOperation op = (EOperation) modelElement;
196                        EClass c = op.getEContainingClass();
197                        EClassifier t = op.getEType();
198                        return shallDocument(c) && shallDocument(t);
199                }
200                if (modelElement instanceof EEnumLiteral) {
201                        EEnum e = ((EEnumLiteral) modelElement).getEEnum();
202                        return shallDocument(e);
203                }
204                
205                return true;
206        }
207        
208        /**
209         * Override to selectively document models, i.e. provide a specific view.
210         * @param modelElement
211         * @return
212         */
213        protected boolean isDocumentable(EModelElement modelElement) {
214                return true;
215        }
216        
217        protected EReference getEClassifierRole(EClassifier eClassifier) {
218                String roleName = NcoreUtil.getNasdanikaAnnotationDetail(eClassifier, "role", "children");
219                return (EReference) AppPackage.Literals.ACTION.getEStructuralFeature(roleName);
220        }
221
222        protected String getLabel(ENamedElement eNamedElement, String defaultLabel) {
223                return NcoreUtil.getNasdanikaAnnotationDetail(eNamedElement, "doc-label", defaultLabel);
224        }
225        
226}