/*
 * DatabaseHostNodeSorter.java
 *
 * Copyright (C) 2002-2009 Takis Diakoumis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.executequery.gui.browser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;

import org.executequery.gui.browser.nodes.DatabaseHostNode;

/**
 *
 * @author   Takis Diakoumis
 * @version  $Revision: 112 $
 * @date     $Date: 2008-10-29 11:01:08 +1100 (Wed, 29 Oct 2008) $
 */
class DatabaseHostNodeSorter {

    public static final String SORT_A_Z = "sortAtoZ";
    
    public static final String SORT_Z_A = "sortZtoA";
    
    public static final String SORT_RESTORE = "sortRestore";
    
    private String lastSort = SORT_RESTORE;

    private Map<String, Comparator<DatabaseHostNode>> comparators; 
    
    public DatabaseHostNodeSorter() {

        comparators = new HashMap<String, Comparator<DatabaseHostNode>>(3);
        
        comparators.put(SORT_A_Z, new AtoZComparator());
        comparators.put(SORT_Z_A, new ZtoAComparator());
        comparators.put(SORT_RESTORE, new RestoreComparator());
    }

    @SuppressWarnings("unchecked")
    public void sort(DefaultMutableTreeNode rootNode) {
        
        List<DatabaseHostNode> children = 
            new ArrayList<DatabaseHostNode>(rootNode.getChildCount());

        for (Enumeration i = rootNode.children(); i.hasMoreElements();) {

            children.add((DatabaseHostNode) i.nextElement());            
        }
        
        Collections.sort(children, nextComparator());
        
        rootNode.removeAllChildren();
        
        for (DatabaseHostNode treeNode : children) {

            rootNode.add(treeNode);
        }
        
    }
    
    private Comparator<DatabaseHostNode> nextComparator() {

        if (SORT_A_Z.equals(lastSort)) {
            
            lastSort = SORT_Z_A;

        } else if (SORT_Z_A.equals(lastSort)) {

            lastSort = SORT_RESTORE;

        } else {
            
            lastSort = SORT_A_Z;
        }

        return comparators.get(lastSort);
    }

    private class AtoZComparator implements Comparator<DatabaseHostNode> {

        public int compare(DatabaseHostNode node1, DatabaseHostNode node2) {

            return node1.getDatabaseConnection().getName().compareToIgnoreCase(
                    node2.getDatabaseConnection().getName());        
        }
    
    }
    
    private class ZtoAComparator implements Comparator<DatabaseHostNode> {

        public int compare(DatabaseHostNode node1, DatabaseHostNode node2) {

            return (node1.getDatabaseConnection().getName().compareToIgnoreCase(
                    node2.getDatabaseConnection().getName())) * -1;        
        }
    
    }
    
    private class RestoreComparator implements Comparator<DatabaseHostNode> {

        public int compare(DatabaseHostNode node1, DatabaseHostNode node2) {

            int node1Order = node1.getOrder();
            int node2Order = node2.getOrder();

            if (node1Order < node2Order) {
                
                return -1;
                
            } else if (node2Order < node1Order) {
            
                return 1;
                
            } else {
                
                return 0;
            }

        }
    
    }

}


