/*
   COPYRIGHT (C) 2010 - 2013 by Alexander Wait. All Rights Reserved.

   This class builds the currency converter GUI.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2012-05-05
*/



import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import javax.swing.*;



public class CurrencyConverterGui extends JApplet
{
   private static final String copyright = "Copyright (c) 2012 by Alexander Wait - All Rights Reserved";

   private static final long serialVersionUID = 1L;
   
   
   static boolean isApplication = false; 
   
   static boolean isVisible = false;
   
   
   private static final String DEFAULT_AMOUNT = "1";
   
   
   private static final int FRAME_SIZE_X = 420;
   
   private static final int FRAME_SIZE_Y = 550;
  

   //************************************** START INITIALISATION CODE **************************************//

   
   /**
    * Browser constructor.
    * ESCA-JAVA0166:
    */
   public void init()
   {
      if (!isApplication) 
      { 
         String copyrightParam = getParameter("copyright");
         
         if ((copyrightParam == null) || !copyrightParam.equals(copyright)) 
         {
            Message.showMessage("Invalid Copyright", "WARNING", "warning"); 
         }       
         
         else if (!getDocumentBase().getHost().equals(Domain.getDomain())) 
         {
            Message.showMessage("Unauthorised Applet Use", "ERROR", "error");
         }
        
         else
         {
            initialiser(); includeResizeEvent(); 
            
            setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
         }
      }
   }
 
   
   
   /**
    * Application constructor.
    */
   public CurrencyConverterGui()
   {
      if (isApplication) 
      {
         initialiser(); 
         
         
         frame = new JFrame(); 
         
         frame.setMinimumSize(new Dimension(FRAME_SIZE_X, FRAME_SIZE_Y));
         
         frame.setSize(FRAME_SIZE_X,FRAME_SIZE_Y);
         
         frame.setLocationRelativeTo(null); 
         
         frame.setTitle("Currency Program");
         
         frame.add(contentPane); 
         
        
         setVisibility(); 
      }
 
   }
   
   
   
   /**
    * GUI initializer.
    * ESCA-JAVA0076:
    */
   private void initialiser()
   {   
      currency = new CurrencyConverter(); validation = new Validation();
      
      
      
      // Initialize content pane and layout.
      
      contentPane = getContentPane(); 
      
      contentPane.setLayout(new GridBagLayout());
      
      
      
      // Initialize text area GUI components.
      
      firstCountryDisplay = new JTextArea(); 
      
      firstCountryDisplay.setEnabled(false);
      
      firstCountryDisplay.setFont(new Font("Dialog", Font.BOLD, 16));
      
      
      secondCountryDisplay = new JTextArea(); 
      
      secondCountryDisplay.setEnabled(false);
      
      secondCountryDisplay.setFont(new Font("Dialog", Font.BOLD, 16));
      
      
      conversionDisplay = new JTextArea(); 
      
      conversionDisplay.setEditable(false);
      
      conversionDisplay.setFont(new Font("Dialog", Font.BOLD, 16));
      
      conversionDisplay.setBorder(BorderFactory.createEtchedBorder());
      
      
      
      // Initialize text field GUI components.
      
      amountField = new JTextField();
      
      amountField.setHorizontalAlignment(JTextField.CENTER);
      
      amountField.setFont(new Font("Serif", Font.BOLD, 16));
      
      
      
      // Initialize combo box GUI components.
      
      firstCurrencyBox = new JComboBox(currency.returnCurrencyName());
      
      firstCurrencyBox.addActionListener(new FirstCurrencyListener());
      
      firstCurrencyBox.setMaximumRowCount(11); 
      
      firstCurrencyBox.setSelectedIndex(1);
      
      firstCurrencyBox.setFocusable(false);
      
      
      secondCurrencyBox = new JComboBox(currency.returnCurrencyName());
      
      secondCurrencyBox.addActionListener(new SecondCurrencyListener());
      
      secondCurrencyBox.setMaximumRowCount(11);
      
      secondCurrencyBox.setSelectedIndex(59);
      
      secondCurrencyBox.setFocusable(false);
      
      
      
      // Initialize the country displays.
      
      firstCountryDisplay.setText
      (
         currency.returnCountryName((String)firstCurrencyBox.getSelectedItem())
      );
      
      
      secondCountryDisplay.setText
      (
         currency.returnCountryName((String)secondCurrencyBox.getSelectedItem())
      );
      
      
      
      // Initialize button GUI components.
      
      run = new JButton(new ImageIcon(this.getClass().getResource
      (
         "files/images/FinanceToolCalculateNormal.png"))
      );
      
      
      run.setRolloverIcon(new ImageIcon(this.getClass().getResource
      (
         "files/images/FinanceToolCalculateRollover.png"))
      );

      
      run.setPressedIcon(new ImageIcon(this.getClass().getResource
      (
         "files/images/FinanceToolCalculatePressed.png"))
      );
      
      
      run.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
      
      run.addActionListener(new RunButtonListener());
      
      
      clear = new JButton(new ImageIcon(this.getClass().getResource
      (
         "files/images/FinanceToolClearNormal.png"))
      );
      
      
      clear.setRolloverIcon(new ImageIcon(this.getClass().getResource
      (
         "files/images/FinanceToolClearRollover.png"))
      );

      
      clear.setPressedIcon(new ImageIcon(this.getClass().getResource
      (
         "files/images/FinanceToolClearPressed.png"))
      );
      
      
      clear.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
      
      clear.addActionListener(new ClearButtonListener());
      
      
      
      // Initialize JPanel GUI components.
      
      buttonPanel = new JPanel(); 
      
      buttonPanel.setLayout (new FlowLayout(FlowLayout.LEADING));
      
      buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      
      
      
      // Add GUI components to JPanels.
      
      buttonPanel.add(run); buttonPanel.add(clear);
      
      
      
      // Method calls for GUI initialization.

      gridBagConstraints(); setColors(); setConvertAmount(DEFAULT_AMOUNT);
   }
   
   
   
