<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package algae.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import java.io.*;
import java.util.*;
import java.util.Date;
import java.text.DateFormat;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.*;
import algae.simulation.*;
import algae.gui.*;

class TabbedListener implements ChangeListener {
	public void stateChanged(ChangeEvent e) 
	{
	       	 JTabbedPane source = (JTabbedPane)e.getSource();         
	       	 System.out.println("Another TabbedPane-panel selected...");
        }
}

public final class SettingsDialog extends JFrame implements ActionListener{
    public MainGUI window=null;  // the main simulation window
    JButton OKBtn = new JButton("OK");
    JButton QuitBtn = new JButton("Quit");
    JButton SaveBtn = new JButton("Save");
    JButton LoadBtn = new JButton("Load");
    String []presets={"-----"};
    JComboBox PresetsCombo = new JComboBox(presets);
    
    JPanel SimulationPanel = new JPanel();
    JPanel AlgaesPanel = new JPanel();
    JPanel SensorsPanel = new JPanel();
    JPanel ActionPanel = new JPanel();

    JPanel ActionTopPanel = new JPanel();
    JPanel ActionBottomPanel = new JPanel();

    JTabbedPane TabbedPane = new JTabbedPane();
    
    Container contentPane;

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    GridBagLayout gridbag2 = new GridBagLayout();
    GridBagConstraints c2 = new GridBagConstraints();

    JPanel TopPanel = new JPanel();
    JPanel BottomPanel = new JPanel();

    JTextField DurationTF = new JTextField(5);
    JTextField WidthTF = new JTextField(3);
    JTextField HeightTF = new JTextField(3);

    JTextField IntensityTF = new JTextField(5);

    JTextField SubstrateAmountTF = new JTextField(5);
    JTextField SubstrateRenewTF = new JTextField(5);
    JTextField PheroDecreaseTF = new JTextField(5);

    JTextField InitialAlgaesTF = new JTextField(5);
    JTextField InitialEnergyTF = new JTextField(5);

    JTextField RandomSeedTF = new JTextField(5);

    JTextField PheromoneFOVTF = new JTextField(5);
    JTextField PheromoneRangeTF = new JTextField(5);
    JTextField SubstrateFOVTF = new JTextField(5);
    JTextField SubstrateRangeTF = new JTextField(5);
    JTextField LightFOVTF = new JTextField(5);
    JTextField LightRangeTF = new JTextField(5);
    JTextField BumperRangeTF = new JTextField(5);

    JTextField EnergyMovementTF = new JTextField(5);
    JTextField EnergyMitoseTF = new JTextField(5);
    JTextField EnergyMeioseTF = new JTextField(5);
    JTextField EnergyPOutputTF = new JTextField(5);
    JTextField EnergySubstrateTF = new JTextField(5);
    JTextField EnergyLightTF = new JTextField(5);
    
    JRadioButton AllTheTimeRB = new JRadioButton("All the time");
    JRadioButton NeverRB = new JRadioButton("Never");
    JRadioButton FunctionRB = new JRadioButton("Function");
    ButtonGroup enabledGroup = new ButtonGroup();    

    JButton SelectDistFuncBtn = new JButton("Select distribution function...");
    JButton SelectLightFuncBtn = new JButton("Select light cycle function...");
	
    XmlDocument xmldoc=new XmlDocument();

    Settings config; // holds configuration for the current session

    public boolean iAmReady = false;

    // throw a debug message to System.out
    public void dm(String s)
    {
	System.out.println(s);
    }

    public Settings StoreSettingsToVar()
    {
	Settings c=new Settings();
	// simulation settings
	c.duration=java.lang.Integer.parseInt(DurationTF.getText());
	c.width=java.lang.Integer.parseInt(WidthTF.getText());
	c.height=java.lang.Integer.parseInt(HeightTF.getText());
	
	if (AllTheTimeRB.isSelected())
    	    c.light_enabled=1;	
    	else  
	if (NeverRB.isSelected())
    	    c.light_enabled=0;	
    	else  
	if (FunctionRB.isSelected())
    	    c.light_enabled=2;

	c.light_intensity=java.lang.Float.parseFloat(IntensityTF.getText());
	c.light_function=-1; // not implemented yet
	c.light_cycle_function=-1; // not implemented yet

	c.randSeed = java.lang.Integer.parseInt(RandomSeedTF.getText());

	c.substrate_amount=java.lang.Integer.parseInt(SubstrateAmountTF.getText());
	c.substrate_renew_rate=java.lang.Float.parseFloat(SubstrateRenewTF.getText());
	c.pheromone_dec_rate=java.lang.Float.parseFloat(PheroDecreaseTF.getText());

	// algaes settings
	c.initial_algaes=java.lang.Integer.parseInt(InitialAlgaesTF.getText());
	c.initial_energy=java.lang.Float.parseFloat(InitialEnergyTF.getText());

	// sensors settings
	c.pheromone_fov=java.lang.Integer.parseInt(PheromoneFOVTF.getText());
	c.pheromone_range=java.lang.Integer.parseInt(PheromoneRangeTF.getText());
	c.substrate_fov=java.lang.Integer.parseInt(SubstrateFOVTF.getText());
	c.substrate_range=java.lang.Integer.parseInt(SubstrateRangeTF.getText());	
	c.light_fov=java.lang.Integer.parseInt(LightFOVTF.getText());
	c.light_range=java.lang.Integer.parseInt(LightRangeTF.getText());
	c.bumper_range=java.lang.Integer.parseInt(BumperRangeTF.getText());

   	// actions settings
        c.energy_movement=java.lang.Float.parseFloat(EnergyMovementTF.getText());
	c.energy_mitose=java.lang.Float.parseFloat(EnergyMitoseTF.getText());
	c.energy_meiose=java.lang.Float.parseFloat(EnergyMeioseTF.getText());
	c.energy_pheromone_output=java.lang.Float.parseFloat(EnergyPOutputTF.getText());
	c.energy_substrate=java.lang.Float.parseFloat(EnergySubstrateTF.getText());
	c.energy_light=java.lang.Float.parseFloat(EnergyLightTF.getText());

	return c;
    }

