/*
 * 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 project UID and Symbian
 * project name
 * 
 * @author mikolaj
 */
public class UidAndPrjNameWizardPage implements ISubstitutionOptionsWizardPage {

    /**
     * Default constructor
     */
    public UidAndPrjNameWizardPage() {
        super();
    }

    public Composite getWizardContents(WizardPage wiz, Composite parent) {
        this.fWizard=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);
        fUid.setText(MiscUtils.to8DigitHex((new Random()).nextInt(0x0fffffff)));        
        fUid.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        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.  The UIDs from the 0x00000001 "+
                "- 0x0FFFFFFF range are\ndevelopment UIDs which can be used "+
                "internally but must not appear in release software.\nOther "+
                "UIDs can be obtained from Symbian");
        data=new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan=2;
        t.setLayoutData(data);
        
        l=new Label(fPanel, SWT.NONE);
        l.setText("Application name:");
        fPrjName=new Text(fPanel, SWT.BORDER);
        data=new GridData(GridData.FILL_HORIZONTAL);
        fPrjName.setLayoutData(data);
        fPrjName.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) { prjNameModified(); }
        });
        
        l=new Label(fPanel, SWT.WRAP);
        l.setText("This string will be used to genereate class names.");
        data=new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan=2;
        l.setLayoutData(data);
        
        setError();
        return fPanel;
    }
    
    protected void setError()
    {
        fWizard.setPageComplete(fUidOk && fPrjNameOk);
        
        if (!fUidOk)
            fWizard.setErrorMessage("The UID should be a number");
        else if (!fPrjNameOk)
            fWizard.setErrorMessage("The project name should be non-empty, compound of letters and digits and begin with a letter");
        else fWizard.setErrorMessage(null);
    }
    
    protected void uidModified()
    {
        fUidOk=true;
        try
        {
            Integer.decode(fUid.getText());
        }
        catch (NumberFormatException e)
        {
            fUidOk=false;
        }
        setError();
    }
    
    protected void prjNameModified()
    {
        fPrjNameOk=true;
        String name=fPrjName.getText();
        if (!name.matches("[A-Za-z_][A-Za-z0-9_]*"))
            fPrjNameOk=false;
        setError();
    }
   
    public void contributeToTranslationMap(Map map) {
        map.put("UID", fUid.getText());
        map.put("PrjName", fPrjName.getText());
    }

    boolean fUidOk=true;
    boolean fPrjNameOk=false;
    
    WizardPage fWizard;
    Composite fPanel;
    Text fUid;
    Text fPrjName;
}