   /**
    * Sets the background color.
    */
   private void setColors()
   {
      contentPane.setBackground
      (
         new Color (ColorFunctions.red(), ColorFunctions.green(), ColorFunctions.blue())
      );
      
      buttonPanel.setBackground
      (
         new Color (ColorFunctions.red(), ColorFunctions.green(), ColorFunctions.blue())
      );
      
      firstCountryDisplay.setBackground
      (
         new Color (ColorFunctions.red(), ColorFunctions.green(), ColorFunctions.blue())
      );
      
      secondCountryDisplay.setBackground
      (
         new Color (ColorFunctions.red(), ColorFunctions.green(), ColorFunctions.blue())
      );
      
      amountField.setBackground
      (
         new Color (ColorFunctions.red(), ColorFunctions.green(), ColorFunctions.blue())
      );
      
      conversionDisplay.setBackground
      (
         new Color (ColorFunctions.red(), ColorFunctions.green(), ColorFunctions.blue())
      );
      
   }
   
   
   
   /**
    * Sets the grid bag layout.
    * ESCA-JAVA0076:
    */   
   private void gridBagConstraints()
   {
      // Initialize grid bag layout of GUI.
  
      constraints = new GridBagConstraints();
      
      constraints.fill = GridBagConstraints.HORIZONTAL;
      
      
      
      // Grid bag constraints 1.
      
      constraints.insets = new Insets(10,0,0,0);
      
      
      constraints.ipadx = 0; 
      
      constraints.ipady = 0;

      constraints.gridwidth = 2;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 0; 
      
      constraints.gridx = 0; 
      
      constraints.gridy = 0;
      
      
      contentPane.add(firstCurrencyBox, constraints);  
      
      
      
      // Grid bag constraints 2.
      
      constraints.insets = new Insets(10,10,0,0);
      
      
      constraints.ipadx = 0; 
      
      constraints.ipady = 0;

      constraints.gridwidth = 2;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 0; 
      
      constraints.gridx = 2; 
      
      constraints.gridy = 0;
      
      
      contentPane.add(firstCountryDisplay, constraints);  
      
      
      
      // Grid bag constraints 3.
      
      constraints.insets = new Insets(10,0,0,0);
      
      
      constraints.ipadx = 0; 
      
      constraints.ipady = 0;

      constraints.gridwidth = 2;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 0; 
      
      constraints.gridx = 0; 
      
      constraints.gridy = 1;
      
      
      contentPane.add(secondCurrencyBox, constraints);  
 
      
      
      // Grid bag constraints 4.
      
      constraints.insets = new Insets(10,10,0,0);
      
      
      constraints.ipadx = 0; 
      
      constraints.ipady = 0;

      constraints.gridwidth = 2;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 0; 
      
      constraints.gridx = 2; 
      
      constraints.gridy = 1;
      
      
      contentPane.add(secondCountryDisplay, constraints);  
      
      
      
      // Grid bag constraints 5.
      
      constraints.insets = new Insets(30,0,0,0);
      
      
      constraints.ipadx = 350; 
      
      constraints.ipady = 1;

      constraints.gridwidth = 4;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 0; 
      
      constraints.gridx = 0; 
      
      constraints.gridy = 3;
      
      
      contentPane.add(amountField, constraints);  
      
      
      
      // Grid bag constraints 6.
      
      constraints.insets = new Insets(30,0,0,0);
      
      
      constraints.ipadx = 0; 
      
      constraints.ipady = 50;

      constraints.gridwidth = 4;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 0; 
      
      constraints.gridx = 0; 
      
      constraints.gridy = 4;
      
      
      contentPane.add(conversionDisplay, constraints);  
      
      
      
      // Grid bag constraints 7.
      
      constraints.anchor = GridBagConstraints.PAGE_END;
      
      constraints.insets = new Insets(10,10,10,0);
      
      
      constraints.ipadx = 0; 
      
      constraints.ipady = 0;

      constraints.gridwidth = 4;
      
      constraints.weightx = 0; 
      
      constraints.weighty = 1; 
      
      constraints.gridx = 0; 
      
      constraints.gridy = 5;
      
      
      contentPane.add(buttonPanel, constraints);
   }
   
   
   //*************************************** END INITIALISATION CODE ***************************************//
   
   
   //************************************* START INTERFACE EVENT CODE **************************************//
   
   
   private class ClearButtonListener   
   implements ActionListener
   {

      public void actionPerformed(ActionEvent event)
      {
         amountField.setText(null);
         
         conversionDisplay.setText(null);
      }
   }
   
   
   
