package cs437.som.demo;

import cs437.som.SOMBuilderConfigPanel;
import cs437.som.TrainableSelfOrganizingMap;

import javax.swing.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class SOMBuilber extends JDialog implements PropertyChangeListener {
    private static final long serialVersionUID = 0L;

    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private SOMBuilderConfigPanel mapConfigPanel;

    private TrainableSelfOrganizingMap map = null;

    private int inputSize;
    private int expectedIterations;

    public SOMBuilber(int inputSize, int expectedIterations) {
        this.inputSize = inputSize;
        this.expectedIterations = expectedIterations;

        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        mapConfigPanel.addPropertyChangeListener(this);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                map = mapConfigPanel.createSOM(SOMBuilber.this.inputSize,
                        SOMBuilber.this.expectedIterations);
                dispose();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { dispose(); }
        });

        // call dispose() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) { dispose(); }
        });

        // call dispose() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    }

    public TrainableSelfOrganizingMap getMap() {
        return map;
    }

    public void propertyChange(PropertyChangeEvent evt) {
        buttonOK.setEnabled(mapConfigPanel.isValid());
    }

    public static void main(String[] args) {
        SOMBuilber dialog = new SOMBuilber(10, 1000);
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    @Override
    public String toString() {
        return "SOMBuilber";
    }
}

@@ -233,16 +233,119 @@
 tener;%0A%0A
+/**%0A * A dialog for gathering a self-organizing map's configuration from an%0A * application's user.%0A */%0A
 public c
@@ -725,24 +725,456 @@
 terations;%0A%0A
+    /**%0A     * Create a new SOMBuilber that will, if successful, return a trainable%0A     * self-organizing map that expects %7B@code inputSize%7D as the length of its%0A     * input vector and %7B@code expectedIterations%7D iterations of training.%0A     * %0A     * @param inputSize The input vector length for the generated map.%0A     * @param expectedIterations  The expected amount of training iterations%0A     * for the generated map.%0A     */%0A
     public S
@@ -2525,79 +2525,459 @@
     
-public TrainableSelfOrganizingMap getMap() %7B%0A        return map;%0A    %7D%0A
+/**%0A     * Get the self-organizing map as configured by input to the form, if one%0A     * exists.%0A     *%0A     * @return A trainable self-organizing map if the user selected 'OK.'  The%0A     * return result will be %7B@code null%7D if the user cancelled the dialog.%0A     */%0A    public TrainableSelfOrganizingMap getMap() %7B%0A        return map;%0A    %7D%0A%0A    /**%0A     * Listen for validity changes on the SOMBuilderConfigPanel.%0A     *%0A     * %7B@inheritDoc%7D%0A     */
 %0A   
@@ -3089,24 +3089,106 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Demo main for SOMBuilber form.%0A     * @param args ignored.%0A     */%0A
     public s

Document SOMBuilber.