package dr.app.bfe;

import dr.xml.XMLModelFile;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;

/**
 * Package: XMLTreeModel
 * Description:
 * <p/>
 * <p/>
 * Created by
 *
 * @author Alexander V. Alekseyenko (alexander.alekseyenko@gmail.com)
 *         Date: Apr 14, 2009
 *         Time: 11:07:19 PM
 */
public class XMLTreeModel implements TreeModel {
    protected class ElementObject {
        public Element element;
        protected LinkedList<ElementObject> children;
        protected String id;

        ElementObject(Element element) {
            this.element = element;
            id = "";
            Attribute idAttribute = element.getAttribute("id");
            if (idAttribute == null) {
                idAttribute = element.getAttribute("idref");
            }
            if (idAttribute != null) {
                id = idAttribute.getValue();
            }
            children = null;
        }

        public String toString() {
            if (element == null)
                return "<<Empty>>";
            return element.getName() + ":" + id;
        }

        public String getId() {
            return id;
        }

        public LinkedList<ElementObject> getChildren() {
            if (children != null) {
                return children;
            }
            children = new LinkedList<ElementObject>();
            if (element == null)
                return children;
            for (Object child : element.getChildren()) {
                if (child instanceof Element) {
                    children.add(new ElementObject((Element) child));
                }
            }
            return children;
        }

    }

    public ElementObject getRootElement() {
        return rootElement;
    }

    public XMLModelFile getXmlModel() {
        return xmlModel;
    }

    private ElementObject rootElement;
    XMLModelFile xmlModel;

    XMLTreeModel(String filename) {

        SAXBuilder parser = new SAXBuilder();

        Document doc;
        try {
            doc = parser.build(new File(filename));
            rootElement = new ElementObject(doc.getRootElement());
            xmlModel = new XMLModelFile(doc.getRootElement());
        }
        catch (IOException e) {
            System.err.println("Error opening file " + filename);
            rootElement = new ElementObject(null);
        }
        catch (JDOMException e) {
        }
    }

    // TreeModel method implementation
    public Object getRoot() {
        return rootElement;  //AUTOGENERATED METHOD IMPLEMENTATION
    }

    public Object getChild(Object o, int i) {
        if (!(o instanceof ElementObject)) {
            return null;
        }

        if (i < ((ElementObject) o).getChildren().size()) {
            return ((ElementObject) o).getChildren().get(i);
        }
        return null;
    }

    public int getChildCount(Object o) {
        return ((ElementObject) o).getChildren().size();
    }

    public boolean isLeaf(Object o) {
        return ((ElementObject) o).getChildren().size() == 0;  //AUTOGENERATED METHOD IMPLEMENTATION
    }

    public void valueForPathChanged(TreePath treePath, Object o) {
        //AUTOGENERATED METHOD IMPLEMENTATION
    }

    public int getIndexOfChild(Object o, Object o1) {
        LinkedList<ElementObject> children = ((ElementObject) o).getChildren();
        for (int i = 0; i < children.size(); ++i) {
            if (o1 == children.get(i))
                return i;
        }
        return -1;
    }

    public void addTreeModelListener(TreeModelListener treeModelListener) {
        //AUTOGENERATED METHOD IMPLEMENTATION
    }

    public void removeTreeModelListener(TreeModelListener treeModelListener) {
        //AUTOGENERATED METHOD IMPLEMENTATION
    }
}