    public void MakeSimulationPanel()
    {
	GridBagLayout lBag = new GridBagLayout();
	GridBagConstraints lc = new GridBagConstraints();
	SimulationPanel.setLayout(lBag);
	
	// Duration stuff
	JLabel DurationLabel=new JLabel("Duration");
	lc.gridx = 1;
        lc.gridy = 1;
        lc.gridwidth=5;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(DurationLabel, lc);
	SimulationPanel.add(DurationLabel);

	lc.gridx = 1;
        lc.gridy = 2;
        lc.gridwidth=4;
        lc.gridheight=1;
        lBag.setConstraints(DurationTF, lc);
	SimulationPanel.add(DurationTF);

	JLabel StepsLabel = new JLabel("steps");
	lc.gridx = 5;
        lc.gridy = 2;
        lc.gridwidth=2;
        lc.gridheight=1;
        lBag.setConstraints(StepsLabel, lc);
	SimulationPanel.add(StepsLabel);

	// Dimension stuff
	JLabel DimensionLabel = new JLabel("Dimension");
	lc.gridx = 11;
        lc.gridy = 1;
        lc.gridwidth=4;
        lc.gridheight=1;
        lBag.setConstraints(DimensionLabel, lc);
	SimulationPanel.add(DimensionLabel);
	
	JLabel WidthLabel = new JLabel("Width");
	lc.gridx = 11;
	lc.gridy = 2;
	lc.gridwidth = 2;
	lc.gridheight = 1;
	lBag.setConstraints(WidthLabel, lc);
	SimulationPanel.add(WidthLabel);

	lc.gridx = 13;
	lc.gridy = 2;
	lc.gridwidth = 3;
	lc.gridheight = 1;
	lBag.setConstraints(WidthTF, lc);
	SimulationPanel.add(WidthTF);

	JLabel HeightLabel = new JLabel("Height");
	lc.gridx = 16;
	lc.gridy = 2;
	lc.gridwidth = 3;
	lc.gridheight = 1;
	lBag.setConstraints(HeightLabel, lc);
	SimulationPanel.add(HeightLabel);

	lc.gridx = 20;
	lc.gridy = 2;
	lc.gridwidth = 3;
	lc.gridheight = 1;
	lBag.setConstraints(HeightTF, lc);
	SimulationPanel.add(HeightTF);

	// light stuff
	JPanel LightPanel = new JPanel();
	GridBagLayout lightBag = new GridBagLayout();
	GridBagConstraints llc = new GridBagConstraints();
	LightPanel.setLayout(lightBag);
	
	lc.gridx = 0;
        lc.gridy = 4;
        lc.gridwidth=24;
        lc.gridheight=7;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(0,10,0,10);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(LightPanel, lc);
	SimulationPanel.add(LightPanel);

   	TitledBorder lightBorder= BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Light settings");
	LightPanel.setBorder(lightBorder);

       	TitledBorder enabledBorder= BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Enabled");
	JPanel EnabledPanel = new JPanel();
	EnabledPanel.setLayout(new GridLayout(3,1));
	EnabledPanel.setBorder(enabledBorder);

	AllTheTimeRB.setSelected(true);
	enabledGroup.add(AllTheTimeRB);
	enabledGroup.add(NeverRB);
	enabledGroup.add(FunctionRB);

	EnabledPanel.add(AllTheTimeRB);
	EnabledPanel.add(NeverRB);
	EnabledPanel.add(FunctionRB);

	llc.gridx = 1;
        llc.gridy = 1;
        llc.gridwidth=9;
        llc.gridheight=5;
        llc.weightx=0;
        llc.weighty=0;
        llc.insets=new Insets(5,5,5,5);
        llc.fill = GridBagConstraints.BOTH;
        lightBag.setConstraints(EnabledPanel, llc);
	LightPanel.add(EnabledPanel);

	JLabel IntensityLabel = new JLabel("Intensity");
	llc.gridx = 10;
        llc.gridy = 2;
        llc.gridwidth=5;
        llc.gridheight=1;
        llc.weightx=0;
        llc.weighty=0;
        llc.insets=new Insets(5,5,5,5);
        llc.fill = GridBagConstraints.BOTH;
        lightBag.setConstraints(IntensityLabel, llc);
	LightPanel.add(IntensityLabel);

	llc.gridx = 15;
        llc.gridy = 2;
        llc.gridwidth=5;
        llc.gridheight=1;
        llc.weightx=0;
        llc.weighty=0;
        llc.insets=new Insets(5,5,5,5);
        llc.fill = GridBagConstraints.BOTH;
        lightBag.setConstraints(IntensityTF, llc);
	LightPanel.add(IntensityTF);

	llc.gridx = 10;
        llc.gridy = 4;
        llc.gridwidth=14;
        llc.gridheight=1;
        llc.weightx=0;
        llc.weighty=0;
        llc.insets=new Insets(5,5,5,5);
        llc.fill = GridBagConstraints.BOTH;
        lightBag.setConstraints(SelectDistFuncBtn, llc);
	LightPanel.add(SelectDistFuncBtn);

	llc.gridx = 10;
        llc.gridy = 5;
        llc.gridwidth=14;
        llc.gridheight=1;
        llc.weightx=0;
        llc.weighty=0;
        llc.insets=new Insets(5,5,5,5);
        llc.fill = GridBagConstraints.BOTH;
        lightBag.setConstraints(SelectLightFuncBtn, llc);
	LightPanel.add(SelectLightFuncBtn);

	JLabel SubstrateAmountLabel = new JLabel("Substrate amount");
	lc.gridx = 0;
        lc.gridy = 11;
        lc.gridwidth=9;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(SubstrateAmountLabel, lc);
	SimulationPanel.add(SubstrateAmountLabel);

	lc.gridx = 10;
        lc.gridy = 11;
        lc.gridwidth=5;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(SubstrateAmountTF , lc);
	SimulationPanel.add(SubstrateAmountTF);

	JLabel SubstrateRenewLabel = new JLabel("Substrate renew rate");
	lc.gridx = 0;
        lc.gridy = 12;
        lc.gridwidth=10;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(SubstrateRenewLabel, lc);
	SimulationPanel.add(SubstrateRenewLabel);

	lc.gridx = 10;
        lc.gridy = 12;
        lc.gridwidth=5;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(SubstrateRenewTF, lc);
	SimulationPanel.add(SubstrateRenewTF);

	JLabel PheroDecLabel = new JLabel("Pheromone decrease rate");
	lc.gridx = 0;
        lc.gridy = 14;
        lc.gridwidth=10;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(PheroDecLabel, lc);
	SimulationPanel.add(PheroDecLabel);

	lc.gridx = 10;
        lc.gridy = 14;
        lc.gridwidth=5;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(PheroDecreaseTF, lc);
	SimulationPanel.add(PheroDecreaseTF);

	JLabel RandSeedLabel = new JLabel("Random seed");
	lc.gridx = 0;
        lc.gridy = 16;
        lc.gridwidth=10;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(RandSeedLabel, lc);
	SimulationPanel.add(RandSeedLabel);

	lc.gridx = 10;
        lc.gridy = 16;
        lc.gridwidth=5;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(RandomSeedTF, lc);
	SimulationPanel.add(RandomSeedTF);
    }

