/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jonasadmin.test.util;

import org.xml.sax.SAXException;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebImage;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;

/**
 * Define a class to manipulate the tree frame of JonasAdmin
 * @author kemlerp
 *
 */
public class JonasAdminTree {

    /**
     * NAME of FRAME tree
     */
    private static final String FRAME_TREE = "tree";

    /**
     * Verify if it is the good link of the tree which is selected
     * @param wr The WebResponse of the 'tree' frame
     * @param name A string with the name of the link
     * @return a boolean. True if the name is found in the link, else false.
     */
    public static boolean treeControlSelected (WebResponse wr, String selectedLink) throws SAXException {
        boolean isSelected = false;     // the result
        int nbSelectedLink = 0;         // number of selected link
        String attribut;                // value of class attribut
        String linkUrl = null;          // url of selected attribut

        // Get all links
        WebLink[] links;
        try {
            links = wr.getLinks();

            // Search links with the 'tree-control-selected' class attribut
            // and increase nbSelectedLink
            for (int i = 0; i < links.length; i++) {
                attribut = links[i].getAttribute("class");
                if (attribut.indexOf("tree-control-selected") != -1) {
                    linkUrl = links[i].getURLString();
                    nbSelectedLink++;
                }
            }

            if (nbSelectedLink == 1) {
                if (selectedLink.equals(linkUrl)) {
                    isSelected = true;
                }
            }
        } catch (SAXException e) {
            throw new SAXException("No link was found : " + e);
        }

        return isSelected;
    }

    /**
     * Open tree
     * @param wr Tree Frame Web Response
     * @return Tree Frame Web Response with opened tree
     * @throws Exception if an error occurs
     */
    public static WebResponse openTree(WebResponse wr, WebConversation wc) throws Exception {
            WebImage[] frameTreeImages = wr.getImages();
        for (int i = 0; i < frameTreeImages.length; i++) {
            if (frameTreeImages[i].getSource().indexOf("node_close") != -1) {
                if (!frameTreeImages[i].getLink().getURLString().endsWith("*mbeans")) {
                    frameTreeImages[i].getLink().click();
                    // get frame tree response
                    wr = wc.getFrameContents(FRAME_TREE);
                    // update frameTreeImages
                    frameTreeImages = wr.getImages();
                }
            }
        }
        return wr;
    }

}
