Search in sources :

Example 11 with MarkupStream

use of org.apache.wicket.markup.MarkupStream in project wicket by apache.

the class AjaxEditableChoiceLabel method newLabel.

/**
 * {@inheritDoc}
 */
@Override
protected WebComponent newLabel(final MarkupContainer parent, final String componentId, final IModel<T> model) {
    Label label = new Label(componentId, model) {

        private static final long serialVersionUID = 1L;

        /**
         * {@inheritDoc}
         */
        @Override
        public <C> IConverter<C> getConverter(final Class<C> type) {
            IConverter<C> c = AjaxEditableChoiceLabel.this.getConverter(type);
            return c != null ? c : super.getConverter(type);
        }

        /**
         * {@inheritDoc}
         */
        @SuppressWarnings("unchecked")
        @Override
        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
            String displayValue = getDefaultModelObjectAsString();
            if (renderer != null) {
                Object displayObject = renderer.getDisplayValue(getModelObject());
                Class<?> objectClass = (displayObject == null ? null : displayObject.getClass());
                if ((objectClass != null) && (objectClass != String.class)) {
                    @SuppressWarnings("rawtypes") final IConverter converter = getConverter(objectClass);
                    displayValue = converter.convertToString(displayObject, getLocale());
                } else if (displayObject != null) {
                    displayValue = displayObject.toString();
                }
            }
            if (Strings.isEmpty(displayValue)) {
                replaceComponentTagBody(markupStream, openTag, defaultNullLabel());
            } else {
                replaceComponentTagBody(markupStream, openTag, displayValue);
            }
        }
    };
    label.setOutputMarkupId(true);
    label.add(new LabelAjaxBehavior(getLabelAjaxEvent()));
    return label;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) Label(org.apache.wicket.markup.html.basic.Label) MarkupStream(org.apache.wicket.markup.MarkupStream) IConverter(org.apache.wicket.util.convert.IConverter)

Example 12 with MarkupStream

use of org.apache.wicket.markup.MarkupStream in project wicket by apache.

the class AjaxEditableLabel method newLabel.

/**
 * Create a new form component instance to serve as label.
 *
 * @param parent
 *            The parent component
 * @param componentId
 *            Id that should be used by the component
 * @param model
 *            The model
 * @return The editor
 */
protected Component newLabel(final MarkupContainer parent, final String componentId, final IModel<T> model) {
    Label label = new Label(componentId, model) {

        private static final long serialVersionUID = 1L;

        @Override
        public <C> IConverter<C> getConverter(final Class<C> type) {
            IConverter<C> c = AjaxEditableLabel.this.getConverter(type);
            return c != null ? c : super.getConverter(type);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
            Object modelObject = getDefaultModelObject();
            if ((modelObject == null) || "".equals(modelObject)) {
                replaceComponentTagBody(markupStream, openTag, defaultNullLabel());
            } else {
                super.onComponentTagBody(markupStream, openTag);
            }
        }
    };
    label.setOutputMarkupId(true);
    label.add(new LabelAjaxBehavior(getLabelAjaxEvent()));
    return label;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) Label(org.apache.wicket.markup.html.basic.Label) MarkupStream(org.apache.wicket.markup.MarkupStream)

Example 13 with MarkupStream

use of org.apache.wicket.markup.MarkupStream in project wicket by apache.

the class AjaxEditableMultiLineLabel method newLabel.

/**
 * {@inheritDoc}
 */
@Override
protected MultiLineLabel newLabel(final MarkupContainer parent, final String componentId, final IModel<T> model) {
    MultiLineLabel label = new MultiLineLabel(componentId, model) {

        private static final long serialVersionUID = 1L;

        /**
         * {@inheritDoc}
         */
        @Override
        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
            Object modelObject = getDefaultModelObject();
            if ((modelObject == null) || "".equals(modelObject)) {
                replaceComponentTagBody(markupStream, openTag, defaultNullLabel());
            } else {
                super.onComponentTagBody(markupStream, openTag);
            }
        }
    };
    label.setOutputMarkupId(true);
    label.add(new LabelAjaxBehavior(getLabelAjaxEvent()));
    return label;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) MultiLineLabel(org.apache.wicket.markup.html.basic.MultiLineLabel)

Example 14 with MarkupStream

use of org.apache.wicket.markup.MarkupStream in project wicket by apache.

the class Component method internalRenderComponent.

/**
 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT!
 * <p>
 * Renders the component at the current position in the given markup stream. The method
 * onComponentTag() is called to allow the component to mutate the start tag. The method
 * onComponentTagBody() is then called to permit the component to render its body.
 */
