Search in sources :

Example 11 with RenderQueue

use of org.apache.tapestry5.runtime.RenderQueue in project tapestry-5 by apache.

the class Tree method toRenderCommand.

/**
 * Renders a single node (which may be the last within its containing node).
 * This is a mix of immediate rendering, and queuing up various Blocks and Render commands
 * to do the rest. May recursively render child nodes of the active node. The label part
 * of the node is rendered inside a {@linkplain org.apache.tapestry5.services.Heartbeat heartbeat}.
 *
 * @param node
 *         to render
 * @param isLast
 *         if true, add "last" attribute to the LI element
 * @return command to render the node
 */
private RenderCommand toRenderCommand(final TreeNode node, final boolean isLast) {
    return new RenderCommand() {

        public void render(MarkupWriter writer, RenderQueue queue) {
            // Inform the component's container about what value is being rendered
            // (this may be necessary to generate the correct label for the node).
            Tree.this.node = node;
            value = node.getValue();
            boolean isLeaf = node.isLeaf();
            writer.element("li");
            if (isLast) {
                writer.attributes("class", "last");
            }
            if (isLeaf) {
                writer.getElement().attribute("class", "leaf-node");
            }
            Element e = writer.element("span", "class", "tree-icon");
            if (!isLeaf && !node.getHasChildren()) {
                e.addClassName("empty-node");
            }
            boolean hasChildren = !isLeaf && node.getHasChildren();
            boolean expanded = hasChildren && expansionModel.isExpanded(node);
            writer.attributes("data-node-id", node.getId());
            if (expanded) {
                // Inform the client side, so it doesn't try to fetch it a second time.
                e.addClassName("tree-expanded");
            }
            // span.tree-icon
            writer.end();
            // From here on in, we're pushing things onto the queue. Remember that
            // execution order is reversed from order commands are pushed.
            // li
            queue.push(RENDER_CLOSE_TAG);
            if (expanded) {
                queue.push(new RenderNodes(node.getChildren()));
            }
            queue.push(RENDER_CLOSE_TAG);
            final RenderCommand startHeartbeat = new RenderCommand() {

                @Override
                public void render(MarkupWriter writer, RenderQueue queue) {
                    heartbeat.begin();
                }
            };
            final RenderCommand endHeartbeat = new RenderCommand() {

                @Override
                public void render(MarkupWriter writer, RenderQueue queue) {
                    heartbeat.end();
                }
            };
            queue.push(endHeartbeat);
            queue.push(label);
            queue.push(startHeartbeat);
            if (isLeaf && selectionModel != null && selectionModel.isSelected(node)) {
                queue.push(MARK_SELECTED);
            }
            queue.push(RENDER_LABEL_SPAN);
        }
    };
}
Also used : RenderCommand(org.apache.tapestry5.runtime.RenderCommand) Element(org.apache.tapestry5.dom.Element) RenderQueue(org.apache.tapestry5.runtime.RenderQueue)

Example 12 with RenderQueue

use of org.apache.tapestry5.runtime.RenderQueue in project tapestry-5 by apache.

the class CompositeRenderCommandTest method nyi_data.

@DataProvider
public Object[][] nyi_data() {
    RenderCommand push = new RenderCommand() {

        public void render(MarkupWriter writer, RenderQueue queue) {
            queue.push(null);
        }
    };
    RenderCommand startComponent = new RenderCommand() {

        public void render(MarkupWriter writer, RenderQueue queue) {
            queue.startComponent(null);
        }
    };
    RenderCommand endComponent = new RenderCommand() {

        public void render(MarkupWriter writer, RenderQueue queue) {
            queue.endComponent();
        }
    };
    return new Object[][] { { push }, { startComponent }, { endComponent } };
}
Also used : RenderCommand(org.apache.tapestry5.runtime.RenderCommand) RenderQueue(org.apache.tapestry5.runtime.RenderQueue) MarkupWriter(org.apache.tapestry5.MarkupWriter) DataProvider(org.testng.annotations.DataProvider)

Example 13 with RenderQueue

use of org.apache.tapestry5.runtime.RenderQueue in project tapestry-5 by apache.

the class TapestryModule method contributeTypeCoercer.