   private class FirstCurrencyListener   
   implements ActionListener
   {

      public void actionPerformed(ActionEvent event)
      {
         firstCountryDisplay.setText
         (
            currency.returnCountryName((String)firstCurrencyBox.getSelectedItem())
         );
      }
   }
   
   
   
   private class SecondCurrencyListener   
   implements ActionListener
   {

      public void actionPerformed(ActionEvent event)
      {
         secondCountryDisplay.setText
         (
            currency.returnCountryName((String)secondCurrencyBox.getSelectedItem())
         );
      }
   }
   
   
   
   private class RunButtonListener
   implements ActionListener
   {
  
      /** 
       * Listener that starts the counter.
       * ESCA-JAVA0166:
       * ESCA-JAVA0266:
       */
      public void actionPerformed(ActionEvent event)
      { 
         
      
         setFirstCurrency((String)firstCurrencyBox.getSelectedItem());
         
         setSecondCurrency((String)secondCurrencyBox.getSelectedItem());
         
         
         if (validation.numberValidation(amountField.getText()))
         {
            setConvertAmount(amountField.getText()); 
    
         
            try 
            {
               currency.setFeed
               (
                  currency.returnCountryCode(getFirstCurrency()), 
    
                  currency.returnCountryCode(getSecondCurrency()), 
     
                  getConvertAmount()
               );
            
            
               conversionDisplay.setText(returnDisplayContent());
            }
         
            catch (Exception e)
            {System.out.println(Tools.thisPathAndLine() + e + "\n");}
         
         }
      }
   }
   

   //************************************** END INTERFACE EVENT CODE ***************************************//
   
   
   //**************************************** START PUBLIC METHODS *****************************************//
   
   
   /**
    * Sets the visibility of the frame.
    */
   public void setVisibility()
   {
   
      if (isVisible) {frame.setVisible(true);}
      
      else {frame.setVisible(false);} 
   }
   
   
   
   /**
    * Displays or Hides contents of GUI.
    * @param shown
    */
   public void setContentDisplay(boolean shown)
   {
      contentPane.setVisible(shown);
   }
   
   
   //***************************************** END PUBLIC METHODS ******************************************//
   
   
   //**************************************** START PRIVATE METHODS ****************************************//
   
   
   /**
    * Returns the display content.
    * @throws IOException 
    * @return
    */
   private String returnDisplayContent() throws IOException
   {
      String content = Tools.returnNewline(1) + Tools.returnSpace(5) + 
            
      getFirstCurrency() + " - " + getConvertAmount() + Tools.returnNewline(2) + 
            
      Tools.returnSpace(5) + getSecondCurrency() + " - " + currency.returnDataFeed() + 

      
      Tools.returnNewline(4) + Tools.returnSpace(5) + "Currency code for " + 
            
      currency.returnCountryName(getFirstCurrency()) + " - " + 
            
      currency.returnCountryCode(getFirstCurrency()) +
            
            
      Tools.returnNewline(2) + Tools.returnSpace(5) + "Currency code for " + 
            
      currency.returnCountryName(getSecondCurrency()) + " - " + 
            
      currency.returnCountryCode(getSecondCurrency());
      
      
      return content;
   }
   
   
   
