Package: NodeViewRenderer
NodeViewRenderer
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NodeViewRenderer(Request, RequestContext, ViewAndControllerLayoutBuilder, BiConsumer) |
|
|
|
|
|
||||||||||||||||||||
getRootComponent() |
|
|
|
|
|
||||||||||||||||||||
postVisit(Layout) |
|
|
|
|
|
||||||||||||||||||||
preVisit(Layout) |
|
|
|
|
|
||||||||||||||||||||
renderView(ViewFactory.ViewAndController, Layout) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.ui.spi;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.NotThreadSafe;
31: import java.util.ArrayDeque;
32: import java.util.Deque;
33: import java.util.function.BiConsumer;
34: import it.tidalwave.role.Composite;
35: import it.tidalwave.northernwind.core.model.Request;
36: import it.tidalwave.northernwind.core.model.RequestContext;
37: import it.tidalwave.northernwind.frontend.ui.Layout;
38: import it.tidalwave.northernwind.frontend.ui.RenderContext;
39: import it.tidalwave.northernwind.frontend.ui.ViewFactory.ViewAndController;
40: import lombok.Getter;
41:
42: /***********************************************************************************************************************
43: *
44: * A {@link Layout} visitor that renders everything for the given layout.
45: *
46: * @stereotype Visitor
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @NotThreadSafe
52: public class NodeViewRenderer<T> implements Composite.Visitor<Layout, T>
53: {
54: @Nonnull
55: private final ViewAndControllerLayoutBuilder vacLayoutBuilder;
56:
57: @Nonnull
58: private final BiConsumer<? super T, ? super T> attacher;
59:
60: @Nonnull
61: private final RenderContext renderContext;
62:
63: private final Deque<T> componentStack = new ArrayDeque<>();
64:
65: @Getter
66: private T rootComponent;
67:
68: /*******************************************************************************************************************
69: *
70: ******************************************************************************************************************/
71: public NodeViewRenderer (@Nonnull final Request request,
72: @Nonnull final RequestContext requestContext,
73: @Nonnull final ViewAndControllerLayoutBuilder vacLayoutBuilder,
74: @Nonnull final BiConsumer<? super T, ? super T> attacher)
75: {
76: this.vacLayoutBuilder = vacLayoutBuilder;
77: this.attacher = attacher;
78: this.renderContext = new DefaultRenderContext(request, requestContext);
79: }
80:
81: /*******************************************************************************************************************
82: *
83: * {@inheritDoc}
84: *
85: ******************************************************************************************************************/
86: @Override
87: public void preVisit (@Nonnull final Layout layout)
88: {
89: final var vac = vacLayoutBuilder.getViewAndControllerFor(layout).get();
90: final var component = renderView(vac, layout);
91:
92:• if (rootComponent == null)
93: {
94: rootComponent = component;
95: }
96: else
97: {
98: attacher.accept(componentStack.peek(), component);
99: }
100:
101: componentStack.push(component);
102: }
103:
104: /*******************************************************************************************************************
105: *
106: * {@inheritDoc}
107: *
108: ******************************************************************************************************************/
109: @Override
110: public void postVisit (@Nonnull final Layout layout)
111: {
112: componentStack.pop();
113: }
114:
115: /*******************************************************************************************************************
116: *
117: ******************************************************************************************************************/
118: @Nonnull
119: private T renderView (@Nonnull final ViewAndController vac, @Nonnull final Layout layout)
120: {
121: try
122: {
123: return (T)vac.renderView(renderContext);
124: }
125: catch (Throwable e)
126: {
127: return (T)vacLayoutBuilder.getErrorViewSupplier().apply(layout, e);
128: }
129: }
130: }