/*  PortDialog.java
 *
 *  Created on June 20, 2005, 7:18 PM
 *
 *  This class is a dialog that appears when the 
 *  user wants to open a port to allow robot communication.
 *
 *  Author: Michael A. Piskuric (2537405) Summer 2005
 */

import Robot.*;
import javax.swing.*;
import javax.comm.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class PortDialog extends JDialog implements ActionListener{
    
    private TeachVAL        parent;         // parent window
    private ButtonGroup     group  = new ButtonGroup();
    private JRadioButton    availPorts[] = new JRadioButton[4]; 
    
    // create a new instance of the port Dialog
    public PortDialog(TeachVAL p) {
        parent = p;
        setTitle("Open a Port");
        getContentPane().setLayout(new GridLayout(3,1));
        getContentPane().add(new JLabel("Which port do you want to open?"));
        JPanel panel = new JPanel();    
        JButton ok = new JButton("OK");
        JButton cancel = new JButton("Cancel");
        ok.addActionListener(this);
        cancel.addActionListener(this);
        getContentPane().add(findPorts(ok));
        panel.add(ok);
        panel.add(cancel);
        getContentPane().add(panel);
        pack();
        int x = ((int)parent.getWidth()/2)-(getWidth()/2);
        int y = ((int)parent.getHeight()/2)-(getHeight()/2);
        setLocation(x,y);
        setVisible(true);
    }
    
    // find what ports are available and add then to the list
    private JPanel findPorts(JButton ok){        
        JPanel panel = new JPanel();
        CommPortIdentifier portId;
        Enumeration en = CommPortIdentifier.getPortIdentifiers();
        int counter = 0;        
        if (parent.getPort() != null) parent.getPort().close();
        
        while (en.hasMoreElements()) {
            portId = (CommPortIdentifier) en.nextElement();                
            if (!portId.isCurrentlyOwned() && portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                availPorts[counter] = new JRadioButton(portId.getName()+ "   ");
                group.add(availPorts[counter]);
                panel.add(availPorts[counter]);
                counter++;
            }
        }        
        if (counter == 0) {
            panel.add(new JLabel("No available ports were found."));
            ok.setEnabled(false);
        }
        else
            availPorts[0].setSelected(true);  
        
        return panel;
   }	// end findPorts
    
    // this listens for the button actions
    public void actionPerformed(ActionEvent e) {
        JButton temp = (JButton)e.getSource();
        if(temp.getText() == "OK"){
            Enumeration en = group.getElements();
            while (en.hasMoreElements()) {
                JRadioButton jrb = (JRadioButton)en.nextElement();
                if(jrb.isSelected()) parent.setPort(new RobotComm(jrb.getText()),jrb.getText());
            }            
            dispose();
        }
        else if(temp.getText() == "Cancel"){
            dispose();
        }
    }
}   // end portPanel