   /**
    * Sets the first currency.
    * @param value
    */
   private void setFirstCurrency(String value)
   {
      firstCurrency = value;
   }
   
   
   
   /**
    * Sets the second currency.
    * @param value
    */
   private void setSecondCurrency(String value)
   {
      secondCurrency = value;
   }
   
   
   
   /**
    * Sets the conversion amount.
    * @param value
    */
   private void setConvertAmount(String value)
   {
      if (!value.equals("")) 
      { 
         convertAmount = value;
      }
   }
   
   
   
   /**
    * Returns the first currency.
    * @return
    */
   private String getFirstCurrency()
   {
      return firstCurrency;
   }
   
   
   
   /**
    * Returns the second currency.
    * @return
    */
   private String getSecondCurrency()
   {
      return secondCurrency;
   }
   
   
   
   /**
    * Returns the conversion amount.
    * @return
    */
   private String getConvertAmount()
   {
      return convertAmount;
   }
   
   
   
   /**
    * Window resize event.
    */
   private void includeResizeEvent()
   {
      this.addComponentListener 
      (
         new ComponentAdapter() 
         {
            public void componentResized(ComponentEvent e) 
            {
               appletDisplayHandler();
            }
          
         }
      ); 
   }
   
   
   
   /**
    * Controls the JApplet component visibility. 
    * ESCA-JAVA0266:
    */
   private void appletDisplayHandler()
   {
      final String ZOOM_WARNING = 
   
      "It appears that web pages in your browser are zoomed out. \n\n" +
      "In order to display the applet, your browser needs to be set \n" +
      "at its default zoom level of 100% or greater. \n\n" +
      "Sorry about that.";
   
   
      if ((getWidth() < FRAME_SIZE_X) || (getHeight() < FRAME_SIZE_Y))
      {
         contentPane.setVisible(false);  
      
         
         if (!zoomWarningShown) 
         {
            Message.showMessage(ZOOM_WARNING, "Browser zoom setting", "warning");
            
            zoomWarningShown = true;
         } 
      }
       
      else 
      {
         contentPane.setVisible(true);
      } 
   }
   
   
   //***************************************** END PRIVATE METHODS *****************************************//

   
   //********************************************** START MAIN *********************************************//
   
   
   /**
    * Main
    * @param a
    */
   public static void main(String[] a)
   {
      isApplication = true;
      
      isVisible = true;
      
      new CurrencyConverterGui();
   }
   
   
   //*********************************************** END MAIN **********************************************//

   
   /**
    * ESCA-JAVA0007:
    */
   public JFrame frame;

   private JButton run, clear;
   private JPanel buttonPanel;
   private Container contentPane;
   private Validation validation;
   private JTextField amountField;
   private boolean zoomWarningShown;
   private CurrencyConverter currency;
   private GridBagConstraints constraints;
   private String convertAmount, firstCurrency, secondCurrency;
   private JComboBox firstCurrencyBox, secondCurrencyBox;
   private JTextArea firstCountryDisplay, secondCountryDisplay, conversionDisplay;
   
   
   //********************************************** END CLASS **********************************************//
  
}


/*
   COPYRIGHT (C) 2010 - 2013 by Alexander Wait. All Rights Reserved.

   This class is used to convert between currencies.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2012-05-05
*/



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;



public class CurrencyConverter
{

   private static final String DATA_FEED_KEY = "zwh3O-ExF36-EztRG";
   
   
   private static final int COUNTRY_NAME_INDEX = 0;
   private static final int CURRENCY_NAME_INDEX = 1;
   private static final int COUNTRY_CODE_INDEX = 2;
   
   
   /**
    * Application Constructor.
    * ESCA-JAVA0057:
    */
   public CurrencyConverter() {}
   
   
   //**************************************** START PUBLIC METHODS *****************************************//

   
   /**
    * Sets the data feed.
    * @param fromCurrency
    * @param toCurrency
    * @param amount
    * @throws MalformedURLException
    */
   public void setFeed(String fromCurrency, String toCurrency, String amount) 
   throws MalformedURLException
   {
      String path = "http://www.exchangerate-api.com/" + fromCurrency + "/" + 
   
      toCurrency + "/" + amount + "?k=" + DATA_FEED_KEY;
   
   
      convert = new URL(path);
   }
   
   
   