    public void MakeAlgaesPanel()
    {
	GridBagLayout lBag = new GridBagLayout();
	GridBagConstraints lc = new GridBagConstraints();
	AlgaesPanel.setLayout(lBag);
	
	// Number of initial Algaes - stuff
	JLabel InitialAlgaesLabel=new JLabel("# of initial Algaes");
	lc.gridx = 1;
        lc.gridy = 1;
        lc.gridwidth=8;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(InitialAlgaesLabel, lc);
	AlgaesPanel.add(InitialAlgaesLabel);

	lc.gridx = 1;
        lc.gridy = 2;
        lc.gridwidth=4;
        lc.gridheight=1;
        lBag.setConstraints(InitialAlgaesTF, lc);
	AlgaesPanel.add(InitialAlgaesTF);

	// initial Energylevel  - stuff
	JLabel InitialEnergyLabel=new JLabel("Initial Energylevel");
	lc.gridx = 11;
        lc.gridy = 1;
        lc.gridwidth=10;
        lc.gridheight=1;
        lc.weightx=0;
        lc.weighty=0;
        lc.insets=new Insets(5,5,5,5);
        lc.fill = GridBagConstraints.BOTH;
        lBag.setConstraints(InitialEnergyLabel, lc);
	AlgaesPanel.add(InitialEnergyLabel);

	lc.gridx = 11;
        lc.gridy = 2;
        lc.gridwidth=4;
        lc.gridheight=1;
        lBag.setConstraints(InitialEnergyTF, lc);
	AlgaesPanel.add(InitialEnergyTF);
    }

