/*
 * @(#)PathListView.java    2.4.0   29 August 2005
 *
 * Copyright 2005
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.*;
import edu.neu.ccs.filter.*;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

/**
 * <p>Class <code>PathListView</code> creates a view for input and output
 * of a <code>PathList</code> in a GUI.<p>
 *
 * <p>This view requires that a <code>PathList</code> returned by one of its
 * methods is valid, that is, that the start node is of type MOVE.</p>
 */
public class PathListView
    extends TablePanel
{
    private static final int gap = 5;
    
    private static final Object[][] windingRuleStuff =
        new Object[][]
            { { "WIND_NON_ZERO", WindingRule.WIND_NON_ZERO },
              { "WIND_EVEN_ODD", WindingRule.WIND_EVEN_ODD } };
    
    
    private static final StringableFilter requireMoveNode =
        new StringableFilter() {
            public Stringable filterStringable(Stringable object)
                throws FilterException
            {
                String message =
                    "PathNode of type MOVE required\n";
                
                if (! (object instanceof PathNode))
                    throw new FilterException(object, message);
                
                PathNode node = (PathNode) object;
                
                if (node.getNodeType() == PathNode.MOVE)
                    return object;
                else
                    throw new FilterException(object, message);
            }
        };
    
    
    protected StringObjectRadioPanel windingRulePanel =
        new StringObjectRadioPanel(windingRuleStuff);
    
    
    protected SimpleArrayPanel nodeListPanel =
        new SimpleArrayPanel(PathNodeView.class, 0, 5, gap, true);
    
    
    protected Display windingRuleWrapper =
        new Display(windingRulePanel, "Winding Rule");
    
    protected Display nodeListWrapper =
        new Display(nodeListPanel, "Path Node List");
    
    
    public PathListView() {
        super(2, 1, gap, gap, CENTER);
        
        addObject(windingRuleWrapper, 0, 0);
        addObject(nodeListWrapper   , 1, 0);
        
        PathNodeView startView = (PathNodeView) nodeListPanel.getTypedView(0);
        
        startView.setFilter(requireMoveNode);
    }
    
    
    public final PathList demandPathList() {
        WindingRule rule =
            (WindingRule) windingRulePanel.getSelectedObject();
        
        PathNode[] nodes =
            (PathNode[]) nodeListPanel.demandObjects();
        
        return new PathList(nodes, rule);
    }
    
    
    public final PathList requestPathList()
        throws CancelledException
    {
        WindingRule rule =
            (WindingRule) windingRulePanel.getSelectedObject();
        
        PathNode[] nodes =
            (PathNode[]) nodeListPanel.requestObjects();
        
        return new PathList(nodes, rule);
    }
    
    
    public final void setViewFromPathList(PathList list) {
        if (list == null)
            return;
        
        windingRulePanel.setSelectedObject(list.getWindingRule());
        
        nodeListPanel.setLength(list.size());
        nodeListPanel.setViewStates(list.getPathNodeStateArray());
    }
    
}
