/*
 * Copyright (C) 2003-2004 Red Hat Inc. All Rights Reserved.
 *
 * 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 (at your option) any 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
 *
 */
package com.arsdigita.cms.ui.category;

import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.SingleSelectionModel;
import com.arsdigita.bebop.Tree;
import com.arsdigita.bebop.tree.TreeModel;
import com.arsdigita.bebop.tree.TreeModelBuilder;
import com.arsdigita.categorization.Category;
import com.arsdigita.categorization.CategoryTreeModelLite;
import com.arsdigita.cms.CMS;
import com.arsdigita.cms.ContentSection;
import com.arsdigita.util.LockableImpl;

/**
 * Lists category tree.
 *
 * @author Tri Tran (tri@arsdigita.com)
 * @version $Id: CategoryTreeModelBuilder.java 287 2005-02-22 00:29:02Z sskracic $
 */
class CategoryTreeModelBuilder extends LockableImpl
        implements TreeModelBuilder {

    private SingleSelectionModel m_contextModel = null;

    public CategoryTreeModelBuilder() {
        this(null);
    }

    public CategoryTreeModelBuilder(SingleSelectionModel contextModel) {
        super();
        m_contextModel = contextModel;
    }

    public static final String versionId =
        "$Id: CategoryTreeModelBuilder.java 287 2005-02-22 00:29:02Z sskracic $" +
        "$Author: sskracic $" +
        "$DateTime: 2004/08/17 23:15:09 $";

    public final TreeModel makeModel(final Tree tree, final PageState state) {
        final ContentSection section = CMS.getContext().getContentSection();
        final Category root = Category.getRootForObject(section, 
                                                        getUseContext(state));
        String order = ContentSection.getConfig().getCategoryTreeOrder();
        final CategoryTreeModelLite model = new CategoryTreeModelLite(root, order);

        return model;
    }
    private String getUseContext(PageState state) {
        String context = null;
        if (m_contextModel != null) {
            context = (String) m_contextModel.getSelectedKey(state);
            if ((CategoryUseContextModelBuilder.DEFAULT_USE_CONTEXT).equals(context)) {
                context = null;
            }
        }
        return context;
    }

}