    public void MakeSensorsPanel()
    {
        SensorsPanel.setLayout(new GridLayout(5,3));
	
	// Number of initial Algaes - stuff
	JLabel FOVLabel=new JLabel("FOV");
	JLabel RangeLabel=new JLabel("Range");
	JLabel PheromoneLabel=new JLabel("Pheromone");
	JLabel SubstrateLabel=new JLabel("Substrate");
	JLabel LightLabel=new JLabel("Light");
	JLabel EmptyLabel=new JLabel("  ");	
        JLabel EmptyLabel2=new JLabel("  "); 
        JLabel BumperLabel=new JLabel("Bumper");

	SensorsPanel.add(EmptyLabel);
	SensorsPanel.add(FOVLabel);
	SensorsPanel.add(RangeLabel);

	SensorsPanel.add(PheromoneLabel);
	SensorsPanel.add(PheromoneFOVTF);
	SensorsPanel.add(PheromoneRangeTF);

	SensorsPanel.add(SubstrateLabel);
	SensorsPanel.add(SubstrateFOVTF);
	SensorsPanel.add(SubstrateRangeTF);

	SensorsPanel.add(LightLabel);
	SensorsPanel.add(LightFOVTF);
	SensorsPanel.add(LightRangeTF);

        SensorsPanel.add(BumperLabel);
        SensorsPanel.add(EmptyLabel2);
        SensorsPanel.add(BumperRangeTF);
    }

    public void MakeActionsPanel()
    {
	ActionPanel.setLayout(gridbag2);

	// build up ...
	ActionTopPanel.setLayout(new GridLayout(4,2));
	ActionBottomPanel.setLayout(new GridLayout(2,2));
	
	//ActionPanel.add(ActionTopPanel,BorderLayout.NORTH);
	//ActionPanel.add(ActionBottomPanel,BorderLayout.SOUTH);

        TitledBorder TopBorder= BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Energy consumption");
	TitledBorder BottomBorder= BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Energy collecting");

	ActionTopPanel.setBorder(TopBorder);
	ActionBottomPanel.setBorder(BottomBorder);

	JLabel EmptyLabel = new JLabel("  ");
	JLabel MovementLabel = new JLabel("Movement");
	JLabel MitoseLabel = new JLabel("Mitosis");
	JLabel MeioseLabel = new JLabel("Meiosis");
	JLabel PheromoneLabel = new JLabel("Pheromone output");
	JLabel SubstrateLabel = new JLabel("Substrate");
	JLabel LightLabel = new JLabel("Light");

	ActionTopPanel.add(MovementLabel);
	ActionTopPanel.add(EnergyMovementTF);
	ActionTopPanel.add(MitoseLabel);
	ActionTopPanel.add(EnergyMitoseTF);
	ActionTopPanel.add(MeioseLabel);
	ActionTopPanel.add(EnergyMeioseTF);
	ActionTopPanel.add(PheromoneLabel);
	ActionTopPanel.add(EnergyPOutputTF);

	ActionBottomPanel.add(SubstrateLabel);
	ActionBottomPanel.add(EnergySubstrateTF);
	ActionBottomPanel.add(LightLabel);
	ActionBottomPanel.add(EnergyLightTF);

	c2.gridx = 0;
        c2.gridy = 0;
        c2.gridwidth=10;
        c2.gridheight=5;
        c2.weightx=1;
        c2.weighty=1;
        c2.insets=new Insets(2,2,2,2);
        c2.fill = GridBagConstraints.BOTH;
        gridbag2.setConstraints(ActionTopPanel, c2);
        ActionPanel.add(ActionTopPanel);

	c2.gridx = 0;
        c2.gridy = 5;
        c2.gridwidth=10;
        c2.gridheight=5;
        c2.weightx=1;
        c2.weighty=1;
        c2.insets=new Insets(2,2,2,2);
        c2.fill = GridBagConstraints.BOTH;
        gridbag2.setConstraints(ActionBottomPanel, c2);
        ActionPanel.add(ActionBottomPanel);	

    }

    /*
    public void mouseClicked(MouseEvent e) {
 	Object src=e.getSource();

  	if (src==TabbedPane)
  	{
  	    System.out.println("Something ;)\n");
   	}
    } 
    */
    
    public void ExportSimulationSettings(XmlDocument xmldoc, ElementNode XML)
    {
    	ElementNode ne = (ElementNode) xmldoc.createElement("Simulation");
    	XML.appendChild(ne);
    	
    	ne.setAttribute("duration",DurationTF.getText());
    	ne.setAttribute("width",WidthTF.getText());
    	ne.setAttribute("height",HeightTF.getText());
    	
    	ne.setAttribute("randseed",RandomSeedTF.getText());
    	    	
    	if (AllTheTimeRB.isSelected())
    	    ne.setAttribute("light_enabled","all the time");	
    	else  
	if (NeverRB.isSelected())
    	    ne.setAttribute("light_enabled","never");	
    	else  
	if (FunctionRB.isSelected())
    	    ne.setAttribute("light_enabled","function");	
    		
	ne.setAttribute("light_intensity",IntensityTF.getText());    	    	
	
	ne.setAttribute("light_distribution_func","not implemented yet");
	ne.setAttribute("light_cycle_func","not implemented yet");
	
	ne.setAttribute("substrate_amount",SubstrateAmountTF.getText());
	ne.setAttribute("substrate_renew_rate",SubstrateRenewTF.getText());
    	ne.setAttribute("pheromone_dec_rate",PheroDecreaseTF.getText());
    }
    