/**
 * Adds coercions:
 * <ul>
 * <li>String to {@link SelectModel}
 * <li>Map to {@link SelectModel}
 * <li>Collection to {@link GridDataSource}
 * <li>null to {@link GridDataSource}
 * <li>List to {@link SelectModel}
 * <li>{@link ComponentResourcesAware} (typically, a component) to {@link ComponentResources}
 * <li>{@link ComponentResources} to {@link PropertyOverrides}
 * <li>String to {@link Renderable}
 * <li>{@link Renderable} to {@link Block}
 * <li>String to {@link DateFormat}
 * <li>String to {@link Resource} (via {@link AssetSource#resourceForPath(String)})
 * <li>{@link Renderable} to {@link RenderCommand}</li>
 * <li>String to {@link Pattern}</li>
 * <li>String to {@link DateFormat}</li>
 * <li>{@link Resource} to {@link DynamicTemplate}</li>
 * <li>{@link Asset} to {@link Resource}</li>
 * <li>{@link ValueEncoder} to {@link ValueEncoderFactory}</li>
 * </ul>
 */
public static void contributeTypeCoercer(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration, final ObjectLocator objectLocator, @Builtin final ThreadLocale threadLocale, @Core final AssetSource assetSource, @Core final DynamicTemplateParser dynamicTemplateParser) {
    CoercionTuple<ComponentResources, PropertyOverrides> componentResourcesToPropertyOverrides = CoercionTuple.create(ComponentResources.class, PropertyOverrides.class, new Coercion<ComponentResources, PropertyOverrides>() {

        public PropertyOverrides coerce(ComponentResources input) {
            return new PropertyOverridesImpl(input);
        }
    });
    configuration.add(componentResourcesToPropertyOverrides.getKey(), componentResourcesToPropertyOverrides);
    // See TAP5-2184 for why this causes some trouble!
    CoercionTuple<String, SelectModel> stringToSelectModel = CoercionTuple.create(String.class, SelectModel.class, new Coercion<String, SelectModel>() {

        public SelectModel coerce(String input) {
            return TapestryInternalUtils.toSelectModel(input);
        }
    });
    configuration.add(stringToSelectModel.getKey(), stringToSelectModel);
    CoercionTuple<Map, SelectModel> mapToSelectModel = CoercionTuple.create(Map.class, SelectModel.class, new Coercion<Map, SelectModel>() {

        @SuppressWarnings("unchecked")
        public SelectModel coerce(Map input) {
            return TapestryInternalUtils.toSelectModel(input);
        }
    });
    configuration.add(mapToSelectModel.getKey(), mapToSelectModel);
    CoercionTuple<Collection, GridDataSource> collectionToGridDataSource = CoercionTuple.create(Collection.class, GridDataSource.class, new Coercion<Collection, GridDataSource>() {

        public GridDataSource coerce(Collection input) {
            return new CollectionGridDataSource(input);
        }
    });
    configuration.add(collectionToGridDataSource.getKey(), collectionToGridDataSource);
    CoercionTuple<Void, GridDataSource> voidToGridDataSource = CoercionTuple.create(void.class, GridDataSource.class, new Coercion<Void, GridDataSource>() {

        private final GridDataSource source = new NullDataSource();

        public GridDataSource coerce(Void input) {
            return source;
        }
    });
    configuration.add(voidToGridDataSource.getKey(), voidToGridDataSource);
    CoercionTuple<List, SelectModel> listToSelectModel = CoercionTuple.create(List.class, SelectModel.class, new Coercion<List, SelectModel>() {

        private SelectModelFactory selectModelFactory;

        @SuppressWarnings("unchecked")
        public SelectModel coerce(List input) {
            // to another value, and a race condition is harmless.
            if (selectModelFactory == null) {
                selectModelFactory = objectLocator.getService(SelectModelFactory.class);
            }
            return selectModelFactory.create(input);
        }
    });
    configuration.add(listToSelectModel.getKey(), listToSelectModel);
    CoercionTuple<String, Pattern> stringToPattern = CoercionTuple.create(String.class, Pattern.class, new Coercion<String, Pattern>() {

        public Pattern coerce(String input) {
            return Pattern.compile(input);
        }
    });
    configuration.add(stringToPattern.getKey(), stringToPattern);
    CoercionTuple<ComponentResourcesAware, ComponentResources> componentResourcesAwareToComponentResources = CoercionTuple.create(ComponentResourcesAware.class, ComponentResources.class, new Coercion<ComponentResourcesAware, ComponentResources>() {

        public ComponentResources coerce(ComponentResourcesAware input) {
            return input.getComponentResources();
        }
    });
    configuration.add(componentResourcesAwareToComponentResources.getKey(), componentResourcesAwareToComponentResources);
    CoercionTuple<String, Renderable> stringToRenderable = CoercionTuple.create(String.class, Renderable.class, new Coercion<String, Renderable>() {

        public Renderable coerce(String input) {
            return new StringRenderable(input);
        }
    });
    configuration.add(stringToRenderable.getKey(), stringToRenderable);
    CoercionTuple<Renderable, Block> renderableToBlock = CoercionTuple.create(Renderable.class, Block.class, new Coercion<Renderable, Block>() {

        public Block coerce(Renderable input) {
            return new RenderableAsBlock(input);
        }
    });
    configuration.add(renderableToBlock.getKey(), renderableToBlock);
    CoercionTuple<String, DateFormat> stringToDateFormat = CoercionTuple.create(String.class, DateFormat.class, new Coercion<String, DateFormat>() {

        public DateFormat coerce(String input) {
            final SimpleDateFormat dateFormat = new SimpleDateFormat(input, threadLocale.getLocale());
            final String lenient = objectLocator.getService(SymbolSource.class).valueForSymbol(SymbolConstants.LENIENT_DATE_FORMAT);
            dateFormat.setLenient(Boolean.parseBoolean(lenient));
            return dateFormat;
        }
    });
    configuration.add(stringToDateFormat.getKey(), stringToDateFormat);
    CoercionTuple<String, Resource> stringToResource = CoercionTuple.create(String.class, Resource.class, new Coercion<String, Resource>() {

        public Resource coerce(String input) {
            return assetSource.resourceForPath(input);
        }
    });
    configuration.add(stringToResource.getKey(), stringToResource);
    CoercionTuple<Renderable, RenderCommand> renderableToRenderCommand = CoercionTuple.create(Renderable.class, RenderCommand.class, new Coercion<Renderable, RenderCommand>() {

        public RenderCommand coerce(final Renderable input) {
            return new RenderCommand() {

                public void render(MarkupWriter writer, RenderQueue queue) {
                    input.render(writer);
                }
            };
        }
    });
    configuration.add(renderableToRenderCommand.getKey(), renderableToRenderCommand);
    CoercionTuple<Date, Calendar> dateToCalendar = CoercionTuple.create(Date.class, Calendar.class, new Coercion<Date, Calendar>() {

        public Calendar coerce(Date input) {
            Calendar calendar = Calendar.getInstance(threadLocale.getLocale());
            calendar.setTime(input);
            return calendar;
        }
    });
    configuration.add(dateToCalendar.getKey(), dateToCalendar);
    CoercionTuple<Resource, DynamicTemplate> resourceToDynamicTemplate = CoercionTuple.create(Resource.class, DynamicTemplate.class, new Coercion<Resource, DynamicTemplate>() {

        public DynamicTemplate coerce(Resource input) {
            return dynamicTemplateParser.parseTemplate(input);
        }
    });
    configuration.add(resourceToDynamicTemplate.getKey(), resourceToDynamicTemplate);
    CoercionTuple<Asset, Resource> assetToResource = CoercionTuple.create(Asset.class, Resource.class, new Coercion<Asset, Resource>() {

        public Resource coerce(Asset input) {
            return input.getResource();
        }
    });
    configuration.add(assetToResource.getKey(), assetToResource);
    CoercionTuple<ValueEncoder, ValueEncoderFactory> valueEncoderToValueEncoderFactory = CoercionTuple.create(ValueEncoder.class, ValueEncoderFactory.class, new Coercion<ValueEncoder, ValueEncoderFactory>() {

        public ValueEncoderFactory coerce(ValueEncoder input) {
            return new GenericValueEncoderFactory(input);
        }
    });
    configuration.add(valueEncoderToValueEncoderFactory.getKey(), valueEncoderToValueEncoderFactory);
}
Also used : ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory) ComponentResources(org.apache.tapestry5.ComponentResources) RenderCommand(org.apache.tapestry5.runtime.RenderCommand) Renderable(org.apache.tapestry5.Renderable) StringRenderable(org.apache.tapestry5.internal.util.StringRenderable) StringRenderable(org.apache.tapestry5.internal.util.StringRenderable) Asset(org.apache.tapestry5.Asset) List(java.util.List) SelectModelFactory(org.apache.tapestry5.services.SelectModelFactory) Resource(org.apache.tapestry5.commons.Resource) PropertyOverrides(org.apache.tapestry5.PropertyOverrides) SelectModel(org.apache.tapestry5.SelectModel) MarkupWriter(org.apache.tapestry5.MarkupWriter) ComponentResourcesAware(org.apache.tapestry5.runtime.ComponentResourcesAware) CollectionGridDataSource(org.apache.tapestry5.internal.grid.CollectionGridDataSource) Collection(java.util.Collection) RenderableAsBlock(org.apache.tapestry5.internal.util.RenderableAsBlock) Block(org.apache.tapestry5.Block) Map(java.util.Map) PropertyOverridesImpl(org.apache.tapestry5.internal.PropertyOverridesImpl) CollectionGridDataSource(org.apache.tapestry5.internal.grid.CollectionGridDataSource) GridDataSource(org.apache.tapestry5.grid.GridDataSource) ValueEncoder(org.apache.tapestry5.ValueEncoder) ContextValueEncoder(org.apache.tapestry5.services.ContextValueEncoder) RenderQueue(org.apache.tapestry5.runtime.RenderQueue) NullDataSource(org.apache.tapestry5.internal.grid.NullDataSource) Pattern(java.util.regex.Pattern) RenderableAsBlock(org.apache.tapestry5.internal.util.RenderableAsBlock) Calendar(java.util.Calendar) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DynamicTemplate(org.apache.tapestry5.services.dynamic.DynamicTemplate)

