/******************************************************************************************
 * Surface Plotter version 1.10                                               Oct 14 1996 *
 * Surface Plotter version 1.20                                               Nov  8 1996 *
 *                                                                                        *
 * SettingPanel.java: Setting Panel of Surface Plotter                                    *
 *                                                                                        *
 * Copyright (c) 1996 Yanto Suryono. All Rights Reserved.                                 *
 *                                                                                        *
 * Permission to use, copy, and distribute this software for NON-COMMERCIAL purposes      *
 * and without fee is hereby granted, provided that this copyright notice appears in all  *
 * copies and this software is not modified in any way                                    *
 *                                                                                        *
 * Please send bug reports and/or corrections/suggestions to                              *
 * Yanto Suryono <d0771@cranesv.egg.kushiro-ct.ac.jp>                                     *
 ******************************************************************************************/

//import java.awt.*;
//
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;

class SettingPanel extends JPanel implements ActionListener {
    
    
    private InputField xmin,xmax,ymin,ymax,zmin,zmax;
    private InputField CalcDivisions,DispDivisions;
    private JCheckBox boxed,mesh,grids,scalebox;
    private String[] tick_mode_string = {"X-Y-Z ticks", "X-Y ticks", "Z tick", "no tick"};
    private JComboBox tick_mode;
    private JTextField min,max;
    
    private final int MIN_DIVISIONS = 5;
    private final int MAX_DIVISIONS = 200;
    private final int DEF_DIVISIONS = 20;
    
    private final int SIZE1 = 9;
    private final int SIZE2 = 9;
    
    private final String[] divisionname = {"Calculate", "Display"};
    private final String[] typename = {"Minimums", "Maximums"};
    private final String[] axisname = {"real", "imag", "mag"};
    
    ThreeDResponse threedresponse;
    PzmapPanel pzmap_panel;
    
    /*--------------------------------------------------------------------------------                     
      Constructor for SettingPanel
      --------------------------------------------------------------------------------*/  
    
    SettingPanel(ThreeDResponse response, int init_calc_divisions, int init_disp_divisions) {
	JTextField message;
	threedresponse=response;
	setBackground(Color.lightGray);
	setLayout(new BorderLayout());
	JPanel panel = new JPanel();
	panel.setLayout(new GridLayout(1,2));    
	
	JButton calculate_button = new JButton("Calculate");
	JButton stop_button = new JButton("Stop");
	calculate_button.addActionListener(this);
	stop_button.addActionListener(this);
	
	panel.add(calculate_button); 
	panel.add(stop_button);
	
	add("North",panel); 
	//add(panel);
	add("Center",panel = new JPanel());
	panel.setLayout(new BorderLayout());
	
	JPanel inputpanel = new JPanel();
	panel.add("North",inputpanel);
	inputpanel.setLayout(new GridLayout(15,1));
	inputpanel.add(message = new JTextField("Divisions"));
	message.setEditable(false);
	message.setHorizontalAlignment(0);
	
	JPanel subpanel;
	InputField[] inputfield = new InputField[6];
	
	for (int i=0; i < 2; i++) {
	    subpanel = new JPanel();
	    subpanel.setLayout(new BorderLayout());
	    subpanel.add("East", inputfield[i] = 
			 new InputField(SIZE1,(float)MIN_DIVISIONS,(float)MAX_DIVISIONS,1.0f,true));
	    subpanel.add("Center",message = new JTextField(divisionname[i]));
	    message.setHorizontalAlignment(0);
	    
	    inputpanel.add(subpanel);
	}
	
	CalcDivisions = inputfield[0];
	CalcDivisions.setText(init_calc_divisions + "");
	DispDivisions = inputfield[1];
	DispDivisions.setText(init_disp_divisions + "");
	
	for (int i=0; i < 2; i++) {
	    inputpanel.add(message = new JTextField(typename[i]));
	    message.setEditable(false);
	    message.setHorizontalAlignment(0);
	    
	    for (int j=0; j < 3; j++) {
		subpanel = new JPanel();
		subpanel.setLayout(new BorderLayout());
		subpanel.add("East", inputfield[i*3+j] = 
			     new InputField(SIZE2,-1.0f/0.0f,1.0f/0.0f,1.0f,false));
		subpanel.add("Center",message = new JTextField(axisname[j]));
		message.setHorizontalAlignment(0);
		
		message.setEditable(false);
		inputpanel.add(subpanel);
	    }
	}
	
	xmin = inputfield[0];
	ymin = inputfield[1];
	zmin = inputfield[2];
	xmax = inputfield[3];
	ymax = inputfield[4];
	zmax = inputfield[5];
	
	inputpanel.add(message = new JTextField("Minimum Mag"));
	message.setEditable(false);
	message.setHorizontalAlignment(0);
	
	inputpanel.add(min = new JTextField());
	min.setEditable(false);
	inputpanel.add(message = new JTextField("Maximum Mag"));
	message.setEditable(false);
	message.setHorizontalAlignment(0);
	
	inputpanel.add(max = new JTextField());
	max.setEditable(false);
	
	JPanel choicepanel = new JPanel();
	panel.add("Center",panel = new JPanel());
	panel.setLayout(new BorderLayout());
	panel.add("North", choicepanel);
	
	tick_mode = new JComboBox(tick_mode_string);
	tick_mode.addActionListener(this);
	tick_mode.setActionCommand("tick_mode");
	
	choicepanel.setLayout(new GridLayout(1,1));
	choicepanel.add(tick_mode);
	
	JPanel checkboxpanel = new JPanel();
	panel.add("Center",panel = new JPanel());
	panel.setLayout(new BorderLayout());
	panel.add("North", checkboxpanel);
	
	checkboxpanel.setLayout(new GridLayout(6,1));
	
	checkboxpanel.add(boxed = new JCheckBox("Boxed")); 
	boxed.addActionListener(this);
	boxed.setActionCommand("check_box");
	boxed.setSelected(true);
	
	
	checkboxpanel.add(mesh = new JCheckBox("X-Y Mesh"));
	mesh.addActionListener(this);
	mesh.setActionCommand("check_box");
	mesh.setSelected(true);
	
	checkboxpanel.add(grids = new JCheckBox("Face Grids"));
	grids.addActionListener(this);
	grids.setActionCommand("check_box");
	grids.setSelected(true);
	
	checkboxpanel.add(scalebox = new JCheckBox("Scale Box"));
	scalebox.addActionListener(this);
	scalebox.setActionCommand("check_box");
	scalebox.setSelected(true);
	
    }
    
    
    /*-------------------------------------------------------------------------
      handle the button events
      ______________________________________________________________________*/
    
