Creating the User Interface


The PortFax.java file is the main file of the Fax application. This file creates the user interface of the Fax application. The user interface of the Fax application consists of text fields to set the number to which the end user wants to send the fax, and the initial command string for the modem. In addition, the user interface of the Fax application contains an edit pane that displays the text file that the end user wants to send as a fax.

Listing 6-1 shows the PortFax.java file:

Listing 6-1: The PortFax.java File

start example
 /* Imports required javax.comm package classes */ import javax.comm.*; /* Imports required java.i/o package classes */ import java.io.*; /*Imports required java.awt package classes*/ import java.awt.*; import java.awt.event.*; /*Imports required javax.swing package classes*/ import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; /* class PortFax - This class is the main class of the application. This class initializes the interface and loads all components like the menubar, textareas, and buttons  before displaying the result. Constructor:    PortFax - This constructor creates GUI. Methods:    createGUI - This method creates GUI. actionPerformed - This method describes which action will be performed on which component. main - This method creates the main window of the application and displays all the components.*/ class PortFax extends JFrame implements ActionListener {    /*Set constants*/    public static final int ST_OPEN_PORT = 1;     public static final int ST_INIT_MODEM = 2;     public static final int ST_CONNECTING = 3;     public static final int ST_SEND_PAGE = 4;     public static final int ST_CLOSE = 5;     public static final int ST_CONVERT_FIlES = 6;     public static final int ST_REC_PAGE = 4;     public static final int ST_REC_CALL = 7;     public static final int ST_WAIT_CALL = 8;    /*Declare objects of String class.*/    String oldcommname;    String olddialing;    String oldmodemclass;    String oldflowcontrol;    /*Declare objects of JLabel class.*/    JLabel fexLabel;    JLabel modemstringLabel;    JLabel processlabel;    /*Declare objects of JTextField class.*/    JTextField modemstringtext;    JTextField faxtextfield;    /*Declare objects of JEditorPane class.*/    JEditorPane faxeditor;    /*Declare objects of JButton class.*/    JButton sendbutton;    JButton cancelbutton;    JButton closebutton;    /*Declare objects of JPanel class.*/    JPanel toppanel;    JPanel EditorPanel;    /*Declare objects of JMenuBar class.*/    JMenuBar menubar;    /*Declare objects of JMenu class.*/    JMenu filemenu;    /*Declare objects of JMenuItem class.*/    JMenuItem openmenuitem,viewpropertiesmenuitem,exitmenuitem;    /*Declare objects of JFileChooser class.*/    JFileChooser filechooser;    /*Declare objects of JScrollPane class.*/    JScrollPane editorscrollpane;    /*Declare objects of JCheckBox class.*/    JCheckBox htmlcheckbox;    /*Declare objects of PreparedFaxDoc class.*/    PreparedFaxDoc faxdoc=null;    /*Declare objects of SendingFax class.*/    SendingFax sendfax=null;    /*Declare objects of FaxPropertyPage class.*/    FaxPropertyPage faxpropertypage;    int oldflowcontrolin=0;    boolean d=false;    public PortFax()    {       /*Set parent window title.*/       super(" Fax Application ");       /*createGUI method is called*/       createGUI();       /*Set window size.*/       setSize(500,500);       /*Set window visible.*/       setVisible(true);    }    public static void main(String[] args)     {       /*Declare and initialize object of PortFax.*/       PortFax portfax=new PortFax();    }    public void createGUI()    {       /*       Declare and Initialize object of Container class with return value of getContentPane function.       */       Container contentpane=getContentPane();       /*       Initialize and set the Look and Feel of the application to Windows Look and Feel.       */       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }       catch(Exception e)       {          System.out.println("Problem in changing windows look and feel");       }       /*Set location at which window will be displayed.*/       Dimension screensize=Toolkit.getDefaultToolkit()getScreenSize();       setLocation(screensize.width/2-250,screensize.height/250);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       /*Initialize the object of JMenuBar class.*/       menubar=new JMenuBar();       /*Initialize the object of JMenu class.*/       filemenu=new JMenu("File");       filemenu.setMnemonic('f');       /*       Initialize the object of JMenuItem class.       */       openmenuitem=new JMenuItem("Open");       openmenuitem.setMnemonic('o');       openmenuitem.setActionCommand("open");       openmenuitem.addActionListener(this);       filemenu.add(openmenuitem);       /*       Initialize the object of JMenuItem class.       */       viewpropertiesmenuitem=new JMenuItem("View Properties");       viewpropertiesmenuitem.setMnemonic('v');       viewpropertiesmenuitem.setActionCommand("view");       viewpropertiesmenuitem.addActionListener(this);       filemenu.add(viewpropertiesmenuitem);       /*       Initialize the object of JMenuItem class.       */       exitmenuitem=new JMenuItem("Exit");       exitmenuitem.setMnemonic('e');       exitmenuitem.setActionCommand("exit");       exitmenuitem.addActionListener(this);       filemenu.add(exitmenuitem);       menubar.add(filemenu);       setJMenuBar(menubar);       /*       Initialize the object of FaxPropertyPage class.       */       faxpropertypage=new FaxPropertyPage();       /*       Add action listener to cancel button of faxpropertypage object.       */       faxpropertypage.getCancelButton()addActionListener(new ActionListener()       {          public void actionPerformed(ActionEvent ae)          {          if (oldcommname!=null)          {             faxpropertypage.setCommPort(oldcommname);          }          else if(oldcommname.length()>0)          {             faxpropertypage.setCommPort(oldcommname);          }          if (olddialing!=null)          {             faxpropertypage.setdialingMode(olddialing);          }          else if (olddialing.length()>0)          {             faxpropertypage.setdialingMode(olddialing);          }          if(oldmodemclass!=null)          {             faxpropertypage.setModemClass(oldmodemclass);          }          else if(oldmodemclass.length()>0)          {             faxpropertypage.setModemClass(oldmodemclass);          }          if(oldflowcontrol!=null)          {             faxpropertypage.setFlowControl(oldflowcontrol);          }          else if(oldflowcontrol.length()>0)          {             faxpropertypage.setFlowControl(oldflowcontrol);          }          faxpropertypage.setFlowControlIn(oldflowcontrolin);       }       });       /*Initialize object of JFileChooser.*/       filechooser=new JFileChooser();       /*       Declare and initialize object of GridBagLayout class        */       GridBagLayout gridbaglayout=new GridBagLayout();       /*       Declare and initialize object of GridBagConstraints class        */       GridBagConstraints gridbagconstraints =new GridBagConstraints();       /*       Initialize object of JPanel and set its layout as GridBagLayout.       */       toppanel=new JPanel(gridbaglayout);       /*       Initialize object of JLabel and set constraints to this label       */       modemstringLabel=new JLabel("Modem Initial String");       gridbagconstraints.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraints.gridx=0;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1.0;       gridbagconstraints.insets=new Insets(10, 10, 5, 10);       gridbagconstraints.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(modemstringLabel, gridbagconstraints);       /*Add label to panel*/       toppanel.add(modemstringLabel);       /*       Initialize object of JTextField and set constraints to this label       */       modemstringtext=new JTextField("ATV1Q0");       modemstringtext.setPreferredSize(new Dimension(150, 23));       gridbagconstraints.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraints.gridx=1;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1.0;       gridbagconstraints.insets=new Insets(10, 10, 5, 10);       gridbagconstraints.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(modemstringtext, gridbagconstraints);       /*Add button to panel*/       toppanel.add(modemstringtext);       /*       Initialize object of JLabel and set constraints to this label       */       fexLabel=new JLabel("Fax Number");       gridbagconstraints.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraints.gridx=0;       gridbagconstraints.gridy=1;       gridbagconstraints.weightx=1.0;       gridbagconstraints.insets=new Insets(0, 10, 10, 10);       gridbagconstraints.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(fexLabel, gridbagconstraints);       /*Add label to panel*/       toppanel.add(fexLabel);       /*       Initialize object of JTextField and set constraints to this label       */       faxtextfield=new JTextField("0, 51610236");       faxtextfield.setPreferredSize(new Dimension(150, 23));       gridbagconstraints.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraints.gridx=1;       gridbagconstraints.gridy=1;       gridbagconstraints.weightx=1.0;       gridbagconstraints.insets=new Insets(0, 10, 10, 10);       gridbagconstraints.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(faxtextfield,gridbagconstraints);       /*Add button to panel*/       toppanel.add(faxtextfield);       /*       Initialize object of JCheckBox and set constraints to this label       */       htmlcheckbox=new JCheckBox(" HTML");       htmlcheckbox.setBorderPaintedFlat(true);       gridbagconstraints.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraints.gridx=2;       gridbagconstraints.gridy=2;       gridbagconstraints.weightx=0.0;       gridbagconstraints.gridwidth=2;       gridbagconstraints.insets=new Insets(0, 10, 0, 0);       gridbagconstraints.anchor=GridBagConstraints.EAST;       gridbaglayout.setConstraints(htmlcheckbox, gridbagconstraints);       /*Add checkbox to panel*/       toppanel.add(htmlcheckbox);       /*Add panel to main window*/       contentpane.add(toppanel, BorderLayout.NORTH);       /*Initialize object of editorpane.*/       faxeditor=new JEditorPane();       /*Initialize object of scrollpane.*/       editorscrollpane=new JScrollPane(faxeditor);       /*Add scrollbar to main window.*/       contentpane.add(editorscrollpane,BorderLayout.CENTER);       /*       Declare and initialize object of JPanel class and set its layout as GridLayout.       */       JPanel bottomlabelpanel=new JPanel(new GridLayout(1, 1, 0, 0));       bottomlabelpanel.add(processlabel=new JLabel(""));       processlabel.setForeground(Color.blue);       /*        and initialize object of JPanel class and set its layout as GridLayout.       */Declare       JPanel bottombuttonpanel=new JPanel(new GridLayout(1, 5, 10, 10));       bottombuttonpanel.add(new JLabel(""));       /*Initialize object of JButton.*/       sendbutton=new JButton(" Send Fax ");       /*Set button size.*/       sendbutton.setPreferredSize(new Dimension(20,23));       sendbutton.setMnemonic('s');       sendbutton.setActionCommand("send");       sendbutton.addActionListener(this);       bottombuttonpanel.add(sendbutton);       cancelbutton=new JButton(" Cancel ");       /*Set button size.*/       cancelbutton.setPreferredSize(new Dimension(20,23));       cancelbutton.setMnemonic('c');       cancelbutton.setActionCommand("cancel");       cancelbutton.addActionListener(this);       bottombuttonpanel.add(cancelbutton);       closebutton=new JButton(" Close ");       /*Set button size.*/       closebutton.setPreferredSize(new Dimension(20, 23));       closebutton.setMnemonic('l');       closebutton.setActionCommand("close");       closebutton.addActionListener(this);       bottombuttonpanel.add(closebutton);       bottombuttonpanel.add(new JLabel(""));       /*       Declare and initialize object of JPanel class and set its layout as GridLayout.       */       JPanel bottompanel=new JPanel(new GridLayout(2, 1));       bottompanel.add(bottomlabelpanel);       bottompanel.add(bottombuttonpanel);       /*Set panel size.*/       bottompanel.setPreferredSize(new Dimension(550, 50));       /*Add panel to main window.*/       contentpane.add(bottompanel,BorderLayout.SOUTH);    }    /*    actionPerformed - This method is called when the user clicks the Send or Close button,    selects file menu items.    Parameters: ae - an ActionEvent object containing details of the event.    Return Value: NA     */    public void actionPerformed(ActionEvent ae)    {       String actioncommand=ae.getActionCommand();       /*       This is executed when user clicks the Open menu item.       */       if ("open".equals(actioncommand))       {          int returnVal =filechooser.showOpenDialog(PortFax.this);          if (returnVal==JFileChooser.APPROVE_OPTION)           {             try             {                File file = filechooser.getSelectedFile();                InputStream inputStream = new FileInputStream(file);                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));                String strTemp="";                faxeditor.setText("");                while ((strTemp = br.readLine())!=null)                {                   faxeditor.setText(faxeditor.getText()+strTemp+'\n');                }                inputStream.close();                }                catch( FileNotFoundException e )                {                   System.out.println( "File not found" );                }                catch( IOException e )                {                   System.out.println( "I/O error" );                }              }       }       /*       This is executed when user clicks the View Property menu item.       */       else if("view".equals(actioncommand))       {          oldcommname=faxpropertypage.getCommPortName();          olddialing=faxpropertypage.getDialing();          oldmodemclass=(String)faxpropertypage.modemclasscombo.getSelectedItem();          oldflowcontrol=faxpropertypage.getFlowcontrol();          oldflowcontrolin=faxpropertypage.getFlowControlIn();          faxpropertypage.setVisible(true);       }       /*       This is executed when user clicks the Exit menu item.       */       else if("exit".equals(actioncommand))       {          System.exit(0);       }       /*       This is executed when user clicks the Send button.       */       else if ("send".equals(actioncommand))       {          sendbutton.setEnabled(false);          closebutton.setEnabled(true);          if (htmlcheckbox.isSelected())          {             faxdoc= new ConvertIntoHTML();             String[] pag=new String[1];             pag[0]=faxeditor.getText();             ((ConvertIntoHTML) faxdoc).text=pag;             ((ConvertIntoHTML) faxdoc).pageimage=createImage(800, 550);          }          else            {             faxdoc= new ConvertIntoTextImage();             ((ConvertIntoTextImage) faxdoc).text=faxeditor.getText();             ((ConvertIntoTextImage) faxdoc).pageimage=createImage(800, 550);             ((ConvertIntoTextImage) faxdoc).prepare();          }          /* Set modem configuration*/          sendfax=new SendingFax();          sendfax.bitorder=true;          sendfax.log=true;          sendfax.debug=d;          sendfax.faxclass=1;          sendfax.resolution=sendfax.RESOLUTION_NORMAL;          sendfax.timeout=60;          if (faxpropertypage.getmodemclass()=="Class 1") sendfax.faxclass=2;          if (faxpropertypage.getDialing()=="Class 2") sendfax.faxclass=20;          sendfax.dialingmode=faxpropertypage.getDialing();          sendfax.flowcontrol=faxpropertypage.getFlowControlIn();          if (faxpropertypage.getFlowcontrol()!="")          {             sendfax.flowcontrolin=(String)faxpropertypage.getFlowcontrol();          }          sendfax.setPortName(faxpropertypage.getCommPortName());          sendfax.modemFBOR=true;          if (modemstringtext.getText().length()>0) sendfax.setInitString(modemstringtext.getText());          sendfax.listener=this;          if (sendfax.openForFax(faxdoc))          {             if (sendfax.sendFax(faxtextfield.getText()))              {                this.processlabel.setText("Fax successfully sent");                sendbutton.setEnabled(true);              }          }       }       /*       This is executed when user clicks the Close button.       */       else if("cancel".equals(actioncommand))       {          if (sendfax!=null) sendfax.close();          processlabel.setForeground(java.awt.Color.blue);          processlabel.setText("Inactive...");          sendbutton.setEnabled(true);          closebutton.setEnabled(false);          sendfax.serialPort.close();       }          /*          This is executed when user clicks the Cancel button.          */          else if("close".equals(actioncommand))          {             System.exit(0);          }    }    /*    faxProgress - This method is called to show status of fax sending operation in the label.    Parameters: status ,page    Return Value:      */    public void faxProgress(int status,int page)    {       if (status==ST_CLOSE) processlabel.setText("Closing...");       if (status==ST_CONNECTING) processlabel.setText("Connecting...");       if (status==ST_CONVERT_FIlES) processlabel.setText("Converting fax files...");       if (status==ST_INIT_MODEM) processlabel.setText("Initializing modem...");       if (status==ST_OPEN_PORT) processlabel.setText("Opening port...");       if (status==ST_SEND_PAGE) processlabel.setText("Sending page " + (int) (page +1));       if (status==FaxStatusListener.ST_REC_PAGE) processlabel.setText("Receiving page " + (int) (page +1));       if (status==FaxStatusListener.ST_REC_CALL) processlabel.setText("Call detected...");       if (status==FaxStatusListener.ST_WAIT_CALL) processlabel.setText("Waiting for call...");       this.paintAll(this.getGraphics());    } } 