Example 14 with RenderQueue

use of org.apache.tapestry5.runtime.RenderQueue in project tapestry-5 by apache.

the class RenderPhaseEventHandler method handleResult.

/**
 * Handles a result (a return value from an event handler method). The result
 * must be Boolean, {@link RenderCommand} or {@link Renderable}.  For the latter two types, the result
 * is converted to a {@link RenderCommand} and added to an internal list; the commands in the list
 * are pushed onto the {@link RenderQueue} at the end of the render phase, when {@link #enqueueSavedRenderCommands()}} is invoked.
 *
 * @param result the result value returned from the event handler method
 * @return true if the event is aborted (a Boolean), false if event processing should continue (other types)
 * @throws RuntimeException for any other type
 */
public boolean handleResult(Object result) {
    if (result instanceof Boolean) {
        this.result = (Boolean) result;
        // abort other handler methods
        return true;
    }
    if (result instanceof RenderCommand) {
        RenderCommand command = (RenderCommand) result;
        add(command);
        // do not abort!
        return false;
    }
    if (result instanceof Renderable) {
        final Renderable renderable = (Renderable) result;
        RenderCommand wrapper = new RenderCommand() {

            public void render(MarkupWriter writer, RenderQueue queue) {
                renderable.render(writer);
            }
        };
        add(wrapper);
        return false;
    }
    throw new RuntimeException(StructureMessages.wrongPhaseResultType(Arrays.asList(Boolean.class.getName(), Renderable.class.getName(), RenderCommand.class.getName())));
}
Also used : RenderCommand(org.apache.tapestry5.runtime.RenderCommand) Renderable(org.apache.tapestry5.Renderable) RenderQueue(org.apache.tapestry5.runtime.RenderQueue) MarkupWriter(org.apache.tapestry5.MarkupWriter)

Example 15 with RenderQueue

use of org.apache.tapestry5.runtime.RenderQueue in project tapestry-5 by apache.

the class BlockImplTest method empty_block.

@Test
public void empty_block() {
    BlockImpl block = new BlockImpl(null, null);
    RenderQueue queue = mockRenderQueue();
    MarkupWriter writer = mockMarkupWriter();
    replay();
    block.render(writer, queue);
    verify();
}
Also used : RenderQueue(org.apache.tapestry5.runtime.RenderQueue) MarkupWriter(org.apache.tapestry5.MarkupWriter) Test(org.testng.annotations.Test)

Aggregations

MarkupWriter (org.apache.tapestry5.MarkupWriter)14 RenderQueue (org.apache.tapestry5.runtime.RenderQueue)14 RenderCommand (org.apache.tapestry5.runtime.RenderCommand)13 Test (org.testng.annotations.Test)9 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)3 Binding (org.apache.tapestry5.Binding)2 Block (org.apache.tapestry5.Block)2 ComponentResources (org.apache.tapestry5.ComponentResources)2 Renderable (org.apache.tapestry5.Renderable)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 Element (org.apache.tapestry5.dom.Element)2 Logger (org.slf4j.Logger)2 AccessControlException (java.security.AccessControlException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1