import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;
import java.util.HashMap;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Choose a port, any port!
 * 
 * Java Communications is a "standard extention" and must be downloaded and
 * installed separately from the JDK before you can even compile this program.
 * 
 * @author Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: PortChooser.java,v 1.9 2004/04/11 23:50:40 ian Exp $
 * methods added by M. Turhan Coban 18.11.2011
*/
 
public class PortChooser extends JDialog implements ItemListener {
	 public static final long serialVersionUID = 934786974L;
  /** A mapping from names to CommPortIdentifiers. */
  protected HashMap<String,CommPortIdentifier> map = new HashMap<String,CommPortIdentifier>();

  /** The name of the choice the user made. */
  protected String selectedPortName;

  /** The CommPortIdentifier the user chose. */
  protected CommPortIdentifier selectedPortIdentifier;

  /** The JComboBox for serial ports */
  protected JComboBox<String> serialPortsChoice;

  /** The JComboBox for parallel ports */
  protected JComboBox<String> parallelPortsChoice;

  /** The JComboBox for anything else */
  protected JComboBox<String> other;

  /** The SerialPort object */
  protected SerialPort ttya;

  /** To display the chosen */
  protected JLabel choice;

  /** Padding in the GUI */
  protected final int PAD = 5;

  /**
   * This will be called from either of the JComboBoxen when the user selects
   * any given item.
   */
  public void itemStateChanged(ItemEvent e) {
    // Get the name
    selectedPortName = (String) ((JComboBox) e.getSource())
        .getSelectedItem();
    // Get the given CommPortIdentifier
    selectedPortIdentifier =  map.get(selectedPortName);
    // Display the name.
    choice.setText(selectedPortName);
  }

  /* The public "getter" to retrieve the chosen port by name. */
  public String getSelectedName() {
    return selectedPortName;
  }

  /* The public "getter" to retrieve the selection by CommPortIdentifier. */
  public CommPortIdentifier getSelectedIdentifier() {
    return selectedPortIdentifier;
  }

  /** A test program to show up this chooser. */
  public static void main(String[] ap) {
    PortChooser c = new PortChooser(null);
    c.setVisible(true); // blocking wait
    System.out.println("Seçtiğiniz kanal " + c.getSelectedName() + " kanal tanım ismi "
        + c.getSelectedIdentifier() + ").");
    System.exit(0);
  }
  
  public static CommPortIdentifier select_port(JFrame parent) {
    PortChooser c = new PortChooser(parent);
    c.setVisible(true); 
    return c.getSelectedIdentifier();
  }
  public PortChooser(JFrame parent) {
    super(parent, "Mikrokontoller/Datalogger bağlantısı kanal seçici ", true);
    makeGUI();
    populate();
    finishGUI();
  }
  protected void makeGUI() {
    Container cp = getContentPane();
    JPanel centerPanel = new JPanel();
    cp.add(BorderLayout.CENTER, centerPanel);
    centerPanel.setLayout(new GridLayout(0, 2, PAD, PAD));
    centerPanel.add(new JLabel("seri bağlantılar ", JLabel.RIGHT));
    serialPortsChoice = new JComboBox<String>();
    centerPanel.add(serialPortsChoice);
    serialPortsChoice.setEnabled(false);
    centerPanel.add(new JLabel("paralel bağlantılar ", JLabel.RIGHT));
    parallelPortsChoice = new JComboBox<String>();
    centerPanel.add(parallelPortsChoice);
    parallelPortsChoice.setEnabled(false);
    centerPanel.add(new JLabel("Bilinmiyen bağlantılar", JLabel.RIGHT));
    other = new JComboBox<String>();
    centerPanel.add(other);
    other.setEnabled(false);
    centerPanel.add(new JLabel("Sizin seçiminiz :", JLabel.RIGHT));
    centerPanel.add(choice = new JLabel());
    JButton okButton;
    cp.add(BorderLayout.SOUTH, okButton = new JButton("SEÇTİKTEN SONRA BASINIZ"));
    okButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        PortChooser.this.dispose();
      }
    });

  }


  protected void populate() {
    Enumeration pList = CommPortIdentifier.getPortIdentifiers();
    while (pList.hasMoreElements()) {
      CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();
      map.put(cpi.getName(), cpi);
      if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        serialPortsChoice.setEnabled(true);
        serialPortsChoice.addItem(cpi.getName());
      } else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
        parallelPortsChoice.setEnabled(true);
        parallelPortsChoice.addItem(cpi.getName());
      } else {
        other.setEnabled(true);
        other.addItem(cpi.getName());
      }
    }
    serialPortsChoice.setSelectedIndex(-1);
    parallelPortsChoice.setSelectedIndex(-1);
  }

  protected void finishGUI() {
    serialPortsChoice.addItemListener(this);
    parallelPortsChoice.addItemListener(this);
    other.addItemListener(this);
    pack();
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }
}