/*
 * Created on 2005-04-03
 *
 * 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.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
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.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IAIFDeclaration;

/**
 * This class implements the section for viewing/editing the AIF file
 * as on the Extra Page of the MMP editor
 * 
 * @author mikolaj
 */
public class AIFPropertiesSection extends MMPSectionPart {

    public AIFPropertiesSection(Composite parent, FormToolkit toolkit, IAIFDeclaration aif,
            SymbianSDKEntry sdk, IPath currentDir) {
        super(parent, toolkit, "AIF", "The Application Information File " +
        		"holds the information about the filename to display " +
        		"in the application list the icon and some other informations " +
        		"about an application", 3);
        
        fAIF=aif;
        fSDK=sdk;
        fCurrentDir=currentDir;
        fTarget=addTextWithLabel(fContainer, "Target filename: ", fAIF.getTargetFilename(),
                "Select the name of the created AIF file");
        fTarget.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                targetChanged();
            }
        });
        fTarget.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                targetChanged();
            }			
        });
        fSourceLabel=toolkit.createLabel(fContainer, getResourceLabelString(), SWT.WRAP);
        TableWrapData td=new TableWrapData(TableWrapData.FILL_GRAB);
        td.colspan=3;
        fSourceLabel.setLayoutData(td);
/*        Button b=toolkit.createButton(fContainer, "Change", SWT.PUSH);
        b.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                changeClicked();
            }
        });*/
        
        fBitmaps=new BitmapContainerWidget(fContainer, toolkit, fAIF, fSDK, fCurrentDir);
        td=new TableWrapData(TableWrapData.FILL, TableWrapData.TOP, 1, 3);
        fBitmaps.setLayoutData(td);        
        
        Label l=toolkit.createLabel(fContainer,
                "Note: using different color depths for different bitmaps " +
        		"will only work under Symbian OS 7.0 or later SDKs.",
        		SWT.WRAP);
        td=new TableWrapData(TableWrapData.FILL);
        td.colspan=3;
        l.setLayoutData(td);
        
        fModelListener=new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                modelChanged();
            }			
        };
        fAIF.addChangeListener(fModelListener);
        contentCreated();
    }
    
    String getResourceLabelString()
    {
        return "Resrouce file to generate the AIF: "+
        	fAIF.getSourceFile().getFileStringType(fSDK.epocRoot, fCurrentDir.toString())
        	.getStringName();
    }
    
/*    protected void changeClicked() {
    }*/
    
    protected void targetChanged()
    {
        if (fTarget.isDisposed())
            return;
        try {
	        fAIF.setTargetFilename(fTarget.getText().trim());
        }
        catch (CoreException e) {
            SymbdevPlugin.showError(e, "Error changing AIF target filename",
                    getSection().getShell());
        }
    }
    
    protected void modelChanged()
    {
        if (!fTarget.isDisposed())
            fTarget.setText(fAIF.getTargetFilename());
        if (!fSourceLabel.isDisposed())
            fSourceLabel.setText(getResourceLabelString());
    }
    
    public void dispose() {
        fAIF.removeChangeListener(fModelListener);
        super.dispose();
    }
    
    BitmapContainerWidget fBitmaps;
    Label fSourceLabel;
    IAIFDeclaration fAIF;
    Listener fModelListener;
    SymbianSDKEntry fSDK;
    IPath fCurrentDir;
    Text fTarget;
}