protected final void internalRenderComponent() {
    final IMarkupFragment markup = getMarkup();
    if (markup == null) {
        throw new MarkupException("Markup not found. Component: " + toString());
    }
    final MarkupStream markupStream = new MarkupStream(markup);
    // Get mutable copy of next tag
    final ComponentTag openTag = markupStream.getTag();
    final ComponentTag tag = openTag.mutable();
    // Call any tag handler
    onComponentTag(tag);
    // If we're an openclose tag
    if (!tag.isOpenClose() && !tag.isOpen()) {
        // We were something other than <tag> or <tag/>
        markupStream.throwMarkupException("Method renderComponent called on bad markup element: " + tag);
    }
    if (tag.isOpenClose() && openTag.isOpen()) {
        markupStream.throwMarkupException("You can not modify a open tag to open-close: " + tag);
    }
    try {
        // Render open tag
        boolean renderBodyOnly = getRenderBodyOnly();
        if (renderBodyOnly) {
            ExceptionSettings.NotRenderableErrorStrategy notRenderableErrorStrategy = ExceptionSettings.NotRenderableErrorStrategy.LOG_WARNING;
            if (Application.exists()) {
                notRenderableErrorStrategy = getApplication().getExceptionSettings().getNotRenderableErrorStrategy();
            }
            if (getFlag(FLAG_OUTPUT_MARKUP_ID)) {
                String message = String.format("Markup id set on a component that renders its body only. " + "Markup id: %s, component id: %s.", getMarkupId(), getId());
                if (notRenderableErrorStrategy == ExceptionSettings.NotRenderableErrorStrategy.THROW_EXCEPTION) {
                    throw new IllegalStateException(message);
                }
                log.warn(message);
            }
            if (getFlag(FLAG_PLACEHOLDER)) {
                String message = String.format("Placeholder tag set on a component that renders its body only. " + "Component id: %s.", getId());
                if (notRenderableErrorStrategy == ExceptionSettings.NotRenderableErrorStrategy.THROW_EXCEPTION) {
                    throw new IllegalStateException(message);
                }
                log.warn(message);
            }
        } else {
            renderComponentTag(tag);
        }
        markupStream.next();
        // Render the body only if open-body-close. Do not render if open-close.
        if (tag.isOpen()) {
            // Render the body. The default strategy will simply call the component's
            // onComponentTagBody() implementation.
            getMarkupSourcingStrategy().onComponentTagBody(this, markupStream, tag);
            // Render close tag
            if (openTag.isOpen()) {
                renderClosingComponentTag(markupStream, tag, renderBodyOnly);
            } else if (renderBodyOnly == false) {
                if (needToRenderTag(openTag)) {
                    // Close the manually opened tag. And since the user might have changed the
                    // tag name ...
                    getResponse().write(tag.syntheticCloseTagString());
                }
            }
        }
    } catch (WicketRuntimeException wre) {
        throw wre;
    } catch (RuntimeException re) {
        throw new WicketRuntimeException("Exception in rendering component: " + this, re);
    }
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupException(org.apache.wicket.markup.MarkupException) ExceptionSettings(org.apache.wicket.settings.ExceptionSettings)

Example 15 with MarkupStream

use of org.apache.wicket.markup.MarkupStream in project wicket by apache.

the class Component method internalRender.

/**
 * Performs a render of this component as part of a Page level render process.
 */
private void internalRender() {
    // Make sure there is a markup available for the Component
    IMarkupFragment markup = getMarkup();
    if (markup == null) {
        throw new MarkupNotFoundException("Markup not found for Component: " + toString());
    }
    // MarkupStream is an Iterator for the markup
    MarkupStream markupStream = new MarkupStream(markup);
    MarkupElement elem = markup.get(0);
    if (elem instanceof ComponentTag) {
        // Guarantee that the markupStream is set and determineVisibility not yet tested
        // See WICKET-2049
        ((ComponentTag) elem).onBeforeRender(this, markupStream);
    }
    // and the isVisible property.
    if (determineVisibility()) {
        setFlag(FLAG_HAS_BEEN_RENDERED, true);
        // Rendering is beginning
        if (log.isDebugEnabled()) {
            log.debug("Begin render {}", this);
        }
        try {
            notifyBehaviorsComponentBeforeRender();
            onRender();
            notifyBehaviorsComponentRendered();
            // Component has been rendered
            rendered();
        } catch (RuntimeException ex) {
            onException(ex);
        }
        if (log.isDebugEnabled()) {
            log.debug("End render {}", this);
        }
    } else // elem is null when rendering a page
    if ((elem != null) && (elem instanceof ComponentTag)) {
        if (getFlag(FLAG_PLACEHOLDER)) {
            renderPlaceholderTag((ComponentTag) elem, getResponse());
        }
    }
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupNotFoundException(org.apache.wicket.markup.MarkupNotFoundException) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupElement(org.apache.wicket.markup.MarkupElement)

Aggregations

MarkupStream (org.apache.wicket.markup.MarkupStream)24 ComponentTag (org.apache.wicket.markup.ComponentTag)14 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)6 MarkupException (org.apache.wicket.markup.MarkupException)6 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)5 MarkupElement (org.apache.wicket.markup.MarkupElement)5 WicketTag (org.apache.wicket.markup.WicketTag)5 Component (org.apache.wicket.Component)3 MarkupContainer (org.apache.wicket.MarkupContainer)3 IOException (java.io.IOException)2 MarkupNotFoundException (org.apache.wicket.markup.MarkupNotFoundException)2 Label (org.apache.wicket.markup.html.basic.Label)2 Response (org.apache.wicket.request.Response)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Page (org.apache.wicket.Page)1 AuthorizationException (org.apache.wicket.authorization.AuthorizationException)1 UnauthorizedActionException (org.apache.wicket.authorization.UnauthorizedActionException)1 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)1