/*
 * @(#)PathListView.java    2.6.0a   23 October 2007
 *
 * Copyright 2007
 * 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>As of 2.6.0a, does not impose the restriction that the first non-empty
 * <code>PathNodeView</code> contain a <code>MOVE</code> operation.  Instead,
 * if the caller demands or requests a <code>PathList</code>, the list will
 * be created provided that all <code>PathNodeView</code>&rsquo;s are either
 * entirely whitespace or contain a valid <code>PathNode</code> string.  It
 * is up to the caller to determine whether or not the <code>isValid()</code>
 * method of the <code>PathList</code> returns <code>true</code>.</p>
 * </p>
 * 
 * <p>As of 2.6.0a, added methods to get and set the winding rule independent
 * of the path node data.</p>
 * 
 * @version 2.6.0a
 * @since   2.4.0
 */
public class PathListView
    extends TablePanel
{
    /** The table panel gap = 5. */
    private static final int gap = 5;
    
    
    /** The winding rule objects for the radio panel. */
    private static final Object[][] windingRuleStuff =
        new Object[][]
            { { "WIND_NON_ZERO", WindingRule.WIND_NON_ZERO },
              { "WIND_EVEN_ODD", WindingRule.WIND_EVEN_ODD } };
    
    
    /** The winding rule radio panel layout. */
    protected TableLayout windingRuleLayout =
        new TableLayout(1, 2, 2 * gap, 2 * gap, CENTER);
    
    
    /** The winding rule radio panel. */
    protected StringObjectRadioPanel windingRulePanel =
        new StringObjectRadioPanel(windingRuleStuff, null, 0, windingRuleLayout);
    
    
    /** The node list simple array panel. */
    protected SimpleArrayPanel nodeListPanel =
        new SimpleArrayPanel(PathNodeView.class, 0, 5, gap, true);
    
    
    /** The titled wrapper for the winding rule radio panel. */
    protected Display windingRuleWrapper =
        new Display(windingRulePanel, "Winding Rule");
    
    
    /** The titled wrapper for the node list simple array panel. */
    protected Display nodeListWrapper =
        new Display(nodeListPanel, "Path Node List");
    
    
    /** The default constructor. */
    public PathListView() {
        super(2, 1, gap, gap, CENTER);
        
        addObject(windingRuleWrapper, 0, 0);
        addObject(nodeListWrapper   , 1, 0);
    }
    
    
    /** The demand method to return the path list defined in this view. */
    public final PathList demandPathList() {
        WindingRule rule =
            (WindingRule) windingRulePanel.getSelectedObject();
        
        PathNode[] nodes =
            (PathNode[]) nodeListPanel.demandObjects();
        
        return new PathList(nodes, rule);
    }
    
    
    /** The request method to return the path list defined in this view. */
    public final PathList requestPathList()
        throws CancelledException
    {
        WindingRule rule =
            (WindingRule) windingRulePanel.getSelectedObject();
        
        PathNode[] nodes =
            (PathNode[]) nodeListPanel.requestObjects();
        
        return new PathList(nodes, rule);
    }
    
    
    /** Set the path list data from the given path list. */
    public final void setViewFromPathList(PathList pathList) {
        if (pathList == null)
            return;
        
        windingRulePanel.setSelectedObject(pathList.getWindingRule());
        
        nodeListPanel.setLength(pathList.size());
        nodeListPanel.setViewStates(pathList.getPathNodeStateArray());
    }
    
    
    /** Get the winding rule setting of this view. */
    public final WindingRule getWindingRule() {
        return (WindingRule) windingRulePanel.getSelectedObject();
    }
    
    
    /** Set the winding rule setting of this view. */
    public final void setWindingRule(WindingRule rule) {
        if (rule == null)
            return;
        
        windingRulePanel.setSelectedObject(rule);
    }
    
}
