/*
    Shows how to create custom panels that follow different paths through
    the wizard depending on the user's answer.
    
    Include DecisionTree, Branch1, Branch2, and Branch3 in the Custom tab of 
    JExpress Developer. You may include them in any of the custom fields, but they
    must appear in the order presented. 
    
    When you run, you'll see that if you answer 1, then Branch1 and Branch 3
    appear to the user. If you answer 2, then Branch2 and Branch 3 appear. If
    the user backs up and changes their mind, then the appropriate branch of the
    tree is followed as they proceed forward again.
    
    Copyright (C) 1998-2008 DeNova
    
*/

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import com.denova.ui.GridBagControl;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.InstallPropertyNames;


public class DecisionTree extends WizardPanel
                          implements InstallPropertyNames {

    public DecisionTree ( PropertyList properties) {

        // initialize WizardPanel superclass
        super ( properties);

        setLayout ( new BorderLayout () );

        JPanel panel = new JPanel ();

        GridBagControl gc = new GridBagControl ( panel );
        GridBagConstraints optionConstraints = gc. getConstraints ();
        optionConstraints. anchor = GridBagConstraints. WEST;
        
        gc. addVerticalSpace ();
        yesButton = new JRadioButton( "1", true);
        noButton = new JRadioButton( "2", false);
        gc. add ( new JLabel("Option: ") );

        ButtonGroup group = new ButtonGroup();
        Box box = Box. createHorizontalBox ();

        group. add ( yesButton );
        box. add ( yesButton );

        group. add ( noButton );
        box. add ( noButton );

        gc. endRow ( optionConstraints, box );
        gc. addVerticalSpace ();

        final int BeveledMargin = 5;
        BevelBorder outerBorder = new BevelBorder ( BevelBorder. LOWERED );
        Border innerBorder = new EmptyBorder ( BeveledMargin, BeveledMargin, BeveledMargin, BeveledMargin );

        JPanel outerPanel = new JPanel ( new GridLayout ( 1, 1 ) );
        outerPanel. setBorder ( outerBorder );

        JPanel innerPanel = new JPanel ( new GridLayout ( 1, 1 ) );
        innerPanel. setBorder ( innerBorder );

        innerPanel. add ( panel );
        outerPanel. add ( innerPanel );

        add ( outerPanel, BorderLayout. CENTER );
    }

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
    public synchronized void enter()
    {
        // make sure all the special custom panels that follow this one are active
        setEnabledNamedPanel ( "Branch1", true );
        setEnabledNamedPanel ( "Branch2", true );
        setEnabledNamedPanel ( "Branch3", true );
    }

    public boolean isOk () {

        boolean ok = true;

        if ( yesButton. isSelected () ) {
            
            // disable these panels if the user answered Yes
            setEnabledNamedPanel ( "Branch2", false );
            
	    }
        
        else if ( noButton. isSelected () ) {
            setEnabledNamedPanel ( "Branch1", false );
        }
        
        else {
            note ( "Please select Yes or No." );
            ok = false;
        }

		return ok;
	}

    public String getName () {
        return "DecisionTree";
    }

    JRadioButton yesButton;
    JRadioButton noButton;
    
}  // DecisionTree