   /**
    * Returns the data feed.
    * @return
    * @throws IOException
    */
   public String returnDataFeed() throws IOException
   {
      BufferedReader in = new BufferedReader(new InputStreamReader(convert.openStream()));
  
      String answer = in.readLine(); in.close();
      
      
      return answer;
   }
   
   
   
   /**
    * Returns the currency array.
    * @return
    */
   public String[] returnCurrencyName()
   {
      String[] currencyName = new String[currencies.length];
      
      
      for (int i = 0; i < currencies.length; i ++)
      {
         String[] parts = currencies[i].split(",");
         
         currencyName[i] = parts[CURRENCY_NAME_INDEX]; 
      }

      
      return currencyName;
   }
   
   
   
   /**
    * Returns the country name given the currency.
    * @param currencyName
    * @return
    */
   public String returnCountryName(String currencyName)
   {
      String countryName = null;
   
      
      for (String currencie : currencies) 
      {
         String[] parts = currencie.split(",");
         
         if(currencyName.equals(parts[CURRENCY_NAME_INDEX]))
         {
            countryName = parts[COUNTRY_NAME_INDEX];
         }
      }
      
      return countryName;
   }
   
   
   
   /**
    * Returns the country code given the currency.
    * @param currencyName
    * @return
    */
   public String returnCountryCode(String currencyName)
   {
      String countryCode = null;
   
      
      for (String currencie : currencies) 
      {
         String[] parts = currencie.split(",");
         
         if(currencyName.equals(parts[CURRENCY_NAME_INDEX]))
         {
            countryCode = parts[COUNTRY_CODE_INDEX];
         }
      }
      
      return countryCode;
   }
   
   
   //***************************************** END PUBLIC METHODS ******************************************//
   
   
   private URL convert;
   
   private String[] currencies =
   {
      "Argentina,Argentine Peso,ARS","Australia,Australian Dollar,AUD",
      "Bahamas,Bahamian Dollar,BSD","Bahrain,Bahraini Dinar,BHD",
      "Barbados,Barbados Dollar,BBD","Benin,Cfa Franc Bceao,XOF",
      "Brazil,Brazilian Real,BRL","Cameroon,Cfa Franc Beac,XAF",
      "Canada,Canadian Dollar,CAD","Chile,Chilean Peso,CLP",
      "China,Chinese Yuan,CNY","Croatian,Croatia Kuna,HRK",
      "Czech Republic,Czech Koruna,CZK","Denmark,Danish Krone,DKK",
      "East Caribbean,East Caribbean Dollar,XCD","Egypt,Egyptian Pound,EGP",
      "Estonia,Estonian Kroon,EEK","Eurozone,Euro,EUR",
      "Fiji,Fiji Dollar,FJD","Hong Kong,Hong Kong Dollar,HKD",
      "Hungary,Hungarian Forint,HUF","Iceland,Icelandic Krona,ISK",
      "India,Indian Rupee,INR","Indonesia,Indonesian Rupiah,IDR",
      "Israel,Israeli New Shekel,ILS","Jamaica,Jamaican Dollar,JMD",
      "Japan,Japanese Yen,JPY","Kenya,Kenyan Shilling,KES",
      "Korea,Korean Won,KRW","Latvian Lats,Latvia,LVL",
      "Lithuania,Lithuanian Litas,LTL","Malasia,Malasian Ringgit,MYR",
      "Mexico,Mexican Pesos,MXN","Morocco,Moroccan Dirham,MAD",
      "Netherlands Antilles,Netherlands Antillian Guilder,ANG",
      "New Zealand,New Zealand Dollar,NZD","Norway,Norwegian Krone,NOK",
      "Oman,Omani Rial,OMR","Pakistan,Pakistani Rupee,PKR",
      "Panama,Panamanian Balboa,PAB","Peru,Peruvian Nuevo Sol,PEN",
      "Philippines,Philippine Peso,PHP","Poland,Polish Zloty,PLN",
      "Qatar,Qatari Rial,QAR","Romania,New Romanian Leu,RON",
      "Russia,Russian Rouble,RUB","Saudi Arabia,Saudi Riyal,SAR",
      "Serbia,Serbian Dinar,RSD","Singapore,Singapore Dollar,SGD",
      "South Africa,South African Rand,ZAR","Sri Lanka,Sri Lanka Rupee,LKR",
      "Sweden,Swedish Krona,SEK","Switzerland,Swiss Franc,CHF",
      "Taiwan,Taiwan New Dollar,TWD","Thailand,Thai Baht,THB",
      "Trinidad and Tobago,Trinidad And Tobago Dollar,TTD",
      "Turkey,New Turkish Lira,TRY","United Arab Emirates,Uae Dirham,AED",
      "United Kingdom,Pound Sterling,GBP","United States,US Dollar,USD",
      "Venezuela,Venezuelan Bolivar Fuerte,VEF","Viet Nam,Vietnamese Dong,VND"
   };
   
   
   //********************************************** END CLASS **********************************************//
   
}