    public void ExportAlgaesSettings(XmlDocument xmldoc, ElementNode XML)
    {
    	ElementNode ne = (ElementNode) xmldoc.createElement("Algaes");
    	XML.appendChild(ne);
    	
    	ne.setAttribute("initial_algaes",InitialAlgaesTF.getText());
    	ne.setAttribute("initial_energy",InitialEnergyTF.getText());
    }
    
    public void ExportSensorsSettings(XmlDocument xmldoc, ElementNode XML)
    {
    	ElementNode ne = (ElementNode) xmldoc.createElement("Sensors");
    	XML.appendChild(ne);
    	
    	ne.setAttribute("pheromone_fov",PheromoneFOVTF.getText());
    	ne.setAttribute("pheromone_range",PheromoneRangeTF.getText());
    	
    	ne.setAttribute("substrate_fov",SubstrateFOVTF.getText());
    	ne.setAttribute("substrate_range",SubstrateRangeTF.getText());
    	
    	ne.setAttribute("light_fov",LightFOVTF.getText());
    	ne.setAttribute("light_range",LightRangeTF.getText());
    	
    	ne.setAttribute("bumper_range",BumperRangeTF.getText());
    }
        
    public void ExportActionsSettings(XmlDocument xmldoc, ElementNode XML)
    {
    	ElementNode ne = (ElementNode) xmldoc.createElement("Actions");
    	XML.appendChild(ne);
    
	ne.setAttribute("energy_movement",EnergyMovementTF.getText());
	ne.setAttribute("energy_mitose",EnergyMitoseTF.getText());
	ne.setAttribute("energy_meiose",EnergyMeioseTF.getText());    
    	ne.setAttribute("energy_pheromone_output",EnergyPOutputTF.getText());
	ne.setAttribute("energy_substrate",EnergySubstrateTF.getText());
    	ne.setAttribute("energy_light",EnergyLightTF.getText());
    }
            
    public void ExportSettingsFile()
    {
	JFileChooser saveas_dialog = new JFileChooser("." + java.io.File.separator + "models"); // Should go to a global variable ?!?
        saveas_dialog.setFileFilter(new XMLFileFilter());
         
        if (saveas_dialog.showDialog(new JFrame(),"Save as...") != JFileChooser.APPROVE_OPTION) 
            return;
            
        File xmlfile = saveas_dialog.getSelectedFile();
	
	XmlDocument xmldoc=new XmlDocument(); 
       	
       	ElementNode xmldata = (ElementNode) xmldoc.createElement ("Settingsblock");
	
	Date today = new Date();
	DateFormat df;
	
	xmldata.setAttribute ("version","v1.0a");
        xmldata.setAttribute ("creator","ALGAE preBeta");
        xmldata.setAttribute ("name",xmlfile.getName());
        xmldata.setAttribute ("date",DateFormat.getDateTimeInstance().format(today));  
                
        ExportSimulationSettings(xmldoc,xmldata);
        ExportAlgaesSettings(xmldoc,xmldata);
        ExportSensorsSettings(xmldoc,xmldata);
        ExportActionsSettings(xmldoc,xmldata);
        
	// Write the stuff peviously created
        try  
        {
            FileOutputStream ostream  = new FileOutputStream(xmlfile);
            OutputStreamWriter owrite = new OutputStreamWriter(ostream);
            owrite.write("&lt;?xml version=\"1.0\"?&gt;\n\r");
            owrite.write("&lt;!DOCTYPE Element_Definitions SYSTEM \"Element_Definitions.dtd\"&gt;\n\r");
            xmldata.write(owrite);
            owrite.flush();
            ostream.close();
        }
        catch (IOException ex) 
        {
            System.out.println("ERROR at Export: IOException writing to " + xmlfile.getAbsolutePath());
        };
    } 

    public void ImportSimulationSettings(org.w3c.dom.Node xn)
    {
    	DurationTF.setText(xn.getAttributes().getNamedItem("duration").getNodeValue());
	WidthTF.setText(xn.getAttributes().getNamedItem("width").getNodeValue());
	HeightTF.setText(xn.getAttributes().getNamedItem("height").getNodeValue());
    	
    	RandomSeedTF.setText(xn.getAttributes().getNamedItem("randseed").getNodeValue());
    	    	
    	AllTheTimeRB.setSelected(false);
    	NeverRB.setSelected(false);
    	FunctionRB.setSelected(false);    	
    	
    	if (xn.getAttributes().getNamedItem("light_enabled").getNodeValue().equals("all the time"))
    	    AllTheTimeRB.setSelected(true);
    	if (xn.getAttributes().getNamedItem("light_enabled").getNodeValue().equals("never"))
    	    NeverRB.setSelected(true);
    	if (xn.getAttributes().getNamedItem("light_enabled").getNodeValue().equals("function"))
    	    FunctionRB.setSelected(true);
    	
    	IntensityTF.setText(xn.getAttributes().getNamedItem("light_intensity").getNodeValue());
    	
	//ne.setAttribute("light_distribution_func","not implemented yet");
	//ne.setAttribute("light_cycle_func","not implemented yet");
	
	SubstrateAmountTF.setText(xn.getAttributes().getNamedItem("substrate_amount").getNodeValue());
    	SubstrateRenewTF.setText(xn.getAttributes().getNamedItem("substrate_renew_rate").getNodeValue());
    	PheroDecreaseTF.setText(xn.getAttributes().getNamedItem("pheromone_dec_rate").getNodeValue());
    }

