/*
 * Created on 2004-11-11
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.ui.mmpedit;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.util.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IMMPModel;

/**
 * The Overview page of the MMP editor
 * 
 * @author mikolaj
 */
public class OverviewPage extends MMPFormPage {

    static final String PAGE_ID="mmp_overview";
    
    /**
     * Constructs the Overview page
     * @param editor The editro this page is in
     */
    public OverviewPage(FormEditor editor, IMMPModel model) {
        super(editor, PAGE_ID, "Overview");
        fModel=model;
    }

    protected class TypeComboItem 
    {
        TypeComboItem(String name, String id, int uid2)
        {
            this.name=name;
            this.id=id;
            this.uid2=uid2;
        }
        String name;
        String id;
        int uid2;
    }
    
    TypeComboItem[] fTypeItems=new TypeComboItem[]
        {
            new TypeComboItem("Application (APP)", "APP", 0x100039ce),
            new TypeComboItem("Dynamic library (DLL)", "DLL", 0x1000008d),
            new TypeComboItem("Server (EXE)", "EXE", 0),
            new TypeComboItem("Other", null, 0)
        };
    public final static int OTHER_SELECTION=3;
    
    protected SectionPart createGeneralSection(ScrolledForm form)
    {
        FormToolkit toolkit=getManagedForm().getToolkit();
		MMPSectionPart part=new MMPSectionPart(form.getBody(), toolkit,
		    "General information", "Set general information about the target:");
		
		fTargetFilename=part.addTextWithLabel("Target filename:", fModel.getTargetFilename(), "The name of the file to be build");
		fTargetFilename.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(Event e) {
                updateTargetFilename();
            }			
        });
		fTargetFilename.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                updateTargetFilename();
            }			
        });
		fFilenameListener=new Listener() {
            public void handleEvent(Event e) {
                fTargetFilename.setText(fModel.getTargetFilename());
            }			
        };
		fModel.addChangeListener(IMMPModel.CHANGE_TARGET_NAME, fFilenameListener);
		
		String[] comboItems=new String[fTypeItems.length];
		for (int i=0; i<fTypeItems.length; i++)
		    comboItems[i]=fTypeItems[i].name;
        fTypeCombo=part.addComboWithLabel("Target type:", comboItems, 0);
        selectTypeCombo();
        fTypeCombo.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                typeComboChanged();
            }			
        });
        fTypeListener=new Listener() {
            public void handleEvent(Event e) {
                modelTypeChanged();
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_TARGET_TYPE, fTypeListener);
        fUID2Listener=new Listener() {
            public void handleEvent(Event e) {
                modelTypeChanged();
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_UID2, fUID2Listener);

		fTargetPath=part.addTextWithLabel("Target installation path:", fModel.getTargetPath(),
		        "The installation directory of the target file. Appliocations\n"+
		        "are usually installed in \\System\\apps\\<appname>\\, EXE\n"+
		        "servers in \\System\\programs\\ and ECOM libraries in\n"+
		        "\\System\\libs\\plugins\\.\n"+
		        "Note: the drive letter as it is chosen by the user\n" +
		        "during the installation (C: for phone memory and E: for\n" +
		        "memory card. In emulator the drive is Z: - the ROM)");
		fTargetPath.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(Event e) {
                updateTargetPath();
            }			
        });
		fTargetPath.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                updateTargetPath();
            }			
        });
		fPathListener=new Listener() {
            public void handleEvent(Event e) {
                fTargetPath.setText(fModel.getTargetPath());
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_TARGET_PATH, fPathListener);
		part.contentCreated();
        return part;
    }
    
    protected void updateTargetFilename()
    {
        try {
            fModel.setTargetFilename(fTargetFilename.getText());
        } catch (CoreException e1) {
            SymbdevPlugin.showError(e1, "Couldn't change target filename", getEditorSite().getShell());
        }        
    }
    
    protected void updateTargetPath()
    {
        try {
            fModel.setTargetPath(fTargetPath.getText());
        } catch (CoreException e1) {
            SymbdevPlugin.showError(e1, "Couldn't change target path", getEditorSite().getShell());
        }                
    }
    
    protected void createTypeSection()
    {
        Composite parent=fTargetTypeContainer;
        FormToolkit toolkit=getManagedForm().getToolkit();
        switch (fTypeCombo.getSelectionIndex())
        {
        	case 0:
        	    fTargetSection=new GenericTargetSection(parent, toolkit, "Application",
                        "Select properties for the Application target", 2, fModel, fTypeItems[0].uid2);
                fTargetSection.contentCreated();
        	    break;
        	case 1:
        	    fTargetSection=new GenericTargetSection(parent, toolkit, "Library",
                        "Select properties for the DLL (Dynamic Link Library) target", 2, fModel, fTypeItems[1].uid2);
        	    fTargetSection.contentCreated();
        	    break;
        	case 2:
        	    fTargetSection=new GenericTargetSection(parent, toolkit, "Server",
                        "Select properties for the Server target", 2, fModel, 0);
                fTargetSection.contentCreated();
        	    break;
        	case 3:
        	    fTargetSection=new OtherTargetSection(parent, toolkit, fModel);
        	    break;
        	default: Assert.isTrue(false);
        }
        GridData gd=new GridData(GridData.FILL_BOTH);
        fTargetSection.getSection().setLayoutData(gd);
    }
    
    protected void typeComboChanged()
    {
        if (fTargetSection!=null)
        {
            GenericTargetSection tmp=fTargetSection;
            fTargetSection=null;
            tmp.dispose();
            tmp.getSection().dispose();
        }
        try {
            int sel=fTypeCombo.getSelectionIndex();
            if (sel==OTHER_SELECTION)
                fUserChosenOther=true;
            	else fUserChosenOther=false;
            
            if (fTypeItems[sel].uid2!=0)
                fModel.setUID2(fTypeItems[sel].uid2);
            if (fTypeItems[sel].id!=null)
                fModel.setTargetType(fTypeItems[sel].id);
        } catch (CoreException e) {
            SymbdevPlugin.showError(e, null, getEditorSite().getShell());
        }
        createTypeSection();
        fTargetTypeContainer.layout();
    }
    
    protected void selectTypeCombo()
    {
        String type=fModel.getTargetType();
        int uid2=fModel.getUID2();
        for (int i=0; i<fTypeItems.length; i++)
        {
            if (fTypeItems[i].id==null || fTypeItems[i].id.equalsIgnoreCase(type))
                if (fTypeItems[i].uid2==0 || fTypeItems[i].uid2==uid2)
                {
                    fTypeCombo.select(i);
                    break;
                }
        }
    }
    
    protected void modelTypeChanged()
    {
        if (fUserChosenOther || fTypeCombo.isDisposed())
            return;
        selectTypeCombo();
    }
    
	protected void createFormContent(IManagedForm managedForm) {
	    super.createFormContent(managedForm);
	    fFormCreated=true;
		ScrolledForm form = managedForm.getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		
		form.setText("Overview");

		fGeneralSection=createGeneralSection(form);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 1;
		fGeneralSection.getSection().setLayoutData(gd);

		fLanguagesSection=new LanguagesSection(form.getBody(), toolkit, fModel);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 1;
		gd.verticalSpan = 2;
		fLanguagesSection.getSection().setLayoutData(gd);

		fTargetTypeContainer=toolkit.createComposite(form.getBody());
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 1;
		fTargetTypeContainer.setLayoutData(gd);
		GridLayout layout=new GridLayout(1, false);
		fTargetTypeContainer.setLayout(layout);
		createTypeSection();
	}
	
	public void dispose()
	{
	    if (fFormCreated)
	    {
	        fModel.removeChangeListener(IMMPModel.CHANGE_TARGET_NAME, fFilenameListener);
	    	fModel.removeChangeListener(IMMPModel.CHANGE_TARGET_PATH, fPathListener);
	    	fModel.removeChangeListener(IMMPModel.CHANGE_TARGET_TYPE, fTypeListener);
	    	fModel.removeChangeListener(IMMPModel.CHANGE_UID2, fUID2Listener);
	    }
	    super.dispose();
	}
	
	SectionPart fGeneralSection;
	GenericTargetSection fTargetSection;
	LanguagesSection fLanguagesSection;
	
	boolean fFormCreated;
	Combo fTypeCombo;
	Composite fTargetTypeContainer;
	Listener fTypeListener;
	Listener fUID2Listener;
	Text fTargetFilename;
	Listener fFilenameListener;
	Text fTargetPath;	
	Listener fPathListener;
	IMMPModel fModel;
	
	boolean fUserChosenOther;
}