/*
   COPYRIGHT (C) 2010 -2013 by Alexander Wait. All Rights Reserved.

   This class defines colors and color methods.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2012-05-18
*/



public class ColorFunctions
{

   private static final int HEXADECIMAL_BASE = 16;
   
   private static final int COLOR_COMPONENT_RED = 250;
   
   private static final int COLOR_COMPONENT_GREEN = 250;
   
   private static final int COLOR_COMPONENT_BLUE = 250;
   
   
   
   private ColorFunctions() {}
   
   
   //**************************************** START PUBLIC METHODS *****************************************//

   
   /**
    * Returns the color in hexadecimal format.
    * @param sliderValue
    * @return
    */
   public static String returnHexadecimalColor(int sliderValue)
   {
      String compFirst = ""; String compSecond = ""; int counter = -1;
 
      for (int i = 0; i < HEXADECIMAL_BASE; i ++)
      {
         compFirst = returnComponent(i);

          for (int j = 0; j < HEXADECIMAL_BASE; j ++)
          {
             compSecond = returnComponent(j); counter ++; 
  
             if (counter == sliderValue)
             {
                break;
             }
             
          }
  
          if (counter == sliderValue)
          {
             break;
          }
  
       }
   
       return 
       (
          "#" + compFirst + compSecond + compFirst + 
          
          compSecond + compFirst + compSecond
       );
   
   }
   
   
   
   /**
    * Return the set red color component 
    * for setting the programs background color. 
    * @return
    */
   public static int red()
   {
      return COLOR_COMPONENT_RED;
   }
   
   
   
   /**
    * Return the set green color component 
    * for setting the programs background color. 
    * @return
    */
   public static int green()
   {
      return COLOR_COMPONENT_GREEN;
   }
   
   
   
   /**
    * Return the set blue color component 
    * for setting the programs background color. 
    * @return
    */
   public static int blue()
   {
      return COLOR_COMPONENT_BLUE;
   }
   
   
   //***************************************** END PUBLIC METHODS ******************************************//


   //**************************************** START PRIVATE METHODS ****************************************//
   
   
   /**
    * Returns the hexadecimal component.
    * ESCA-JAVA0076:
    * @param index
    * @return
    */
   private static String returnComponent(int index)
   {
      String component = "";
  
      switch (index)
      {
         case 10: component = "A"; break; case 11: component = "B"; break;  
         
         case 12: component = "C"; break; case 13: component = "D"; break; 
         
         case 14: component = "E"; break; case 15: component = "F"; break;
   
         default: component = Integer.toString(index); break;
      }
   
      return component;   
   }
   
 
   //***************************************** END PRIVATE METHODS *****************************************//

   
   //********************************************** END CLASS **********************************************//
   
}


/*
   COPYRIGHT (C) 2010 - 2013 by Alexander Wait. All Rights Reserved.

   This class returns the domain attributes.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2012-07-06
*/



public class Domain 
{
  
   private static final String DOMAIN = "javaika.com";

   private static final boolean RUN_LOCAL = false; 
   
   private static final boolean prefix = true;
   
   private static final String PORT = "80";
   
   
   private Domain() {}
   
   
   //**************************************** START PUBLIC METHODS *****************************************//
   
   
   /**
    * Returns the domain name.
    * @return
    */
   public static final String getDomain()
   {
      if (prefix)
      {
         return ("www." + DOMAIN);
      }
      
      else
      {
         return (DOMAIN);
      }
   }
   
   
   
