package lemming.sense;

import lemming.tree.AbstractNestedTree;
import lemming.tree.INestedTreeProvider;
import lemming.tree.NodeLabel;
import org.apache.wicket.Component;
import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.ResourceReference;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * A nested tree for senses.
 */
public class SenseTree extends AbstractNestedTree<Sense> {
    /**
     * Creates a sennse tree,
     *
     * @param id ID of a tree
     * @param provider provider of tree data
     */
    public SenseTree(String id, INestedTreeProvider<Sense> provider) {
        super(id, provider);
    }

    /**
     * Creates a sennse tree,
     *
     * @param id ID of a tree
     * @param provider provider of tree data
     * @param selectedSenseObject model of the selected sense object
     */
    public SenseTree(String id, INestedTreeProvider<Sense> provider, Sense selectedSenseObject) {
        super(id, provider);
    }

    /**
     * Creates a new content component.
     *
     * @param id ID of the component
     * @param model model of the node object
     * @return A node label component.
     */
    @Override
    protected Component newContentComponent(String id, IModel<Sense> model) {
        return new NodeLabel<Sense>(id) {
            @Override
            protected String getLabelText() {
                return model.getObject().getMeaning();
            }
        };
    }

    /**
     * Defines which sense nodes are expanded.
     *
     * @param selectedNode the selected node object
     * @return A model with a set of senses.
     */
    @Override
    @SuppressWarnings("unchecked")
    protected IModel<Set<Sense>> createExpandState(Sense selectedNode) {
        Iterator iterator = getProvider().getRoots();
        HashSet<Sense> expandedSenses = new HashSet<Sense>();

        while (iterator.hasNext()) {
            expandedSenses.add((Sense) iterator.next());
        }

        return new Model(expandedSenses);
    }

    /**
     * Renders to the web response what the component wants to contribute.
     *
     * @param response response object
     */
    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        ResourceReference styleReference = new CssResourceReference(SenseTree.class, "styles/sense-tree.css");
        response.render(CssHeaderItem.forReference(styleReference));
    }
}

@@ -41,98 +41,9 @@
 ree.
-AbstractNestedTree;%0Aimport lemming.tree.INestedTreeProvider;%0Aimport lemming.tree.NodeLabel
+*
 ;%0Aim
@@ -555,17 +555,169 @@
 %3CSense%3E 
-%7B
+implements IDraggableTree %7B%0A    /**%0A     * A set of drop listeners.%0A     */%0A    private Set%3CIDropListener%3E dropListeners = new HashSet%3CIDropListener%3E();%0A
 %0A    /**
@@ -1740,32 +1740,354 @@
       %7D;%0A    %7D%0A%0A
+    /**%0A     * Creates a node component.%0A     *%0A     * @param id ID of the component%0A     * @param model model of the node object%0A     * @return A node component.%0A     */%0A    @Override%0A    public Component newNodeComponent(String id, IModel%3CSense%3E model) %7B%0A        return new DraggableNode%3CSense%3E(id, this, model);%0A    %7D%0A%0A
     /**%0A     * D
@@ -2983,16 +2983,16 @@
 .css%22);%0A
-
         
@@ -3057,11 +3057,323 @@
 );%0A    %7D
+%0A%0A    /**%0A     *%0A     * @param listener%0A     */%0A    @Override%0A    public void addDropListener(IDropListener listener) %7B%0A        dropListeners.add(listener);%0A    %7D%0A%0A    /**%0A     *%0A     * @return%0A     */%0A    @Override%0A    public Set%3C? extends IDropListener%3E getDropListeners() %7B%0A        return dropListeners;%0A    %7D
 %0A%7D%0A

Use draggable nodes for class SenseTree.