/*
 * Created on 2004-12-04
 *
 * 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.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 pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IMMPModel;

/**
 * A section that contains information for the "other" target type -
 * the user can set the UID2, UID3 and target type string
 * 
 * @author mikolaj
 */
public class OtherTargetSection extends GenericTargetSection {

    /**
     * @param parent The parent of the section
     * @param toolkit Form toolkit
     * @param model The MMP model which data is presented
     */
    public OtherTargetSection(Composite parent, FormToolkit toolkit,
            IMMPModel model)
    {
        super(parent, toolkit, "Other", "Select properties for the generic target",
                2, model, 0);
        fTargetType=addTextWithLabel("Target id:", model.getTargetType());
        fTargetType.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(Event e) {
                storeType();
            }			
        });
        fTargetType.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeType();
            }			
        });
        fTypeListener=new Listener() {
            public void handleEvent(Event e) {
                fTargetType.setText(fModel.getTargetType());
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_TARGET_TYPE, fTypeListener);
        contentCreated();     
    }

    public void dispose()
    {
        fModel.removeChangeListener(IMMPModel.CHANGE_TARGET_TYPE, fTypeListener);
        super.dispose();
    }
    
    void storeType()
    {
        try {
            fModel.setTargetType(fTargetType.getText());
        } catch (CoreException e) {
            SymbdevPlugin.showError(e, "Could not store target type", getSection().getShell());
        }
    }
    
    Text fTargetType;
    Listener fTypeListener;
}
