package com.businesslogic.ireport.ui;

import com.businesslogic.ireport.*;

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;

/**
 * Questa classe puņ accettare un numero variabile di parametri sulla riga
 * di comando. L'esecuzione del programma inizia con il metodo main(). Il costruttore
 * della classe non viene richiamato fino a quando un oggetto di tipo "OptionsForm"
 * non viene creato nel metodo main().
 */
public class OptionsForm extends Form
{
	MainForm mf = null;
	public OptionsForm(MainForm mf)
	{
		super();

		// Necessario per il supporto della finestra di progettazione form di Visual J++
		initForm();		
		this.mf = mf;
		
		this.comboBoxUM.setSelectedIndex(0);
		
		// Setting properties...
		if (mf.ireportProperties.getProperty("MAX_MRU")!=null)
			numberEditMRU.setValue(Integer.parseInt(	mf.ireportProperties.getProperty("MAX_MRU") ) );
		else
			numberEditMRU.setValue(4 );
		if (mf.ireportProperties.getProperty("RULES")!=null)
			checkBoxRules.setChecked(	((String)mf.ireportProperties.getProperty("RULES")).equals("true") );
		if (mf.ireportProperties.getProperty("SYNTAX_HIGHLIGHTING")!=null)
			checkBoxSyntax.setChecked(	((String)mf.ireportProperties.getProperty("SYNTAX_HIGHLIGHTING")).equals("true") );
		if (mf.ireportProperties.getProperty("DEFAULT_UNITS")!=null)
			comboBoxUM.setSelectedItem( (String)mf.ireportProperties.getProperty("DEFAULT_UNITS") );
		if (mf.ireportProperties.getProperty("TEMPLATE_FILE")!=null)
			editTemplate.setText((String)mf.ireportProperties.getProperty("TEMPLATE_FILE") );
		if (mf.ireportProperties.getProperty("COMPILER_GENPDF")!=null)
			checkBoxPDF.setChecked(	((String)mf.ireportProperties.getProperty("COMPILER_GENPDF")).equals("true") );
		if (mf.ireportProperties.getProperty("COMPILER_OUT_DIR")!=null)
			editCOutdir.setText((String)mf.ireportProperties.getProperty("COMPILER_OUT_DIR") );
		if (mf.ireportProperties.getProperty("BACKUP_DIR")!=null)
			editBakdir.setText((String)mf.ireportProperties.getProperty("BACKUP_DIR") );
		if (mf.ireportProperties.getProperty("BACKUP_MODE")!=null)
		{
			if (((String)mf.ireportProperties.getProperty("BACKUP_MODE")).equals("0"))
				radioButtonNB.setChecked(true);
			if (((String)mf.ireportProperties.getProperty("BACKUP_MODE")).equals("1"))
				radioButtonRB.setChecked(true);
			if (((String)mf.ireportProperties.getProperty("BACKUP_MODE")).equals("2"))
				radioButtonAB.setChecked(true);
		}
		else
			radioButtonAB.setChecked(true);
		if (mf.ireportProperties.getProperty("EDITOR")!=null)
			editEditor.setText((String)mf.ireportProperties.getProperty("EDITOR") );
		else
			editEditor.setText("notepad.exe");
		if ( (String)mf.ireportProperties.getProperty("COMPILER_SAVE")!=null )
		{
				if (((String)mf.ireportProperties.getProperty("COMPILER_SAVE")).equalsIgnoreCase("false"))
				checkBoxASBeforeCompiling.setChecked(false);
		}
	}

	/**
	 * OptionsForm esegue l'overriding di dispose e quindi puņ svuotare
	 * l'elenco dei componenti.
	 */
	public void dispose()
	{
		super.dispose();
		components.dispose();
	}

	private void button6_click(Object source, Event e)
	{
		OpenFileDialog ofd = new OpenFileDialog();
		ofd.setCheckFileExists(true);
		ofd.setFilter("Jasper XML Report *.xml|*.xml");
		ofd.setDefaultExt("xml");
		int result = ofd.showDialog();
		if (result == DialogResult.OK)
		{
			editTemplate.setText(ofd.getFileName());
		}
	}

	private void button5_click(Object source, Event e)
	{
			com.businesslogic.ireport.ui.directorydialog.FileExplorer fe = new com.businesslogic.ireport.ui.directorydialog.FileExplorer(this.mf.programDir);
		if (fe.showDialog() == DialogResult.OK)
		{
			this.editCOutdir.setText( fe.getDirectory() );
		}
	}

	private void button4_click(Object source, Event e)
	{
					com.businesslogic.ireport.ui.directorydialog.FileExplorer fe = new com.businesslogic.ireport.ui.directorydialog.FileExplorer(this.mf.programDir);
		if (fe.showDialog() == DialogResult.OK)
		{
			this.editBakdir.setText( fe.getDirectory() );
		}
	}

