/*
 * MLPerceptronWizard.java
 *
 * Created on October 3, 2008, 12:19 AM
 */

package org.neuroph.easyneurons.dialog;

import java.awt.event.KeyEvent;

import org.jdesktop.application.Action;
import org.neuroph.easyneurons.EasyNeuronsApplicationView;
import org.neuroph.util.TransferFunctionType;

/**
 * 
 * @author Zoran Sevarac <sevarac@gmail.com>
 */
public class MLPerceptronWizard extends javax.swing.JDialog {
	private static final long serialVersionUID = 1L;
	
	EasyNeuronsApplicationView application;

	/** Creates new form MLPerceptronWizard */
	public MLPerceptronWizard(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public MLPerceptronWizard(java.awt.Frame parent, boolean modal,
			EasyNeuronsApplicationView application) {
		super(parent, modal);
		initComponents();
		this.application = application;
		fillTransferFunctionCombo();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;

		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		inputPanel = new javax.swing.JPanel();
		neuronsNumLabel = new javax.swing.JLabel();
		neuronsNumField = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		transferComboBox = new javax.swing.JComboBox();
		buttonPanel = new javax.swing.JPanel();
		createButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		helpButton = new javax.swing.JButton();

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null } }, new String[] { "Title 1",
						"Title 2", "Title 3", "Title 4" }));
		jTable1.setName("jTable1"); // NOI18N
		jScrollPane1.setViewportView(jTable1);

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
				.getInstance(
						org.neuroph.easyneurons.EasyNeuronsApplication.class)
				.getContext().getResourceMap(MLPerceptronWizard.class);
		setTitle(resourceMap.getString("Form.title")); // NOI18N
		setName("Form"); // NOI18N
		setResizable(false);
		getContentPane().setLayout(new java.awt.GridBagLayout());

		inputPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		inputPanel.setName("inputPanel"); // NOI18N
		inputPanel.setLayout(new java.awt.GridBagLayout());

		neuronsNumLabel.setText(resourceMap.getString("neuronsNumLabel.text")); // NOI18N
		neuronsNumLabel.setName("neuronsNumLabel"); // NOI18N
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 5);
		inputPanel.add(neuronsNumLabel, gridBagConstraints);

		neuronsNumField.setColumns(20);
		neuronsNumField.setText(resourceMap.getString("neuronsNumField.text")); // NOI18N
		neuronsNumField.setName("neuronsNumField"); // NOI18N
		neuronsNumField.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				neuronsNumFieldKeyPressed(evt);
			}
		});
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 10);
		inputPanel.add(neuronsNumField, gridBagConstraints);

		jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
		jLabel1.setName("jLabel1"); // NOI18N
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(5, 10, 10, 5);
		inputPanel.add(jLabel1, gridBagConstraints);

		transferComboBox.setName("transferComboBox"); // NOI18N
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 10);
		inputPanel.add(transferComboBox, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 400.0;
		gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
		getContentPane().add(inputPanel, gridBagConstraints);

		buttonPanel.setName("buttonPanel"); // NOI18N

		javax.swing.ActionMap actionMap = org.jdesktop.application.Application
				.getInstance(
						org.neuroph.easyneurons.EasyNeuronsApplication.class)
				.getContext().getActionMap(MLPerceptronWizard.class, this);
		createButton.setAction(actionMap.get("create")); // NOI18N
		createButton.setText(resourceMap.getString("createButton.text")); // NOI18N
		createButton.setName("createButton"); // NOI18N
		buttonPanel.add(createButton);

		cancelButton.setAction(actionMap.get("cancel")); // NOI18N
		cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
		cancelButton.setName("cancelButton"); // NOI18N
		buttonPanel.add(cancelButton);

		helpButton.setText(resourceMap.getString("helpButton.text")); // NOI18N
		helpButton.setEnabled(false);
		helpButton.setName("helpButton"); // NOI18N
		buttonPanel.add(helpButton);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
		getContentPane().add(buttonPanel, gridBagConstraints);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void neuronsNumFieldKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_neuronsNumFieldKeyPressed
		int key = evt.getKeyCode();
		if (key == KeyEvent.VK_ENTER) {
			this.create();
		} else if (key == KeyEvent.VK_ESCAPE) {
			this.cancel();
		}
	}// GEN-LAST:event_neuronsNumFieldKeyPressed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				MLPerceptronWizard dialog = new MLPerceptronWizard(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	private void fillTransferFunctionCombo() {
		transferComboBox.removeAllItems();
		transferComboBox.addItem(new ComboItem("Sigmoid", TransferFunctionType.SIGMOID.toString()));
		transferComboBox.addItem(new ComboItem("Tanh", TransferFunctionType.TANH.toString()));
	}

	@Action
	public void cancel() {
		this.dispose();
	}

	@Action
	public void create() {
		String neuronsNum = this.neuronsNumField.getText().trim();
		TransferFunctionType tfType = TransferFunctionType.valueOf( ((ComboItem)transferComboBox.getSelectedItem()).getValue().toString());

		application.newMLPerceptronNetwork(neuronsNum, tfType);
		this.setVisible(false);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JPanel buttonPanel;
	private javax.swing.JButton cancelButton;
	private javax.swing.JButton createButton;
	private javax.swing.JButton helpButton;
	private javax.swing.JPanel inputPanel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField neuronsNumField;
	private javax.swing.JLabel neuronsNumLabel;
	private javax.swing.JComboBox transferComboBox;
	// End of variables declaration//GEN-END:variables

}
