Added ability to defer component template setting until after construction.

This commit is contained in:
JesseBrault0709 2024-05-30 08:30:29 +02:00
parent 3362011a8a
commit 3be4761541
4 changed files with 32 additions and 14 deletions

View File

@ -21,19 +21,17 @@ public abstract class AbstractViewComponent implements ViewComponent {
} }
} }
private final ComponentTemplate template; private ComponentTemplate componentTemplate;
private ComponentContext context; private ComponentContext context;
public AbstractViewComponent() { public AbstractViewComponent() {}
this.template = null;
}
public AbstractViewComponent(ComponentTemplate template) { public AbstractViewComponent(ComponentTemplate componentTemplate) {
this.template = template; this.componentTemplate = componentTemplate;
} }
public AbstractViewComponent(Class<? extends ComponentTemplate> templateClass) { public AbstractViewComponent(Class<? extends ComponentTemplate> templateClass) {
this.template = instantiateTemplate(templateClass); this.componentTemplate = instantiateTemplate(templateClass);
} }
public AbstractViewComponent( public AbstractViewComponent(
@ -46,7 +44,17 @@ public abstract class AbstractViewComponent implements ViewComponent {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
final var templateClass = templateClassFactory.getTemplateClass(compileResult); final var templateClass = templateClassFactory.getTemplateClass(compileResult);
this.template = instantiateTemplate(templateClass); this.componentTemplate = instantiateTemplate(templateClass);
}
@Override
public ComponentTemplate getComponentTemplate() {
return this.componentTemplate;
}
@Override
public void setComponentTemplate(ComponentTemplate componentTemplate) {
this.componentTemplate = componentTemplate;
} }
@Override @Override
@ -59,10 +67,6 @@ public abstract class AbstractViewComponent implements ViewComponent {
return Objects.requireNonNull(this.context); return Objects.requireNonNull(this.context);
} }
protected ComponentTemplate getTemplate() {
return Objects.requireNonNull(template);
}
protected void beforeRender() {} protected void beforeRender() {}
protected void afterRender() {} protected void afterRender() {}
@ -76,7 +80,7 @@ public abstract class AbstractViewComponent implements ViewComponent {
*/ */
@Override @Override
public void renderTo(Writer out) throws IOException { public void renderTo(Writer out) throws IOException {
final Closure<?> closure = this.getTemplate().getRenderer(); final Closure<?> closure = this.getComponentTemplate().getRenderer();
closure.setDelegate(this); closure.setDelegate(this);
closure.setResolveStrategy(Closure.DELEGATE_FIRST); closure.setResolveStrategy(Closure.DELEGATE_FIRST);
this.beforeRender(); this.beforeRender();

View File

@ -5,6 +5,9 @@ import groowt.view.component.context.ComponentContext;
public interface ViewComponent extends View { public interface ViewComponent extends View {
ComponentTemplate getComponentTemplate();
void setComponentTemplate(ComponentTemplate componentTemplate);
/** /**
* <em>Note:</em> compiled templates are required to automatically * <em>Note:</em> compiled templates are required to automatically
* call this method after the component is constructed. One * call this method after the component is constructed. One

View File

@ -89,7 +89,7 @@ public abstract class AbstractWebViewComponent extends AbstractViewComponent imp
@Override @Override
public void renderTo(Writer out) throws IOException { public void renderTo(Writer out) throws IOException {
final ComponentWriter webWriter = new DefaultComponentWriter(out); final ComponentWriter webWriter = new DefaultComponentWriter(out);
final Closure<?> renderer = this.getTemplate().getRenderer(); final Closure<?> renderer = this.getComponentTemplate().getRenderer();
renderer.setDelegate(this); renderer.setDelegate(this);
renderer.setResolveStrategy(Closure.DELEGATE_FIRST); renderer.setResolveStrategy(Closure.DELEGATE_FIRST);
this.beforeRender(); this.beforeRender();

View File

@ -1,5 +1,6 @@
package groowt.view.component.web.compiler; package groowt.view.component.web.compiler;
import groowt.view.component.ComponentTemplate;
import groowt.view.component.context.ComponentContext; import groowt.view.component.context.ComponentContext;
import groowt.view.component.runtime.ComponentWriter; import groowt.view.component.runtime.ComponentWriter;
import groowt.view.component.web.WebViewComponent; import groowt.view.component.web.WebViewComponent;
@ -11,6 +12,16 @@ import java.util.List;
@ApiStatus.Internal @ApiStatus.Internal
public final class AnonymousWebViewComponent implements WebViewComponent { public final class AnonymousWebViewComponent implements WebViewComponent {
@Override
public ComponentTemplate getComponentTemplate() {
throw new UnsupportedOperationException();
}
@Override
public void setComponentTemplate(ComponentTemplate componentTemplate) {
throw new UnsupportedOperationException();
}
@Override @Override
public void setContext(ComponentContext context) { public void setContext(ComponentContext context) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();