	private void button1_click(Object source, Event e)
	{
		this.dispose();
	}

	private void button2_click(Object source, Event e)
	{
		mf.ireportProperties.put("MAX_MRU",""+(int)numberEditMRU.getValue());
		mf.ireportProperties.put("RULES",""+checkBoxRules.getChecked());
		mf.ireportProperties.put("DEFAULT_UNITS",""+comboBoxUM.getSelectedItem() );
		mf.ireportProperties.put("TEMPLATE_FILE",""+editTemplate.getText());
		mf.ireportProperties.put("COMPILER_GENPDF",""+checkBoxPDF.getChecked());
		mf.ireportProperties.put("COMPILER_OUT_DIR",""+editCOutdir.getText());
		mf.ireportProperties.put("BACKUP_DIR",""+editBakdir.getText());
		mf.ireportProperties.put("EDITOR",""+editEditor.getText());
		mf.ireportProperties.put("COMPILER_SAVE",""+checkBoxASBeforeCompiling.getChecked());
		mf.ireportProperties.put("SYNTAX_HIGHLIGHTING",""+checkBoxSyntax.getChecked());
		
		int mode = 2;
		if (radioButtonNB.getChecked()) mode = 0;
		else if (radioButtonRB.getChecked()) mode = 1;
		else if (radioButtonAB.getChecked()) mode = 2;
		mf.ireportProperties.put("BACKUP_MODE",""+mode);
		mf.saveProperties();
	}

	private void button3_click(Object source, Event e)
	{
		button2_click(source,e);
		this.dispose();
	}

	private void button7_click(Object source, Event e)
	{
		OpenFileDialog ofd = new OpenFileDialog();
		ofd.setCheckFileExists(true);
		ofd.setFilter("Executable *.exe|*.exe|All files|*.*");
		ofd.setDefaultExt("xml");
		int result = ofd.showDialog();
		if (result == DialogResult.OK)
		{
			editEditor.setText(ofd.getFileName());
		}
	}

	/**
	 * NOTA: il seguente codice č richiesto dalla finestra di progettazione form 
	 * di Visual J++. Č possibile modificarlo con l'Editor di form.  Non modificarlo
	 * con l'Editor del codice.
	 */
	Container components = new Container();
	TabControl tabControl1 = new TabControl();
	TabPage tabPage1 = new TabPage();
	TabPage tabPage2 = new TabPage();
	Button button1 = new Button();
	Button button2 = new Button();
	Button button3 = new Button();
	TabPage tabPage3 = new TabPage();
	GroupBox groupBox1 = new GroupBox();
	RadioButton radioButtonNB = new RadioButton();
	RadioButton radioButtonRB = new RadioButton();
	RadioButton radioButtonAB = new RadioButton();
	Label label1 = new Label();
	Edit editBakdir = new Edit();
	Button button4 = new Button();
	ComboBox comboBoxUM = new ComboBox();
	CheckBox checkBoxRules = new CheckBox();
	Label label3 = new Label();
	Edit editCOutdir = new Edit();
	Button button5 = new Button();
	Label label4 = new Label();
	NumberEdit numberEditMRU = new NumberEdit();
	Label label2 = new Label();
	GroupBox groupBox2 = new GroupBox();
	Label label5 = new Label();
	Label label6 = new Label();
	Edit editTemplate = new Edit();
	Button button6 = new Button();
	CheckBox checkBoxPDF = new CheckBox();
	TabPage tabPage4 = new TabPage();
	GroupBox groupBox3 = new GroupBox();
	Button button7 = new Button();
	Edit editEditor = new Edit();
	CheckBox checkBoxASBeforeCompiling = new CheckBox();
	CheckBox checkBoxSyntax = new CheckBox();

