package lemming.context.inbound;

import lemming.context.BaseContext;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.extensions.markup.html.repeater.tree.AbstractTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.ITreeProvider;
import org.apache.wicket.extensions.markup.html.repeater.tree.NestedTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.content.Folder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import java.util.Iterator;

/**
 * A tree used to map inbound contexts to contexts.
 */
public class ContextTree extends NestedTree<BaseContext> {
    /**
     * Creates a tree.
     *
     * @param provider tree provider
     */
    public ContextTree(ITreeProvider<BaseContext> provider) {
        super("inboundContextTree", provider);
    }

    /**
     * Creates a content component.
     *
     * @param id if of the component
     * @param model model of the component
     * @return The component.
     */
    @Override
    protected Component newContentComponent(String id, IModel<BaseContext> model) {
        return new ContextFolder(id, this, model);
    }

    /**
     * Collapses all roots.
     */
    public void collapseAll() {
        Iterator<? extends BaseContext> iterator = getProvider().getRoots();

        while (iterator.hasNext()) {
            collapse(iterator.next());
        }
    }

    /**
     * Expands all roots.
     */
    public void expandAll() {
        Iterator<? extends BaseContext> iterator = getProvider().getRoots();

        while (iterator.hasNext()) {
            expand(iterator.next());
        }
    }

    /**
     * A folder component for contexts.
     */
    private class ContextFolder extends Folder<BaseContext> {
        /**
         * Creates a folder.
         *
         * @param id if of the folder
         * @param tree parent tree
         * @param model folder model
         */
        ContextFolder(String id, AbstractTree<BaseContext> tree, IModel<BaseContext> model) {
            super(id, tree, model);
        }

        /**
         * Creates a label model.
         *
         * @param model folder model
         * @return The label model.
         */
        @Override
        protected IModel<?> newLabelModel(IModel<BaseContext> model) {
            BaseContext context = model.getObject();
            return Model.of(String.format("<b>%d</b>: %s", context.getNumber(), context.getKeyword()));
        }

        /**
         * Creates a label component.
         *
         * @param id id of the component
         * @param model model of the component
         * @return The component.
         */
        @Override
        protected Component newLabelComponent(String id, IModel<BaseContext> model) {
            Component component = super.newLabelComponent(id, model);
            component.setEscapeModelStrings(false);
            return component;
        }

        /**
         * Creates a link component.
         *
         * @param id id of the component
         * @param model model of the component
         * @return The component.
         */
        @Override
        protected MarkupContainer newLinkComponent(String id, IModel<BaseContext> model) {
            MarkupContainer container = super.newLinkComponent(id, model);
            container.setEscapeModelStrings(false);
            return container;
        }
    }
}

@@ -541,24 +541,75 @@
 va.util.
-Iterator
+ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List
 ;%0A%0A/**%0A 
@@ -3493,21 +3493,1454 @@
 r;%0A        %7D
+%0A%0A        /**%0A         * Returns a style class for the folder.%0A         *%0A         * @return A style class.%0A         */%0A        @Override%0A        protected String getStyleClass() %7B%0A            String styleClass = super.getStyleClass();%0A            ITreeProvider%3CBaseContext%3E provider = ContextTree.this.getProvider();%0A            StringBuilder stringBuilder = new StringBuilder();%0A%0A            if (getModelObject() instanceof InboundContext) %7B%0A                return styleClass;%0A            %7D%0A%0A            if (provider.hasChildren(getModelObject())) %7B%0A                List%3CBaseContext%3E children = new ArrayList%3C%3E();%0A                Iterator%3C? extends BaseContext%3E iterator = provider.getChildren(getModelObject());%0A%0A                while (iterator.hasNext()) %7B%0A                    children.add(iterator.next());%0A                %7D%0A%0A                if (children.size() %3E 1) %7B%0A                    return stringBuilder.append(styleClass).append(%22 %22).append(%22has-multiple-children%22).toString();%0A                %7D else if (getModelObject().getKeyword().equals(children.get(0).getKeyword())) %7B%0A                    return styleClass;%0A                %7D else %7B%0A                    return stringBuilder.append(styleClass).append(%22 %22).append(%22has-different-child%22).toString();%0A                %7D%0A            %7D else %7B%0A                return stringBuilder.append(styleClass).append(%22 %22).append(%22has-no-children%22).toString();%0A            %7D%0A        %7D
 %0A    %7D%0A%7D%0A

Change the style class for root nodes depending on their children.