From a7c6b9afa2148d34edfcfbd8807e187106e7108a Mon Sep 17 00:00:00 2001 From: Fabienne Ducroquet Date: Tue, 27 Sep 2016 15:59:53 +0200 Subject: [PATCH] gs-reactor: Try to improve framework --- .../reactor/example/AppHtml5.java | 157 +++++++----------- .../annotations/ReactorDependencies.java | 16 +- .../reactor/az3/GSComposite.java | 10 +- .../reactor/az3/GSCompositeDiv.java | 69 +++----- 4 files changed, 103 insertions(+), 149 deletions(-) diff --git a/gs-example-reactor/src/main/java/org/genericsystem/reactor/example/AppHtml5.java b/gs-example-reactor/src/main/java/org/genericsystem/reactor/example/AppHtml5.java index 48b18c5dc..4e8d296eb 100644 --- a/gs-example-reactor/src/main/java/org/genericsystem/reactor/example/AppHtml5.java +++ b/gs-example-reactor/src/main/java/org/genericsystem/reactor/example/AppHtml5.java @@ -14,7 +14,6 @@ import org.genericsystem.reactor.annotations.DependsOnModel; import org.genericsystem.reactor.annotations.DirectSelect; import org.genericsystem.reactor.annotations.ForEach.ChildForEach; -import org.genericsystem.reactor.annotations.Parent; import org.genericsystem.reactor.annotations.ReactorDependencies; import org.genericsystem.reactor.annotations.ReactorDependencies.ChildReactorDependencies; import org.genericsystem.reactor.annotations.RunScript; @@ -24,6 +23,9 @@ import org.genericsystem.reactor.annotations.Styles.Flex; import org.genericsystem.reactor.annotations.Styles.FlexDirectionStyle; import org.genericsystem.reactor.annotations.Styles.FlexWrap; +import org.genericsystem.reactor.annotations.Styles.MarginBottom; +import org.genericsystem.reactor.annotations.Styles.MarginRight; +import org.genericsystem.reactor.annotations.Styles.Style; import org.genericsystem.reactor.appserver.ApplicationServer; import org.genericsystem.reactor.az.FlexDirection; import org.genericsystem.reactor.az.GSApp; @@ -35,24 +37,21 @@ import org.genericsystem.reactor.example.AppHtml5.AudiTT.AudiTTGreen; import org.genericsystem.reactor.example.AppHtml5.AudiTT.AudiTTPower; import org.genericsystem.reactor.example.AppHtml5.AudiTT.Green; -import org.genericsystem.reactor.example.AppHtml5.GSCell.GSSubcell; import org.genericsystem.reactor.example.AppHtml5.GSInstancesComposite; import org.genericsystem.reactor.example.AppHtml5.GSRowInstancesComposite; import org.genericsystem.reactor.example.AppHtml5.GSTypeAttributes; import org.genericsystem.reactor.example.AppHtml5.GSTypeAttributesRow; import org.genericsystem.reactor.example.AppHtml5.GSTypeLabeledInstancesComposite; -import org.genericsystem.reactor.example.AppHtml5.GSTypeLabeledInstancesComposite.GSHeaderComponent_; -import org.genericsystem.reactor.example.AppHtml5.GSTypeLabeledInstancesComposite.GSHeaderComponent_.GSTypeAttributesRow_; +import org.genericsystem.reactor.example.AppHtml5.GSTypeTableInstancesComposite; import org.genericsystem.reactor.example.AppHtml5.GSValueComponents; import org.genericsystem.reactor.example.AppHtml5.TestCell; -import org.genericsystem.reactor.example.AppHtml5.TestCell.GSCell_; import org.genericsystem.reactor.example.AppHtml5.Unit; import org.genericsystem.reactor.model.ObservableListExtractor; import org.genericsystem.reactor.modelproperties.SelectionDefaults; @DependsOnModel({ Car.class, Power.class, UsedCar.class, Color.class, CarColor.class, CarColor2.class, AudiTT.class, Green.class, AudiTTGreen.class, AudiTTPower.class, Unit.class }) @RunScript(ExampleReactorScript.class) -@ReactorDependencies({ GSInstancesComposite.class, GSRowInstancesComposite.class, GSTypeAttributes.class, GSTypeAttributesRow.class, GSValueComponents.class, GSTypeLabeledInstancesComposite.class, /* GSTypeTableInstancesComposite.class, */TestCell.class }) +@ReactorDependencies({ GSInstancesComposite.class, GSRowInstancesComposite.class, GSTypeAttributes.class, GSTypeAttributesRow.class, GSValueComponents.class, GSTypeLabeledInstancesComposite.class, GSTypeTableInstancesComposite.class, TestCell.class }) @FlexWrap("wrap") @Flex("1 1 0%") public class AppHtml5 extends GSApp implements SelectionDefaults { @@ -64,7 +63,7 @@ public static void main(String[] mainArgs) { @BackgroundColor("Green") @DirectSelect(Car.class) @ChildForEach(ObservableListExtractor.SUBINSTANCES.class) - public static class GSInstancesComposite extends org.genericsystem.reactor.az3.GSComposite { + public static class GSInstancesComposite extends GSComposite { } @@ -100,21 +99,21 @@ public static class GSTypeAttributesRow extends GSComposite { @ChildFlexDirection("row") @ChildForEach(ObservableListExtractor.ATTRIBUTES_OF_INSTANCES.class) @ReactorDependencies({ GSHeaderComponent.class, GSContentComponent.class }) - // PB here, the parent is not exactly le good ... that change behavior of parent search annotation @ParentReactorDependency - @Parent(GSContentComponent.class) public static class GSInstanceAttributesRow extends GSComposite { } @BackgroundColor("Brown") @ChildForEach(ObservableListExtractor.HOLDERS.class) - @ReactorDependencies({ GSContentComponent.class }) + @ChildReactorDependencies(GSValueComponents2.class) + @MarginRight("1px") + @MarginBottom("1px") public static class GSHolders extends GSComposite { } - @DirectSelect(Power.class) - @BackgroundColor("Yellow") + // @DirectSelect(Power.class) + @BackgroundColor("Purple") @FlexDirectionStyle(FlexDirection.ROW) @ChildForEach(ObservableListExtractor.COMPONENTS.class) @ReactorDependencies({ GSHeaderComponent.class, GSContentComponent.class }) @@ -122,6 +121,58 @@ public static class GSValueComponents extends GSComposite { } + @ChildForEach(ObservableListExtractor.OTHER_COMPONENTS_1.class) + public static class GSValueComponents1 extends GSValueComponents { + + } + + @BackgroundColor("Yellow") + @ChildForEach(ObservableListExtractor.OTHER_COMPONENTS_2.class) + public static class GSValueComponents2 extends GSValueComponents { + + } + + @DirectSelect(AudiTT.class) + @ReactorDependencies(GSCell.class) + public static class TestCell extends GSComposite { + } + + @DirectSelect(CarColor.class) + public static class GSCell extends GSHolders { + + } + + @DirectSelect(Car.class) + @ReactorDependencies({ GSHeaderComponent.class, GSContentComponent.class }) + @ChildReactorDependencies(decorate = GSHeaderComponent.class, value = GSTypeAttributesRow.class) + @ChildReactorDependencies(GSInstanceAttributesRow.class) + @ChildFlexDirection("row") + @Style(propertyName = "margin", propertyValue = "4px") + public static class GSTypeLabeledInstancesComposite extends GSInstancesComposite { + + } + + @DirectSelect(Car.class) + @ChildReactorDependencies(decorate = GSHeaderComponent.class, value = GSTypeAttributesRowWithComponents.class) + @ChildReactorDependencies(GSInstanceAttributesRowWithComponents.class) + public static class GSTypeTableInstancesComposite extends GSTypeLabeledInstancesComposite { + + } + + @Styles.Color("#000000") + @BackgroundColor("purple") + @ChildReactorDependencies(decorate = GSHeaderComponent.class, value = GSValueComponents1.class) + @ChildReactorDependencies(GSValueComponents1.class) + public static class GSTypeAttributesRowWithComponents extends GSTypeAttributesRow { + + } + + @ChildReactorDependencies(decorate = GSHeaderComponent.class, value = GSValueComponents2.class) + @ChildReactorDependencies(GSHolders.class) + public static class GSInstanceAttributesRowWithComponents extends GSInstanceAttributesRow { + + } + @SystemGeneric @Meta(Car.class) @StringValue("Audi TT") @@ -149,88 +200,6 @@ public static class AudiTTGreen { } } - @DirectSelect(AudiTT.class) - @ReactorDependencies(GSCell_.class) - public static class TestCell extends GSComposite { - @DirectSelect(CarColor.class) - public static class GSCell_ extends GSCell { - - } - } - - // @org.genericsystem.reactor.annotations.ReactorDependencies(GSCellContentComponent.class) - // public static class GSCell extends GSHolders { - // @org.genericsystem.reactor.annotations.ReactorDependencies({ GSSubcell.class }) - // public static class GSCellContentComponent extends GSContentComponent { - // public static class GSSubcell extends GSValueComponents { - // - // } - // } - // } - - @ChildReactorDependencies(GSSubcell.class) - public static class GSCell extends GSHolders { - @Parent(GSContentComponent.class) - public static class GSSubcell extends GSValueComponents { - - } - } - - @DirectSelect(Car.class) - @ReactorDependencies({ GSHeaderComponent_.class, GSInstanceAttributesRow.class }) - @ChildReactorDependencies(GSInstanceAttributesRow.class) - @ChildFlexDirection("row") - public static class GSTypeLabeledInstancesComposite extends GSInstancesComposite { - @org.genericsystem.reactor.annotations.ReactorDependencies({ GSTypeAttributesRow_.class }) - public static class GSHeaderComponent_ extends GSHeaderComponent { - public static class GSTypeAttributesRow_ extends GSTypeAttributesRow { - - } - } - - // @Parent(GSContentComponent.class) - // @ChildReactorDependencies(GSContentComponent.class) - // public static class GSInstanceAttributesRow_ extends GSInstanceAttributesRow { - // - // } - - // @org.genericsystem.reactor.annotations.ReactorDependencies({ GSInstanceAttributesRow_.class }) - // public static class GSInstanceContentComponent_ extends GSContentComponent { - // public static class GSInstanceAttributesRow_ extends GSInstanceAttributesRow { - // - // } - // } - } - - @SystemGeneric - @Components(Power.class) - @StringValue("Unit") public static class Unit { } - - // @org.genericsystem.reactor.annotations.DirectSelect(Car.class) - // @org.genericsystem.reactor.annotations.ReactorDependencies({ GSHeaderComponent_.class, GSInstanceContentComponent__.class }) - // @ChildReactorDependencies(GSInstanceContentComponent__.class) - // public static class GSTypeTableInstancesComposite extends GSTypeLabeledInstancesComposite { - // @org.genericsystem.reactor.annotations.ReactorDependencies({ GSInstanceAttributesRow__.class }) - // public static class GSInstanceContentComponent__ extends GSInstanceContentComponent_ { - // @org.genericsystem.reactor.annotations.ReactorDependencies({ GSInstanceNameComponent.class, GSHoldersContentComponent.class }) - // public static class GSInstanceAttributesRow__ extends GSInstanceAttributesRow_ { - // @org.genericsystem.reactor.annotations.ReactorDependencies(GSInstanceNameSubcell_.class) - // public static class GSInstanceNameComponent extends GSHeaderComponent { - // @BackgroundColor("Orange") - // public static class GSInstanceNameSubcell_ extends GSValueComponents { - // - // } - // } - // - // @org.genericsystem.reactor.annotations.ReactorDependencies({ GSCell__.class }) - // public static class GSHoldersContentComponent extends GSContentComponent { - // public static class GSCell__ extends GSCell { - // - // } - // } - // } - // } - // } } diff --git a/gs-reactor/src/main/java/org/genericsystem/reactor/annotations/ReactorDependencies.java b/gs-reactor/src/main/java/org/genericsystem/reactor/annotations/ReactorDependencies.java index 81c203b4a..c39b815ee 100644 --- a/gs-reactor/src/main/java/org/genericsystem/reactor/annotations/ReactorDependencies.java +++ b/gs-reactor/src/main/java/org/genericsystem/reactor/annotations/ReactorDependencies.java @@ -2,11 +2,13 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; +import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.genericsystem.reactor.az.GSTagImpl; +import org.genericsystem.reactor.az3.GSComposite.GSContentComponent; /** * @author Nicolas Feybesse @@ -22,14 +24,24 @@ @Target({ ElementType.TYPE }) @Inherited public @interface ParentReactorDependencies { - int pos() default 0; + int value() default 0; } @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) @Inherited + @Repeatable(ChildReactorDependenciesMult.class) public @interface ChildReactorDependencies { - Class[] value(); + Class decorate() default GSContentComponent.class; + + Class value(); + } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.TYPE }) + @Inherited + public @interface ChildReactorDependenciesMult { + ChildReactorDependencies[] value(); } } diff --git a/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSComposite.java b/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSComposite.java index 8a781b5f3..d4e6b3dbf 100644 --- a/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSComposite.java +++ b/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSComposite.java @@ -3,12 +3,15 @@ import org.genericsystem.reactor.annotations.ForEach.ParentForEach; import org.genericsystem.reactor.annotations.ReactorDependencies; import org.genericsystem.reactor.annotations.ReactorDependencies.ChildReactorDependencies; -import org.genericsystem.reactor.annotations.ReactorDependencies.ParentReactorDependencies; import org.genericsystem.reactor.annotations.Styles.ChildFlexDirection; import org.genericsystem.reactor.annotations.Styles.Flex; import org.genericsystem.reactor.annotations.Styles.Overflow; import org.genericsystem.reactor.annotations.Styles.ParentFlexDirection; import org.genericsystem.reactor.az3.GSComposite.GSContentComponent; +import org.genericsystem.reactor.az3.GSComposite.GSFooterComponent; +import org.genericsystem.reactor.az3.GSComposite.GSFooterComponent.GSFooterComponentLabel; +import org.genericsystem.reactor.az3.GSComposite.GSHeaderComponent; +import org.genericsystem.reactor.az3.GSComposite.GSHeaderComponent.GSHeaderComponentLabel; import org.genericsystem.reactor.gstag.HtmlLabel.GSLabelDisplayer; @Flex("1 1 0%") @@ -16,13 +19,14 @@ @ReactorDependencies({ GSComposite.GSContentComponent.class }) @ChildFlexDirection("column") @ChildReactorDependencies(GSContentComponent.GSContentComponentLabel.class) +@ChildReactorDependencies(decorate = GSHeaderComponent.class, value = GSHeaderComponentLabel.class) +@ChildReactorDependencies(decorate = GSFooterComponent.class, value = GSFooterComponentLabel.class) public abstract class GSComposite extends GSCompositeDiv { @Flex("1 1 0%") @Overflow("hidden") @ParentForEach @ParentFlexDirection - @ParentReactorDependencies public static class GSContentComponent extends GSCompositeDiv { public static class GSContentComponentLabel extends GSLabelDisplayer { @@ -32,7 +36,6 @@ public static class GSContentComponentLabel extends GSLabelDisplayer { @Flex("1 1 0%") @Overflow("hidden") @ParentFlexDirection - @ReactorDependencies(GSHeaderComponent.GSHeaderComponentLabel.class) public static class GSHeaderComponent extends GSCompositeDiv { public static class GSHeaderComponentLabel extends GSLabelDisplayer { @@ -42,7 +45,6 @@ public static class GSHeaderComponentLabel extends GSLabelDisplayer { @Flex("1 1 0%") @Overflow("hidden") @ParentFlexDirection - @ReactorDependencies(GSFooterComponent.GSFooterComponentLabel.class) public static class GSFooterComponent extends GSCompositeDiv { public static class GSFooterComponentLabel extends GSLabelDisplayer { diff --git a/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSCompositeDiv.java b/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSCompositeDiv.java index 9973fb00c..704c82fe2 100644 --- a/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSCompositeDiv.java +++ b/gs-reactor/src/main/java/org/genericsystem/reactor/az3/GSCompositeDiv.java @@ -11,12 +11,9 @@ import org.genericsystem.reactor.annotations.ForEach; import org.genericsystem.reactor.annotations.ForEach.ChildForEach; import org.genericsystem.reactor.annotations.ForEach.ParentForEach; -import org.genericsystem.reactor.annotations.Parent; import org.genericsystem.reactor.annotations.ReactorDependencies; import org.genericsystem.reactor.annotations.ReactorDependencies.ChildReactorDependencies; -import org.genericsystem.reactor.annotations.ReactorDependencies.ParentReactorDependencies; import org.genericsystem.reactor.annotations.Select; -import org.genericsystem.reactor.annotations.Styles; import org.genericsystem.reactor.annotations.Styles.AlignItems; import org.genericsystem.reactor.annotations.Styles.BackgroundColor; import org.genericsystem.reactor.annotations.Styles.ChildFlexDirection; @@ -70,58 +67,36 @@ public Tag get(Object key) { }; public GSCompositeDiv() { - super(); - initComposite(); } public GSCompositeDiv(Tag parent) { super(parent); - init(); initComposite(); processAnnotations(getClass(), this); + init(); } private void initComposite() { nodes.put(getClass(), this); - ParentReactorDependencies parentDependencies = getClass().getAnnotation(ParentReactorDependencies.class); - if (parentDependencies == null) { - ReactorDependencies dependencies = getClass().getAnnotation(ReactorDependencies.class); - if (dependencies != null) { - // System.out.println("Declaring classes : " + Arrays.toString(getClass().getDeclaredClasses())); - // System.out.println("ReactorDependencies : " + Arrays.toString(deps.value())); - for (Class clazz : dependencies.value()) - find(clazz); - } - } else { - ChildReactorDependencies childDependencies = getParentTagClass(getClass()).getAnnotation(ChildReactorDependencies.class); - if (childDependencies != null) { - find(childDependencies.value()[parentDependencies.pos()]); - } else - log.warn("Warning : unable to find ChildReactorDependencies on : " + getParentTagClass(getClass()).getSimpleName() + " for : " + getClass().getSimpleName()); - } - - ReactorDependencies deps = getClass().getAnnotation(ReactorDependencies.class); - if (deps != null) { - // System.out.println("Declaring classes : " + Arrays.toString(getClass().getDeclaredClasses())); + ReactorDependencies dependencies = getClass().getAnnotation(ReactorDependencies.class); + if (dependencies != null) { + // System.out.println("Declaring classes : " + Arrays.toString(getClass().getDeclaredClasses())); // System.out.println("ReactorDependencies : " + Arrays.toString(deps.value())); - for (Class clazz : deps.value()) + for (Class clazz : dependencies.value()) find(clazz); } + if (getParent() != null) { + ChildReactorDependencies[] childDependenciesMult = getParent().getClass().getAnnotationsByType(ChildReactorDependencies.class); + for (ChildReactorDependencies cd : childDependenciesMult) { + if (cd.decorate().isAssignableFrom(getClass())) + find(cd.value()); + } + } for (Tag tag : nodes.values()) tag.postfix(); } - private static Class getParentTagClass(Class tagClass) { - Parent parent = tagClass.getAnnotation(Parent.class); - if (parent != null) - return parent.value(); - Class enclosing = (Class) tagClass.getEnclosingClass(); - if (enclosing != null && !enclosing.isAssignableFrom(tagClass)) - return enclosing; - return null; - } - @Override public T find(Class tagClass) { T result = (T) nodes.get(tagClass); @@ -131,8 +106,9 @@ public T find(Class tagClass) { } catch (IllegalAccessException | InstantiationException e) { throw new IllegalStateException(e); } - Class parentClass = getParentTagClass(tagClass); - ((GSTagImpl) result).setParent(parentClass != null ? find(parentClass) : this); + ((GSTagImpl) result).setParent(this); + if (GSCompositeDiv.class.isAssignableFrom(tagClass)) + ((GSCompositeDiv) result).initComposite(); processAnnotations(tagClass, result); result.init(); nodes.put(tagClass, result); @@ -152,7 +128,6 @@ private static void processAnnotations(Class tagClass, Tag re } } } else { - ChildForEach childForEach = result.getParent().getClass().getAnnotation(ChildForEach.class); if (childForEach != null) { try { @@ -205,8 +180,8 @@ private static void processAnnotations(Class tagClass, Tag re result.addStyle("background-color", backgroundColor.value()); if (tagClass.getAnnotation(GenericBackgroundColor.class) != null) - result.addPrefixBinding(modelContext -> result.addStyle(modelContext, "background-color", "Color".equals(StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(modelContext.getGeneric().getMeta())) ? ((GenericStringDefaults) result) - .getGenericStringProperty(modelContext).getValue() : "#e5ed00")); + result.addPrefixBinding(modelContext -> result.addStyle(modelContext, "background-color", + "Color".equals(StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(modelContext.getGeneric().getMeta())) ? ((GenericStringDefaults) result).getGenericStringProperty(modelContext).getValue() : "#e5ed00")); FlexWrap flexWrap = tagClass.getAnnotation(FlexWrap.class); if (flexWrap != null) result.addStyle("flex-wrap", flexWrap.value()); @@ -238,12 +213,8 @@ private static void processAnnotations(Class tagClass, Tag re if (width != null) result.addStyle("width", width.value()); - Style style = tagClass.getAnnotation(Style.class); - if (style != null) - result.addStyle(style.propertyName(), style.propertyValue()); - Styles styles = tagClass.getAnnotation(Styles.class); - if (styles != null) - for (Style style_ : styles.value()) - result.addStyle(style_.propertyName(), style_.propertyValue()); + Style[] styles = tagClass.getAnnotationsByType(Style.class); + for (Style style_ : styles) + result.addStyle(style_.propertyName(), style_.propertyValue()); } } \ No newline at end of file