   /**
    * Returns the port number.
    * @return
    */
   public static final String getPort()
   {
      if (RUN_LOCAL)
      {
         return (":" + PORT);
         
      }
      
      else {return ("");} 
   }
   
   
   //***************************************** END PUBLIC METHODS ******************************************//
   
   
   //********************************************** END CLASS **********************************************//

}


/*
   COPYRIGHT (C) 2010 - 2013 by Alexander Wait. All Rights Reserved.

   This class displays message dialogs.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2011-07-18
*/



import javax.swing.*;



public class Message
{

   private Message(){}
   
   
   //**************************************** START PUBLIC METHODS *****************************************//


   /**
    * Displays a message dialog.
    * @param text
    * @param heading
    * @param type 
    */
   public static void showMessage(String text, String heading, String type)
   {
      new JOptionPane(); Object[] options = {}; 

      if (type.equals("error"))
      {
         JOptionPane.showOptionDialog(null, text, heading,  
         JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null, options, null);
      }

      if (type.equals("information"))
      {
         JOptionPane.showOptionDialog(null, text, heading, 
         JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, null);
      }

      if (type.equals("warning"))
      {
         JOptionPane.showOptionDialog(null, text, heading, 
         JOptionPane.YES_OPTION, JOptionPane.WARNING_MESSAGE, null, options, null);
      }

      if (type.equals("question"))
      {
         JOptionPane.showOptionDialog(null, text, heading, 
         JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
      }
   }
   
   
   //***************************************** END PUBLIC METHODS ******************************************//
   
   
   //********************************************** END CLASS **********************************************//
   
}


/*
   COPYRIGHT (C) 2010 - 2013 by Alexander Wait. All Rights Reserved.

   This class provides handy tools.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2012-02-01
*/



public class Tools 
{

   private static final int EXCEPTION_LINE_BEAK = 2;
   
   
   private Tools() {}
   
   
   //**************************************** START PUBLIC METHODS *****************************************//


   /**
    * This method returns the line number and path where it's called.
    * The syntax new Exception().getStackTrace()[0].getLineNumber() 
    * will only give the current line number of the source file.
    * @return
    */
   public static String thisPathAndLine()
   {
   
      String lineAndPath = ""; String stackTrace = "";
 
      stackTrace = new Exception().getStackTrace()[1].toString();
      
      
      for (int i = stackTrace.length()-1; i >= 0; i --)
      {
         if (stackTrace.charAt(i) == ')') {continue;} 
        
         else if (stackTrace.charAt(i) == '(') {break;}
        
         else {lineAndPath = (stackTrace.charAt(i) + lineAndPath);}
      }
         
      return lineAndPath + returnNewline(EXCEPTION_LINE_BEAK);
      
   }
   
   
   
   /**
    * Delays execution.
    * @param length
    */
   public static void delay(long length) 
   {
      for (long i = 0; i < length; i ++) {}
   }
   
  
 
   /**
    * This method returns a space.
    * @param length
    * @return
    */
   public static String returnSpace(int length)
   {
      String space = "";
      
      
      for (int i = 0; i < length; i ++)
      {
         space += " ";
      }
      
      return space;
   }
   
   
   
   /**
    * Assigns newlines to a string.
    * @param lines
    * @return
    */
   public static String returnNewline(int lines)
   {
      String newline = "";
      
      
      for (int i = 0; i < lines; i++)
      {
         newline += "\n";
      }
      
      
      return newline;
   
   }
   
   
   
   /**
    * Returns the users desktop path.
    * @return
    */
   public static String returnDesktopPath()
   {
      return (System.getProperty("user.home") + "/Desktop").replace("\\", "/") + "/"; 
   }
   

   //***************************************** END PUBLIC METHODS ******************************************//

   
   //********************************************** END CLASS **********************************************//
   
}


/*
   COPYRIGHT (C) 2010 - 2013 by Alexander Wait. All Rights Reserved.

   This class is used for sophisticated validation of data.

   @site http://www.javaika.com
   @author Alexander Wait
   @version 2010-07-18
*/



import java.util.regex.Pattern;



public class Validation
{

   //************************************** START INITIALISATION CODE **************************************//


   private static final String IP_ADDRESS_PATTERN = 
   
   "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +

   "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
   
   
   private static final String NUMBER_PATTERN = "[+-]?[0-9]*[.]?[0-9]*";
   
   private static final String ALPHANUMERIC_PATTERN = "[A-Za-z0-9]*";
   
   private static final String LETTER_PATTERN = "[A-Za-z]*";
   
