package triana.tools;

UWCC_HEADER

import triana.ocl.*;
import triana.types.*;
import triana.gui.windows.*;
import triana.gui.panels.*;
import java.awt.*;
import java.awt.event.*;
import triana.util.*;
import triana.gui.*;

/**
 * A NAME_OF_UNIT unit to ..
 *
 * @version VERSION DATE
 * @author AUTHOR
 */
public class NAME_OF_UNIT extends Unit {

    // some examples of parameters

    /**
     * The number of TextFields
     */
    public int textFields = TEXT_FIELDS;

    /**
     * The names of the textFields
     */
    public String[] names;

    /**
     * The UnitPanel for NAME_OF_UNIT
     */
    TextFieldPanel myPanel;

    /**
     **********************************************
     *** USER CODE of NAME_OF_UNIT goes here    ***
     **********************************************
     */
    public void process() throws Exception {         
        PROCESS_FUNCTION_CODE
        }

    /**
     * Initialses information specific to NAME_OF_UNIT. 
     */
    public void init() {
        super.init();

        setResizableInputs(INRESIZE);
        setResizableOutputs(OUTRESIZE);

        // set these to true if your unit can process double-precision
        // arrays
        setRequireDoubleInputs(false);
        setCanProcessDoubleArrays(false);

        textFields = TEXT_FIELDS;

        names = new String[textFields];

DEFINE_NAMES
        myPanel = new TextFieldPanel();
        myPanel.setObject(this, textFields, names);
        }

    /**
     * Called when the reset button is pressed within the MainTriana Window
     */
    public void reset() {
        super.reset();
        }

    /**
     * Called when the stop button is pressed within the MainTriana Window
     */
    public void stopping() {
        super.stopping();
        }

    /**
     * Called when the start button is pressed within the MainTriana Window
     */
    public void starting() {
        super.starting();
        }

    /**
     * Saves NAME_OF_UNIT parameters to the parameter file.
     */
    public void saveParameters() {
        saveParameter("fields", textFields);

        for(int i=0; i<textFields; ++i) {
            saveParameter("text" + toString(i), myPanel.getName(i));
            saveParameter("content" + toString(i), myPanel.getContents(i));
           }
        }

    /**
     * Used to set each of NAME_OF_UNIT parameters. The fields paramater
     * must be set first followed by each name and content parameter
     * in sequential order e.g. :- </p>
     *<pre> 
      unit.setParameter("fields", 2);
      unit.setParameter("text0", "1st name", "1st Parameter");
      unit.setParameter("text1", "2nd name", "2nd Parameter");
      </pre>
     *
     */
    public void setParameter(String name, String value) {
        if (name.equals("fields")) {
            textFields = strToInt(value);
            myPanel = new TextFieldPanel();
            myPanel.setObject(this, textFields);
            }

        if (name.indexOf("name") != -1) {  
            int n = getNumber(name);
            myPanel.setName(n, value);
            }

        if (name.indexOf("content") != -1) {  
            int n = getNumber(name);
            myPanel.setContents(n, value);
            if (n==(textFields-1))
                myPanel.layoutWindow();
            }
        }

    /**
     *
     * @return a string containing the names of the types allowed 
     * to be input to NAME_OF_UNIT, each separated by a white space.
     */
    public String inputTypes() {
        return "INPUT_TYPES";
        }

    /**
     * @return a string containing the names of the types output
     * from NAME_OF_UNIT, each separated by a white space.
     */
    public String outputTypes() {
        return "OUTPUT_TYPES";
        }

    /**
     * This returns a <b>brief!</b> description of what the unit does. The
     * text here is shown in a pop up window when the user puts the mouse
     * over the unit icon for more than a second.
     */
    public String getPopUpDescription() {
        return "Put NAME_OF_UNIT's brief description here";
        }

    /**
     *
     * @returns the location of the help file for this unit.  
     */
    public String getHelpFile() {
        return "HELP_FILE";
        }

    /**
     * @return NAME_OF_UNIT's parameter window sp that Grid 
     * can move and display it.
     */
    public UnitPanel myParameterPanel() {
        return myPanel;
        }

    /**
     * Captures the events thrown out by ScrollerWindow.
     */   
    public void actionPerformed(ActionEvent e) {
        super.actionPerformed(e);   // we need this
 
        if (e.getSource() instanceof TextField) {
            // there was an event in one of the textFields
            // do something if necessary ?
            }
        }
     }