    public void ImportAlgaesSettings(org.w3c.dom.Node xn)
    {
    	InitialAlgaesTF.setText(xn.getAttributes().getNamedItem("initial_algaes").getNodeValue());
    	InitialEnergyTF.setText(xn.getAttributes().getNamedItem("initial_energy").getNodeValue());
    }

    public void ImportActionsSettings(org.w3c.dom.Node xn)
    {
    	EnergyMovementTF.setText(xn.getAttributes().getNamedItem("energy_movement").getNodeValue());
    	EnergyMitoseTF.setText(xn.getAttributes().getNamedItem("energy_mitose").getNodeValue());
    	EnergyMeioseTF.setText(xn.getAttributes().getNamedItem("energy_meiose").getNodeValue());
    	EnergyPOutputTF.setText(xn.getAttributes().getNamedItem("energy_pheromone_output").getNodeValue());
    	EnergySubstrateTF.setText(xn.getAttributes().getNamedItem("energy_substrate").getNodeValue());
    	EnergyLightTF.setText(xn.getAttributes().getNamedItem("energy_light").getNodeValue());
    }

    public void ImportSensorsSettings(org.w3c.dom.Node xn)
    {
    	PheromoneFOVTF.setText(xn.getAttributes().getNamedItem("pheromone_fov").getNodeValue());
    	PheromoneRangeTF.setText(xn.getAttributes().getNamedItem("pheromone_range").getNodeValue());
    	
    	SubstrateFOVTF.setText(xn.getAttributes().getNamedItem("substrate_fov").getNodeValue());
    	SubstrateRangeTF.setText(xn.getAttributes().getNamedItem("substrate_range").getNodeValue());
    	
    	LightFOVTF.setText(xn.getAttributes().getNamedItem("light_fov").getNodeValue());
    	LightRangeTF.setText(xn.getAttributes().getNamedItem("light_range").getNodeValue());
    	
    	BumperRangeTF.setText(xn.getAttributes().getNamedItem("bumper_range").getNodeValue());
    }

    public void DialogImportSettingsFile()
    {
    	JFileChooser open_dialog = new JFileChooser("." + java.io.File.separator + "models"); // Should go to a global variable ?!?
        open_dialog.setFileFilter(new XMLFileFilter());
         
        if (open_dialog.showDialog(new JFrame(),"Load file...") != JFileChooser.APPROVE_OPTION) 
            return;
            
        String fileName = open_dialog.getSelectedFile().getName();
        ImportSettingsFile(fileName);        
    }

    public void ImportSettingsFile(String s)
    {
        File xmlfile = new File(s);//

    	System.out.println("Trying to parse \""+s+"\"");
    	
        XmlDocument xmldoc = new XmlDocument();
        // Load the File
        try 
        {
            InputSource xmlinput = Resolver.createInputSource (xmlfile);
            xmldoc = XmlDocument.createXmlDocument (xmlinput, false);
        }
         
        // Do finer catches !!!!!
         
        catch (SAXParseException err) 
        {   
               System.out.println ("** Parsing error" 
                                   + ", line " + err.getLineNumber ()
                                   + ", uri " + err.getSystemId ());
               System.out.println("   " + err.getMessage ());
        } 
         
        catch (SAXException ex) 
        {
            Exception   x = ex.getException ();
            ((x == null) ? ex : x).printStackTrace ();
        }
            
        catch (Throwable t) 
        {
            t.printStackTrace ();
        }   
      	
      	//// get Root XmlNode
        org.w3c.dom.Node xmlRoot = xmldoc.getDocumentElement();
        NodeList rootChilds = xmlRoot.getChildNodes();
        System.out.println("Rootnodename = "+xmlRoot.getNodeName());
        for (int i=0; i&lt;rootChilds.getLength(); i++) 
        {
            if (rootChilds.item(i).getNodeName().equals("Algaes")) 
            {
            	ImportAlgaesSettings(rootChilds.item(i));
               	System.out.println("Algaes-subsection imported...");
            }
            else 
            if (rootChilds.item(i).getNodeName().equals("Actions")) 
            {
            	ImportActionsSettings(rootChilds.item(i));
               	System.out.println("Actions-subsection found...");
            }
            else 
            if (rootChilds.item(i).getNodeName().equals("Simulation")) 
            {
            	ImportSimulationSettings(rootChilds.item(i));
               	System.out.println("Simulation-subsection found...");
            }
            else 
            if (rootChilds.item(i).getNodeName().equals("Sensors")) 
            {
            	ImportSensorsSettings(rootChilds.item(i));
               	System.out.println("Sensors-subsection found...");
            }
            else System.out.println("Import: Unknown node in XML file (" + rootChilds.item(i).getNodeName() + ")!");
	};
    }
    
