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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.forms.IManagedForm;
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.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IMMPModel;

/**
 * This class implements the Extra page with
 * system resources/resources/AIF/MBMs
 * 
 * @author mikolaj
 */
public class ExtraPage extends MMPFormPage {

    public static final String PAGE_ID="mmp_extra";    
    
    public ExtraPage(FormEditor editor, IMMPModel model, SymbianSDKEntry sdk,
            IPath currentDir, IProject project) {
        super(editor, PAGE_ID, "Extra");
        fModel=model;
        fSDK=sdk;
        fProject=project;
        fCurrentDir=currentDir;
    }
    
    protected void createFormContent(IManagedForm managedForm)
    {
        super.createFormContent(managedForm);
        ScrolledForm form=managedForm.getForm();
        form.setText("Extra");
        FormToolkit toolkit=managedForm.getToolkit();
        
        
        fExtraTree=new ExtraTree(form.getBody(), toolkit, fModel,
                fSDK, fCurrentDir, new Listener() {
                    public void handleEvent(org.eclipse.swt.widgets.Event e) {
                        treeSelected(e.count, e.detail);
                    }			
                }, fProject);
        GridData gd=new GridData(GridData.FILL_VERTICAL);
        fExtraTree.getSection().setLayoutData(gd);
        
        fPropertiesPane=toolkit.createComposite(form.getBody());
        gd=new GridData(GridData.FILL_BOTH);
        fPropertiesPane.setLayoutData(gd);
        GridLayout layout=new GridLayout(1, false);
        fPropertiesPane.setLayout(layout);
    }    
    
    protected void treeSelected(int type, int pos)
    {
        // AIF selected will be called on every modification of the
        // AIF declaration
        if (type==ExtraTree.ELEMENT_AIF && fSecondSection!=null &&
                fSecondSection instanceof AIFPropertiesSection)
            return;
        if (fSecondSection!=null)
        {
            fSecondSection.dispose();
            fSecondSection.getSection().dispose();
            fSecondSection=null;
        }
        if (fPropertiesPane==null)  // not yet initialized
            return;
        switch (type)
        {
        	case ExtraTree.ELEMENT_RESOURCE:
        	    fSecondSection=new ResourcesPropertiesSection(fPropertiesPane,
        	            getManagedForm().getToolkit(), fModel, false,
        	            pos, fSDK.epocRoot, fCurrentDir);
        		break;
        	case ExtraTree.ELEMENT_SYSTEM_RESOURCE:
        	    fSecondSection=new ResourcesPropertiesSection(fPropertiesPane,
        	            getManagedForm().getToolkit(), fModel, true,
        	            pos, fSDK.epocRoot, fCurrentDir);
        		break;
            case ExtraTree.ELEMENT_AIF:
                fSecondSection=new AIFPropertiesSection(fPropertiesPane,
                        getManagedForm().getToolkit(), fModel.getAIFDeclaration(),
                        fSDK, fCurrentDir);
            	break;
        	default: return;
        }
    	GridData gd=new GridData(GridData.FILL_BOTH);
    	fSecondSection.getSection().setLayoutData(gd);        	
		fPropertiesPane.layout(true);
    }
    
    IMMPModel fModel;
    ExtraTree fExtraTree;
    IPath fCurrentDir;
    IProject fProject;
    SymbianSDKEntry fSDK;
    MMPSectionPart fSecondSection;
    Composite fPropertiesPane;
}