end example

Download this listing.

In the above listing, the main() method creates an instance of the PortFax class. This class generates the main window of the Fax application, as shown in Figure 6-2:

click to expand: this figure shows the fax application window with a menu bar, two text boxes, an edit pane, and two buttons.
Figure 6-2: The Main Window of the Fax Application

The File menu provides three menu items: Open, View Properties, and Exit. The Open command opens the file selected by the end user in the edit pane of the Fax application. The View Properties command displays a user interface to set the properties of the COM port. The Fax application uses the FaxPropertyPage.jave file to create the user interface. The Exit command terminates the Fax application.

The Modem Initial String text box allows an end user to set the initial command string for the modem. The Fax Number text box specifies the number to which the end user wants to send the fax. The HTML check box enables an end user to send the fax as an HTML page. If the HTML check box is not selected, the Fax application sends the Fax as a text image.

The various methods defined in the PortFax.java File are:

  • createGUI():Creates the user interface of the Fax application.

  • actionPerformed():Acts as an event listener and activates an appropriate class or method based on the button that the end user clicks. For example, when the end user selects the HTML check box and clicks the Send Fax button, the actionPerformed() method creates an object of the ConvertIntoHTML class, which converts the text file into an image of an HTML page. If the end user does not select the HTML check box and clicks the Send Fax button, the actionPerformed() method creates an object of the ConvertIntoTextImage class. This class converts the file that is open in the edit pane to a text image. When the end user clicks the Close button of the Fax application, the actionPerformed() method closes the main window.

  • faxProgress():Displays the status of the Fax application.




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