/*
 * Created on 2004-08-10
 *
 */
package pl.zalewski.mikolaj.symbdev.preferences;

import java.io.IOException;
import java.util.Properties;

import org.eclipse.cdt.utils.spawner.EnvironmentReader;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.SymbianSDKsList;
import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;
import pl.zalewski.mikolaj.symbdev.Win32ToolsList;
import pl.zalewski.mikolaj.symbdev.widgets.ChooseWin32ToolAndSDKDialog;

/**
 * Root preferences page for Symbian OS development  
 * @author mikolaj
 */
public class SymbianOSPreferencesPage extends PreferencePage
		implements IWorkbenchPreferencePage {

	/**
	 * default contructor
	 */
	public SymbianOSPreferencesPage() {
		super();
		setDescription("Configure the tools for Symbian OS development");
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
	 */
	protected Control createContents(Composite ancestor)
	{
		Composite parent=new Composite(ancestor, SWT.NONE);
		GridLayout layout=new GridLayout();
		layout.numColumns=1;
		parent.setLayout(layout);
		
		Label label;
		label=new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		label.setLayoutData(gridData);
		
		Text l=new Text(parent, SWT.READ_ONLY|SWT.WRAP);
		l.setText("The Symbian OS development plug-in has been written by Mikołaj Zalewski\n"+
		          "(mikolaj@zalewski.pl)\n\n"+
		          "You can redistribute it and/or modify it under the terms of the GNU General\n"+
		          "Public License as published by the Free Software Foundation; either version 2\n"+
		          "of the License, or (at your option) any later version.\n\n"+
		          "This plug-in was written in the hope that it will be useful, but WITHOUT ANY\n"+
		          "WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\nFOR "+
		          "A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\n");
		
		label=new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		label.setLayoutData(gridData);
		
		l=new Text(parent, SWT.READ_ONLY|SWT.WRAP);
		l.setText("\nIn the subpages of this page you can configure the tools to create SymbianOS\nprograms. "+
				  "The plug-in will set enviroment variables to enable them. "+
  		          "You can test\nthese settings by starting a console ");
//		GridData data=new GridData();
//		data.widthHint=convertWidthInCharsToPixels(60);
//		l.setData(data);
		
		Button b=new Button(parent, SWT.PUSH);
		b.setText("Start command shell...");
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				console();
			}
		});
		
		return parent;
	}
	
	protected void console()
	{
		ChooseWin32ToolAndSDKDialog dlg=new ChooseWin32ToolAndSDKDialog(getShell(), true);
		if (dlg.open() == Window.OK)
		{
			Properties env=EnvironmentReader.getEnvVars();
			
			String name=dlg.getSDKResult();
			SymbianSDKEntry sdk;
			if (name!=null) sdk=SymbianSDKsList.getInstance().getEntry(name);
				else sdk=null;

			int id=dlg.getWin32ToolResult();
			Win32ToolEntry tool;
			if (id!=-1) tool=Win32ToolsList.getInstance().getEntry(id);
				else tool=null;
			
			if (tool!=null)
				tool.alterEnvironment(env);
			if (sdk!=null)
				sdk.alterEnvironment(env);
			
			String[] tabenv=SymbdevPlugin.envPropertiesToArray(env);
			String[] cmd=new String[4];
			cmd[0]=env.getProperty("COMSPEC", "cmd.exe");
			cmd[1]="/c";
			cmd[2]="start";
			cmd[3]=env.getProperty("COMSPEC", "cmd.exe");
			
			try
			{
				Runtime.getRuntime().exec(cmd, tabenv);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}

}