    public void actionPerformed(ActionEvent e) {
	if (e.getActionCommand().equals("Calculate"))
	    {
		threedresponse.doCalculate();
	    }
	else if (e.getActionCommand().equals("Stop"))
	    {
		threedresponse.setVisible(false); 
		threedresponse.doStop();
	    }
	
	else if (e.getActionCommand().equals("tick_mode"))
	    {
		threedresponse.changeTick();
	    }
	else if (e.getActionCommand().equals("check_box"))
	    {
		threedresponse.changeGrid();
	    }
    }
    
    
    
    /*--------------------------------------------------------------------------------                     
      Routines for manipulating variables range
      --------------------------------------------------------------------------------*/  
    
    public void setRanges(float xi, float yi, float zi, 
			  float xa, float ya, float za) {
	xmin.setText(String.valueOf(xi));
	ymin.setText(String.valueOf(yi));
	zmin.setText(String.valueOf(zi));
	xmax.setText(String.valueOf(xa));
	ymax.setText(String.valueOf(ya));
	zmax.setText(String.valueOf(za));
    }
    
    public String getXMin() {
	return xmin.getText();
    }
    
    public String getYMin() {
	return ymin.getText();
    }
    
    public String getZMin() {
	return zmin.getText();
    }
    
    public String getXMax() {
	return xmax.getText();
    }
    
    public String getYMax() {
	return ymax.getText();
    }
    
    public String getZMax() {
	return zmax.getText();
    }
    
    /*--------------------------------------------------------------------------------                     
      Returns checkboxes status. Called ONLY by routines in surface applet
      --------------------------------------------------------------------------------*/  
    
    public boolean isBoxed() {
	return boxed.isSelected();
    }
    
    public boolean isMesh() {
	return mesh.isSelected();
    }
    
    public boolean isScaleBox() {
	return scalebox.isSelected();
    }
    
    public boolean isDisplayXY() {
	int i = tick_mode.getSelectedIndex();
	return ((i == 0) || (i == 1));
    }
    
    public boolean isDisplayZ() {
	int i = tick_mode.getSelectedIndex();
	return ((i == 0) || (i == 2));
    }
    
    public boolean isDisplayGrids() {
	return grids.isSelected();
    }
    
    /*--------------------------------------------------------------------------------                     
      Sets minimum value and maximum value of calculated values 
      --------------------------------------------------------------------------------*/  
    
    public void setMinimumResult(String value) {
	min.setText(value);
    }
    
    public void setMaximumResult(String value) {
	max.setText(value);
    }
    
    /*--------------------------------------------------------------------------------                     
      Gets / sets number of divisions 
      --------------------------------------------------------------------------------*/  
    
    public Dimension PreferredSize() {
	return new Dimension(220,450);  // initial canvas size
    }
    
    public int getCalcDivisions() {
	int value;
	
	try {
	    value = Integer.parseInt(CalcDivisions.getText());
	}
	catch(NumberFormatException e) {
	    value = -1;
	}
	if ((value < MIN_DIVISIONS) || (value > MAX_DIVISIONS)) {
	    value = DEF_DIVISIONS; 
	    CalcDivisions.setText("" + DEF_DIVISIONS);
	}
	return value; 
    }
    
    public int getDispDivisions() {
	int value;
	
	try {
	    value = Integer.parseInt(DispDivisions.getText());
	}
	catch(NumberFormatException e) {
	    value = -1;
	}
	if ((value < MIN_DIVISIONS) || (value > MAX_DIVISIONS)) {
	    value = DEF_DIVISIONS; 
	    DispDivisions.setText("" + DEF_DIVISIONS);
	}
	return value; 
    }
    
    public void setDispDivisions(String value) {
	DispDivisions.setText(value);
    }  
}
