Displaying Port Properties


The FaxPropertyPage.java file displays the properties of the port to which the modem is connected. The modem helps send the fax to the fax number of the destination. The FaxPropertyPage.java file creates a user interface to display and set the properties of the port.

Listing 6-5 shows the FaxPropertyPage.java file:

Listing 6-5: The FaxPropertyPage.java File

start example
 /*Imports required Comm classes*/ import javax.comm.*; /*Imports required javax.swing package classes*/ import javax.swing.*; import javax.swing.event.*; /*Imports required java.awt package classes*/ import java.awt.*; import java.awt.event.*; /*Imports required java.util package classes*/ import java.util.*; /* class FaxPropertyPage - This class is the Port Property dialog window,  Enable the end user to change the property of port. Constructor:    FaxPropertyPage - This constructor creates GUI. Methods:    setParity - Sets Parity bit.    setStopbits - Sets Stop bit.    setDatabits - Set Data bit    setBaudRate - Sets Baud rate. getCancelButton - Returns object of cancel button.    getOkButton -  Returns object of ok button. getFlowControlOut - Returns flow control out value. getFlowControlIn - Returns flow control in value.    getParity - Returns Parity bit value.    getDatabits - Returns Data bit value.    getStopbits - Returns Stop bit value.    getBaudRate - Returns Baud rate value.    getCommPortName - Returns  port name.    getCommPort -  Returns object of CommPort class */ class FaxPropertyPage extends JFrame implements ActionListener  {    /*Declare object of Enumeration class.*/    Enumeration listport;    /*Declare object of JButton class.*/    JButton okbutton;    JButton cancelbutton;    /*Declare object of JPanel class.*/    JPanel buttonpanel;    JPanel propertylist;    /*Declare object of JLabel class.*/    JLabel portlistlabel;    JLabel flowlabel;    JLabel paritylabel;    JLabel dialinglabel;    JLabel modemclasslabel;    JLabel flowcontrollabel;    JLabel stoplabel;    /*Declare object of JComboBox class.*/    JComboBox comportcombo;    JComboBox flowcombo;    JComboBox modemclasscombo;    JComboBox dialingcombo;    JComboBox flowcontrolcombo;    /*     Declare object of CommPortIdentifier class.    */    CommPortIdentifier portId;    public FaxPropertyPage()    {       /* Set title of main window. */       super("Fax Property Page");       /*       Declare and initialize object of Container class.       */       Container contentpane=getContentPane();       comportcombo=new JComboBox();       listport=CommPortIdentifier.getPortIdentifiers();        while (listport.hasMoreElements())       {          portId=(CommPortIdentifier)listport.nextElement();          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)           {             comportcombo.addItem(portId.getName());          }       }       /*Initialize Object of JComboBox. */       flowcombo=new JComboBox();       flowcombo.addActionListener(this);       /*       Declare and initialize object of GridBagLayout class       */       GridBagLayout gridbaglayout=new GridBagLayout();       /*       Declare and initialize object of GridBagConstraints class       */       GridBagConstraints gridbagconstraint=new GridBagConstraints();       /*       Initialize object of JPanel class and set its layout as gridbaglayout.       */       propertylist=new JPanel(gridbaglayout);          /*Set background color as white.*/       propertylist.setBackground(Color.white);       /*       Initialize object of JLabel class and gives its name.       */       Portlistlabel=new JLabel("Port Name",JLabel.RIGHT);       /*       Initialize object of JLabel class and gives its name.       */       flowlabel=new JLabel("Flow Control In");       /*       Set constraints for gridbagconstraint.       */       gridbagconstraint.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbagconstraint.insets=new Insets(5,5,5,5);       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=0;       gridbagconstraint.weightx=1.0;       gridbagconstraint.weighty=1.0;       gridbaglayout.setConstraints(portlistlabel,gridbagconstraint);       /* Add portlistlabel lable to panel. */       propertylist.add(portlistlabel);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbagconstraint.insets=new Insets(5,5,5,5);       gridbaglayout.setConstraints(comportcombo,gridbagconstraint);       /*Add comportcombo combo box to panel.*/       propertylist.add(comportcombo);       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowlabel,gridbagconstraint);       /*Add flowlabel lable to panel.*/       propertylist.add(flowlabel);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowcombo,gridbagconstraint);       /*Add comportcombo combo box to panel.*/       propertylist.add(flowcombo);       /*       Initialize object of JLabel class and gives its name.       */       modemclasslabel=new JLabel("Modem Class",JLabel.RIGHT);       /*       Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=2;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(modemclasslabel,gridbagconstraint);       /*Add modemclasslabel label to panel.*/       propertylist.add(modemclasslabel);       /*        Initialize object of JComboBox class.        */       modemclasscombo=new JComboBox();       modemclasscombo.addActionListener(this);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=2;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(modemclasscombo,gridbagconstraint);       /*Add comportcombo combo box to panel.*/       propertylist.add(modemclasscombo);       /*       Initialize object of JLabel class and gives its name.       */       dialinglabel=new JLabel("Dialing",JLabel.RIGHT);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=2;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(dialinglabel,gridbagconstraint);       /*Add dialinglabel label to panel.*/       propertylist.add(dialinglabel);       /*        Initialize object of JComboBox class.        */       dialingcombo=new JComboBox();       dialingcombo.addActionListener(this);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=3;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(dialingcombo,gridbagconstraint);       /*Add dialingcombo combo to panel.*/       propertylist.add(dialingcombo);       /*       Initialize object of JLabel class and gives its name.       */       flowcontrollabel=new JLabel("Flow Control Command",JLabel.RIGHT);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=2;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowcontrollabel,gridbagconstraint);       /*        Add flowcontrollabel label to panel.        */       propertylist.add(flowcontrollabel);       /*        Initialize object of JComboBox class.        */       flowcontrolcombo=new JComboBox();       flowcontrolcombo.addActionListener(this);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=3;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowcontrolcombo,gridbagconstraint);       /*        Add flowcontrolcombo combo to panel.        */       propertylist.add(flowcontrolcombo);       contentpane.add(propertylist);       /*Fill dialingcombo combo box.*/       dialingcombo.addItem("Tone");       dialingcombo.addItem("Pulse");       dialingcombo.setSelectedItem("Tone");       /*Fill flowcombo combo box.*/       flowcombo.addItem("none");       flowcombo.addItem("RtsCts");       flowcombo.addItem("Xon/Xoff In");       flowcombo.setSelectedItem("none");       /*Fill flowcontrolcombo combo box.*/       flowcontrolcombo.addItem("");       flowcontrolcombo.addItem("-- RtsCts --");       flowcontrolcombo.addItem("-- XonXoff --");       flowcontrolcombo.addItem("AT+FLO=2");       flowcontrolcombo.addItem("AT&K3");       flowcontrolcombo.addItem("AT&\\Q3");       flowcontrolcombo.addItem("AT+FLO=1");       flowcontrolcombo.addItem("AT&K4");       flowcontrolcombo.addItem("AT&\\Q4");       flowcontrolcombo.setSelectedItem("");       /*Fill modemclasscombo combo box.*/       modemclasscombo.addItem("Class 1");       modemclasscombo.addItem("Class 2");       modemclasscombo.addItem("Class 2.0");       modemclasscombo.setSelectedItem("Class 1");       /*       Initialize object of JButton and set its label.       */       okbutton=new JButton("OK");       okbutton.setActionCommand("ok");       okbutton.addActionListener(new ActionListener()       {public void actionPerformed(ActionEvent ae){setVisible(false);}});       /*       Initialize object of JButton and set its label       */       cancelbutton=new JButton("Cancel");       okbutton.addActionListener(new ActionListener()       {public void actionPerformed(ActionEvent ae){setVisible(false);}});       cancelbutton.setActionCommand("cancel");       cancelbutton.addActionListener(this);       /* Initialize object of JPanel. */       buttonpanel=new JPanel();       /*       Set buttonpanel  layout as gridbaglayout.       */       buttonpanel.setLayout(gridbaglayout);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.fill = GridBagConstraints.HORIZONTAL;       gridbagconstraint.insets=new Insets(5,5,5,5);       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(okbutton,gridbagconstraint);       /*Add button to panel*/       buttonpanel.add(okbutton);       /*        Set constraints for gridbagconstraint.        */       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(cancelbutton,gridbagconstraint);       /*Add button to panel*/       buttonpanel.add(cancelbutton);       contentpane.add(buttonpanel,BorderLayout.SOUTH);       addWindowListener(new WindowAdapter()       {          public void windowClosed(WindowEvent we)          {             setVisible(false);          }       });       /*Set frame size.*/       setSize(400,200);    }    /*    actionPerformed - This methods is executed when the end user selects items from combo box or clickes on button.    Parameters: e - Object of ActionEvent class    Return Value: NA    */    public void actionPerformed(ActionEvent e)     {       if (e.getSource() instanceof JButton)       {          setVisible(false);       }       else       {          JComboBox cb = (JComboBox)e.getSource();          String petName = (String)cb.getSelectedItem();       }    }    /*    getCommPort - Gives object of  CommPort class    Parameters: NA    Return Value: CommPortIdentifier    */    public CommPortIdentifier getCommPort()    {       CommPortIdentifier comp=null;       try       {          comp= javax.comm.CommPortIdentifier.getPortIdentifier((String)comportcombo.getSelectedItem());       }       catch(NoSuchPortException e)       {          JOptionPane.showMessageDialog(null,"open","Port not exists",JOptionPane.PLAIN_MESSAGE);       }       return comp;    }    /*    getCommPortName - Gives name of selected port.    Parameters: NA    Return Value: String    */    public String getCommPortName()    {       return (String)comportcombo.getSelectedItem();    }       /*    getDialing - Gives dialing mode.    Parameters: NA    Return Value: String    */    public String getDialing()    {       return (String)dialingcombo.getSelectedItem();    }    /*    getmodemclass - Gives modem class.    Parameters: NA    Return Value: String    */    public String getmodemclass()    {       return (String)modemclasscombo.getSelectedItem();    }    /*    getFlowControlIn - Gives type of flow in control.    Parameters: NA    Return Value: int    */    public int getFlowControlIn()    {       String flowcontrol=(String)flowcombo.getSelectedItem();       if (flowcontrol.equals("none"))        {          return 0;       }       if (flowcontrol.equals("RtsCts"))        {          return 2;       }       if (flowcontrol.equals("Xon/Xoff In"))        {          return 1;       }       return 0;    }    /*    getCancelButton - Gives object of JButton class.    Parameters: NA    Return Value: JButton    */    public JButton getCancelButton()    {       return cancelbutton;    }        public void setCommPort(String commname)    {       comportcombo.setSelectedItem(commname);    }    public void setdialingMode(String dialing)    {       dialingcombo.setSelectedItem(dialing);    }    public void setModemClass(String modemclass)    {       modemclasscombo.setSelectedItem(modemclass);    }    public void setFlowControl(String flowcontrol)    {       flowcontrolcombo.setSelectedItem(flowcontrol);    }    public void setFlowControlIn(int flowcontrolin)    {       if(flowcontrolin==0)       {          flowcombo.setSelectedItem("none");       }       else if (flowcontrolin==1)       {          flowcombo.setSelectedItem("RtsCts")          }       else       {          flowcombo.setSelectedItem("Xon/Xoff In");       }    }    /*    getFlowcontrol - Gives flow control.    Parameters: NA    Return Value: String    */    public String getFlowcontrol()    {       return (String)flowcontrolcombo.getSelectedItem();    } } 
