//Michael Janulawicz

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;

/*
 * Added by Richard Rasala
 * 
 * The text fields product1 and product2 in order to
 * test that a*b equals lcm*gcd.
 * 
 * Also added main to launch program directly.
 */

public class Janulawicz_GCDLCM extends TablePanel {
    
    private static int width =
        TextFieldView.getSampleWidth(20, '0');
    
    private TextFieldView view1 =
        new TextFieldView("", width);
    
    private TextFieldView view2 =
        new TextFieldView("", width);

    private TextFieldView rgcd =
        new TextFieldView("", width);
    
    private TextFieldView rlcm =
        new TextFieldView("", width);
    
    // Added by Richard Rasala
    private TextFieldView product1 =
        new TextFieldView("", width);
    
    // Added by Richard Rasala
    private TextFieldView product2 =
        new TextFieldView("", width);
    
    private SimpleAction gcdlcm =
        new SimpleAction("Calculate") {
            public void perform() { gcdlcm(); }
        
    };
    
    private Object[][] dataStuff =
        new Object[][] {
    		{ "Enter integers for a and b" },
            { "a", new Halo(view1) },
            { "b", new Halo(view2) },
            { "gcd(a,b) = ", new Halo(rgcd) },
            { "lcm(a,b) = ", new Halo(rlcm) },
            { "a*b = ", new Halo(product1) },     // Added by Richard Rasala
            { "gcd*lcm = ", new Halo(product2) }  // Added by Richard Rasala
    };
    
    private TablePanel dataPanel =
        new TablePanel(
            dataStuff, 10, 10, WEST);
    
    private Object[] mainStuff =
        new Object[] { dataPanel, gcdlcm };
    
    private TablePanel mainPanel =
        new TablePanel(
            mainStuff, VERTICAL, 10, 10, CENTER);
    
    
    public Janulawicz_GCDLCM() {
        add(new Halo(mainPanel, 10, 10));
        addListeners();
        
        frame("Janulawicz_GCDLCM");
    }
    
    
    private void gcdlcm() {
        int a = view1.demandInt();
        int b = view2.demandInt();
        
        
        int thegcd = getgcd(a,b);
        int thelcm = getlcm(a,b,thegcd);
        
        // Added by Richard Rasala
        int p1 = a*b;
        int p2 = thegcd*thelcm;
        
        rgcd.setViewState(String.valueOf(thegcd));
        rlcm.setViewState(String.valueOf(thelcm));
        
        // Added by Richard Rasala
        product1.setViewState(String.valueOf(p1));
        product2.setViewState(String.valueOf(p2));
    }
    private int getgcd (int a, int b)
    {
    	if (b == 0)
    		return a;
    	return getgcd (b, a % b);
    }
    
    private int getlcm (int a, int b, int c)
    {
    	return a*(b/c);
    }

    private void addListeners() {
        view1.addActionListener(gcdlcm);
        view2.addActionListener(gcdlcm);
    }
    
    
    /*
     * Added by Richard Rasala
     */
    public static void main(String[] args) {
        new Janulawicz_GCDLCM();
    }
    
}

