/* Copyright Dassault Systemes, 1999, 2009 */


package examples.development.components.mycommand;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.engineous.sdk.component.DefaultComponentEditor;
import com.engineous.sdk.exception.SDKException;
import com.engineous.sdk.gui.ProgressRange;
import com.engineous.sdk.model.DtComponent;
import com.engineous.sdk.model.DtScalarVariable;
import com.engineous.sdk.model.exceptions.DtModelException;
import com.engineous.sdk.vars.VariableException;

/**
 * Editor for the MyCommand example component.
 * This component behaves as an OSCommand with a very simplified interface.
 * The editor has checkboxes for selecting different program options.
 * The input and output files are fixed, and are always passed on the command line as simple file names
 * (not paths).
 *
 */
public class MyCommandEditor
		extends DefaultComponentEditor {

	/** The text fields for entering the Windows and Unix commmand names. */
	protected JTextField winCommandField, unixCommandField;

	/** String Properties to store the two edit commands in. */
	protected DtScalarVariable winProperty, unixProperty;

	/**
	 * Set up the editor UI.  DefaultComponentEditor extends JPanel, so just pack components into the panel.
	 * This is called exactly once in the life of the DtComponent.
	 * @param myComponent The MyCommand DtComponent
	 * @param progress A Progress range.  Is mostly ignored.
	 * @throws SDKException
	 * @see com.engineous.sdk.component.DefaultComponentEditor#initEditor(com.engineous.sdk.model.DtComponent, com.engineous.sdk.gui.ProgressRange)
	 */
	public void initEditor(DtComponent myComponent, ProgressRange progress)
			throws SDKException {

		// You must call super here in order to initialize the AbstraceComponentEditor superclass.
		// The superclass saves the component in member 'component'.
		super.initEditor(myComponent, progress);

		try {
			// Get the properties of this Component and save them for later use.
			winProperty = (DtScalarVariable)myComponent.getProperty("wincommand");
			unixProperty = (DtScalarVariable)myComponent.getProperty("unixcommand");
		}
		catch (DtModelException ex) {
			// A Model exception here is fatal.
			throw new SDKException(ex, "Required property is missing.");
		}

		// The Editor UI consists of a top label and two text boxes for the Windows and Unix commands,
		// each with their own label.
		JLabel topLabel = new JLabel("File Parameter Text Editor\n" + "Edits the file parameter 'inputFile' and saves as 'outputFile'\n"
			+ "The graphical editor command to use on Windows and Unix is set below.", SwingConstants.CENTER);
		JLabel winLabel = new JLabel("Windows command");
		JLabel unixLabel = new JLabel("Unix command");
		winCommandField = new JTextField();
		unixCommandField = new JTextField();

		// Lay out the component using GridBagLayout
		this.removeAll();
		this.setLayout(new GridBagLayout());
		this.add(topLabel,
				 new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(winLabel,
				 new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 5, 2, 2), 0, 0));
		this.add(winCommandField,
				 new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 5), 0, 0));
		this.add(unixLabel,
				 new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 5, 2, 2), 0, 0));
		this.add(unixCommandField,
				 new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 5), 0, 0));

	}

	/**
	 * Called just before the editor is displayed.  This may be called many times.
	 * It should initialize the editor GUI to match the current state of properties and parameters.
	 * @throws SDKException
	 * @see com.engineous.sdk.component.DefaultComponentEditor#open()
	 */
	public void open()
			throws SDKException {

		super.open();

		try {
			// Set the text fields based on the properties.
			winCommandField.setText(winProperty.getValueObj().getAsString());
			unixCommandField.setText(unixProperty.getValueObj().getAsString());
		}
		catch (VariableException ex) {
			// Again, any error is fatal.
			throw new SDKException(ex, "Unable to get value of comamand property");
		}

	}

	/**
	 * Save the state of the editor back into the component Properties.
	 * @param willClose
	 * @throws SDKException
	 * @see com.engineous.desktop.sdk.AbstractDesktopEditor#apply(boolean)
	 */
	public void apply(boolean willClose)
			throws SDKException {

		super.apply(willClose);

		try {
			winProperty.getValueObj().setValue(winCommandField.getText().trim());
			unixProperty.getValueObj().setValue(unixCommandField.getText().trim());
		}
		catch (VariableException ex) {
			// Again, any error is fatal.
			throw new SDKException(ex, "Unable to set value of comamand property");
		}

	}
}

