/*
 * Created on 2004-12-10
 *
 * 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.swt.SWT;
import org.eclipse.swt.widgets.Button;
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.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;

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

/**
 * The advanced options section on the Options page of the MMP editor
 * 
 * @author mikolaj
 */
public class AdvancedOptionsSection extends MMPSectionPart {

    public AdvancedOptionsSection(Composite parent, FormToolkit toolkit, IMMPModel model) {
        super(parent, toolkit, "Advanced", "Advanced options", 2);
        
        fModel=model;
        fLinkAs=addTextWithLabel("Link as: ", "");
        TableWrapData td;
        
        fSrcDbg=toolkit.createButton(fContainer, "Disable optimalisations in debug builds", SWT.CHECK|SWT.WRAP);
        td=new TableWrapData();
        td.colspan=2;
        fSrcDbg.setLayoutData(td);

        fStrictDepend=toolkit.createButton(fContainer, "Strict dependencies", SWT.CHECK);
        td=new TableWrapData();
        td.colspan=2;
        fStrictDepend.setLayoutData(td);        

        fNoStrictDef=toolkit.createButton(fContainer, "No strict def", SWT.CHECK);
        td=new TableWrapData();
        td.colspan=2;
        fNoStrictDef.setLayoutData(td);
        
        updateLinkAs();
        updateSrcDbg();
        updateNoStrictDef();
        updateStrictDepend();
        
        fNoStrictDef.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                storeNoStrictDef();
            }			
        });
        fNoStrictDef.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeNoStrictDef();
            }
        });
        fNoStrictDefListener=new Listener() {
            public void handleEvent(Event e) {
                updateNoStrictDef();
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_NO_STRICT_DEF, fNoStrictDefListener);
        
        fSrcDbg.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                storeSrcDbg();
            }
        });
        fSrcDbg.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeSrcDbg();
            }			
        });
        fSrcDbgListener=new Listener() {
            public void handleEvent(Event e) {
                updateSrcDbg();
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_SRC_DBG, fSrcDbgListener);
        
        fStrictDepend.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                storeStrictDepend();
            }			
        });
        fStrictDepend.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeStrictDepend();
            }			
        });
        fStrictDependListener=new Listener() {
            public void handleEvent(Event e) {
                updateStrictDepend();
            }
        };
        fModel.addChangeListener(IMMPModel.CHANGE_STRICT_DEPEND, fStrictDependListener);
        
        fLinkAs.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(Event e) {
                storeLinkAs();
            }			
        });
        fLinkAs.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeLinkAs();
            }			
        });
        fLinkAsListener=new Listener() {
            public void handleEvent(Event e) {
                updateLinkAs();
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_LINK_AS, fLinkAsListener);
        
        contentCreated();
    }
    
    void updateLinkAs()
    {
        if (!fLinkAs.isDisposed())
            fLinkAs.setText(fModel.getLinkAs());
    }
    
    void storeLinkAs()
    {
        try {
            String linkAs=fLinkAs.getText();
            fModel.setLinkAs(linkAs);
        }
        catch (CoreException e) {
            SymbdevPlugin.showError(e, null, getSection().getShell());
        }
    }
    
    void updateSrcDbg()
    {
        if (!fSrcDbg.isDisposed())
            fSrcDbg.setSelection(fModel.getSrcDbg());
    }
    
    void storeSrcDbg()
    {
        try {
            fModel.setSrcDbg(fSrcDbg.getSelection());
        }
        catch (CoreException e)
        {
           SymbdevPlugin.showError(e, null, getSection().getShell());
        }
    }
    
    void updateStrictDepend()
    {
        if (!fStrictDepend.isDisposed())
            fStrictDepend.setSelection(fModel.getStrictDepend());
    }

    void storeStrictDepend()
    {
        try {
            fModel.setStrictDepend(fStrictDepend.getSelection());
        }
        catch (CoreException e)
        {
           SymbdevPlugin.showError(e, null, getSection().getShell());
        }
    }    
    
    void updateNoStrictDef()
    {
        if (!fNoStrictDef.isDisposed())
            fNoStrictDef.setSelection(fModel.getNoStrictDef());
    }

    void storeNoStrictDef()
    {
        try {
            fModel.setNoStrictDef(fNoStrictDef.getSelection());
        }
        catch (CoreException e)
        {
           SymbdevPlugin.showError(e, null, getSection().getShell());
        }
    }
    
    public void dispose() {
        fModel.removeChangeListener(IMMPModel.CHANGE_SRC_DBG, fSrcDbgListener);
        fModel.removeChangeListener(IMMPModel.CHANGE_STRICT_DEPEND, fStrictDependListener);
        fModel.removeChangeListener(IMMPModel.CHANGE_NO_STRICT_DEF, fNoStrictDefListener);
        fModel.removeChangeListener(IMMPModel.CHANGE_LINK_AS, fLinkAsListener);
        super.dispose();
    }
    
    Button fSrcDbg;
    Button fStrictDepend;
    Button fNoStrictDef;
    Text fLinkAs;
    IMMPModel fModel;
    
    Listener fSrcDbgListener;
    Listener fStrictDependListener;
    Listener fNoStrictDefListener;
    Listener fLinkAsListener;
}