	private void initForm()
	{
		this.setText("iReport options");
		this.setAutoScaleBaseSize(new Point(5, 13));
		this.setBorderStyle(FormBorderStyle.FIXED_TOOLWINDOW);
		this.setClientSize(new Point(343, 254));

		tabControl1.setLocation(new Point(4, 4));
		tabControl1.setSize(new Point(336, 216));
		tabControl1.setTabIndex(0);
		tabControl1.setText("tabControl1");
		tabControl1.setSelectedIndex(3);

		tabPage1.setLocation(new Point(4, 25));
		tabPage1.setSize(new Point(328, 187));
		tabPage1.setTabIndex(0);
		tabPage1.setText("General");

		tabPage2.setLocation(new Point(4, 25));
		tabPage2.setSize(new Point(328, 187));
		tabPage2.setTabIndex(1);
		tabPage2.setText("Compiler");

		button1.setLocation(new Point(268, 224));
		button1.setSize(new Point(72, 24));
		button1.setTabIndex(3);
		button1.setText("&Cancel");
		button1.addOnClick(new EventHandler(this.button1_click));

		button2.setLocation(new Point(192, 224));
		button2.setSize(new Point(72, 24));
		button2.setTabIndex(2);
		button2.setText("&Apply");
		button2.addOnClick(new EventHandler(this.button2_click));

		button3.setLocation(new Point(116, 224));
		button3.setSize(new Point(72, 24));
		button3.setTabIndex(1);
		button3.setText("&OK");
		button3.addOnClick(new EventHandler(this.button3_click));

		tabPage3.setLocation(new Point(4, 25));
		tabPage3.setSize(new Point(328, 187));
		tabPage3.setTabIndex(2);
		tabPage3.setText("Backup");

		groupBox1.setLocation(new Point(8, 8));
		groupBox1.setSize(new Point(312, 84));
		groupBox1.setTabIndex(0);
		groupBox1.setTabStop(false);
		groupBox1.setText("Backup Files on Save");

		radioButtonNB.setLocation(new Point(12, 16));
		radioButtonNB.setSize(new Point(100, 23));
		radioButtonNB.setTabIndex(2);
		radioButtonNB.setText("No backup");

		radioButtonRB.setLocation(new Point(12, 36));
		radioButtonRB.setSize(new Point(156, 23));
		radioButtonRB.setTabIndex(1);
		radioButtonRB.setText("Replace extension with .BAK");

		radioButtonAB.setLocation(new Point(12, 56));
		radioButtonAB.setSize(new Point(228, 23));
		radioButtonAB.setTabIndex(0);
		radioButtonAB.setText("Append .BAK to exsisting extension");

		label1.setLocation(new Point(12, 100));
		label1.setSize(new Point(172, 16));
		label1.setTabIndex(1);
		label1.setTabStop(false);
		label1.setText("Default backup directory");

		editBakdir.setLocation(new Point(12, 116));
		editBakdir.setSize(new Point(256, 20));
		editBakdir.setTabIndex(2);
		editBakdir.setText("");

		button4.setLocation(new Point(272, 112));
		button4.setSize(new Point(52, 24));
		button4.setTabIndex(3);
		button4.setText("Browse");
		button4.addOnClick(new EventHandler(this.button4_click));

		comboBoxUM.setLocation(new Point(180, 68));
		comboBoxUM.setSize(new Point(96, 21));
		comboBoxUM.setTabIndex(4);
		comboBoxUM.setText("");
		comboBoxUM.setStyle(ComboBoxStyle.DROPDOWNLIST);
		comboBoxUM.setItems(new Object[] {
							"cm", 
							"inches", 
							"pixels"});

		checkBoxRules.setLocation(new Point(180, 44));
		checkBoxRules.setSize(new Point(100, 23));
		checkBoxRules.setTabIndex(3);
		checkBoxRules.setText("");
		checkBoxRules.setChecked(true);
		checkBoxRules.setCheckState(CheckState.CHECKED);

		label3.setLocation(new Point(8, 8));
		label3.setSize(new Point(172, 16));
		label3.setTabIndex(0);
		label3.setTabStop(false);
		label3.setText("Default compilation directory");

		editCOutdir.setLocation(new Point(8, 24));
		editCOutdir.setSize(new Point(256, 20));
		editCOutdir.setTabIndex(1);
		editCOutdir.setText("");

		button5.setLocation(new Point(268, 20));
		button5.setSize(new Point(52, 24));
		button5.setTabIndex(2);
		button5.setText("Browse");
		button5.addOnClick(new EventHandler(this.button5_click));

		label4.setLocation(new Point(100, 72));
		label4.setSize(new Point(72, 16));
		label4.setTabIndex(2);
		label4.setTabStop(false);
		label4.setText("Default units");
		label4.setTextAlign(HorizontalAlignment.RIGHT);

		numberEditMRU.setLocation(new Point(180, 20));
		numberEditMRU.setSize(new Point(44, 20));
		numberEditMRU.setTabIndex(1);
		numberEditMRU.setText("0");
		numberEditMRU.setTextAlign(HorizontalAlignment.CENTER);
		numberEditMRU.setIsDouble(false);
		numberEditMRU.setDecimals(0);
		numberEditMRU.setValue(0.0d);
		numberEditMRU.setViewPlus(false);
		numberEditMRU.setSeparatoreMigliaia(true);

		label2.setLocation(new Point(16, 24));
		label2.setSize(new Point(156, 20));
		label2.setTabIndex(0);
		label2.setTabStop(false);
		label2.setText("Num. of recent files on file menu");
		label2.setTextAlign(HorizontalAlignment.RIGHT);

		groupBox2.setLocation(new Point(8, 4));
		groupBox2.setSize(new Point(312, 100));
		groupBox2.setTabIndex(0);
		groupBox2.setTabStop(false);
		groupBox2.setText("General");

		label5.setLocation(new Point(20, 48));
		label5.setSize(new Point(152, 24));
		label5.setTabIndex(5);
		label5.setTabStop(false);
		label5.setText("View rules");
		label5.setTextAlign(HorizontalAlignment.RIGHT);

		label6.setLocation(new Point(8, 116));
		label6.setSize(new Point(252, 16));
		label6.setTabIndex(1);
		label6.setTabStop(false);
		label6.setText("Use this file as template for new documents");

		editTemplate.setLocation(new Point(8, 132));
		editTemplate.setSize(new Point(256, 20));
		editTemplate.setTabIndex(2);
		editTemplate.setText("");

		button6.setLocation(new Point(268, 128));
		button6.setSize(new Point(52, 24));
		button6.setTabIndex(3);
		button6.setText("Browse");
		button6.addOnClick(new EventHandler(this.button6_click));

		checkBoxPDF.setLocation(new Point(8, 52));
		checkBoxPDF.setSize(new Point(152, 23));
		checkBoxPDF.setTabIndex(3);
		checkBoxPDF.setText("Generate PDF preview");
		checkBoxPDF.setChecked(true);
		checkBoxPDF.setCheckState(CheckState.CHECKED);

		tabPage4.setLocation(new Point(4, 25));
		tabPage4.setSize(new Point(328, 187));
		tabPage4.setTabIndex(3);
		tabPage4.setText("Editor");

		groupBox3.setLocation(new Point(4, 40));
		groupBox3.setSize(new Point(324, 60));
		groupBox3.setTabIndex(0);
		groupBox3.setTabStop(false);
		groupBox3.setText("External code editor");

		button7.setLocation(new Point(240, 20));
		button7.setSize(new Point(76, 24));
		button7.setTabIndex(0);
		button7.setText("Browse");
		button7.addOnClick(new EventHandler(this.button7_click));

		editEditor.setLocation(new Point(8, 24));
		editEditor.setSize(new Point(228, 20));
		editEditor.setTabIndex(1);
		editEditor.setText("");

		checkBoxASBeforeCompiling.setLocation(new Point(8, 76));
		checkBoxASBeforeCompiling.setSize(new Point(156, 23));
		checkBoxASBeforeCompiling.setTabIndex(4);
		checkBoxASBeforeCompiling.setText("Auto save before compiling");
		checkBoxASBeforeCompiling.setChecked(true);
		checkBoxASBeforeCompiling.setCheckState(CheckState.CHECKED);

		checkBoxSyntax.setForeColor(Color.WINDOWTEXT);
		checkBoxSyntax.setLocation(new Point(12, 8));
		checkBoxSyntax.setSize(new Point(172, 23));
		checkBoxSyntax.setTabIndex(1);
		checkBoxSyntax.setText("Use syntax highlighting");
		checkBoxSyntax.setChecked(true);
		checkBoxSyntax.setCheckState(CheckState.CHECKED);

		this.setNewControls(new Control[] {
							button3, 
							button2, 
							button1, 
							tabControl1});
		tabControl1.setNewControls(new Control[] {
								   tabPage1, 
								   tabPage2, 
								   tabPage3, 
								   tabPage4});
		tabPage1.setNewControls(new Control[] {
								button6, 
								editTemplate, 
								label6, 
								groupBox2});
		tabPage2.setNewControls(new Control[] {
								checkBoxASBeforeCompiling, 
								checkBoxPDF, 
								button5, 
								editCOutdir, 
								label3});
		tabPage3.setNewControls(new Control[] {
								button4, 
								editBakdir, 
								label1, 
								groupBox1});
		groupBox1.setNewControls(new Control[] {
								 radioButtonAB, 
								 radioButtonRB, 
								 radioButtonNB});
		groupBox2.setNewControls(new Control[] {
								 label5, 
								 comboBoxUM, 
								 checkBoxRules, 
								 label4, 
								 numberEditMRU, 
								 label2});
		tabPage4.setNewControls(new Control[] {
								checkBoxSyntax, 
								groupBox3});
		groupBox3.setNewControls(new Control[] {
								 editEditor, 
								 button7});
	}

	/**
	 * Punto di ingresso principale per l'applicazione. 
	 *
	 * @param args Matrice di parametri passata all'applicazione
	 * tramite la riga di comando.
	 */
	public static void main(String args[])
	{
		Application.run(new OptionsForm(null));
	}
}
