// Fig. 28.11: UsingHugeIntegerJFrame.java
// Client desktop application for the HugeInteger web service.
package com.deitel.jhtp7.ch28.hugeintegerclient;

// import classes for accessing HugeInteger web service's proxy
import com.deitel.jhtp7.ch28.hugeintegerclient.HugeInteger;
import com.deitel.jhtp7.ch28.hugeintegerclient.HugeIntegerService;

import javax.swing.JOptionPane; // used to display errors to the user

public class UsingHugeIntegerJFrame extends javax.swing.JFrame
{
   private HugeIntegerService hugeIntegerService; // used to obtain proxy
   private HugeInteger hugeIntegerProxy; // used to access the web service
   
   // no-argument constructor
   public UsingHugeIntegerJFrame()
   {
      initComponents();
      
      try
      { 
         // create the objects for accessing the HugeInteger web service
         hugeIntegerService = new HugeIntegerService();
         hugeIntegerProxy = hugeIntegerService.getHugeIntegerPort();
      }
      catch ( Exception exception )
      {
         exception.printStackTrace();
      }        
   } // end UsingHugeIntegerJFrame constructor
   
   // The initComponents method is autogenerated by Netbeans and is called
   // from the constructor to initialize the GUI. This method is not shown
   // here to save space. Open UsingHugeIntegerJFrame.java in this 
   // example's folder to view the complete generated code. 
   // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
   private void initComponents()
   {
      directionsJLabel = new javax.swing.JLabel();
      firstJTextField = new javax.swing.JTextField();
      secondJTextField = new javax.swing.JTextField();
      addJButton = new javax.swing.JButton();
      subtractJButton = new javax.swing.JButton();
      biggerJButton = new javax.swing.JButton();
      smallerJButton = new javax.swing.JButton();
      equalsJButton = new javax.swing.JButton();
      resultsJScrollPane = new javax.swing.JScrollPane();
      resultsJTextArea = new javax.swing.JTextArea();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setTitle("Using the HugeInteger Web Service");
      directionsJLabel.setText("Enter two positive integers, up to 100 digits each:");

      addJButton.setText("Add");
      addJButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            addJButtonActionPerformed(evt);
         }
      });

      subtractJButton.setText("Subtract");
      subtractJButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            subtractJButtonActionPerformed(evt);
         }
      });

      biggerJButton.setText("Greater Than");
      biggerJButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            biggerJButtonActionPerformed(evt);
         }
      });

      smallerJButton.setText("Less Than");
      smallerJButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            smallerJButtonActionPerformed(evt);
         }
      });

      equalsJButton.setText("Equals");
      equalsJButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            equalsJButtonActionPerformed(evt);
         }
      });

      resultsJScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      resultsJTextArea.setColumns(20);
      resultsJTextArea.setRows(5);
      resultsJScrollPane.setViewportView(resultsJTextArea);

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
               .add(org.jdesktop.layout.GroupLayout.LEADING, resultsJScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
               .add(org.jdesktop.layout.GroupLayout.LEADING, firstJTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
               .add(org.jdesktop.layout.GroupLayout.LEADING, directionsJLabel)
               .add(org.jdesktop.layout.GroupLayout.LEADING, secondJTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
               .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                  .add(addJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(subtractJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(biggerJButton)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(smallerJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .add(equalsJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
      );

      layout.linkSize(new java.awt.Component[] {addJButton, biggerJButton, equalsJButton, smallerJButton, subtractJButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

      layout.setVerticalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(directionsJLabel)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(firstJTextField, 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(secondJTextField, 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(subtractJButton)
               .add(biggerJButton)
               .add(smallerJButton)
               .add(equalsJButton)
               .add(addJButton))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(resultsJScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );
      pack();
   }// </editor-fold>//GEN-END:initComponents

   // invokes HugeInteger web service's add method to add HugeIntegers
   private void addJButtonActionPerformed(//GEN-FIRST:event_addJButtonActionPerformed
      java.awt.event.ActionEvent evt )
   {//GEN-HEADEREND:event_addJButtonActionPerformed
      String firstNumber = firstJTextField.getText();
      String secondNumber = secondJTextField.getText();

      if ( isValid( firstNumber ) && isValid( secondNumber ) )
      {
         try
         {
            resultsJTextArea.setText( 
               hugeIntegerProxy.add( firstNumber, secondNumber ) );
         } // end try
         catch ( Exception e )
         {
            JOptionPane.showMessageDialog( this, e.toString(), 
               "Add method failed", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace();
         } // end catch
      } // end if
   }//GEN-LAST:event_addJButtonActionPerformed

   // invokes HugeInteger web service's subtract method to subtract the 
   // second HugeInteger from the first
   private void subtractJButtonActionPerformed(                                                 
      java.awt.event.ActionEvent evt )
   {                                                    
      String firstNumber = firstJTextField.getText();
      String secondNumber = secondJTextField.getText();

      if ( isValid( firstNumber ) && isValid( secondNumber ) )
      {
         try
         {
            resultsJTextArea.setText( 
               hugeIntegerProxy.subtract( firstNumber, secondNumber ) );
         } // end try
         catch ( Exception e )
         {
            JOptionPane.showMessageDialog( this, e.toString(), 
               "Subtract method failed", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace();
         } // end catch
      } // end if
   }                                               

   // invokes HugeInteger web service's bigger method to determine whether
   // the first HugeInteger is greater than the second
   private void biggerJButtonActionPerformed(//GEN-FIRST:event_biggerJButtonActionPerformed
      java.awt.event.ActionEvent evt )
   {//GEN-HEADEREND:event_biggerJButtonActionPerformed
      String firstNumber = firstJTextField.getText();
      String secondNumber = secondJTextField.getText();

      if ( isValid( firstNumber ) && isValid( secondNumber ) )
      {
         try
         {
            boolean result = 
               hugeIntegerProxy.bigger( firstNumber, secondNumber );
            resultsJTextArea.setText( String.format( "%s %s %s %s", 
               firstNumber, ( result ? "is" : "is not" ), "greater than", 
               secondNumber ) );
         } // end try 
         catch ( Exception e )
         {
            JOptionPane.showMessageDialog( this, e.toString(), 
               "Bigger method failed", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace();
         } // end catch
      } // end if
   }//GEN-LAST:event_biggerJButtonActionPerformed

   // invokes HugeInteger web service's smaller method to determine
   // whether the first HugeInteger is less than the second
   private void smallerJButtonActionPerformed(//GEN-FIRST:event_smallerJButtonActionPerformed
      java.awt.event.ActionEvent evt )
   {//GEN-HEADEREND:event_smallerJButtonActionPerformed
      String firstNumber = firstJTextField.getText();
      String secondNumber = secondJTextField.getText();

      if ( isValid( firstNumber ) && isValid( secondNumber ) )
      {
         try
         {
            boolean result = 
               hugeIntegerProxy.smaller( firstNumber, secondNumber );
            resultsJTextArea.setText( String.format( "%s %s %s %s", 
               firstNumber, ( result ? "is" : "is not" ), "less than", 
               secondNumber ) );
         } // end try 
         catch ( Exception e )
         {
            JOptionPane.showMessageDialog( this, e.toString(), 
               "Smaller method failed", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace();
         } // end catch
      } // end if
   }//GEN-LAST:event_smallerJButtonActionPerformed

   // invokes HugeInteger web service's equals method to determine whether
   // the first HugeInteger is equal to the second
   private void equalsJButtonActionPerformed(//GEN-FIRST:event_equalsJButtonActionPerformed
      java.awt.event.ActionEvent evt )
   {//GEN-HEADEREND:event_equalsJButtonActionPerformed
      String firstNumber = firstJTextField.getText();
      String secondNumber = secondJTextField.getText();

      if ( isValid( firstNumber ) && isValid( secondNumber ) )
      {
         try
         {
            boolean result = 
               hugeIntegerProxy.equals( firstNumber, secondNumber );
            resultsJTextArea.setText( String.format( "%s %s %s %s", 
               firstNumber, ( result ? "is" : "is not" ), "equal to", 
               secondNumber ) );
         } // end try 
         catch ( Exception e )
         {
            JOptionPane.showMessageDialog( this, e.toString(), 
               "Equals method failed", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace();
         } // end catch
      } // end if
   }//GEN-LAST:event_equalsJButtonActionPerformed

   // checks the size of a String to ensure that it is not too big 
   // to be used as a HugeInteger; ensure only digits in String
   private boolean isValid( String number )
   {
      // check String's length
      if ( number.length() > 100 )
      {
         JOptionPane.showMessageDialog( this,
            "HugeIntegers must be <= 100 digits.", "HugeInteger Overflow", 
            JOptionPane.ERROR_MESSAGE );
         return false;
      } // end if
      
      // look for nondigit characters in String
      for ( char c : number.toCharArray() )
      {
         if ( !Character.isDigit( c ) )
         {
            JOptionPane.showMessageDialog( this,
               "There are nondigits in the String", 
               "HugeInteger Contains Nondigit Characters", 
               JOptionPane.ERROR_MESSAGE );
            return false;         
         } // end if  
      } // end for
   
      return true; // number can be used as a HugeInteger
   } // end method validate
   
   // main method begins execution
   public static void main( String args[] )
   {
      java.awt.EventQueue.invokeLater( 
         new Runnable()
         {
            public void run()
            {
               new UsingHugeIntegerJFrame().setVisible( true );
            } // end method run
         } // end anonymous inner class 
      ); // end call to java.awt.EventQueue.invokeLater
   } // end method main
   
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton addJButton;
   private javax.swing.JButton biggerJButton;
   private javax.swing.JLabel directionsJLabel;
   private javax.swing.JButton equalsJButton;
   private javax.swing.JTextField firstJTextField;
   private javax.swing.JScrollPane resultsJScrollPane;
   private javax.swing.JTextArea resultsJTextArea;
   private javax.swing.JTextField secondJTextField;
   private javax.swing.JButton smallerJButton;
   private javax.swing.JButton subtractJButton;
   // End of variables declaration//GEN-END:variables
} // end class UsingHugeIntegerJFrame



/**************************************************************************
 * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/
