/*
 * AttributeMatrixImportDialog.java
 *
 * Created on 2006/05/08, 11:33
 */
package cytoscape.dialogs;

import cytoscape.Cytoscape;

import cytoscape.util.CyFileFilter;
import cytoscape.util.FileUtil;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import java.io.File;

import java.util.Iterator;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JDialog;


/**
 *
 * This class prompts the user for data for entry of the attribute matrix,
 * which includes expression data as a use case.  The user supplies two
 * things: a filename, and a "key attribute name".  This key attribute
 * establishes the correspondence between nodes in the network and data
 * in the attribute matrix.  For example, when inputting expression data,
 * the data might be listed by probe set.  The user might enter a "probe set"
 * attribute for relating the expression data to nodes on the graph.
 *
 * The key attribute name defaults to "ID".
 *
 * @author cline
 */
public class ImportAttributeMatrixDialog extends javax.swing.JDialog {
	private boolean status;
	private String currentKeyAttributeName = "ID";
	private DefaultComboBoxModel keyAttributeModel = new DefaultComboBoxModel();
	private JComboBox keyAttributeBox = new JComboBox(keyAttributeModel);

	protected class KeyAttributeListener implements ItemListener {
		public void itemStateChanged(ItemEvent event) {
			currentKeyAttributeName = (String) keyAttributeBox.getSelectedItem();
		}
	}

	/** Creates new form AttributeMatrixImportDialog */
	public ImportAttributeMatrixDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		status = false;
		pack();
	}

	/**
	 *  DOCUMENT ME!
	 *
	 * @return  DOCUMENT ME!
	 */
	public String getKeyAttributeName() {
		return currentKeyAttributeName;
	}

	/**
	 *  DOCUMENT ME!
	 *
	 * @return  DOCUMENT ME!
	 */
	public String getFilename() {
		String attributeMatrixFilename = attributeMatrixFileNameTextField.getText();

		return attributeMatrixFilename;
	}

	/**
	 *  DOCUMENT ME!
	 *
	 * @return  DOCUMENT ME!
	 */
	public boolean getStatus() {
		return status;
	}

	/**
	 * 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.
	 */

	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
	private void initComponents() {
		this.setTitle("Import an Attribute/Expression Matrix");

		titleLabel = new javax.swing.JLabel();
		attributeMatrixFileNameTextField = new javax.swing.JTextField();
		selectAttributeMatrixFileButton = new javax.swing.JButton();
		jSeparator1 = new javax.swing.JSeparator();
		cancelButton = new javax.swing.JButton();
		importButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		titleLabel.setFont(new java.awt.Font("Dialog", 1, 14));
		titleLabel.setText("Import Attribute/Expression Matrix File");

		attributeMatrixFileNameTextField.setText("Please select an attribute or expression matrix file...");
		attributeSelectionSliderLabel = new javax.swing.JLabel();
		attributeSelectionSliderLabel.setText("Assign values to nodes using...");

		selectAttributeMatrixFileButton.setText("Select");
		selectAttributeMatrixFileButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					selectAttributeMatrixFileButtonActionPerformed(evt);
				}
			});

		cancelButton.setText("Cancel");
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					cancelButtonActionPerformed(evt);
				}
			});

		importButton.setText("Import");
		importButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					importButtonActionPerformed(evt);
				}
			});
		importButton.setEnabled(false);

		keyAttributeBox = new JComboBox(keyAttributeModel);
		keyAttributeBox.setBounds(10, 75, 260, 20);

		String[] attribs = Cytoscape.getNodeAttributes().getAttributeNames();
		boolean hasCanonicalNameAttribute = false;
		this.keyAttributeModel.addElement("ID");

		for (int ii = 0; ii < attribs.length; ++ii) {
			this.keyAttributeModel.addElement(attribs[ii]);
		}

		keyAttributeBox.setEditable(true);
		keyAttributeBox.setSelectedItem("ID");

		KeyAttributeListener listen = new KeyAttributeListener();
		keyAttributeBox.addItemListener(listen);
		keyAttributeBox.setVisible(true);

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                .add(layout.createSequentialGroup().addContainerGap()
		                                           .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                                      .add(jSeparator1,
		                                                           org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                           379, Short.MAX_VALUE)
		                                                      .add(titleLabel,
		                                                           org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
		                                                           367,
		                                                           org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
		                                                      .add(layout.createSequentialGroup()
		                                                                 .add(attributeMatrixFileNameTextField,
		                                                                      org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                                      302, Short.MAX_VALUE)
		                                                                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                                                 .add(selectAttributeMatrixFileButton)
		                                                                 .add(1, 1, 1))
		                                                      .add(keyAttributeBox)
		                                                      .add(attributeSelectionSliderLabel,
		                                                           org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                           312, Short.MAX_VALUE)
		                                                      .add(org.jdesktop.layout.GroupLayout.TRAILING,
		                                                           layout.createSequentialGroup()
		                                                                 .add(importButton)
		                                                                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                                                 .add(cancelButton)
		                                                                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
		                                           .addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                              .add(layout.createSequentialGroup().addContainerGap()
		                                         .add(titleLabel)
		                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                         .add(jSeparator1,
		                                              org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
		                                              org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                              org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
		                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
		                                                    .add(attributeMatrixFileNameTextField,
		                                                         org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
		                                                         23,
		                                                         org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
		                                                    .add(selectAttributeMatrixFileButton))
		                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                         .add(attributeSelectionSliderLabel)
		                                         .add(keyAttributeBox)
		                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                         .add(29, 29, 29)
		                                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
		                                                    .add(importButton).add(cancelButton))
		                                         .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                          Short.MAX_VALUE)));
		pack();
	} // </editor-fold>

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		status = false;
		this.dispose();
	}

	private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		status = true;
		this.dispose();
	}

	private void selectAttributeMatrixFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:

		//         Create FileFilter
		CyFileFilter filter = new CyFileFilter();

		// Add accepted File Extensions
		filter.addExtension("mrna");
		filter.addExtension("mRNA");
		filter.addExtension("pvals");
		filter.setDescription("All Attribute Matrix files");

		// Get the file name
		File file = FileUtil.getFile("Import AttributeMatrix File", FileUtil.LOAD,
		                             new CyFileFilter[] { filter });

		if (file != null) {
			attributeMatrixFileNameTextField.setText(file.getAbsolutePath());
			attributeMatrixFileNameTextField.setToolTipText(file.getAbsolutePath());
			importButton.setEnabled(true);
		}
	}

	// Variables declaration - do not modify
	private javax.swing.JButton cancelButton;
	private javax.swing.JButton importButton;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JTextField attributeMatrixFileNameTextField;
	private javax.swing.JLabel attributeSelectionSliderLabel;
	private javax.swing.JButton selectAttributeMatrixFileButton;
	private javax.swing.JLabel titleLabel;

	// End of variables declaration
}