end example

Download this listing.

In the above listing, the constructor of the FaxPropertyPage class creates the user interface of the FaxPropertyPage.java file, as shown in Figure 6-3:

click to expand: this figure shows the user interface of the fax property page window. this user interface contains the port name, dialing, flow control in, flow control command, and modem class drop-down list boxes.
Figure 6-3: The Fax Property Page Window

The various methods defined in the FaxPropertyPage.java file are:

  • actionPerformed():Acts as an event listener and activates an appropriate class or method based on the button that an end user clicks. When the end user clicks the OK button, the actionPerformed() method sets the properties of the port to the values selected on the Fax Property Page window that the FaxPropertyPage class creates. When an end user clicks the Cancel button, the actionPerformed() method closes the Fax Property Page window.

  • getCommPort(): Determines whether the selected COM port exists or not.

  • setCommPort():Sets the value of the selected COM port.

  • getDialing():Returns the dialing mode of the selected COM port, such as?.

  • setdialingMode():Sets the dialing mode property for the selected COM port.

  • getmodemclass():Returns the class of the modem that helps send the fax.

  • setModemClass():Sets the value of the modem class to the selected value.

  • getFlowcontrol():Returns the value of the Flow control property of the selected COM port. The Flow control property indicates data flow from the modem to the selected COM port.

  • setFlowControl():Sets the value of the Flow Control property of the selected COM port.

  • getFlowControlIn():Returns the value of the Flow Control In property of the selected COM port. The Flow Control In property indicates data flow from the selected COM port to the modem.

  • setFlowControlIn:Sets the value of the Flow Control In property of the selected COM port.

  • getCommPortName()Returns the name of the COM port that sends the fax.

  • getCancelButton():Creates and returns an instance of the Cancel button.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net