OSPE_SlitPropPanel.java 5.93 KB
package osp.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JTextField;

import osp.FieldOfView;
import osp.Mask;
import osp.Slit;
import osp.utils.PropertiesPanel;

/**
 * This class represents the slit properties panel on the left of the visu
 * panel, displaying the various slit properties.
 */
public class OSPE_SlitPropPanel extends PropertiesPanel

{
	private static final long serialVersionUID = 1L;
	/** Title of the properties panel. */
	private static String title = " Slit Properties ";
	/** Number of the "position" field */
	public static final int posit = 0;
	/** Number of the "width" field */
	public static final int aperture = 1;
	/** Number of the "lambda min" field */
	public static final int lMin = 2;
	/** Number of the "lambda" field */
	public static final int lambda = 3;
	/** Number of the "lambda max" field */
	public static final int lMax = 4;
	/** Number of the "length" field */
	//public static final int length = 5;
	/** Number of the "object position" field */
//	public static final int posObj = 6;
	/** The visu panel - parent */
	private OSPE_VisuPanel visualPanel;
	/** The field of view */
	private FieldOfView fov;
	private double value = 0.0;
	private JTextField textFieldPos, textFieldWidth;
	private JComponent compPosit, compWidth;
	private StatusInterface statusInterface;

	/**
	 * Constructs a new slit properties panel with all the text fields
	 * corresponding to the properties.
	 * 
	 * @param p
	 *            : The visu panel of the application.
	 * @param statusInterface
	 * @param fov
	 */
	public OSPE_SlitPropPanel(final OSPE_VisuPanel p, FieldOfView fov,
			StatusInterface statusInterface) {
		this.visualPanel = p;
		this.fov = fov;
		this.statusInterface = statusInterface;

		setTitle(title);

		/* Add component Position */
		addTextField(posit, "Position (arcsec)");
		getJComponent(posit).setInputVerifier(new DoubleInputVerifier());

		compPosit = getJComponent(posit);
		textFieldPos = (JTextField) compPosit;

		textFieldPos.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				positionAction(e);
			}

		});

		addTextField(aperture, "Width (arcsec)");
		getJComponent(aperture).setInputVerifier(new DoubleInputVerifier());

		compWidth = getJComponent(aperture);

		textFieldWidth = (JTextField) compWidth;
		textFieldWidth.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				apertureAction(e);

			}

		}); // End Action Listener on aperture

		addTextField(lMin, "lambdaMin");
		getJComponent(lMin).setEnabled(false);
		addTextField(lambda, "lambda");
		getJComponent(lambda).setEnabled(false);
		addTextField(lMax, "lambdaMax");
		getJComponent(lMax).setEnabled(false);
//		addTextField(length, "length");
//		getJComponent(length).setEnabled(false);
//		addTextField(posObj, "Object Pos");
//		getJComponent(posObj).setEnabled(false);

	} // End OSPE_SlitPropPanel constructor

	/**
	 * Modification of slit position by typing on position slit propriety field
	 * 
	 * @param e
	 */
	private void positionAction(ActionEvent e) {
		Mask currentMask = fov.getCurrentMaskOfCurrentImage();

		JTextField tf = (JTextField) e.getSource();
		if (tf == getJComponent(posit)) // Slit Position
		{
			if (tf.getInputVerifier().verify(getJComponent(posit))) {
				value = Double.valueOf(tf.getText()).doubleValue();
				final Slit currentSlit = currentMask.getCurrentSlit();
				if (value > currentSlit.PosMax)
					value = currentSlit.PosMax;
				if (value < currentSlit.PosMin)
					value = currentSlit.PosMin;

				Double oldPosition = new Double(currentSlit.getPosition());
				if (oldPosition.doubleValue() != value) {
					currentMask.getHistory().addEntry("Change position of slit");
					currentMask.getHistory().addAction(
							"setPosition " + currentMask.getSelectedSlit(), oldPosition);
				}

				currentSlit.setPosition(value);
				visualPanel.isFromSlitComboBox = true;
				visualPanel.slide_position.setValue((int) (Math.floor(value)));
				currentMask.update();
				statusInterface.setStatusBarText(Color.black,
						"INFO: Slit Position value is " + value);
				visualPanel.updateByHand();
			} else
				statusInterface.setStatusBarText(Color.red,
						"WARNING: Slit Position value must be a double !!!");
		} // End if component(posit)
	}

	/**
	 * Modification of slit aperture by typing on aperture slit propriety field
	 * 
	 * @param e
	 */
	private void apertureAction(ActionEvent e) {

		Mask currentMask = fov.getCurrentMaskOfCurrentImage();

		JTextField tf = (JTextField) e.getSource();
		if (tf == getJComponent(aperture)) // Slit Aperture
		{
			if (tf.getInputVerifier().verify(getJComponent(aperture))) {
				value = Double.valueOf(tf.getText()).doubleValue();
				if (value > currentMask.getCurrentSlit().ApeMax)
					value = currentMask.getCurrentSlit().ApeMax;
				if (value < currentMask.getCurrentSlit().ApeMin)
					value = currentMask.getCurrentSlit().ApeMin;

				Double oldAperture = new Double(currentMask.getCurrentSlit().getAperture());
				if (oldAperture.doubleValue() != value) {
					currentMask.getHistory().addEntry("Change aperture of slit");
					currentMask.getHistory().addAction(
							"setAperture " + currentMask.getSelectedSlit(), oldAperture);
				}

				currentMask.getCurrentSlit().setAperture(value);
				visualPanel.isFromSlitComboBox = true;
				visualPanel.slide_aperture.setValue((int) (Math.floor(value)));
				currentMask.update();
				statusInterface.setStatusBarText(Color.black,
						"INFO: Slit Aperture value is " + value);
				visualPanel.updateByHand();
			} else
				statusInterface.setStatusBarText(Color.red,
						"WARNING: Slit Aperture value must be a double !!!");
		}
		visualPanel.isFromSlitComboBox = false;
	}

}