import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.net.URL;
import java.text.*;
import java.util.Map;
import java.util.Hashtable;
import java.util.jar.Attributes;
//import javax.swing.*;

// Chapter 3 Module 1
public class Mod1 extends Frame implements AdjustmentListener,
                                           ItemListener,
                                           ActionListener,
                                           WindowListener {
    TitlePanel titlePanel;
    Panel choicePanel;
    Mod1Input inputPanel;  // Polar to rectangular
    Mod1State state;
    Mod1Plot plotPanel;
    Mod1Info infoPanel;

    Font ttfFont, phiFont, italicFont;

    private static final boolean useOldFonts = true;    
    

  public void start() {
      // default starts with input panel 1 (cartesian vs cylindrical)
      //inputPanel.calculateXandY();  // from initial default values
      inputPanel.calculateZandTheta();  // from initial default values
  }

  public void stop(){}

  public void destroy(){}

    
    public static void main(String[] args){
        Mod1 f = new Mod1();
        int xmove = 20;
        int ymove = 41;
        f.setSize(983+xmove,620+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }


  public Mod1() { 
    setLayout(null);    
    String lcOSName = System.getProperty("os.name").toLowerCase();
    boolean MAC_OS_X = lcOSName.startsWith("mac os x");
    boolean PC_OS = lcOSName.startsWith("windows");
    //System.out.println(lcOSName);
    TheFonts.setMAC_OS_X(MAC_OS_X);
    TheFonts.setPC_OS(PC_OS);
    
    if (!useOldFonts) {
        //getUlabyFonts();
        state = new Mod1State();
        state.ttfFont = ttfFont;
        state.sanSerifFont = ttfFont;
        state.serifFont = ttfFont;
        state.symbolFont = phiFont;
        state.italicFont = phiFont;
        TheFonts.setNewFonts(ttfFont, phiFont, italicFont);
    } else {
        state = new Mod1State();
        state.ttfFont = new Font("SanSerif",Font.PLAIN,12);
        state.sanSerifFont = new Font("SanSerif",Font.PLAIN,12);
        state.serifFont = new Font("Serif",Font.PLAIN,12);
        state.symbolFont = new Font("Symbol",Font.PLAIN,12);
        state.italicFont = new Font("Serif",Font.ITALIC,12);
        TheFonts.setOldFonts();
    }    
    
    int xmove = 10;
    int ymove = 34;

    // Get focus off any visible component ... looks nicer.
    Panel nullPanel = new Panel();
    Button nullButton = new Button();
    nullPanel.add(nullButton);
    nullPanel.setVisible(false);

    titlePanel = new TitlePanel(state.ttfFont.deriveFont(Font.BOLD,15f),
                                "Module 3.1","Vector Addition and Subtraction");
    titlePanel.setBounds(10+xmove,10+ymove,498,40);

    //choicePanel = new Mod1Choice(state);
    choicePanel = new Panel();
    choicePanel.setLayout(null);
    choicePanel.setBounds(517+xmove,13+ymove,450,34);
    choicePanel.setBackground(Color.lightGray);

    inputPanel = new Mod1Input(state);
    inputPanel.setVisible(true);
    inputPanel.setBounds(517+xmove,58+ymove,450,459);

    plotPanel = new Mod1Plot(state);
    plotPanel.setVisible(true);
    plotPanel.setBounds(11+xmove,57+ymove,496,541);

    infoPanel = new Mod1Info(state);
    infoPanel.setVisible(true);
    infoPanel.setBounds(517+xmove,527+ymove,450,70); // 469+58 = 527;  128 - 58 = 70

    // backpanels for choice,input, plot and info
    Panel p_c_b = new Panel();
    p_c_b.setBackground(Color.black);
    p_c_b.setBounds(516+xmove,12+ymove,452,37);
    Panel p_i_w = new Panel();
    p_i_w.setBackground(Color.white);
    p_i_w.setBounds(516+xmove,57+ymove,452,461);
    Panel p_i_b = new Panel();
    p_i_b.setBackground(Color.black);
    p_i_b.setBounds(515+xmove,56+ymove,454,463);
    Panel p_p_b = new Panel();
    p_p_b.setBackground(Color.black);
    p_p_b.setBounds(10+xmove,56+ymove,498,543);
    Panel p_inf_w = new Panel();
    p_inf_w.setBackground(Color.white);
    p_inf_w.setBounds(516+xmove,526+ymove,452,72);
    Panel p_inf_b = new Panel();
    p_inf_b.setBackground(Color.black);
    p_inf_b.setBounds(515+xmove,525+ymove,454,74);


    //backpanels for applet
    Panel p0 = new Panel();
    p0.setBackground(Color.cyan);
    p0.setBounds(3+xmove,3+ymove,975,603);
	    
    Panel p00 = new Panel();
    p00.setBackground(Color.black);
    p00.setBounds(xmove,ymove,981,609);
   




    add(nullPanel);

    add(choicePanel);
    add(titlePanel);

    add(inputPanel);
    add(plotPanel);
    add(infoPanel);

    add(p_c_b);
    add(p_i_w);
    add(p_i_b);
    add(p_p_b);
    add(p_inf_w);
    add(p_inf_b);

    plotPanel.initialPlot();
    inputPanel.createMiniGraphs();

    infoPanel.displayOutput();
    add(p0);
    add(p00);

    inputPanel.sliderxA.addAdjustmentListener(this);
    inputPanel.sliderxB.addAdjustmentListener(this);
    inputPanel.slideryA.addAdjustmentListener(this);
    inputPanel.slideryB.addAdjustmentListener(this);
    inputPanel.sliderzA.addAdjustmentListener(this);
    inputPanel.sliderzB.addAdjustmentListener(this);
    inputPanel.sliderThetaA.addAdjustmentListener(this);
    inputPanel.sliderThetaB.addAdjustmentListener(this);

    inputPanel.textxA.addActionListener(this);
    inputPanel.textxB.addActionListener(this);
    inputPanel.textyA.addActionListener(this);
    inputPanel.textyB.addActionListener(this);
    inputPanel.textzA.addActionListener(this);
    inputPanel.textzB.addActionListener(this);
    inputPanel.textThetaA.addActionListener(this);
    inputPanel.textThetaB.addActionListener(this);


    this.addWindowListener(this);
    inputPanel.ch.addItemListener(this);
    inputPanel.choiceA.addItemListener(this);
    inputPanel.choiceB.addItemListener(this);
    inputPanel.choiceAplusB.addItemListener(this);
    inputPanel.choiceAminusB.addItemListener(this);
    nullButton.requestFocus();
  }


/*
    private void getUlabyFonts() {
        String path1 = getCodeBase().getPath() + "times.ttf";
        String path2 = getCodeBase().getPath() + "palai.ttf";
        String path3 = getCodeBase().getPath() + "timesi.ttf";

        try {

            // SWAP COMMENT TO OTHER fi= STATEMENT WHEN FINAL VERSION OK
            //  TO RUN ON THE BROWSER (use the other one when debugging ...)

            //File fi = new File ("/home/jar/newcd/fonts/times.ttf");
            File fi = new File (path1);
            FileInputStream in = new FileInputStream (fi);
            ttfFont = Font.createFont (Font.TRUETYPE_FONT, in);

            //fi = new File ("/home/jar/newcd/fonts/palai.ttf");
            fi = new File (path2);
            in = new FileInputStream (fi);
            phiFont = Font.createFont (Font.TRUETYPE_FONT, in);
            phiFont = phiFont.deriveFont (14f);  // SAVE

            fi = new File (path3);
            in = new FileInputStream (fi);
            italicFont = Font.createFont (Font.TRUETYPE_FONT, in);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
*/

    
    public void paint(Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        g.setColor(Color.white);	
	g.fillRect(0,0,626,585);  	
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    }





public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }
    
    public void windowOpened(WindowEvent evt){}
    
    public void windowIconified(WindowEvent evt){}
    
    public void windowClosed(WindowEvent evt){}
    
    public void windowDeiconified(WindowEvent evt){}
    
    public void windowActivated(WindowEvent evt){}
    
    public void windowDeactivated(WindowEvent evt){}



    public void actionPerformed(ActionEvent evt) {
        //public Scrollbar slider1, slider2, slider3;
        if(evt.getSource()==inputPanel.textzA){
            inputPanel.readTextUpdateSlider(inputPanel.textzA,
                                            inputPanel.sliderzA,
                                            state.zMin,state.zMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textzB){
            inputPanel.readTextUpdateSlider(inputPanel.textzB,
                                            inputPanel.sliderzB,
                                            state.zMin,state.zMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textThetaA){
            inputPanel.readTextUpdateSlider(inputPanel.textThetaA,
                                            inputPanel.sliderThetaA,
                                            state.thetaMin,state.thetaMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textThetaB){
            inputPanel.readTextUpdateSlider(inputPanel.textThetaB,
                                            inputPanel.sliderThetaB,
                                            state.thetaMin,state.thetaMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textxA){
            inputPanel.readTextUpdateSlider(inputPanel.textxA,
                                            inputPanel.sliderxA,
                                            state.xMin,state.xMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textxB){
            inputPanel.readTextUpdateSlider(inputPanel.textxB,
                                            inputPanel.sliderxB,
                                            state.xMin,state.xMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textyA){
            inputPanel.readTextUpdateSlider(inputPanel.textyA,
                                            inputPanel.slideryA,
                                            state.yMin,state.yMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
        if(evt.getSource()==inputPanel.textyB){
            inputPanel.readTextUpdateSlider(inputPanel.textyB,
                                            inputPanel.slideryB,
                                            state.yMin,state.yMax);
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	}
    }



    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==inputPanel.sliderxA){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
        } else if(evt.getSource()==inputPanel.sliderxB){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	} else if(evt.getSource()==inputPanel.slideryA){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	} else if(evt.getSource()==inputPanel.slideryB){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	} else if(evt.getSource()==inputPanel.sliderzA){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
	} else if(evt.getSource()==inputPanel.sliderzB){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
        } else if(evt.getSource()==inputPanel.sliderThetaA){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
        } else if(evt.getSource()==inputPanel.sliderThetaB){
            inputPanel.drawMiniGraphs();
            plotPanel.drawPlot();
            infoPanel.displayOutput();
        }
    }



    public void itemStateChanged(ItemEvent evt){
	ItemSelectable ie = evt.getItemSelectable();
        if(evt.getSource() == inputPanel.ch){
            if(ie.getSelectedObjects()[0]==" rectangular (x,y)"){
                inputPanel.setRectVisible();
                state.outputC_XandY = true;
                state.outputC_ZandTheta = false;
                // set new sliders to previously calculated values
                inputPanel.initializeRectInputs();
                // redisplay (new) calculated values in output window
                // (NOTE: the plot will look the same initially)
                infoPanel.displayOutput();
                //plotPanel.drawPlot();  // not nec.
            } else { //if (ie.getSelectedObjects()[0]==" polar ..."
                inputPanel.setPolarVisible();
                state.outputC_XandY = false;
                state.outputC_ZandTheta = true;
                // set new sliders to previously calculated values
                inputPanel.initializePolarInputs();
                // display (new) calculated values in output window
                // (NOTE: the plot will look the same initially)
                //inputPanel.calculateXandY();
                infoPanel.displayOutput();
                plotPanel.drawPlot(); // in case z from >5 to 5
            }
        } else if (evt.getSource() == inputPanel.choiceA) {
            //System.out.println("choice A");
            //state.outputA = inputPanel.choiceA.getState();
            state.outputA = true; state.outputB = false;
            state.outputAplusB = false; state.outputAminusB = false;
            //if (state.outputA) plotPanel.drawPlot();
            infoPanel.displayOutput();
            plotPanel.drawPlot();
        } else if (evt.getSource() == inputPanel.choiceB) {
            //System.out.println("choice B");
            //state.outputB = inputPanel.choiceB.getState();
            state.outputA = false; state.outputB = true;
            state.outputAplusB = false; state.outputAminusB = false;
            //if (state.outputB) plotPanel.drawPlot();
            infoPanel.displayOutput();
            plotPanel.drawPlot();
        } else if (evt.getSource() == inputPanel.choiceAplusB) {
            //System.out.println("choice A+b");
            //state.outputAplusB = inputPanel.choiceAplusB.getState();
            state.outputA = false; state.outputB = false;
            state.outputAplusB = true; state.outputAminusB = false;
            //if (state.outputAplusB) plotPanel.drawPlot();
            infoPanel.displayOutput();
            plotPanel.drawPlot();
        } else if (evt.getSource() == inputPanel.choiceAminusB) {
            //System.out.println("choice A-b");
            //state.outputAminusB = inputPanel.choiceAminusB.getState();
            state.outputA = false; state.outputB = false;
            state.outputAplusB = false; state.outputAminusB = true;
            //if (state.outputAminusB) plotPanel.drawPlot();
            infoPanel.displayOutput();
            plotPanel.drawPlot();
        }   
    }
}