    public void msgErr(String s)
    {
    	JOptionPane.showMessageDialog(this, s, "Input error", JOptionPane.ERROR_MESSAGE);
    }
    
    public boolean VerifySettings()
    {
    	int dummyInt=0;
    	float dummyFloat=0;
               
        try {	
    	    dummyInt=java.lang.Integer.parseInt(DurationTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Duration is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Duration is a bad number (Integer&gt;0)");
            return false;
        }
        
        try {	
    	    dummyInt=java.lang.Integer.parseInt(WidthTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Width is a bad number (Integer&gt;=10)");
            return false;
        }
        if (dummyInt&lt;10)
        {
            msgErr("Width is a bad number (Integer&gt;=10)");
            return false;
        }
        
        try {	
    	    dummyInt=java.lang.Integer.parseInt(HeightTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Height is a bad number (Integer&gt;=10)");
            return false;
        }
        if (dummyInt&lt;10)
        {
            msgErr("Height is a bad number (Integer&gt;=10)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(IntensityTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Light intensity is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Light intensity is a bad number (Float&gt;0)");
            return false;
        }
        
        try {	
    	    dummyInt=java.lang.Integer.parseInt(SubstrateAmountTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Substrate amount is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Substrate amount is a bad number (Integer&gt;0)");
            return false;
        }
    
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(SubstrateRenewTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Substrate renew rate is a bad number (1&gt;Float&gt;0)");
            return false;
        }
        if ((dummyFloat&lt;=0)||(dummyFloat&gt;=1))
        {
            msgErr("Substrate renew rate is a bad number (1&gt;Float&gt;0)");
            return false;
        }
    
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(PheroDecreaseTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Pheromone decrease rate is a bad number (1&gt;Float&gt;0)");
            return false;
        }
        if ((dummyFloat&lt;=0)||(dummyFloat&gt;=1))
        {
            msgErr("Pheromone decrease renew rate is a bad number (1&gt;Float&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(InitialAlgaesTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Initial algaes is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Initial algaes is a bad number (Integer&gt;0)");
            return false;
        }
    
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(InitialEnergyTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Initial energy is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Initial energy is a bad number (Float&gt;0)");
            return false;
        }
    
    	try {	
    	    dummyInt=java.lang.Integer.parseInt(RandomSeedTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Random seed is a bad number (Integer&gt;=0)");
            return false;
        }
        if (dummyInt&lt;0)
        {
            msgErr("Random seed is a bad number (Integer&gt;=0)");
            return false;
        }
    
    	try {	
    	    dummyInt=java.lang.Integer.parseInt(PheromoneFOVTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Pheromone FOV is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Pheromone FOV is a bad number (Integer&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(PheromoneRangeTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Pheromone range is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Pheromone range is a bad number (Integer&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(SubstrateFOVTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Substrate FOV is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Substrate FOV is a bad number (Integer&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(SubstrateRangeTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Substrate range is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Substrate range is a bad number (Integer&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(LightFOVTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Light FOV is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Light FOV is a bad number (Integer&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(LightRangeTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Light range is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Light range is a bad number (Integer&gt;0)");
            return false;
        }

	try {	
    	    dummyInt=java.lang.Integer.parseInt(BumperRangeTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Bumper range is a bad number (Integer&gt;0)");
            return false;
        }
        if (dummyInt&lt;=0)
        {
            msgErr("Bumper range is a bad number (Integer&gt;0)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(EnergyMovementTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Energy consumption for movement is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Energy consumption for movement is a bad number (Float&gt;0)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(EnergyMitoseTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Energy consumption for mitosis is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Energy consumption for mitosis is a bad number (Float&gt;0)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(EnergyMeioseTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Energy consumption for meiosis is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Energy consumption for meiosis is a bad number (Float&gt;0)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(EnergyPOutputTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Energy consumption for pheromone output is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Energy consumption for pheromone output is a bad number (Float&gt;0)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(EnergySubstrateTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Energy collecting for substrate is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Energy collecting for substrate is a bad number (Float&gt;0)");
            return false;
        }
        
        try {	
    	    dummyFloat=java.lang.Float.parseFloat(EnergyLightTF.getText());
    	} catch (NumberFormatException e)
        {
            msgErr("Energy collecting for light is a bad number (Float&gt;0)");
            return false;
        }
        if (dummyFloat&lt;=0)
        {
            msgErr("Energy collecting for light is a bad number (Float&gt;0)");
            return false;
        }
        
    	return true;
    }
    
    public ImagePanel getAqua()
    {
	if (window==null)
	    return null;
	return window.getAqua();
    }
    
    public Settings getSettingsVar()
    {
    	return config;
    }

    public void actionPerformed(ActionEvent e)
        {
	    Object src=e.getSource();
	    if (src==SaveBtn) 
	    {
		System.out.println("Saving file...");
		ExportSettingsFile();		
	    }
	    if (src==LoadBtn)
	    {
	 	System.out.println("Loading file...");
		DialogImportSettingsFile();
		config=StoreSettingsToVar();
	    }
	    if (src==QuitBtn)
	    {
	    	//Custom button text
	        Object[] options = {"Yes","No",};
                int result = JOptionPane.showOptionDialog(this,
                        "Do you really like to quit ALGAE ?",
                        "Confirmation",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        options,
                        options[1]);
		if (result==JOptionPane.YES_OPTION)
		    System.exit(0);
	    }
	    if (src==OKBtn)
	    {
		if (!VerifySettings())
		   return;
		config=StoreSettingsToVar();
		    		
    		if ((window!=null)&amp;&amp;(window.isDisplayable()==false))
    		{
    		    iAmReady=false;
    		    window=null;
    		    System.out.println("Happening: window undisplayable - recreating it");
    		}
    		
    		if (window==null)
    		    window = new MainGUI(config);
						
		window.setTitle("ALGAE Simulation - "+(String)PresetsCombo.getSelectedItem());
		window.setResizable(true);
		window.setSize(800,600);
		window.setVisible(true);
			
		iAmReady=true;

	    }
	    if (src==PresetsCombo)
	    {
		System.out.println("COMBO DONE");
	    	if ((PresetsCombo.getSelectedItem()!="No presets")&amp;&amp;
	    	    (PresetsCombo.getSelectedItem()!="-----"))	    	
	    	{
	    	    System.out.println("Huhu - good selection");
	    	    ImportSettingsFile((String)PresetsCombo.getSelectedItem());	
	    	}
	    	else
	    	{
	    	    System.out.println("No, \"No presets\" is not a good choice");	
	    	}
	    }
	}
	
    public boolean areYouReady()
    {
    	return iAmReady;
    }
             
    public boolean isPreset(String s)
    {
    	String xml_extension="asf";
    	int i = s.lastIndexOf('.');
        if (i &gt; 0 &amp;&amp;  i &lt; s.length() - 1)
        {
            String extension = s.substring(i+1).toLowerCase();
            if (xml_extension.equals(extension))
                return true;
            else
                return false;
        }
        return false;
    }
         
    public void AddASFFilesToCombo()
    {
    	boolean found=false;
    	
    	File filePath = new File(".");
    	File[] files=filePath.listFiles();
    	if (files==null)
	{
	    System.out.println("No appropriate files (*.asf) found...");
	    return;
	}
	for (int k=0; k&lt;files.length; k++) 
        {
            if (isPreset(files[k].getName()))
            {
               	PresetsCombo.addItem(files[k].getName());
               	found=true;
            }
        }
        
        if (!found)
        {
            String []preNone={"No presets"};
            PresetsCombo = new JComboBox(preNone);
        }
        
        System.out.println("Several asf-files found (at least one)");
    }
   
    public SettingsDialog(){
	addWindowListener(new WindowAdapter() 
        {
	    public void windowClosing(WindowEvent e) 
	    {
		 System.exit(0);
            }
        });
	
	/*
	try {
	UIManager.setLookAndFeel(
        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        //"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        //"javax.swing.plaf.metal.MetalLookAndFeel");  	
	} catch (Exception e) { System.out.println("EXCEPTION: UIManager.setLookAndFeel");}
	*/
		  
  	iAmReady=false;
		      
	contentPane=getContentPane();
	contentPane.setLayout(gridbag);

	TabbedPane.addChangeListener(new TabbedListener());
	TabbedPane.addTab("Simulation",SimulationPanel);
	TabbedPane.addTab("Algaes",AlgaesPanel);
	TabbedPane.addTab("Sensors",SensorsPanel); 
	TabbedPane.addTab("Actions",ActionPanel);
	
	//TabbedPane.addMouseListener(this);

	TopPanel.setLayout(new GridLayout(1,1));
	TopPanel.add(TabbedPane);

	BottomPanel.setLayout(new GridLayout(1,5));

	SaveBtn.addActionListener(this);
	LoadBtn.addActionListener(this); 
        OKBtn.addActionListener(this);
	QuitBtn.addActionListener(this);
	BottomPanel.add(OKBtn);
	BottomPanel.add(QuitBtn);
        BottomPanel.add(LoadBtn);
	BottomPanel.add(SaveBtn);
	
	PresetsCombo.addActionListener(this);
	BottomPanel.add(PresetsCombo);

	// search for all files in current directory
        // matching the "*.asf" file extension and store
	// them into a combo box
	AddASFFilesToCombo();

	c.gridx = 0;
        c.gridy = 0;
        c.gridwidth=23;
        c.gridheight=12;
        c.weightx=1;
        c.weighty=1;
        c.insets=new Insets(2,2,2,2);
        c.fill = GridBagConstraints.BOTH;
        gridbag.setConstraints(TopPanel, c);
        contentPane.add(TopPanel);

	c.gridx = 0;
        c.gridy = 12;
        c.gridwidth=23;
        c.gridheight=2;
        c.weightx=1;
        c.weighty=0;
        c.insets=new Insets(2,2,2,2);
        c.fill = GridBagConstraints.NONE;
        gridbag.setConstraints(BottomPanel, c);
        contentPane.add(BottomPanel);	

	MakeSimulationPanel();
	MakeAlgaesPanel();
 	MakeSensorsPanel();	
       	MakeActionsPanel();      	
    }
}
</pre></body></html>