/*
 * Created on 2004-11-13
 *
 * 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.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
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.core.mmpmodel.IMMPModel;

/**
 * A generic part what queries for one or two UIDs. Can be exteneded
 * to query for more target-dependent information
 * 
 * @author mikolaj
 */
public class GenericTargetSection extends MMPSectionPart {

    static final String UID3_TOOLTIP=
        "The UID3 is usually used to distinguish specific modules.\n"+
        "For example each application should have a different UID3.\n" +
        "Different versions of the same application should have the\n" +
        "same UID3. You should use one of your private UIDs obtained\n" +
        "from Symbian (or, during development, experimental UIDs from\n" +
        "the range 0x01000000-0x0FFFFFFF).";
    
    /**
     * @param parent
     * @param toolkit
     * @param title
     * @param description
     * @param numColumns
     * @param fixedUID2 shoudl be !=0 if UID2 fixed
     */
    public GenericTargetSection(Composite parent, FormToolkit toolkit,
            String title, String description, int numColumns, IMMPModel model,
            int fixedUID2) {
        super(parent, toolkit, title, description, numColumns);
        
        fModel=model;
        fFixedUID2=fixedUID2;
        if (fixedUID2==0)
        {
          fUID2=addTextWithLabel("UID2:", uidToString(model.getUID2()),
                  "The UID2 is used to define the type of the executable,\n"+
                  "e.g. static-interface DLL, application DLL etc. You should\n"+
                  "use a UID for your target type.");
          fUID2Listener=new Listener() {
            public void handleEvent(Event e) {
                updateUID2();
            }			
          };
          model.addChangeListener(IMMPModel.CHANGE_UID2, fUID2Listener);
          fUID2.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(Event e) {
                storeUID2();
            }			
          });
          fUID2.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeUID2();
            }			
          });
          fUID3=addTextWithLabel("UID3:", uidToString(model.getUID3()), UID3_TOOLTIP);
        } else
        {
            Label l=toolkit.createLabel(fContainer,
                    "UID2 for this target type is "+uidToString(fixedUID2));
            TableWrapData td=new TableWrapData();
            td.colspan=2;
            l.setLayoutData(td);
            fUID3=addTextWithLabel("UID3:", uidToString(model.getUID3()), UID3_TOOLTIP);
        }
        
        fUID3Listener=new Listener() {
            public void handleEvent(Event e) {
                updateUID3();
            }			
        };
        model.addChangeListener(IMMPModel.CHANGE_UID3, fUID3Listener);
        fUID3.addListener(SWT.FocusOut, new Listener() {
            public void handleEvent(Event e) {
                storeUID3();
            }			
        });
        fUID3.addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event e) {
                storeUID3();
            }			
        });
    }
    
    static String uidToString(int uid)
    {
        return "0x"+Integer.toHexString(uid);
    }
    
    void updateUID2()
    {
        if (!fUID2.isDisposed())
            fUID2.setText(uidToString(fModel.getUID2()));
    }
    
    void updateUID3()
    {
        if (!fUID3.isDisposed())
            fUID3.setText(uidToString(fModel.getUID3()));        
    }
    
    void storeUID2()
    {
        storeUID(true);
    }
    
    void storeUID3()
    {
        storeUID(false);
    }
    
    void storeUID(boolean uid2)
    {
        String txt;
        String uidString;
        if (uid2)
        {
            txt=fUID2.getText();
            uidString="UID2";
        } else
        {
            txt=fUID3.getText();
            uidString="UID3";
        }
        if (txt.startsWith("0x"))
            txt=txt.substring(2);
        try {
	        int val=Integer.parseInt(txt, 16);
	        if (uid2) fModel.setUID2(val);
	            else fModel.setUID3(val);
        }
        catch (NumberFormatException e2)
        {
            MessageDialog.openError(getSection().getShell(),
                    SymbdevPlugin.MESSAGE_TITLE,
                    uidString+" number not valid");
        }
        catch (CoreException e)
        {
            SymbdevPlugin.showError(e, "Error storing "+uidString,
                    getSection().getShell());
        }
    }
    
    public void dispose()
    {
        if (fUID2Listener!=null)
            fModel.removeChangeListener(IMMPModel.CHANGE_UID2, fUID2Listener);
        fModel.removeChangeListener(IMMPModel.CHANGE_UID3, fUID3Listener);
        super.dispose();
    }
    
    Text fUID2, fUID3;
    Listener fUID2Listener, fUID3Listener;
    int fFixedUID2;
    IMMPModel fModel;
}