   private static final String INTEGER_PATTERN = "[0-9]*";



   /**
    *  Validation constructor.
    */
   public Validation()
   {
      count = 0;
   }
   
   
   //*************************************** END INITIALISATION CODE ***************************************//

   
   //**************************************** START PUBLIC METHODS *****************************************//
   
   
   /**
    * Tests a string against a supplied mask string.
    * The key for testing a string is as follows.
    * '^' represents a upper case letter.
    * '#' represents a lower case letter.
    * '?' represents a digit.
    * @param maskString
    * @param testString 
    * @return result()
    */
   public boolean maskValidation(String maskString, String testString)
   {
      count = 0;

      
      if (maskString.length() == testString.length())
      {
         for (int i = 0; i < maskString.length(); i ++)
         {
            if (maskString.charAt(i) == testString.charAt(i))
            {
               count ++;
            }

            if (maskString.charAt(i) == '?')
            {
               for (char element : digit) 
               {
                  if (testString.charAt(i) == element) 
                  {
                     count ++;
                  }
               }
            }

            if (maskString.charAt(i) == '#')
            {
               for (char element : lowerCase) 
               {
                  if (testString.charAt(i) == element)
                  {
                     count ++;
                  }
               }
            }

            if (maskString.charAt(i) == '^')
            {
               for (char element : upperCase) 
               {
                  if (testString.charAt(i) == element)
                  {
                     count ++;
                  }
               }
            }
         }
      }
      
      
      if (count != testString.length()) {return false;}
      
      else {return true;}
   }
   
   
   
   /**
    * Checks the validation of an IP address.
    * @param testString
    * @return
    */
   public boolean ipAddressValidation(String testString)
   {
      pattern = Pattern.compile(IP_ADDRESS_PATTERN);
      
      
      return (pattern.matcher(testString).matches());
   }
   
   
   
   /**
    * Checks the validation of an integer.
    * @param testString
    * @return
    */
   public boolean integerValidation(String testString)
   {
      pattern = Pattern.compile(INTEGER_PATTERN);
      
      
      return (pattern.matcher(testString).matches());
   }
   
   
   
   /**
    * Checks the validation of a letter string.
    * @param testString
    * @return
    */
   public boolean letterValidation(String testString)
   {
      pattern = Pattern.compile(LETTER_PATTERN);
      
      
      return (pattern.matcher(testString).matches());
   }
   
   
   
   /**
    * Checks the validation of an alphanumeric string.
    * @param testString
    * @return
    */
   public boolean alphanumericValidation(String testString)
   {
      pattern = Pattern.compile(ALPHANUMERIC_PATTERN);
      
      
      return (pattern.matcher(testString).matches());
   }
   
   
   
   /**
    * Checks the validation of an alphanumeric string.
    * @param testString
    * @return
    */
   public boolean numberValidation(String testString)
   {
      pattern = Pattern.compile(NUMBER_PATTERN);
      
      
      return (pattern.matcher(testString).matches());
   }


   //***************************************** END PUBLIC METHODS ******************************************//


   private int count;
   
   private Pattern pattern;


   /**
    * ESCA-JAVA0007:
    */
   public static String[] numberOperator = {"PI", "e"};
   
   /**
    * ESCA-JAVA0007:
    */
   public static char[] digit = {'0','1','2','3','4','5','6','7','8','9'};
   
   /**
    * ESCA-JAVA0007:
    */
   public static char[] simpleOperator = {'-', '+', 'v', '^', '/', 'x', '*', ','};
   
   /**
    * ESCA-JAVA0007:
    */
   public static char[] number = 
   {'-','+','.','0','1','2','3','4','5','6','7','8','9','E','I','n','f','i','t','y'};
   
   /**
    * ESCA-JAVA0007:
    */
   public static String[] singleOperator = {"Sin", "Cos", "Tan", "Fac", "Inv", "Abs"};
   
   /**
    * ESCA-JAVA0007:
    */
   public static String[] doubleOperator = {"Log", "Mod", "Pwr", "Pnr", "Cwr", "Cnr"};
  
   /**
    * ESCA-JAVA0007:
    */
   public static char[] lowerCase = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
   'p','q','r','s','t','u','v','w','x','y','z'};
   
   /**
    * ESCA-JAVA0007:
    */
   public static char[] upperCase = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
   'P','Q','R','S','T','U','V','W','X','Y','Z'};
   
   
   //********************************************** END CLASS **********************************************//

}