001package org.nasdanika.html.model.bootstrap.gen; 002 003import java.util.Map; 004import java.util.Map.Entry; 005 006import org.eclipse.emf.common.notify.impl.AdapterImpl; 007import org.eclipse.emf.ecore.EObject; 008import org.eclipse.emf.ecore.EStructuralFeature; 009import org.nasdanika.common.BiSupplier; 010import org.nasdanika.common.Consumer; 011import org.nasdanika.common.ConsumerFactory; 012import org.nasdanika.common.Context; 013import org.nasdanika.common.FunctionFactory; 014import org.nasdanika.common.ListCompoundSupplierFactory; 015import org.nasdanika.common.MapCompoundSupplierFactory; 016import org.nasdanika.emf.EObjectAdaptable; 017import org.nasdanika.html.HTMLElement; 018import org.nasdanika.html.bootstrap.BootstrapElement; 019import org.nasdanika.html.bootstrap.BootstrapFactory; 020import org.nasdanika.html.bootstrap.Breakpoint; 021import org.nasdanika.html.bootstrap.Color; 022import org.nasdanika.html.bootstrap.Placement; 023import org.nasdanika.html.bootstrap.Size; 024import org.nasdanika.html.bootstrap.Text.Alignment; 025import org.nasdanika.html.bootstrap.Text.Transform; 026import org.nasdanika.html.bootstrap.Text.Weight; 027import org.nasdanika.html.model.bootstrap.Appearance; 028import org.nasdanika.html.model.bootstrap.Border; 029import org.nasdanika.html.model.bootstrap.Float; 030import org.nasdanika.html.model.bootstrap.Spacing; 031import org.nasdanika.html.model.bootstrap.Text; 032import org.nasdanika.html.model.html.HtmlPackage; 033import org.nasdanika.html.model.html.gen.HtmlElementAdapter; 034 035public class AppearanceConsumerFactoryAdapter extends AdapterImpl implements ConsumerFactory<HTMLElement<?>> { 036 037 public AppearanceConsumerFactoryAdapter(Appearance target) { 038 setTarget(target); 039 } 040 041 @Override 042 public Appearance getTarget() { 043 return (Appearance) super.getTarget(); 044 } 045 046 private java.util.function.Consumer<HTMLElement<?>> backgroundBuilder = htmlElement -> { 047 Color background = AppearanceConsumerFactoryAdapter.this.getTarget().getBackground(); 048 if (background != null) { 049 BootstrapElement<?, ?> bootstrapElement = BootstrapFactory.INSTANCE.wrap(htmlElement); 050 bootstrapElement.background(background); 051 } 052 }; 053 054 private java.util.function.Consumer<HTMLElement<?>> borderBuilder = htmlElement -> { 055 BootstrapElement<?, ?> bootstrapElement = BootstrapFactory.INSTANCE.wrap(htmlElement); 056 for (Border border: AppearanceConsumerFactoryAdapter.this.getTarget().getBorder()) { 057 Color color = border.getColor(); 058 if (border.isBottom() || border.isLeft() || border.isRight() || border.isTop()) { 059 if (border.isBottom()) { 060 bootstrapElement.border(color, Placement.BOTTOM); 061 } 062 if (border.isTop()) { 063 bootstrapElement.border(color, Placement.TOP); 064 } 065 if (border.isLeft()) { 066 bootstrapElement.border(color, Placement.LEFT); 067 } 068 if (border.isRight()) { 069 bootstrapElement.border(color, Placement.RIGHT); 070 } 071 } else { 072 bootstrapElement.border(color); 073 } 074 } 075 Color bgColor = AppearanceConsumerFactoryAdapter.this.getTarget().getBackground(); 076 if (bgColor != null) { 077 bootstrapElement.background(bgColor); 078 } 079 }; 080 081 private java.util.function.Consumer<HTMLElement<?>> spacingBuilder = htmlElement -> { 082 BootstrapElement<?, ?> bootstrapElement = BootstrapFactory.INSTANCE.wrap(htmlElement); 083 for (Spacing margin : AppearanceConsumerFactoryAdapter.this.getTarget().getMargin()) { 084 Size size = margin.getSize(); 085 Breakpoint breakpoint = margin.getBreakpoint(); 086 if (breakpoint == null) { 087 breakpoint = Breakpoint.DEFAULT; 088 } 089 090 if (margin.isBottom() || margin.isLeft() || margin.isRight() || margin.isTop() || margin.isX() || margin.isY()) { 091 if (margin.isBottom()) { 092 bootstrapElement.margin().bottom(breakpoint, size); 093 } 094 if (margin.isTop()) { 095 bootstrapElement.margin().top(breakpoint, size); 096 } 097 if (margin.isLeft()) { 098 bootstrapElement.margin().left(breakpoint, size); 099 } 100 if (margin.isRight()) { 101 bootstrapElement.margin().right(breakpoint, size); 102 } 103 104 if (margin.isX()) { 105 bootstrapElement.margin().x(breakpoint, size); 106 } 107 if (margin.isY()) { 108 bootstrapElement.margin().y(breakpoint, size); 109 } 110 } else { 111 bootstrapElement.margin().all(breakpoint, size); 112 } 113 } 114 115 for (Spacing padding : AppearanceConsumerFactoryAdapter.this.getTarget().getPadding()) { 116 Size size = padding.getSize(); 117 Breakpoint breakpoint = padding.getBreakpoint(); 118 if (breakpoint == null) { 119 breakpoint = Breakpoint.DEFAULT; 120 } 121 122 if (padding.isBottom() || padding.isLeft() || padding.isRight() || padding.isTop() || padding.isX() || padding.isY()) { 123 if (padding.isBottom()) { 124 bootstrapElement.padding().bottom(breakpoint, size); 125 } 126 if (padding.isTop()) { 127 bootstrapElement.padding().top(breakpoint, size); 128 } 129 if (padding.isLeft()) { 130 bootstrapElement.padding().left(breakpoint, size); 131 } 132 if (padding.isRight()) { 133 bootstrapElement.padding().right(breakpoint, size); 134 } 135 136 if (padding.isX()) { 137 bootstrapElement.padding().x(breakpoint, size); 138 } 139 if (padding.isY()) { 140 bootstrapElement.padding().y(breakpoint, size); 141 } 142 } else { 143 bootstrapElement.padding().all(breakpoint, size); 144 } 145 } 146 147 }; 148 149 private java.util.function.Consumer<HTMLElement<?>> textBuilder = htmlElement -> { 150 BootstrapElement<?, ?> bootstrapElement = BootstrapFactory.INSTANCE.wrap(htmlElement); 151 Text text = AppearanceConsumerFactoryAdapter.this.getTarget().getText(); 152 if (text != null) { 153 org.nasdanika.html.bootstrap.Text<?> bsText = bootstrapElement.text(); 154 155 Color color = text.getColor(); 156 if (color != null) { 157 bsText.color(color); 158 } 159 160 Alignment alignment = text.getAlignment(); 161 if (alignment != null) { 162 bsText.alignment(alignment); 163 } 164 165 Transform transform = text.getTransform(); 166 if (transform != null) { 167 bsText.transform(transform); 168 } 169 170 Weight weight = text.getWeight(); 171 if (weight != null) { 172 bsText.weight(weight); 173 } 174 175 bsText.monospace(text.isMonospace()); 176 bsText.italic(text.isItalic()); 177 bsText.nowrap(text.isNowrap()); 178 bsText.truncate(text.isTruncate()); 179 180 } 181 }; 182 183 private java.util.function.Consumer<HTMLElement<?>> floatBuilder = htmlElement -> { 184 BootstrapElement<?, ?> bootstrapElement = BootstrapFactory.INSTANCE.wrap(htmlElement); 185 for (Float _float: AppearanceConsumerFactoryAdapter.this.getTarget().getFloat()) { 186 org.nasdanika.html.bootstrap.Float<?> bsFloat = bootstrapElement._float(); 187 Breakpoint breakpoint = _float.getBreakpoint(); 188 if (breakpoint == null) { 189 breakpoint = Breakpoint.DEFAULT; 190 } 191 switch (_float.getSide()) { 192 case "left": 193 bsFloat.left(breakpoint); 194 break; 195 case "right": 196 bsFloat.right(breakpoint); 197 break; 198 case "none": 199 bsFloat.none(breakpoint); 200 break; 201 default: 202 throw new IllegalArgumentException("Invalid float side: "+_float.getSide()+", shall be left, right, or none"); 203 } 204 } 205 }; 206 207 @Override 208 public Consumer<HTMLElement<?>> create(Context context) throws Exception { 209 MapCompoundSupplierFactory<String,Object> attributesFactory = new MapCompoundSupplierFactory<>("Attributes"); 210 for (Entry<String, EObject> ae: getTarget().getAttributes()) { 211 EObject value = ae.getValue(); 212 attributesFactory.put(ae.getKey(), EObjectAdaptable.adaptToSupplierFactoryNonNull(value, Object.class)); 213 } 214 215 ListCompoundSupplierFactory<Object> contentFactory = new ListCompoundSupplierFactory<>("Content"); // Empty 216 217 MapCompoundSupplierFactory<EStructuralFeature,Object> configurationFactory = new MapCompoundSupplierFactory<>("Attributes and Content"); 218 configurationFactory.put(HtmlPackage.Literals.HTML_ELEMENT__ATTRIBUTES, attributesFactory); 219 configurationFactory.put(HtmlPackage.Literals.HTML_ELEMENT__CONTENT, contentFactory); 220 221 FunctionFactory<HTMLElement<?>, BiSupplier<HTMLElement<?>, Map<EStructuralFeature, Object>>> configurationFunctionFactory = configurationFactory.asFunctionFactory(); 222 FunctionFactory<BiSupplier<HTMLElement<?>, Map<EStructuralFeature, Object>>, HTMLElement<?>> applyAttributesAndContentFunctionFactory = HtmlElementAdapter::createApplyAttributesAndContentFunction; 223 224 return configurationFunctionFactory 225 .then(applyAttributesAndContentFunctionFactory) 226 .create(context) 227 .then(Consumer.fromConsumer(backgroundBuilder, "Background", 1).asFunction()) 228 .then(Consumer.fromConsumer(borderBuilder, "Border", 1).asFunction()) 229 .then(Consumer.fromConsumer(spacingBuilder, "Spacing", 1).asFunction()) 230 .then(Consumer.fromConsumer(textBuilder, "Text", 1).asFunction()) 231 .then(Consumer.fromConsumer(floatBuilder, "Float", 1)); 232 } 233 234}