/*
 * Created on 2004-12-14
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.templates;

import java.util.Map;
import java.util.Random;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import pl.zalewski.mikolaj.symbdev.utils.MiscUtils;

/**
 * A wizard page with a field to enter the application name and UID.
 * Used in the AIF generation wizard
 * 
 * @author mikolaj
 */
public class UidAndCaptionWizardPage implements ISubstitutionOptionsWizardPage {

    /**
     * Default constructor
     */
    public UidAndCaptionWizardPage() {
        super();
    }
    
    public void setDefaultUid(String uid) {
        fDefaultUid=uid;
    }
    
    public void setDefaultCaption(String caption) {
        fDefaultCaption=caption;
    }

    public Composite getWizardContents(WizardPage wiz, Composite parent) {
        fWizardPage=wiz;
        
        fPanel=new Composite(parent, SWT.NONE);
        GridLayout layout=new GridLayout(2, false);
        fPanel.setLayout(layout);
        GridData data;
        
        Label l=new Label(fPanel, SWT.NONE);
        l.setText("Program UID:");
        fUid=new Text(fPanel, SWT.BORDER);
        if (fDefaultUid==null)
            fUid.setText(MiscUtils.to8DigitHex((new Random()).nextInt(0x0fffffff)));
        	else fUid.setText(fDefaultUid);
        data=new GridData(GridData.FILL_HORIZONTAL);
        fUid.setLayoutData(data);
        fUid.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) { uidModified(); }
        });
        
        Text t=new Text(fPanel, SWT.WRAP|SWT.READ_ONLY);
        t.setText("UID for the application.  Must be the same as the one\n" +
        		"declared in the MMP file and returned by the Application\n" +
        		"class");
        data=new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan=2;
        t.setLayoutData(data);
        
        l=new Label(fPanel, SWT.NONE);
        l.setText("Application caption:");
        fCaption=new Text(fPanel, SWT.BORDER);
        data=new GridData(GridData.FILL_HORIZONTAL);
        fCaption.setLayoutData(data);
        if (fDefaultCaption!=null)
            fCaption.setText(fDefaultCaption);
        
        l=new Label(fPanel, SWT.WRAP);
        l.setText("The caption to show in the launcher.");
        data=new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan=2;
        l.setLayoutData(data);
        
        setError();
        return fPanel;
    }
    
    protected void setError()
    {
        fWizardPage.setPageComplete(fUidOk);
        
        if (!fUidOk)
            fWizardPage.setErrorMessage("The UID should be a number");
        else fWizardPage.setErrorMessage(null);
    }
    
    protected void uidModified()
    {
        fUidOk=true;
        try
        {
            Integer.decode(fUid.getText());
        }
        catch (NumberFormatException e)
        {
            fUidOk=false;
        }
        setError();
    }
    
    public void contributeToTranslationMap(Map map) {
        map.put("uid", fUid.getText());
        map.put("caption", fCaption.getText());
    }

    boolean fUidOk=true;
    
    WizardPage fWizardPage;
    Composite fPanel;
    String fDefaultUid, fDefaultCaption;
    Text fUid;
    Text fCaption;
}
